Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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