Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

metadata_manager.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 8.82 KiB


001  <?php
002  /**
003  *
004  * This file is part of the phpBB Forum Software package.
005  *
006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
007  * @license GNU General Public License, version 2 (GPL-2.0)
008  *
009  * For full copyright and license information, please see
010  * the docs/CREDITS.txt file.
011  *
012  */
013   
014  namespace phpbb\extension;
015   
016  /**
017  * The extension metadata manager validates and gets meta-data for extensions
018  */
019  class metadata_manager
020  {
021      /**
022      * phpBB Config instance
023      * @var \phpbb\config\config
024      */
025      protected $config;
026   
027      /**
028      * phpBB Extension Manager
029      * @var \phpbb\extension\manager
030      */
031      protected $extension_manager;
032   
033      /**
034      * phpBB root path
035      * @var string
036      */
037      protected $phpbb_root_path;
038   
039      /**
040      * Name (including vendor) of the extension
041      * @var string
042      */
043      protected $ext_name;
044   
045      /**
046      * Metadata from the composer.json file
047      * @var array
048      */
049      protected $metadata;
050   
051      /**
052      * Link (including root path) to the metadata file
053      * @var string
054      */
055      protected $metadata_file;
056   
057      /**
058      * Creates the metadata manager
059      *
060      * @param string                $ext_name            Name (including vendor) of the extension
061      * @param \phpbb\config\config        $config                phpBB Config instance
062      * @param \phpbb\extension\manager    $extension_manager    An instance of the phpBB extension manager
063      * @param string                $phpbb_root_path    Path to the phpbb includes directory.
064      */
065      public function __construct($ext_name, \phpbb\config\config $config, \phpbb\extension\manager $extension_manager, $phpbb_root_path)
066      {
067          $this->config = $config;
068          $this->extension_manager = $extension_manager;
069          $this->phpbb_root_path = $phpbb_root_path;
070   
071          $this->ext_name = $ext_name;
072          $this->metadata = array();
073          $this->metadata_file = '';
074      }
075   
076      /**
077      * Processes and gets the metadata requested
078      *
079      * @param  string $element            All for all metadata that it has and is valid, otherwise specify which section you want by its shorthand term.
080      * @return array                    Contains all of the requested metadata, throws an exception on failure
081      */
082      public function get_metadata($element = 'all')
083      {
084          $this->set_metadata_file();
085   
086          // Fetch the metadata
087          $this->fetch_metadata();
088   
089          // Clean the metadata
090          $this->clean_metadata_array();
091   
092          switch ($element)
093          {
094              case 'all':
095              default:
096                  // Validate the metadata
097                  if (!$this->validate())
098                  {
099                      return false;
100                  }
101   
102                  return $this->metadata;
103              break;
104   
105              case 'version':
106              case 'name':
107                  return ($this->validate($element)) ? $this->metadata[$element] : false;
108              break;
109   
110              case 'display-name':
111                  if (isset($this->metadata['extra']['display-name']))
112                  {
113                      return $this->metadata['extra']['display-name'];
114                  }
115                  else
116                  {
117                      return ($this->validate('name')) ? $this->metadata['name'] : false;
118                  }
119              break;
120          }
121      }
122   
123      /**
124      * Sets the filepath of the metadata file
125      *
126      * @throws \phpbb\extension\exception
127      */
128      private function set_metadata_file()
129      {
130          $ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
131          $metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json';
132   
133          $this->metadata_file = $metadata_filepath;
134   
135          if (!file_exists($this->metadata_file))
136          {
137              throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
138          }
139      }
140   
141      /**
142      * Gets the contents of the composer.json file
143      *
144      * @return bool True if success, throws an exception on failure
145      * @throws \phpbb\extension\exception
146      */
147      private function fetch_metadata()
148      {
149          if (!file_exists($this->metadata_file))
150          {
151              throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
152          }
153          else
154          {
155              if (!($file_contents = file_get_contents($this->metadata_file)))
156              {
157                  throw new \phpbb\extension\exception('FILE_CONTENT_ERR', array($this->metadata_file));
158              }
159   
160              if (($metadata = json_decode($file_contents, true)) === null)
161              {
162                  throw new \phpbb\extension\exception('FILE_JSON_DECODE_ERR', array($this->metadata_file));
163              }
164   
165              array_walk_recursive($metadata, array($this, 'sanitize_json'));
166              $this->metadata = $metadata;
167   
168              return true;
169          }
170      }
171   
172      /**
173       * Sanitize input from JSON array using htmlspecialchars()
174       *
175       * @param mixed        $value    Value of array row
176       * @param string    $key    Key of array row
177       */
178      public function sanitize_json(&$value, $key)
179      {
180          $value = htmlspecialchars($value);
181      }
182   
183      /**
184      * This array handles the cleaning of the array
185      *
186      * @return array Contains the cleaned metadata array
187      */
188      private function clean_metadata_array()
189      {
190          return $this->metadata;
191      }
192   
193      /**
194      * Validate fields
195      *
196      * @param string $name  ("all" for display and enable validation
197      *                         "display" for name, type, and authors
198      *                         "name", "type")
199      * @return Bool True if valid, throws an exception if invalid
200      * @throws \phpbb\extension\exception
201      */
202      public function validate($name = 'display')
203      {
204          // Basic fields
205          $fields = array(
206              'name'        => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#',
207              'type'        => '#^phpbb-extension$#',
208              'license'    => '#.+#',
209              'version'    => '#.+#',
210          );
211   
212          switch ($name)
213          {
214              case 'all':
215                  $this->validate('display');
216   
217                  $this->validate_enable();
218              break;
219   
220              case 'display':
221                  foreach ($fields as $field => $data)
222                  {
223                      $this->validate($field);
224                  }
225   
226                  $this->validate_authors();
227              break;
228   
229              default:
230                  if (isset($fields[$name]))
231                  {
232                      if (!isset($this->metadata[$name]))
233                      {
234                          throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array($name));
235                      }
236   
237                      if (!preg_match($fields[$name], $this->metadata[$name]))
238                      {
239                          throw new \phpbb\extension\exception('META_FIELD_INVALID', array($name));
240                      }
241                  }
242              break;
243          }
244   
245          return true;
246      }
247   
248      /**
249      * Validates the contents of the authors field
250      *
251      * @return boolean True when passes validation, throws exception if invalid
252      * @throws \phpbb\extension\exception
253      */
254      public function validate_authors()
255      {
256          if (empty($this->metadata['authors']))
257          {
258              throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('authors'));
259          }
260   
261          foreach ($this->metadata['authors'] as $author)
262          {
263              if (!isset($author['name']))
264              {
265                  throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('author name'));
266              }
267          }
268   
269          return true;
270      }
271   
272      /**
273      * This array handles the verification that this extension can be enabled on this board
274      *
275      * @return bool True if validation succeeded, False if failed
276      */
277      public function validate_enable()
278      {
279          // Check for valid directory & phpBB, PHP versions
280          if (!$this->validate_dir() || !$this->validate_require_phpbb() || !$this->validate_require_php())
281          {
282              return false;
283          }
284   
285          return true;
286      }
287   
288      /**
289      * Validates the most basic directory structure to ensure it follows <vendor>/<ext> convention.
290      *
291      * @return boolean True when passes validation
292      */
293      public function validate_dir()
294      {
295          return (substr_count($this->ext_name, '/') === 1 && $this->ext_name == $this->get_metadata('name'));
296      }
297   
298   
299      /**
300      * Validates the contents of the phpbb requirement field
301      *
302      * @return boolean True when passes validation
303      */
304      public function validate_require_phpbb()
305      {
306          if (!isset($this->metadata['extra']['soft-require']['phpbb/phpbb']))
307          {
308              return false;
309          }
310   
311          return true;
312      }
313   
314      /**
315      * Validates the contents of the php requirement field
316      *
317      * @return boolean True when passes validation
318      */
319      public function validate_require_php()
320      {
321          if (!isset($this->metadata['require']['php']))
322          {
323              return false;
324          }
325   
326          return true;
327      }
328   
329      /**
330      * Outputs the metadata into the template
331      *
332      * @param \phpbb\template\template    $template    phpBB Template instance
333      */
334      public function output_template_data(\phpbb\template\template $template)
335      {
336          $template->assign_vars(array(
337              'META_NAME'            => $this->metadata['name'],
338              'META_TYPE'            => $this->metadata['type'],
339              'META_DESCRIPTION'    => (isset($this->metadata['description'])) ? $this->metadata['description'] : '',
340              'META_HOMEPAGE'        => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
341              'META_VERSION'        => (isset($this->metadata['version'])) ? $this->metadata['version'] : '',
342              'META_TIME'            => (isset($this->metadata['time'])) ? $this->metadata['time'] : '',
343              'META_LICENSE'        => $this->metadata['license'],
344   
345              'META_REQUIRE_PHP'        => (isset($this->metadata['require']['php'])) ? $this->metadata['require']['php'] : '',
346              'META_REQUIRE_PHP_FAIL'    => !$this->validate_require_php(),
347   
348              'META_REQUIRE_PHPBB'        => (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? $this->metadata['extra']['soft-require']['phpbb/phpbb'] : '',
349              'META_REQUIRE_PHPBB_FAIL'    => !$this->validate_require_phpbb(),
350   
351              'META_DISPLAY_NAME'    => (isset($this->metadata['extra']['display-name'])) ? $this->metadata['extra']['display-name'] : '',
352          ));
353   
354          foreach ($this->metadata['authors'] as $author)
355          {
356              $template->assign_block_vars('meta_authors', array(
357                  'AUTHOR_NAME'        => $author['name'],
358                  'AUTHOR_EMAIL'        => (isset($author['email'])) ? $author['email'] : '',
359                  'AUTHOR_HOMEPAGE'    => (isset($author['homepage'])) ? $author['homepage'] : '',
360                  'AUTHOR_ROLE'        => (isset($author['role'])) ? $author['role'] : '',
361              ));
362          }
363      }
364  }
365