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-07-23 18:22:35 -05:00
|
|
|
protected $metadata;
|
2012-05-19 19:56:02 +01:00
|
|
|
protected $metadata_file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2012-07-23 18:22:35 -05:00
|
|
|
if (!$this->validate())
|
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 18:22:35 -05:00
|
|
|
return ($this->validate('name')) ? $this->metadata['name'] : false;
|
2012-07-23 14:01:13 -05:00
|
|
|
break;
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-07-23 14:01:13 -05:00
|
|
|
case 'display-name':
|
2012-07-23 18:22:35 -05:00
|
|
|
if (isset($this->metadata['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 18:22:35 -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
|
|
|
|
*
|
2012-07-23 18:22:35 -05:00
|
|
|
* @return array Contains the cleaned 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 18:22:35 -05:00
|
|
|
* Validate fields
|
|
|
|
*
|
|
|
|
* @param string $name ("all" for display and enable validation
|
|
|
|
* "display" for name, type, and authors
|
|
|
|
* "name", "type")
|
|
|
|
* @return Bool False if validation fails, true if valid
|
|
|
|
*/
|
|
|
|
public function validate($name = 'display')
|
|
|
|
{
|
|
|
|
// Basic fields
|
|
|
|
$fields = array(
|
|
|
|
'name' => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#',
|
|
|
|
'type' => '#^phpbb3-extension$#',
|
|
|
|
'licence' => '#.+#',
|
|
|
|
'version' => '#.+#',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($fields[$name]))
|
|
|
|
{
|
|
|
|
return (isset($this->metadata[$name])) ? (bool) preg_match($this->validation[$name], $this->metadata[$name]) : false;
|
|
|
|
}
|
2012-07-23 15:17:42 -05:00
|
|
|
|
2012-07-23 18:22:35 -05:00
|
|
|
// Validate all fields
|
|
|
|
if ($name == 'all')
|
|
|
|
{
|
|
|
|
foreach ($fields as $field => $data)
|
2012-07-23 15:17:42 -05:00
|
|
|
{
|
2012-07-23 18:22:35 -05:00
|
|
|
if (!$this->validate($field))
|
2012-07-23 15:22:48 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-23 15:17:42 -05:00
|
|
|
}
|
2012-07-23 18:22:35 -05:00
|
|
|
|
|
|
|
return $this->validate_authors();
|
2012-07-23 15:17:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:22:35 -05:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-23 15:39:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 18:22:35 -05:00
|
|
|
// Check for phpBB, PHP versions
|
|
|
|
if (!$this->validate_require_phpbb || !$this->validate_require_php)
|
2012-07-23 15:17:42 -05:00
|
|
|
{
|
2012-07-23 18:22:35 -05:00
|
|
|
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
|
|
|
|
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']),
|
2012-07-23 18:22:35 -05:00
|
|
|
'MD_DESCRIPTION' => (isset($this->metadata['description'])) ? htmlspecialchars($this->metadata['description']) : '',
|
2012-07-22 20:24:20 -05:00
|
|
|
'MD_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
|
2012-07-23 18:22:35 -05:00
|
|
|
'MD_VERSION' => (isset($this->metadata['version'])) ? htmlspecialchars($this->metadata['version']) : '',
|
|
|
|
'MD_TIME' => (isset($this->metadata['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']),
|
2012-07-23 18:22:35 -05:00
|
|
|
'AUTHOR_EMAIL' => (isset($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
|
|
|
}
|