<?php
/**
*
* @package phpBB Extension - Football Football
* @copyright (c) 2016 football (http://football.bplaced.net)
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

namespace football\football\includes;

class version_check
{
	/**
	* @var array version_data
	*/
	protected $version_data;

	/**
	* @var \phpbb\config\config
	*/
	protected $config;

	/**
	* @var \phpbb\version_helper $version_helper phpBB version helper
	*/
	protected $version_helper;

	/**
	* @var \phpbb\template\twig\twig
	*/
	protected $template;

	/**
	* @var \phpbb\user
	*/
	protected $user;

	/**
	 * @var string Current version
	 */
	protected $current_version;

	/**
	* Construct a version_check object
	*
	* @param array $version_data Version data
	* @param \phpbb\config\config $config phpBB config
	* @param \phpbb\version_helper $version_helper phpBB version helper
	* @param \phpbb\template\twig\twig $template phpBB template object
	* @param \phpbb\user $user phpBB user object
	*/
	public function __construct($version_data, $config, $version_helper, $template, $user)
	{
		$this->version_data = $version_data;
		$this->config = $config;
		$this->version_helper = $version_helper;
		$this->template = $template;
		$this->user = $user;
		$this->current_version = $this->config[str_replace(' ', '', $this->version_data['version'])];
	}

	/**
	* Check Extension version and assign template variables for version info if not
	* returning current version
	*
	* @param bool $return_version Yes if current version should be returned
	* @return string Current version if $return_version is set to true
	*/
	public function check($return_version = false)
	{
		$allow_url_fopen = (int) @ini_get('allow_url_fopen');
		if ($allow_url_fopen)
		{
			
			// Set file location
			$this->version_helper->set_file_location($this->version_data['file'][0], $this->version_data['file'][1], $this->version_data['file'][2]);
			// Set current version
			$this->version_helper->set_current_version($this->current_version);

			$this->version_helper->force_stability(($this->config['extension_force_unstable'] || !$this->version_helper->is_stable($this->current_version)) ? 'unstable' : null);

			$updates = $this->version_helper->get_suggested_updates(true);

			// Return version if $return_version is set to true
			if ($return_version)
			{
				return $this->current_version;
			}

			$version_up_to_date = empty($updates);

			$template_data = array(
				'AUTHOR'			=> $this->version_data['author'],
				'CURRENT_VERSION'	=> $this->current_version,
				'UP_TO_DATE'		=> sprintf((!$version_up_to_date) ? $this->user->lang['NOT_UP_TO_DATE'] : $this->user->lang['UP_TO_DATE'], $this->version_data['title']),
				'S_UP_TO_DATE'		=> $version_up_to_date,
				'U_AUTHOR'			=> 'http://www.phpbb.com/community/memberlist.php?mode=viewprofile&un=' . $this->version_data['author'],
				'TITLE'				=> (string) $this->version_data['title'],
				'LATEST_VERSION'	=> $this->current_version,
			);

			if (!$version_up_to_date)
			{
				$updates = array_shift($updates);
				$template_data = array_merge($template_data, array(
					'ANNOUNCEMENT'		=> (string) $updates['announcement'],
					'DOWNLOAD'			=> (string) $updates['download'],
					'LATEST_VERSION'	=> $updates['current'],
				));
			}
			$this->template->assign_block_vars('mods', $template_data);
		}
	}
}
