2012-05-19 19:56:02 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package extension
|
|
|
|
* @copyright (c) 2012 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The extension metadata manager validates and gets meta-data for extensions
|
|
|
|
*
|
|
|
|
* @package extension
|
|
|
|
*/
|
|
|
|
class phpbb_extension_metadata_manager
|
|
|
|
{
|
|
|
|
protected $phpEx;
|
|
|
|
protected $extension_manager;
|
|
|
|
protected $db;
|
|
|
|
protected $phpbb_root_path;
|
2012-07-22 18:11:56 -05:00
|
|
|
protected $template;
|
2012-05-19 19:56:02 +01:00
|
|
|
protected $ext_name;
|
2012-05-20 14:16:00 +01:00
|
|
|
public $metadata;
|
2012-05-19 19:56:02 +01:00
|
|
|
protected $metadata_file;
|
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
/**
|
|
|
|
* Array of validation regular expressions, see __call()
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $validation = array(
|
|
|
|
'name' => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#',
|
|
|
|
'type' => '#^phpbb3-extension$#',
|
|
|
|
'description' => '#.*#',
|
|
|
|
'version' => '#.+#',
|
|
|
|
'licence' => '#.+#',
|
2012-07-23 15:39:13 -05:00
|
|
|
//'homepage' => '#([\d\w-.]+?\.(a[cdefgilmnoqrstuwz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmnoz]|e[ceghrst]|f[ijkmnor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eouw]|s[abcdeghijklmnortuvyz]|t[cdfghjkmnoprtvwz]|u[augkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]|aero|arpa|biz|com|coop|edu|info|int|gov|mil|museum|name|net|org|pro)(\b|\W(?<!&|=)(?!\.\s|\.{3}).*?))(\s|$)#',
|
2012-07-23 15:17:42 -05:00
|
|
|
'extra' => array(
|
|
|
|
'display-name' => '#.*#',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic method to catch validation calls
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $arguments
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function __call($name, $arguments)
|
|
|
|
{
|
|
|
|
// Validation Magic methods
|
|
|
|
if (strpos($name, 'validate_') === 0)
|
|
|
|
{
|
|
|
|
// Remove validate_
|
|
|
|
$name = substr($name, 9);
|
|
|
|
|
|
|
|
// Replace underscores with dashes (underscores are not used)
|
|
|
|
$name = str_replace('_', '-', $name);
|
|
|
|
|
|
|
|
if (strpos($name, 'extra-') === 0)
|
|
|
|
{
|
|
|
|
// Remove extra_
|
|
|
|
$name = substr($name, 6);
|
|
|
|
|
|
|
|
if (isset($this->validation['extra'][$name]))
|
|
|
|
{
|
|
|
|
// Extra means it's optional, so return true if it does not exist
|
|
|
|
return (isset($this->metadata['extra'][$name])) ? preg_match($this->validation['extra'][$name], $this->metadata['extra'][$name]) : true;
|
|
|
|
}
|
|
|
|
}
|
2012-07-23 15:39:13 -05:00
|
|
|
else if (isset($this->validation[$name]) && isset($this->metadata[$name]))
|
2012-07-23 15:17:42 -05:00
|
|
|
{
|
|
|
|
return preg_match($this->validation[$name], $this->metadata[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-19 19:56:02 +01:00
|
|
|
/**
|
|
|
|
* Creates the metadata manager
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
2012-05-19 19:56:02 +01:00
|
|
|
* @param dbal $db A database connection
|
|
|
|
* @param string $extension_manager An instance of the phpbb extension manager
|
|
|
|
* @param string $phpbb_root_path Path to the phpbb includes directory.
|
|
|
|
* @param string $phpEx php file extension
|
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
public function __construct($ext_name, dbal $db, phpbb_extension_manager $extension_manager, $phpbb_root_path, $phpEx = '.php', phpbb_template $template, phpbb_config $config)
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
$this->phpbb_root_path = $phpbb_root_path;
|
|
|
|
$this->db = $db;
|
2012-07-23 15:17:42 -05:00
|
|
|
$this->config = $config;
|
2012-05-19 19:56:02 +01:00
|
|
|
$this->phpEx = $phpEx;
|
2012-07-22 18:11:56 -05:00
|
|
|
$this->template = $template;
|
2012-05-19 19:56:02 +01:00
|
|
|
$this->extension_manager = $extension_manager;
|
|
|
|
$this->ext_name = $ext_name;
|
|
|
|
$this->metadata = array();
|
|
|
|
$this->metadata_file = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes and gets the metadata requested
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
|
|
|
* @param string $element All for all metadata that it has and is valid, otherwise specify which section you want by its shorthand term.
|
|
|
|
* @return bool|array Contains all of the requested metadata or bool False if not valid
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
public function get_metadata($element = 'all')
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
// TODO: Check ext_name exists and is an extension that exists
|
2012-05-20 14:16:00 +01:00
|
|
|
if (!$this->set_metadata_file())
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
// Fetch the metadata
|
2012-07-22 18:11:56 -05:00
|
|
|
if (!$this->fetch_metadata())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
// Clean the metadata
|
|
|
|
if (!$this->clean_metadata_array())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($element)
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
case 'all':
|
|
|
|
default:
|
2012-07-23 15:17:42 -05:00
|
|
|
// Validate the metadata
|
|
|
|
if (!$this->validate_metadata_array())
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->metadata;
|
|
|
|
break;
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-05-19 19:56:02 +01:00
|
|
|
case 'name':
|
2012-07-23 14:01:13 -05:00
|
|
|
return ($this->validate_name()) ? $this->metadata['name'] : false;
|
|
|
|
break;
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-07-23 14:01:13 -05:00
|
|
|
case 'display-name':
|
2012-07-23 15:17:42 -05:00
|
|
|
if (isset($this->metadata['extra']['display-name']) && $this->validate_extra_display_name())
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 14:01:13 -05:00
|
|
|
return $this->metadata['extra']['display-name'];
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-23 14:01:13 -05:00
|
|
|
return ($this->validate_name()) ? $this->metadata['name'] : false;
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
break;
|
2012-05-20 14:16:00 +01:00
|
|
|
// TODO: Add remaining cases as needed
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the filepath of the metadata file
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
2012-05-19 23:32:34 +01:00
|
|
|
* @return boolean Set to true if it exists
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-05-20 14:16:00 +01:00
|
|
|
private function set_metadata_file()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
$ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
|
|
|
|
$metadata_filepath = $this->phpbb_root_path . $ext_filepath . '/composer.json';
|
|
|
|
|
|
|
|
$this->metadata_file = $metadata_filepath;
|
|
|
|
|
2012-05-19 23:32:34 +01:00
|
|
|
if (!file_exists($this->metadata_file))
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* Gets the contents of the composer.json file
|
|
|
|
*
|
|
|
|
* @return bool True of false (if loading succeeded or failed)
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
private function fetch_metadata()
|
|
|
|
{
|
|
|
|
if (!file_exists($this->metadata_file))
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-23 15:17:42 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!($file_contents = file_get_contents($this->metadata_file)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-19 19:56:02 +01:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
if (($metadata = json_decode($file_contents, true)) === NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-19 19:56:02 +01:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
$this->metadata = $metadata;
|
2012-05-19 19:56:02 +01:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* This array handles the validation and cleaning of the array
|
|
|
|
*
|
|
|
|
* @return array Contains the cleaned and validated metadata array
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
private function clean_metadata_array()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
// TODO: Remove all parts of the array we don't want or shouldn't be there due to nub mod authors
|
|
|
|
// $this->metadata = $metadata_finished;
|
2012-05-19 19:56:02 +01:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
return $this->metadata;
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* This array handles the validation of strings
|
|
|
|
*
|
|
|
|
* @return bool True if validation succeeded, False if failed
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
public function validate_metadata_array()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:22:48 -05:00
|
|
|
foreach ($this->validation as $name => $regex)
|
2012-07-23 15:17:42 -05:00
|
|
|
{
|
2012-07-23 15:22:48 -05:00
|
|
|
if (is_array($regex))
|
|
|
|
{
|
|
|
|
foreach ($regex as $extra_name => $extra_regex)
|
|
|
|
{
|
|
|
|
$type = 'validate_' . $name . '_' . $extra_name;
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-07-23 15:22:48 -05:00
|
|
|
if (!$this->$type())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-07-23 15:17:42 -05:00
|
|
|
{
|
2012-07-23 15:22:48 -05:00
|
|
|
|
|
|
|
$type = 'validate_' . $name;
|
|
|
|
|
|
|
|
if (!$this->$type())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-23 15:17:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 15:39:13 -05:00
|
|
|
return $this->validate_authors();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the contents of the authors field
|
|
|
|
*
|
|
|
|
* @return boolean True when passes validation
|
|
|
|
*/
|
|
|
|
private function validate_authors()
|
|
|
|
{
|
|
|
|
if (empty($this->metadata['authors']))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->metadata['authors'] as $author)
|
|
|
|
{
|
|
|
|
if (!isset($author['name']))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
return true;
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* This array handles the verification that this extension can be enabled on this board
|
|
|
|
*
|
|
|
|
* @return bool True if validation succeeded, False if failed
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
public function validate_enable()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
$validate = array(
|
|
|
|
'require_phpbb',
|
|
|
|
'require_php',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($validate as $type)
|
|
|
|
{
|
|
|
|
$type = 'validate_' . $type;
|
|
|
|
|
|
|
|
if (!$this->$type())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-05-19 19:56:02 +01:00
|
|
|
/**
|
|
|
|
* Validates the contents of the phpbb requirement field
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
2012-05-20 14:16:00 +01:00
|
|
|
* @return boolean True when passes validation
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
|
|
|
private function validate_require_phpbb()
|
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
if (!isset($this->metadata['require']['phpbb']))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_validate_version($this->metadata['require']['phpbb'], $this->config['version']);
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* Validates the contents of the php requirement field
|
|
|
|
*
|
2012-05-20 14:16:00 +01:00
|
|
|
* @return boolean True when passes validation
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2012-07-23 15:17:42 -05:00
|
|
|
private function validate_require_php()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
if (!isset($this->metadata['require']['php']))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_validate_version($this->metadata['require']['php'], phpversion());
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* Version validation helper
|
|
|
|
*
|
|
|
|
* @param string $string The string for comparing to a version
|
|
|
|
* @param string $current_version The version to compare to
|
|
|
|
* @return bool True/False if meets version requirements
|
|
|
|
*/
|
|
|
|
private function _validate_version($string, $current_version)
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
// Allow them to specify their own comparison operator (ex: <3.1.2, >=3.1.0)
|
|
|
|
$comparison_matches = false;
|
|
|
|
preg_match('#[=<>]+#', $string, $comparison_matches);
|
|
|
|
|
|
|
|
if (!empty($comparison_matches))
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
return version_compare($current_version, str_replace(array($comparison_matches[0], ' '), '', $string), $comparison_matches[0]);
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
return version_compare($current_version, $string, '>=');
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the metadata into the template
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
2012-05-19 19:56:02 +01:00
|
|
|
* @return null
|
|
|
|
*/
|
2012-07-22 20:24:20 -05:00
|
|
|
public function output_template_data()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-22 20:24:20 -05:00
|
|
|
$this->template->assign_vars(array(
|
2012-05-19 19:56:02 +01:00
|
|
|
'MD_NAME' => htmlspecialchars($this->metadata['name']),
|
|
|
|
'MD_TYPE' => htmlspecialchars($this->metadata['type']),
|
|
|
|
'MD_DESCRIPTION' => htmlspecialchars($this->metadata['description']),
|
2012-07-22 20:24:20 -05:00
|
|
|
'MD_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
|
2012-05-19 19:56:02 +01:00
|
|
|
'MD_VERSION' => htmlspecialchars($this->metadata['version']),
|
|
|
|
'MD_TIME' => htmlspecialchars($this->metadata['time']),
|
2012-07-22 20:24:20 -05:00
|
|
|
'MD_LICENCE' => htmlspecialchars($this->metadata['licence']),
|
|
|
|
'MD_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '',
|
|
|
|
'MD_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '',
|
|
|
|
'MD_DISPLAY_NAME' => (isset($this->metadata['extra']['display-name'])) ? htmlspecialchars($this->metadata['extra']['display-name']) : '',
|
2012-05-19 19:56:02 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
foreach ($this->metadata['authors'] as $author)
|
|
|
|
{
|
2012-07-22 20:24:20 -05:00
|
|
|
$this->template->assign_block_vars('md_authors', array(
|
2012-05-19 19:56:02 +01:00
|
|
|
'AUTHOR_NAME' => htmlspecialchars($author['name']),
|
|
|
|
'AUTHOR_EMAIL' => $author['email'],
|
2012-07-22 20:24:20 -05:00
|
|
|
'AUTHOR_HOMEPAGE' => (isset($author['homepage'])) ? $author['homepage'] : '',
|
|
|
|
'AUTHOR_ROLE' => (isset($author['role'])) ? htmlspecialchars($author['role']) : '',
|
2012-05-19 19:56:02 +01:00
|
|
|
));
|
|
|
|
}
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-05-19 19:56:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-07-22 18:11:56 -05:00
|
|
|
}
|