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

module.php

Zuletzt modifiziert: 09.10.2024, 12:55 - Dateigröße: 13.69 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\db\migration\tool;
015   
016  /**
017  * Migration module management tool
018  */
019  class module implements \phpbb\db\migration\tool\tool_interface
020  {
021      /** @var \phpbb\cache\service */
022      protected $cache;
023   
024      /** @var \phpbb\db\driver\driver_interface */
025      protected $db;
026   
027      /** @var \phpbb\user */
028      protected $user;
029   
030      /** @var string */
031      protected $phpbb_root_path;
032   
033      /** @var string */
034      protected $php_ext;
035   
036      /** @var string */
037      protected $modules_table;
038   
039      /**
040      * Constructor
041      *
042      * @param \phpbb\db\driver\driver_interface $db
043      * @param \phpbb\cache\service $cache
044      * @param \phpbb\user $user
045      * @param string $phpbb_root_path
046      * @param string $php_ext
047      * @param string $modules_table
048      */
049      public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, \phpbb\user $user, $phpbb_root_path, $php_ext, $modules_table)
050      {
051          $this->db = $db;
052          $this->cache = $cache;
053          $this->user = $user;
054          $this->phpbb_root_path = $phpbb_root_path;
055          $this->php_ext = $php_ext;
056          $this->modules_table = $modules_table;
057      }
058   
059      /**
060      * {@inheritdoc}
061      */
062      public function get_name()
063      {
064          return 'module';
065      }
066   
067      /**
068      * Module Exists
069      *
070      * Check if a module exists
071      *
072      * @param string $class The module class(acp|mcp|ucp)
073      * @param int|string|bool $parent The parent module_id|module_langname (0 for no parent).
074      *        Use false to ignore the parent check and check class wide.
075      * @param int|string $module The module_id|module_langname you would like to
076      *         check for to see if it exists
077      * @return bool true/false if module exists
078      */
079      public function exists($class, $parent, $module)
080      {
081          // the main root directory should return true
082          if (!$module)
083          {
084              return true;
085          }
086   
087          $parent_sql = '';
088          if ($parent !== false)
089          {
090              // Allows '' to be sent as 0
091              $parent = $parent ?: 0;
092   
093              if (!is_numeric($parent))
094              {
095                  $sql = 'SELECT module_id
096                      FROM ' . $this->modules_table . "
097                      WHERE module_langname = '" . $this->db->sql_escape($parent) . "'
098                          AND module_class = '" . $this->db->sql_escape($class) . "'";
099                  $result = $this->db->sql_query($sql);
100                  $module_id = $this->db->sql_fetchfield('module_id');
101                  $this->db->sql_freeresult($result);
102   
103                  if (!$module_id)
104                  {
105                      return false;
106                  }
107   
108                  $parent_sql = 'AND parent_id = ' . (int) $module_id;
109              }
110              else
111              {
112                  $parent_sql = 'AND parent_id = ' . (int) $parent;
113              }
114          }
115   
116          $sql = 'SELECT module_id
117              FROM ' . $this->modules_table . "
118              WHERE module_class = '" . $this->db->sql_escape($class) . "'
119                  $parent_sql
120                  AND " . ((is_numeric($module)) ? 'module_id = ' . (int) $module : "module_langname = '" . $this->db->sql_escape($module) . "'");
121          $result = $this->db->sql_query($sql);
122          $module_id = $this->db->sql_fetchfield('module_id');
123          $this->db->sql_freeresult($result);
124   
125          if ($module_id)
126          {
127              return true;
128          }
129   
130          return false;
131      }
132   
133      /**
134      * Module Add
135      *
136      * Add a new module
137      *
138      * @param string $class The module class(acp|mcp|ucp)
139      * @param int|string $parent The parent module_id|module_langname (0 for no parent)
140      * @param array $data an array of the data on the new \module.
141      *     This can be setup in two different ways.
142      *    1. The "manual" way.  For inserting a category or one at a time.
143      *        It will be merged with the base array shown a bit below,
144      *            but at the least requires 'module_langname' to be sent, and,
145      *            if you want to create a module (instead of just a category) you must
146      *            send module_basename and module_mode.
147      *        array(
148      *            'module_enabled'    => 1,
149      *            'module_display'    => 1,
150      *               'module_basename'    => '',
151      *            'module_class'        => $class,
152      *               'parent_id'            => (int) $parent,
153      *            'module_langname'    => '',
154      *               'module_mode'        => '',
155      *               'module_auth'        => '',
156      *        )
157      *    2. The "automatic" way.  For inserting multiple at a time based on the
158      *            specs in the info file for the module(s).  For this to work the
159      *            modules must be correctly setup in the info file.
160      *        An example follows (this would insert the settings, log, and flag
161      *            modes from the includes/acp/info/acp_asacp.php file):
162      *         array(
163      *             'module_basename'    => 'asacp',
164      *             'modes'                => array('settings', 'log', 'flag'),
165      *         )
166      *         Optionally you may not send 'modes' and it will insert all of the
167      *             modules in that info file.
168      *     path, specify that here
169      * @return null
170      * @throws \phpbb\db\migration\exception
171      */
172      public function add($class, $parent = 0, $data = array())
173      {
174          // Allows '' to be sent as 0
175          $parent = $parent ?: 0;
176   
177          // allow sending the name as a string in $data to create a category
178          if (!is_array($data))
179          {
180              $data = array('module_langname' => $data);
181          }
182   
183          if (!isset($data['module_langname']))
184          {
185              // The "automatic" way
186              $basename = (isset($data['module_basename'])) ? $data['module_basename'] : '';
187              $module = $this->get_module_info($class, $basename);
188   
189              $result = '';
190              foreach ($module['modes'] as $mode => $module_info)
191              {
192                  if (!isset($data['modes']) || in_array($mode, $data['modes']))
193                  {
194                      $new_module = array(
195                          'module_basename'    => $basename,
196                          'module_langname'    => $module_info['title'],
197                          'module_mode'        => $mode,
198                          'module_auth'        => $module_info['auth'],
199                          'module_display'    => (isset($module_info['display'])) ? $module_info['display'] : true,
200                          'before'            => (isset($module_info['before'])) ? $module_info['before'] : false,
201                          'after'                => (isset($module_info['after'])) ? $module_info['after'] : false,
202                      );
203   
204                      // Run the "manual" way with the data we've collected.
205                      $this->add($class, $parent, $new_module);
206                  }
207              }
208   
209              return;
210          }
211   
212          // The "manual" way
213          if (!is_numeric($parent))
214          {
215              $sql = 'SELECT module_id
216                  FROM ' . $this->modules_table . "
217                  WHERE module_langname = '" . $this->db->sql_escape($parent) . "'
218                      AND module_class = '" . $this->db->sql_escape($class) . "'";
219              $result = $this->db->sql_query($sql);
220              $module_id = $this->db->sql_fetchfield('module_id');
221              $this->db->sql_freeresult($result);
222   
223              if (!$module_id)
224              {
225                  throw new \phpbb\db\migration\exception('MODULE_NOT_EXIST', $parent);
226              }
227   
228              $parent = $data['parent_id'] = $module_id;
229          }
230          else if (!$this->exists($class, false, $parent))
231          {
232              throw new \phpbb\db\migration\exception('MODULE_NOT_EXIST', $parent);
233          }
234   
235          if ($this->exists($class, $parent, $data['module_langname']))
236          {
237              return;
238          }
239   
240          if (!class_exists('acp_modules'))
241          {
242              include($this->phpbb_root_path . 'includes/acp/acp_modules.' . $this->php_ext);
243              $this->user->add_lang('acp/modules');
244          }
245          $acp_modules = new \acp_modules();
246   
247          $module_data = array(
248              'module_enabled'    => (isset($data['module_enabled'])) ? $data['module_enabled'] : 1,
249              'module_display'    => (isset($data['module_display'])) ? $data['module_display'] : 1,
250              'module_basename'    => (isset($data['module_basename'])) ? $data['module_basename'] : '',
251              'module_class'        => $class,
252              'parent_id'            => (int) $parent,
253              'module_langname'    => (isset($data['module_langname'])) ? $data['module_langname'] : '',
254              'module_mode'        => (isset($data['module_mode'])) ? $data['module_mode'] : '',
255              'module_auth'        => (isset($data['module_auth'])) ? $data['module_auth'] : '',
256          );
257          $result = $acp_modules->update_module_data($module_data, true);
258   
259          // update_module_data can either return a string or an empty array...
260          if (is_string($result))
261          {
262              // Error
263              throw new \phpbb\db\migration\exception('MODULE_ERROR', $result);
264          }
265          else
266          {
267              // Success
268              $module_log_name = ((isset($this->user->lang[$data['module_langname']])) ? $this->user->lang[$data['module_langname']] : $data['module_langname']);
269              add_log('admin', 'LOG_MODULE_ADD', $module_log_name);
270   
271              // Move the module if requested above/below an existing one
272              if (isset($data['before']) && $data['before'])
273              {
274                  $sql = 'SELECT left_id
275                      FROM ' . $this->modules_table . "
276                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
277                          AND parent_id = " . (int) $parent . "
278                          AND module_langname = '" . $this->db->sql_escape($data['before']) . "'";
279                  $this->db->sql_query($sql);
280                  $to_left = (int) $this->db->sql_fetchfield('left_id');
281   
282                  $sql = 'UPDATE ' . $this->modules_table . "
283                      SET left_id = left_id + 2, right_id = right_id + 2
284                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
285                          AND left_id >= $to_left
286                          AND left_id < {$module_data['left_id']}";
287                  $this->db->sql_query($sql);
288   
289                  $sql = 'UPDATE ' . $this->modules_table . "
290                      SET left_id = $to_left, right_id = " . ($to_left + 1) . "
291                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
292                          AND module_id = {$module_data['module_id']}";
293                  $this->db->sql_query($sql);
294              }
295              else if (isset($data['after']) && $data['after'])
296              {
297                  $sql = 'SELECT right_id
298                      FROM ' . $this->modules_table . "
299                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
300                          AND parent_id = " . (int) $parent . "
301                          AND module_langname = '" . $this->db->sql_escape($data['after']) . "'";
302                  $this->db->sql_query($sql);
303                  $to_right = (int) $this->db->sql_fetchfield('right_id');
304   
305                  $sql = 'UPDATE ' . $this->modules_table . "
306                      SET left_id = left_id + 2, right_id = right_id + 2
307                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
308                          AND left_id >= $to_right
309                          AND left_id < {$module_data['left_id']}";
310                  $this->db->sql_query($sql);
311   
312                  $sql = 'UPDATE ' . $this->modules_table . '
313                      SET left_id = ' . ($to_right + 1) . ', right_id = ' . ($to_right + 2) . "
314                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
315                          AND module_id = {$module_data['module_id']}";
316                  $this->db->sql_query($sql);
317              }
318          }
319   
320          // Clear the Modules Cache
321          $this->cache->destroy("_modules_$class");
322      }
323   
324      /**
325      * Module Remove
326      *
327      * Remove a module
328      *
329      * @param string $class The module class(acp|mcp|ucp)
330      * @param int|string|bool $parent The parent module_id|module_langname(0 for no parent).
331      *     Use false to ignore the parent check and check class wide.
332      * @param int|string $module The module id|module_langname
333      *     specify that here
334      * @return null
335      * @throws \phpbb\db\migration\exception
336      */
337      public function remove($class, $parent = 0, $module = '')
338      {
339          // Imitation of module_add's "automatic" and "manual" method so the uninstaller works from the same set of instructions for umil_auto
340          if (is_array($module))
341          {
342              if (isset($module['module_langname']))
343              {
344                  // Manual Method
345                  return $this->remove($class, $parent, $module['module_langname']);
346              }
347   
348              // Failed.
349              if (!isset($module['module_basename']))
350              {
351                  throw new \phpbb\db\migration\exception('MODULE_NOT_EXIST');
352              }
353   
354              // Automatic method
355              $basename = $module['module_basename'];
356              $module_info = $this->get_module_info($class, $basename);
357   
358              foreach ($module_info['modes'] as $mode => $info)
359              {
360                  if (!isset($module['modes']) || in_array($mode, $module['modes']))
361                  {
362                      $this->remove($class, $parent, $info['title']);
363                  }
364              }
365          }
366          else
367          {
368              if (!$this->exists($class, $parent, $module))
369              {
370                  return;
371              }
372   
373              $parent_sql = '';
374              if ($parent !== false)
375              {
376                  // Allows '' to be sent as 0
377                  $parent = ($parent) ?: 0;
378   
379                  if (!is_numeric($parent))
380                  {
381                      $sql = 'SELECT module_id
382                          FROM ' . $this->modules_table . "
383                          WHERE module_langname = '" . $this->db->sql_escape($parent) . "'
384                              AND module_class = '" . $this->db->sql_escape($class) . "'";
385                      $result = $this->db->sql_query($sql);
386                      $module_id = $this->db->sql_fetchfield('module_id');
387                      $this->db->sql_freeresult($result);
388   
389                      // we know it exists from the module_exists check
390                      $parent_sql = 'AND parent_id = ' . (int) $module_id;
391                  }
392                  else
393                  {
394                      $parent_sql = 'AND parent_id = ' . (int) $parent;
395                  }
396              }
397   
398              $module_ids = array();
399              if (!is_numeric($module))
400              {
401                  $sql = 'SELECT module_id
402                      FROM ' . $this->modules_table . "
403                      WHERE module_langname = '" . $this->db->sql_escape($module) . "'
404                          AND module_class = '" . $this->db->sql_escape($class) . "'
405                          $parent_sql";
406                  $result = $this->db->sql_query($sql);
407                  while ($module_id = $this->db->sql_fetchfield('module_id'))
408                  {
409                      $module_ids[] = (int) $module_id;
410                  }
411                  $this->db->sql_freeresult($result);
412              }
413              else
414              {
415                  $module_ids[] = (int) $module;
416              }
417   
418              if (!class_exists('acp_modules'))
419              {
420                  include($this->phpbb_root_path . 'includes/acp/acp_modules.' . $this->php_ext);
421                  $this->user->add_lang('acp/modules');
422              }
423              $acp_modules = new \acp_modules();
424              $acp_modules->module_class = $class;
425   
426              foreach ($module_ids as $module_id)
427              {
428                  $result = $acp_modules->delete_module($module_id);
429                  if (!empty($result))
430                  {
431                      return;
432                  }
433              }
434   
435              $this->cache->destroy("_modules_$class");
436          }
437      }
438   
439      /**
440      * {@inheritdoc}
441      */
442      public function reverse()
443      {
444          $arguments = func_get_args();
445          $original_call = array_shift($arguments);
446   
447          $call = false;
448          switch ($original_call)
449          {
450              case 'add':
451                  $call = 'remove';
452              break;
453   
454              case 'remove':
455                  $call = 'add';
456              break;
457          }
458   
459          if ($call)
460          {
461              return call_user_func_array(array(&$this, $call), $arguments);
462          }
463      }
464   
465      /**
466      * Wrapper for \acp_modules::get_module_infos()
467      *
468      * @param string $class Module Class
469      * @param string $basename Module Basename
470      * @return array Module Information
471      * @throws \phpbb\db\migration\exception
472      */
473      protected function get_module_info($class, $basename)
474      {
475          if (!class_exists('acp_modules'))
476          {
477              include($this->phpbb_root_path . 'includes/acp/acp_modules.' . $this->php_ext);
478          }
479          $acp_modules = new \acp_modules();
480          $module = $acp_modules->get_module_infos($basename, $class, true);
481   
482          if (empty($module))
483          {
484              throw new \phpbb\db\migration\exception('MODULE_INFO_FILE_NOT_EXIST', $class, $basename);
485          }
486   
487          return array_pop($module);
488      }
489  }
490