[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/extension/ -> metadata_manager.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\extension;
  15  
  16  /**
  17  * The extension metadata manager validates and gets meta-data for extensions
  18  */
  19  class metadata_manager
  20  {
  21      /**
  22      * phpBB Config instance
  23      * @var \phpbb\config\config
  24      */
  25      protected $config;
  26  
  27      /**
  28      * phpBB Extension Manager
  29      * @var \phpbb\extension\manager
  30      */
  31      protected $extension_manager;
  32  
  33      /**
  34      * phpBB Template instance
  35      * @var \phpbb\template\template
  36      */
  37      protected $template;
  38  
  39      /**
  40      * phpBB User instance
  41      * @var \phpbb\user
  42      */
  43      protected $user;
  44  
  45      /**
  46      * phpBB root path
  47      * @var string
  48      */
  49      protected $phpbb_root_path;
  50  
  51      /**
  52      * Name (including vendor) of the extension
  53      * @var string
  54      */
  55      protected $ext_name;
  56  
  57      /**
  58      * Metadata from the composer.json file
  59      * @var array
  60      */
  61      protected $metadata;
  62  
  63      /**
  64      * Link (including root path) to the metadata file
  65      * @var string
  66      */
  67      protected $metadata_file;
  68  
  69      // @codingStandardsIgnoreStart
  70      /**
  71      * Creates the metadata manager
  72      *
  73      * @param string                $ext_name            Name (including vendor) of the extension
  74      * @param \phpbb\config\config        $config                phpBB Config instance
  75      * @param \phpbb\extension\manager    $extension_manager    An instance of the phpBB extension manager
  76      * @param \phpbb\template\template    $template            phpBB Template instance or null
  77      * @param \phpbb\user         $user                 User instance
  78      * @param string                $phpbb_root_path    Path to the phpbb includes directory.
  79      */
  80  	public function __construct($ext_name, \phpbb\config\config $config, \phpbb\extension\manager $extension_manager, \phpbb\template\template $template = null, \phpbb\user $user, $phpbb_root_path)
  81      {
  82          $this->config = $config;
  83          $this->extension_manager = $extension_manager;
  84          $this->template = $template;
  85          $this->user = $user;
  86          $this->phpbb_root_path = $phpbb_root_path;
  87  
  88          $this->ext_name = $ext_name;
  89          $this->metadata = array();
  90          $this->metadata_file = '';
  91      }
  92      // @codingStandardsIgnoreEnd
  93  
  94      /**
  95      * Processes and gets the metadata requested
  96      *
  97      * @param  string $element            All for all metadata that it has and is valid, otherwise specify which section you want by its shorthand term.
  98      * @return array                    Contains all of the requested metadata, throws an exception on failure
  99      */
 100  	public function get_metadata($element = 'all')
 101      {
 102          // Fetch and clean the metadata if not done yet
 103          if ($this->metadata_file === '')
 104          {
 105              $this->fetch_metadata_from_file();
 106          }
 107  
 108          switch ($element)
 109          {
 110              case 'all':
 111              default:
 112                  $this->validate();
 113                  return $this->metadata;
 114              break;
 115  
 116              case 'version':
 117              case 'name':
 118                  $this->validate($element);
 119                  return $this->metadata[$element];
 120              break;
 121  
 122              case 'display-name':
 123                  return (isset($this->metadata['extra']['display-name'])) ? $this->metadata['extra']['display-name'] : $this->get_metadata('name');
 124              break;
 125          }
 126      }
 127  
 128      /**
 129      * Sets the path of the metadata file, gets its contents and cleans loaded file
 130      *
 131      * @throws \phpbb\extension\exception
 132      */
 133  	private function fetch_metadata_from_file()
 134      {
 135          $ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
 136          $metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json';
 137  
 138          $this->metadata_file = $metadata_filepath;
 139  
 140          if (!file_exists($this->metadata_file))
 141          {
 142              throw new \phpbb\extension\exception($this->user->lang('FILE_NOT_FOUND', $this->metadata_file));
 143          }
 144  
 145          if (!($file_contents = file_get_contents($this->metadata_file)))
 146          {
 147              throw new \phpbb\extension\exception($this->user->lang('FILE_CONTENT_ERR', $this->metadata_file));
 148          }
 149  
 150          if (($metadata = json_decode($file_contents, true)) === null)
 151          {
 152              throw new \phpbb\extension\exception($this->user->lang('FILE_JSON_DECODE_ERR', $this->metadata_file));
 153          }
 154  
 155          array_walk_recursive($metadata, array($this, 'sanitize_json'));
 156          $this->metadata = $metadata;
 157      }
 158  
 159      /**
 160       * Sanitize input from JSON array using htmlspecialchars()
 161       *
 162       * @param mixed        $value    Value of array row
 163       * @param string    $key    Key of array row
 164       */
 165  	public function sanitize_json(&$value, $key)
 166      {
 167          $value = htmlspecialchars($value);
 168      }
 169  
 170      /**
 171      * Validate fields
 172      *
 173      * @param string $name  ("all" for display and enable validation
 174      *                         "display" for name, type, and authors
 175      *                         "name", "type")
 176      * @return Bool True if valid, throws an exception if invalid
 177      * @throws \phpbb\extension\exception
 178      */
 179  	public function validate($name = 'display')
 180      {
 181          // Basic fields
 182          $fields = array(
 183              'name'        => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#',
 184              'type'        => '#^phpbb-extension$#',
 185              'license'    => '#.+#',
 186              'version'    => '#.+#',
 187          );
 188  
 189          switch ($name)
 190          {
 191              case 'all':
 192                  $this->validate_enable();
 193                  // no break
 194  
 195              case 'display':
 196                  foreach ($fields as $field => $data)
 197                  {
 198                      $this->validate($field);
 199                  }
 200  
 201                  $this->validate_authors();
 202              break;
 203  
 204              default:
 205                  if (isset($fields[$name]))
 206                  {
 207                      if (!isset($this->metadata[$name]))
 208                      {
 209                          throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', $name));
 210                      }
 211  
 212                      if (!preg_match($fields[$name], $this->metadata[$name]))
 213                      {
 214                          throw new \phpbb\extension\exception($this->user->lang('META_FIELD_INVALID', $name));
 215                      }
 216                  }
 217              break;
 218          }
 219  
 220          return true;
 221      }
 222  
 223      /**
 224      * Validates the contents of the authors field
 225      *
 226      * @return boolean True when passes validation, throws exception if invalid
 227      * @throws \phpbb\extension\exception
 228      */
 229  	public function validate_authors()
 230      {
 231          if (empty($this->metadata['authors']))
 232          {
 233              throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', 'authors'));
 234          }
 235  
 236          foreach ($this->metadata['authors'] as $author)
 237          {
 238              if (!isset($author['name']))
 239              {
 240                  throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', 'author name'));
 241              }
 242          }
 243  
 244          return true;
 245      }
 246  
 247      /**
 248      * This array handles the verification that this extension can be enabled on this board
 249      *
 250      * @return bool True if validation succeeded, throws an exception if invalid
 251      * @throws \phpbb\extension\exception
 252      */
 253  	public function validate_enable()
 254      {
 255          // Check for valid directory & phpBB, PHP versions
 256          return $this->validate_dir() && $this->validate_require_phpbb() && $this->validate_require_php();
 257      }
 258  
 259      /**
 260      * Validates the most basic directory structure to ensure it follows <vendor>/<ext> convention.
 261      *
 262      * @return boolean True when passes validation, throws an exception if invalid
 263      * @throws \phpbb\extension\exception
 264      */
 265  	public function validate_dir()
 266      {
 267          if (substr_count($this->ext_name, '/') !== 1 || $this->ext_name != $this->get_metadata('name'))
 268          {
 269              throw new \phpbb\extension\exception($this->user->lang('EXTENSION_DIR_INVALID'));
 270          }
 271  
 272          return true;
 273      }
 274  
 275  
 276      /**
 277      * Validates the contents of the phpbb requirement field
 278      *
 279      * @return boolean True when passes validation, throws an exception if invalid
 280      * @throws \phpbb\extension\exception
 281      */
 282  	public function validate_require_phpbb()
 283      {
 284          if (!isset($this->metadata['extra']['soft-require']['phpbb/phpbb']))
 285          {
 286              throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', 'soft-require'));
 287          }
 288  
 289          return true;
 290      }
 291  
 292      /**
 293      * Validates the contents of the php requirement field
 294      *
 295      * @return boolean True when passes validation, throws an exception if invalid
 296      * @throws \phpbb\extension\exception
 297      */
 298  	public function validate_require_php()
 299      {
 300          if (!isset($this->metadata['require']['php']))
 301          {
 302              throw new \phpbb\extension\exception($this->user->lang('META_FIELD_NOT_SET', 'require php'));
 303          }
 304  
 305          return true;
 306      }
 307  
 308      /**
 309      * Outputs the metadata into the template
 310      *
 311      * @return null
 312      */
 313  	public function output_template_data()
 314      {
 315          $this->template->assign_vars(array(
 316              'META_NAME'            => $this->metadata['name'],
 317              'META_TYPE'            => $this->metadata['type'],
 318              'META_DESCRIPTION'    => (isset($this->metadata['description'])) ? $this->metadata['description'] : '',
 319              'META_HOMEPAGE'        => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
 320              'META_VERSION'        => (isset($this->metadata['version'])) ? $this->metadata['version'] : '',
 321              'META_TIME'            => (isset($this->metadata['time'])) ? $this->metadata['time'] : '',
 322              'META_LICENSE'        => $this->metadata['license'],
 323  
 324              'META_REQUIRE_PHP'        => (isset($this->metadata['require']['php'])) ? $this->metadata['require']['php'] : '',
 325              'META_REQUIRE_PHP_FAIL'    => (isset($this->metadata['require']['php'])) ? false : true,
 326  
 327              'META_REQUIRE_PHPBB'        => (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? $this->metadata['extra']['soft-require']['phpbb/phpbb'] : '',
 328              'META_REQUIRE_PHPBB_FAIL'    => (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? false : true,
 329  
 330              'META_DISPLAY_NAME'    => (isset($this->metadata['extra']['display-name'])) ? $this->metadata['extra']['display-name'] : '',
 331          ));
 332  
 333          foreach ($this->metadata['authors'] as $author)
 334          {
 335              $this->template->assign_block_vars('meta_authors', array(
 336                  'AUTHOR_NAME'        => $author['name'],
 337                  'AUTHOR_EMAIL'        => (isset($author['email'])) ? $author['email'] : '',
 338                  'AUTHOR_HOMEPAGE'    => (isset($author['homepage'])) ? $author['homepage'] : '',
 339                  'AUTHOR_ROLE'        => (isset($author['role'])) ? $author['role'] : '',
 340              ));
 341          }
 342      }
 343  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1