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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
namespace phpbb\extension;
|
|
|
|
|
2012-05-19 19:56:02 +01:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The extension metadata manager validates and gets meta-data for extensions
|
|
|
|
*
|
|
|
|
* @package extension
|
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
class metadata_manager
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2013-03-18 23:09:37 +01:00
|
|
|
/**
|
|
|
|
* phpBB Config instance
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\config\config
|
2013-03-18 23:09:37 +01:00
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phpBB Extension Manager
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\extension\manager
|
2013-03-18 23:09:37 +01:00
|
|
|
*/
|
2012-05-19 19:56:02 +01:00
|
|
|
protected $extension_manager;
|
2013-03-18 23:09:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* phpBB Template instance
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\template\template
|
2013-03-18 23:09:37 +01:00
|
|
|
*/
|
2012-07-22 18:11:56 -05:00
|
|
|
protected $template;
|
2013-03-18 23:09:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* phpBB root path
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $phpbb_root_path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name (including vendor) of the extension
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-05-19 19:56:02 +01:00
|
|
|
protected $ext_name;
|
2013-03-18 23:09:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Metadata from the composer.json file
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-07-23 18:22:35 -05:00
|
|
|
protected $metadata;
|
2013-03-18 23:09:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Link (including root path) to the metadata file
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-05-19 19:56:02 +01:00
|
|
|
protected $metadata_file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the metadata manager
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
2013-03-18 23:09:37 +01:00
|
|
|
* @param string $ext_name Name (including vendor) of the extension
|
2013-09-10 14:01:09 +02:00
|
|
|
* @param \phpbb\config\config $config phpBB Config instance
|
|
|
|
* @param \phpbb\extension\manager $extension_manager An instance of the phpBBb extension manager
|
|
|
|
* @param \phpbb\template\template $template phpBB Template instance
|
2013-03-18 23:09:37 +01:00
|
|
|
* @param string $phpbb_root_path Path to the phpbb includes directory.
|
2012-05-19 19:56:02 +01:00
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
public function __construct($ext_name, \phpbb\config\config $config, \phpbb\extension\manager $extension_manager, \phpbb\template\template $template, $phpbb_root_path)
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
2012-07-23 15:17:42 -05:00
|
|
|
$this->config = $config;
|
2012-05-19 19:56:02 +01:00
|
|
|
$this->extension_manager = $extension_manager;
|
2013-03-18 23:02:24 +01:00
|
|
|
$this->template = $template;
|
|
|
|
$this->phpbb_root_path = $phpbb_root_path;
|
|
|
|
|
2012-05-19 19:56:02 +01:00
|
|
|
$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.
|
2012-07-23 19:46:21 -05:00
|
|
|
* @return array Contains all of the requested metadata, throws an exception on failure
|
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
|
|
|
{
|
2012-07-23 19:46:21 -05:00
|
|
|
$this->set_metadata_file();
|
2012-05-19 19:56:02 +01:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
// Fetch the metadata
|
2012-07-23 19:46:21 -05:00
|
|
|
$this->fetch_metadata();
|
2012-07-22 18:11:56 -05:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
// Clean the metadata
|
2012-07-23 19:46:21 -05:00
|
|
|
$this->clean_metadata_array();
|
2012-07-23 15:17:42 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the filepath of the metadata file
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
2012-07-23 19:46:21 -05:00
|
|
|
* @return boolean Set to true if it exists, throws an exception on failure
|
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);
|
2012-07-23 19:46:21 -05:00
|
|
|
$metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json';
|
2012-05-19 19:56:02 +01:00
|
|
|
|
|
|
|
$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
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception('The required file does not exist: ' . $this->metadata_file);
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-23 15:17:42 -05:00
|
|
|
* Gets the contents of the composer.json file
|
|
|
|
*
|
2012-07-23 19:46:21 -05:00
|
|
|
* @return bool True if success, throws an exception on failure
|
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
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception('The required file does not exist: ' . $this->metadata_file);
|
2012-05-19 19:56:02 +01:00
|
|
|
}
|
2012-07-23 15:17:42 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!($file_contents = file_get_contents($this->metadata_file)))
|
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception('file_get_contents failed on ' . $this->metadata_file);
|
2012-07-23 15:17:42 -05:00
|
|
|
}
|
2012-05-19 19:56:02 +01:00
|
|
|
|
2012-07-23 15:17:42 -05:00
|
|
|
if (($metadata = json_decode($file_contents, true)) === NULL)
|
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception('json_decode failed on ' . $this->metadata_file);
|
2012-07-23 15:17:42 -05:00
|
|
|
}
|
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 19:46:21 -05:00
|
|
|
* This array handles the cleaning of the array
|
2012-07-23 15:17:42 -05:00
|
|
|
*
|
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
|
|
|
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")
|
2012-07-23 19:46:21 -05:00
|
|
|
* @return Bool True if valid, throws an exception if invalid
|
2012-07-23 18:22:35 -05:00
|
|
|
*/
|
|
|
|
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' => '#.+#',
|
|
|
|
);
|
|
|
|
|
2012-07-28 14:59:55 -05:00
|
|
|
switch ($name)
|
2012-07-23 18:22:35 -05:00
|
|
|
{
|
2012-07-28 14:59:55 -05:00
|
|
|
case 'all':
|
|
|
|
$this->validate('display');
|
2012-07-23 19:46:21 -05:00
|
|
|
|
2012-07-28 14:59:55 -05:00
|
|
|
$this->validate_enable();
|
|
|
|
break;
|
2012-07-23 19:46:21 -05:00
|
|
|
|
2012-07-28 14:59:55 -05:00
|
|
|
case 'display':
|
|
|
|
foreach ($fields as $field => $data)
|
|
|
|
{
|
|
|
|
$this->validate($field);
|
|
|
|
}
|
2012-07-23 18:22:35 -05:00
|
|
|
|
2012-07-28 14:59:55 -05:00
|
|
|
$this->validate_authors();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (isset($fields[$name]))
|
|
|
|
{
|
|
|
|
if (!isset($this->metadata[$name]))
|
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception("Required meta field '$name' has not been set.");
|
2012-07-28 14:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match($fields[$name], $this->metadata[$name]))
|
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception("Meta field '$name' is invalid.");
|
2012-07-28 14:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
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
|
|
|
|
*
|
2012-07-23 19:46:21 -05:00
|
|
|
* @return boolean True when passes validation, throws exception if invalid
|
2012-07-23 15:39:13 -05:00
|
|
|
*/
|
2012-07-28 14:59:55 -05:00
|
|
|
public function validate_authors()
|
2012-07-23 15:39:13 -05:00
|
|
|
{
|
|
|
|
if (empty($this->metadata['authors']))
|
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception("Required meta field 'authors' has not been set.");
|
2012-07-23 15:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->metadata['authors'] as $author)
|
|
|
|
{
|
|
|
|
if (!isset($author['name']))
|
|
|
|
{
|
2013-09-10 14:01:09 +02:00
|
|
|
throw new \phpbb\extension\exception("Required meta field 'author name' has not been set.");
|
2012-07-23 15:39:13 -05: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 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
|
2012-07-23 19:46:21 -05:00
|
|
|
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
|
|
|
*/
|
2012-07-28 14:59:55 -05:00
|
|
|
public function validate_require_phpbb()
|
2012-05-19 19:56:02 +01:00
|
|
|
{
|
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-28 14:59:55 -05:00
|
|
|
public 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-08-17 14:06:23 +01:00
|
|
|
'META_NAME' => htmlspecialchars($this->metadata['name']),
|
|
|
|
'META_TYPE' => htmlspecialchars($this->metadata['type']),
|
|
|
|
'META_DESCRIPTION' => (isset($this->metadata['description'])) ? htmlspecialchars($this->metadata['description']) : '',
|
|
|
|
'META_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
|
|
|
|
'META_VERSION' => (isset($this->metadata['version'])) ? htmlspecialchars($this->metadata['version']) : '',
|
|
|
|
'META_TIME' => (isset($this->metadata['time'])) ? htmlspecialchars($this->metadata['time']) : '',
|
|
|
|
'META_LICENCE' => htmlspecialchars($this->metadata['licence']),
|
2012-08-05 19:00:20 -05:00
|
|
|
|
2012-08-17 14:06:23 +01:00
|
|
|
'META_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? htmlspecialchars($this->metadata['require']['php']) : '',
|
|
|
|
'META_REQUIRE_PHP_FAIL' => !$this->validate_require_php(),
|
2012-08-05 19:00:20 -05:00
|
|
|
|
2012-08-17 14:06:23 +01:00
|
|
|
'META_REQUIRE_PHPBB' => (isset($this->metadata['require']['phpbb'])) ? htmlspecialchars($this->metadata['require']['phpbb']) : '',
|
|
|
|
'META_REQUIRE_PHPBB_FAIL' => !$this->validate_require_phpbb(),
|
2012-08-05 19:00:20 -05:00
|
|
|
|
2012-08-17 14:06:23 +01:00
|
|
|
'META_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-08-17 14:06:23 +01:00
|
|
|
$this->template->assign_block_vars('meta_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-22 18:11:56 -05:00
|
|
|
}
|