Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

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: 02.04.2025, 15:02 - Dateigröße: 6.13 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      * Name (including vendor) of the extension
023      * @var string
024      */
025      protected $ext_name;
026   
027      /**
028      * Metadata from the composer.json file
029      * @var array
030      */
031      protected $metadata;
032   
033      /**
034      * Link (including root path) to the metadata file
035      * @var string
036      */
037      protected $metadata_file;
038   
039      /**
040      * Creates the metadata manager
041      *
042      * @param string                $ext_name            Name (including vendor) of the extension
043      * @param string                $ext_path            Path to the extension directory including root path
044      */
045      public function __construct($ext_name, $ext_path)
046      {
047          $this->ext_name = $ext_name;
048          $this->metadata = array();
049          $this->metadata_file = $ext_path . 'composer.json';
050      }
051   
052      /**
053      * Processes and gets the metadata requested
054      *
055      * @param  string $element            All for all metadata that it has and is valid, otherwise specify which section you want by its shorthand term.
056      * @return array                    Contains all of the requested metadata, throws an exception on failure
057      */
058      public function get_metadata($element = 'all')
059      {
060          // Fetch and clean the metadata if not done yet
061          if ($this->metadata === array())
062          {
063              $this->fetch_metadata_from_file();
064          }
065   
066          switch ($element)
067          {
068              case 'all':
069              default:
070                  $this->validate();
071                  return $this->metadata;
072              break;
073   
074              case 'version':
075              case 'name':
076                  $this->validate($element);
077                  return $this->metadata[$element];
078              break;
079   
080              case 'display-name':
081                  return (isset($this->metadata['extra']['display-name'])) ? $this->metadata['extra']['display-name'] : $this->get_metadata('name');
082              break;
083          }
084      }
085   
086      /**
087      * Gets the metadata file contents and cleans loaded file
088      *
089      * @throws \phpbb\extension\exception
090      */
091      private function fetch_metadata_from_file()
092      {
093          if (!file_exists($this->metadata_file))
094          {
095              throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
096          }
097   
098          if (!($file_contents = file_get_contents($this->metadata_file)))
099          {
100              throw new \phpbb\extension\exception('FILE_CONTENT_ERR', array($this->metadata_file));
101          }
102   
103          if (($metadata = json_decode($file_contents, true)) === null)
104          {
105              throw new \phpbb\extension\exception('FILE_JSON_DECODE_ERR', array($this->metadata_file));
106          }
107   
108          array_walk_recursive($metadata, array($this, 'sanitize_json'));
109          $this->metadata = $metadata;
110      }
111   
112      /**
113       * Sanitize input from JSON array using htmlspecialchars()
114       *
115       * @param mixed        $value    Value of array row
116       * @param string    $key    Key of array row
117       */
118      public function sanitize_json(&$value, $key)
119      {
120          $value = htmlspecialchars($value, ENT_COMPAT);
121      }
122   
123      /**
124      * Validate fields
125      *
126      * @param string $name  ("all" for display and enable validation
127      *                         "display" for name, type, and authors
128      *                         "name", "type")
129      * @return Bool True if valid, throws an exception if invalid
130      * @throws \phpbb\extension\exception
131      */
132      public function validate($name = 'display')
133      {
134          // Basic fields
135          $fields = array(
136              'name'        => '#^[a-zA-Z0-9_\x7f-\xff]{2,}/[a-zA-Z0-9_\x7f-\xff]{2,}$#',
137              'type'        => '#^phpbb-extension$#',
138              'license'    => '#.+#',
139              'version'    => '#.+#',
140          );
141   
142          switch ($name)
143          {
144              case 'all':
145                  $this->validate_enable();
146              // no break
147   
148              case 'display':
149                  foreach ($fields as $field => $data)
150                  {
151                      $this->validate($field);
152                  }
153   
154                  $this->validate_authors();
155              break;
156   
157              default:
158                  if (isset($fields[$name]))
159                  {
160                      if (!isset($this->metadata[$name]))
161                      {
162                          throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array($name));
163                      }
164   
165                      if (!preg_match($fields[$name], $this->metadata[$name]))
166                      {
167                          throw new \phpbb\extension\exception('META_FIELD_INVALID', array($name));
168                      }
169                  }
170              break;
171          }
172   
173          return true;
174      }
175   
176      /**
177      * Validates the contents of the authors field
178      *
179      * @return boolean True when passes validation, throws exception if invalid
180      * @throws \phpbb\extension\exception
181      */
182      public function validate_authors()
183      {
184          if (empty($this->metadata['authors']))
185          {
186              throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('authors'));
187          }
188   
189          foreach ($this->metadata['authors'] as $author)
190          {
191              if (!isset($author['name']))
192              {
193                  throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('author name'));
194              }
195          }
196   
197          return true;
198      }
199   
200      /**
201      * This array handles the verification that this extension can be enabled on this board
202      *
203      * @return bool True if validation succeeded, throws an exception if invalid
204      * @throws \phpbb\extension\exception
205      */
206      public function validate_enable()
207      {
208          // Check for valid directory & phpBB, PHP versions
209          return $this->validate_dir() && $this->validate_require_phpbb() && $this->validate_require_php();
210      }
211   
212      /**
213      * Validates the most basic directory structure to ensure it follows <vendor>/<ext> convention.
214      *
215      * @return boolean True when passes validation, throws an exception if invalid
216      * @throws \phpbb\extension\exception
217      */
218      public function validate_dir()
219      {
220          if (substr_count($this->ext_name, '/') !== 1 || $this->ext_name != $this->get_metadata('name'))
221          {
222              throw new \phpbb\extension\exception('EXTENSION_DIR_INVALID');
223          }
224   
225          return true;
226      }
227   
228   
229      /**
230      * Validates the contents of the phpbb requirement field
231      *
232      * @return boolean True when passes validation, throws an exception if invalid
233      * @throws \phpbb\extension\exception
234      */
235      public function validate_require_phpbb()
236      {
237          if (!isset($this->metadata['extra']['soft-require']['phpbb/phpbb']))
238          {
239              throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('soft-require'));
240          }
241   
242          return true;
243      }
244   
245      /**
246      * Validates the contents of the php requirement field
247      *
248      * @return boolean True when passes validation, throws an exception if invalid
249      * @throws \phpbb\extension\exception
250      */
251      public function validate_require_php()
252      {
253          if (!isset($this->metadata['require']['php']))
254          {
255              throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('require php'));
256          }
257   
258          return true;
259      }
260  }
261