Merge pull request #170 from marc1706/feature/mod_version_check

[feature/mod_version_check] Add newer mod version check service
This commit is contained in:
Marc Alexander
2013-10-29 16:45:25 -07:00
8 changed files with 293 additions and 230 deletions

View File

@@ -23,12 +23,12 @@ class portal_module
public $new_config = array();
protected $c_class;
protected $db, $user, $cache, $template, $display_vars, $config, $phpbb_root_path, $portal_root_path, $phpbb_admin_path, $phpEx;
protected $root_path;
protected $root_path, $mod_version_check;
public function __construct()
{
global $db, $user, $cache, $template;
global $config, $phpbb_root_path, $portal_root_path, $phpbb_admin_path, $phpEx;
global $config, $phpbb_root_path, $portal_root_path, $phpbb_admin_path, $phpbb_container, $phpEx;
$user->add_lang_ext('board3/portal', 'mods/portal');
@@ -46,17 +46,13 @@ class portal_module
$this->phpbb_admin_path = $phpbb_admin_path;
$this->portal_root_path = $this->root_path . 'portal/';
$this->php_ex = $phpEx;
$this->mod_version_check = $phpbb_container->get('board3.version.check');
if (!function_exists('column_string_const'))
{
include($this->portal_root_path . 'includes/functions_modules.' . $this->php_ex);
}
if (!function_exists('mod_version_check'))
{
include($this->portal_root_path . 'includes/functions_version_check.' . $this->php_ex);
}
if(!function_exists('obtain_portal_config'))
{
include($this->portal_root_path . 'includes/functions.' . $this->php_ex);
@@ -161,7 +157,7 @@ class portal_module
else
{
// only show the mod version check if we are on the General Settings page
mod_version_check($this->phpbb_root_path, $this->root_path);
$this->mod_version_check->version_check();
}
$this->new_config = $this->config;

View File

@@ -1,33 +0,0 @@
<?php
/**
*
* @package Board3 Portal v2
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @package mod_version_check
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class board3_portal_check_version
{
public function version()
{
global $config, $phpbb_root_path, $phpEx;
return array(
'author' => 'Saint_hh',
'title' => 'Board3 Portal',
'tag' => 'board3_portal_v2_dev',
'version' => $config['board3_portal_version'],
'file' => array('board3.de', 'updatecheck', 'board3_portal.xml'),
);
}
}

View File

@@ -1,3 +1,14 @@
parameters:
board3.version_data:
author: Marc
title: Board3 Portal
tag: board3_portal_v2_dev
version: board3_portal_version
file:
- board3.de
- updatecheck
- board3_portal.xml
services:
board3.portal.main:
@@ -10,3 +21,13 @@ services:
- @path_helper
- %core.root_path%
- .%core.php_ext%
board3.version.check:
class: \board3\portal\includes\mod_version_check
arguments:
- %board3.version_data%
- @config
- %core.root_path%
- %core.php_ext%
- @template
- @user

View File

@@ -0,0 +1,148 @@
<?php
/**
*
* @package Board3 Portal v2.1
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
namespace board3\portal\includes;
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class mod_version_check
{
/**
* @var version_data
*/
protected $version_data;
/**
* @var \phpbb\config\config
*/
protected $config;
/**
* @var phpbb_root_path
*/
protected $phpbb_root_path;
/**
* @var phpEx
*/
protected $php_ext;
/**
* @var \phpbb\template\twig\twig
*/
protected $template;
/**
* @var \phpbb\user
*/
protected $user;
/**
* Construct a mod_version_check object
*
* @param array $version_data Version data
* @param \phpbb\config\config $config phpBB config
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
* @param \phpbb\template\twig\twig $template phpBB template object
* @param \phpbb\user $user phpBB user object
*/
public function __construct($version_data, $config, $phpbb_root_path, $php_ext, $template, $user)
{
$this->version_data = $version_data;
$this->config = $config;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->template = $template;
$this->user = $user;
}
/**
* Check MOD 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 version_check($return_version = false)
{
if (!function_exists('get_remote_file'))
{
include($this->phpbb_root_path . 'includes/functions_admin.' . $this->php_ext);
}
$var = $this->version_data;
// Get current and latest version
$errstr = '';
$errno = 0;
if (!$return_version)
{
$mod_version = $this->user->lang['NO_INFO'];
$data = array(
'title' => $var['title'],
'description' => $this->user->lang['NO_INFO'],
'download' => $this->user->lang['NO_INFO'],
'announcement' => $this->user->lang['NO_INFO'],
);
}
$file = get_remote_file($var['file'][0], '/' . $var['file'][1], $var['file'][2], $errstr, $errno);
if ($file)
{
// let's not stop the page from loading if a mod author messed up their mod check file
// also take care of one of the easiest ways to mess up an xml file: "&"
$mod = @simplexml_load_string(str_replace('&', '&amp;', $file));
if (isset($mod->$var['tag']))
{
$row = $mod->$var['tag'];
$mod_version = $row->mod_version->major . '.' . $row->mod_version->minor . '.' . $row->mod_version->revision . $row->mod_version->release;
$data = array(
'title' => $row->title,
'description' => $row->description,
'download' => $row->download,
'announcement' => $row->announcement,
);
}
}
// remove spaces from the version in the mod file stored locally
$version = $this->config[str_replace(' ', '', $var['version'])];
if ($return_version)
{
return $version;
}
$version_compare = (version_compare($version, $mod_version, '<')) ? false : true;
$this->template->assign_block_vars('mods', array(
'ANNOUNCEMENT' => (string) $data['announcement'],
'AUTHOR' => $var['author'],
'CURRENT_VERSION' => $version,
'DESCRIPTION' => (string) $data['description'],
'DOWNLOAD' => (string) $data['download'],
'LATEST_VERSION' => $mod_version,
'TITLE' => (string) $data['title'],
'UP_TO_DATE' => sprintf((!$version_compare) ? $this->user->lang['NOT_UP_TO_DATE'] : $this->user->lang['UP_TO_DATE'], $data['title']),
'S_UP_TO_DATE' => $version_compare,
'U_AUTHOR' => 'http://www.phpbb.com/community/memberlist.php?mode=viewprofile&un=' . $var['author'],
));
}
}

View File

@@ -1,189 +0,0 @@
<?php
/**
*
* @package Board3 Portal v2
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* A copy of Handyman` s MOD version check, to view it on the gallery overview
*/
function mod_version_check($phpbb_root_path, $root_path, $return_version = false)
{
global $user, $template;
global $phpEx;
if (!function_exists('get_remote_file'))
{
include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
}
// load version files
$class_functions = array();
include($root_path . 'adm/mods/board3_portal_check_version.' . $phpEx);
$class_name = 'board3_portal_check_version';
$version_check = new $class_name();
$var = call_user_func(array($version_check, 'version'));
// Get current and latest version
$errstr = '';
$errno = 0;
if (!$return_version)
{
$mod_version = $user->lang['NO_INFO'];
$data = array(
'title' => $var['title'],
'description' => $user->lang['NO_INFO'],
'download' => $user->lang['NO_INFO'],
'announcement' => $user->lang['NO_INFO'],
);
}
$file = get_remote_file($var['file'][0], '/' . $var['file'][1], $var['file'][2], $errstr, $errno);
if ($file)
{
if (version_compare(PHP_VERSION, '5.0.0', '<'))
{
$row = array();
$data_array = mvc_setup_array($file);
$row = $data_array['mods'][$var['tag']];
$mod_version = $row['mod_version'];
$mod_version = $mod_version['major'] . '.' . $mod_version['minor'] . '.' . $mod_version['revision'] . $mod_version['release'];
$data = array(
'title' => $row['title'],
'description' => $row['description'],
'download' => $row['download'],
'announcement' => $row['announcement'],
);
}
else
{
// let's not stop the page from loading if a mod author messed up their mod check file
// also take care of one of the easiest ways to mess up an xml file: "&"
$mod = @simplexml_load_string(str_replace('&', '&amp;', $file));
if (isset($mod->$var['tag']))
{
$row = $mod->$var['tag'];
$mod_version = $row->mod_version->major . '.' . $row->mod_version->minor . '.' . $row->mod_version->revision . $row->mod_version->release;
$data = array(
'title' => $row->title,
'description' => $row->description,
'download' => $row->download,
'announcement' => $row->announcement,
);
}
}
}
// remove spaces from the version in the mod file stored locally
$version = str_replace(' ', '', $var['version']);
if ($return_version)
{
return $version;
}
$version_compare = (version_compare($version, $mod_version, '<')) ? false : true;
$template->assign_block_vars('mods', array(
'ANNOUNCEMENT' => $data['announcement'],
'AUTHOR' => $var['author'],
'CURRENT_VERSION' => $version,
'DESCRIPTION' => $data['description'],
'DOWNLOAD' => $data['download'],
'LATEST_VERSION' => $mod_version,
'TITLE' => $data['title'],
'UP_TO_DATE' => sprintf((!$version_compare) ? $user->lang['NOT_UP_TO_DATE'] : $user->lang['UP_TO_DATE'], $data['title']),
'S_UP_TO_DATE' => $version_compare,
'U_AUTHOR' => 'http://www.phpbb.com/community/memberlist.php?mode=viewprofile&un=' . $var['author'],
));
}
/**
* this is for php4 only
* kind of a dirty hack, but since I get the say on how the xml is done, I can have 4 levels max
*/
function mvc_setup_array($xml)
{
// Fire up the built-in XML parser
$values = $index = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
// this takes care of one possible xml error
$xml = str_replace('&', '&amp;', $xml);
// Set tag names and values
xml_parse_into_struct($parser, $xml, $values, $index);
// Close down XML parser
xml_parser_free($parser);
$ary = array();
foreach ($values as $value)
{
switch (trim($value['level']))
{
case 1:
if ($value['type'] == 'open')
{
$one = $value['tag'];
}
else if ($value['type'] == 'complete')
{
$ary[$value['tag']] = $value['value'];
}
break;
case 2:
if ($value['type'] == 'open')
{
$two = $value['tag'];
}
else if ($value['type'] == 'complete')
{
$ary[$one][$value['tag']] = $value['value'];
}
break;
case 3:
if ($value['type'] == 'open')
{
$three = $value['tag'];
}
else if ($value['type'] == 'complete')
{
$ary[$one][$two][$value['tag']] = $value['value'];
}
break;
case 4:
if ($value['type'] == 'complete')
{
$ary[$one][$two][$three][$value['tag']] = isset($value['value']) ? $value['value'] : '';
}
break;
}
}
return $ary;
}

View File

@@ -0,0 +1,49 @@
<?php
/**
*
* @package testing
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
class phpbb_functions_version_check_test extends \board3\portal\tests\testframework\test_case
{
protected $version_check;
public function setUp()
{
global $phpbb_root_path, $phpEx;
$this->template = new \board3\portal\tests\mock\template($this);
$version_data = array(
'author' => 'Saint_hh',
'title' => 'Board3 Portal',
'tag' => 'board3_portal_v2_dev',
'version' => 'board3_portal_version',
'file' => array('board3.de', 'updatecheck', 'board3_portal.xml'),
);
$config = array('board3_portal_version' => '2.1.0');
$user = new \board3\portal\tests\mock\user;
$user->set(array(
'NO_INFO' => 'NO_INFO',
'NOT_UP_TO_DATE' => 'NOT_UP_TO_DATE',
'UP_TO_DATE' => 'UP_TO_DATE',
));
$this->version_check = new \board3\portal\includes\mod_version_check($version_data, $config, $phpbb_root_path, $phpEx, $this->template, $user);
}
public function test_version_check()
{
$this->assertTrue(true);
$this->assertNull($this->version_check->version_check());
$this->assertEquals('2.1.0', $this->version_check->version_check(true));
$this->template->assert_equals(array(
'CURRENT_VERSION' => '2.1.0',
'TITLE' => 'Board3 Portal',
'UP_TO_DATE' => 'UP_TO_DATE',
'S_UP_TO_DATE' => true,
), 'mods');
}
}

46
tests/mock/template.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
/**
*
* @package testing
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace board3\portal\tests\mock;
class template
{
protected $data = array();
protected $test_case;
public function __construct($test_case)
{
$this->test_case = $test_case;
}
public function assign_block_vars($row, $values)
{
$this->test_case->assertEquals(true, is_array($values));
if (!isset($this->data[$row]))
{
$this->data[$row] = array();
}
foreach ($values as $key => $column)
{
$this->test_case->assertArrayNotHasKey($key, $this->data[$row]);
$this->data[$row][$key] = $column;
}
}
public function assert_equals($data, $row)
{
foreach ($data as $key => $value)
{
$this->test_case->assertEquals($value, $this->data[$row][$key]);
}
}
}

25
tests/mock/user.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
/**
*
* @package testing
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace board3\portal\tests\mock;
class user extends \PHPUnit_Framework_TestCase
{
public $lang = array();
public function set($data)
{
$this->assertTrue(is_array($data));
foreach ($data as $key => $column)
{
$this->lang[$key] = $column;
}
}
}