[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/db/migration/tool/ -> module.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\db\migration\tool;
  15  
  16  use phpbb\module\exception\module_exception;
  17  
  18  /**
  19  * Migration module management tool
  20  */
  21  class module implements \phpbb\db\migration\tool\tool_interface
  22  {
  23      /** @var \phpbb\cache\service */
  24      protected $cache;
  25  
  26      /** @var \phpbb\db\driver\driver_interface */
  27      protected $db;
  28  
  29      /** @var \phpbb\user */
  30      protected $user;
  31  
  32      /** @var \phpbb\module\module_manager */
  33      protected $module_manager;
  34  
  35      /** @var string */
  36      protected $phpbb_root_path;
  37  
  38      /** @var string */
  39      protected $php_ext;
  40  
  41      /** @var string */
  42      protected $modules_table;
  43  
  44      /** @var array */
  45      protected $module_categories = array();
  46  
  47      /**
  48      * Constructor
  49      *
  50      * @param \phpbb\db\driver\driver_interface $db
  51      * @param \phpbb\cache\service $cache
  52      * @param \phpbb\user $user
  53      * @param \phpbb\module\module_manager    $module_manager
  54      * @param string $phpbb_root_path
  55      * @param string $php_ext
  56      * @param string $modules_table
  57      */
  58  	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, \phpbb\user $user, \phpbb\module\module_manager $module_manager, $phpbb_root_path, $php_ext, $modules_table)
  59      {
  60          $this->db = $db;
  61          $this->cache = $cache;
  62          $this->user = $user;
  63          $this->module_manager = $module_manager;
  64          $this->phpbb_root_path = $phpbb_root_path;
  65          $this->php_ext = $php_ext;
  66          $this->modules_table = $modules_table;
  67      }
  68  
  69      /**
  70      * {@inheritdoc}
  71      */
  72  	public function get_name()
  73      {
  74          return 'module';
  75      }
  76  
  77      /**
  78      * Module Exists
  79      *
  80      * Check if a module exists
  81      *
  82      * @param string $class The module class(acp|mcp|ucp)
  83      * @param int|string|bool $parent The parent module_id|module_langname (0 for no parent).
  84      *        Use false to ignore the parent check and check class wide.
  85      * @param int|string $module The module_id|module_langname you would like to
  86      *         check for to see if it exists
  87      * @param bool $lazy Checks lazily if the module exists. Returns true if it exists in at
  88      *       least one given parent.
  89      * @return bool true if module exists in *all* given parents, false if not in any given parent;
  90       *      true if ignoring parent check and module exists class wide, false if not found at all.
  91      */
  92  	public function exists($class, $parent, $module, $lazy = false)
  93      {
  94          // the main root directory should return true
  95          if (!$module)
  96          {
  97              return true;
  98          }
  99  
 100          $parent_sqls = [];
 101          if ($parent !== false)
 102          {
 103              $parents = $this->get_parent_module_id($parent, $module, false);
 104              if ($parents === false)
 105              {
 106                  return false;
 107              }
 108  
 109              foreach ((array) $parents as $parent_id)
 110              {
 111                  $parent_sqls[] = 'AND parent_id = ' . (int) $parent_id;
 112              }
 113          }
 114          else
 115          {
 116              $parent_sqls[] = '';
 117          }
 118  
 119          foreach ($parent_sqls as $parent_sql)
 120          {
 121              $sql = 'SELECT module_id
 122                  FROM ' . $this->modules_table . "
 123                  WHERE module_class = '" . $this->db->sql_escape($class) . "'
 124                      $parent_sql
 125                      AND " . ((is_numeric($module)) ? 'module_id = ' . (int) $module : "module_langname = '" . $this->db->sql_escape($module) . "'");
 126              $result = $this->db->sql_query($sql);
 127              $module_id = $this->db->sql_fetchfield('module_id');
 128              $this->db->sql_freeresult($result);
 129  
 130              if (!$lazy && !$module_id)
 131              {
 132                  return false;
 133              }
 134              if ($lazy && $module_id)
 135              {
 136                  return true;
 137              }
 138          }
 139  
 140          // Returns true, if modules exist in all parents and false otherwise
 141          return !$lazy;
 142      }
 143  
 144      /**
 145      * Module Add
 146      *
 147      * Add a new module
 148      *
 149      * @param string $class The module class(acp|mcp|ucp)
 150      * @param int|string $parent The parent module_id|module_langname (0 for no parent)
 151      * @param array $data an array of the data on the new \module.
 152      *     This can be setup in two different ways.
 153      *    1. The "manual" way.  For inserting a category or one at a time.
 154      *        It will be merged with the base array shown a bit below,
 155      *            but at the least requires 'module_langname' to be sent, and,
 156      *            if you want to create a module (instead of just a category) you must
 157      *            send module_basename and module_mode.
 158      *        array(
 159      *            'module_enabled'    => 1,
 160      *            'module_display'    => 1,
 161      *               'module_basename'    => '',
 162      *            'module_class'        => $class,
 163      *               'parent_id'            => (int) $parent,
 164      *            'module_langname'    => '',
 165      *               'module_mode'        => '',
 166      *               'module_auth'        => '',
 167      *        )
 168      *    2. The "automatic" way.  For inserting multiple at a time based on the
 169      *            specs in the info file for the module(s).  For this to work the
 170      *            modules must be correctly setup in the info file.
 171      *        An example follows (this would insert the settings, log, and flag
 172      *            modes from the includes/acp/info/acp_asacp.php file):
 173      *         array(
 174      *             'module_basename'    => 'asacp',
 175      *             'modes'                => array('settings', 'log', 'flag'),
 176      *         )
 177      *         Optionally you may not send 'modes' and it will insert all of the
 178      *             modules in that info file.
 179      *     path, specify that here
 180      * @return null
 181      * @throws \phpbb\db\migration\exception
 182      */
 183  	public function add($class, $parent = 0, $data = array())
 184      {
 185          global $user, $phpbb_log;
 186  
 187          // allow sending the name as a string in $data to create a category
 188          if (!is_array($data))
 189          {
 190              $data = array('module_langname' => $data);
 191          }
 192  
 193          $parents = (array) $this->get_parent_module_id($parent, $data);
 194  
 195          if (!isset($data['module_langname']))
 196          {
 197              // The "automatic" way
 198              $basename = (isset($data['module_basename'])) ? $data['module_basename'] : '';
 199              $module = $this->get_module_info($class, $basename);
 200  
 201              foreach ($module['modes'] as $mode => $module_info)
 202              {
 203                  if (!isset($data['modes']) || in_array($mode, $data['modes']))
 204                  {
 205                      $new_module = array(
 206                          'module_basename'    => $basename,
 207                          'module_langname'    => $module_info['title'],
 208                          'module_mode'        => $mode,
 209                          'module_auth'        => $module_info['auth'],
 210                          'module_display'    => (isset($module_info['display'])) ? $module_info['display'] : true,
 211                          'before'            => (isset($module_info['before'])) ? $module_info['before'] : false,
 212                          'after'                => (isset($module_info['after'])) ? $module_info['after'] : false,
 213                      );
 214  
 215                      // Run the "manual" way with the data we've collected.
 216                      foreach ($parents as $parent)
 217                      {
 218                          $this->add($class, $parent, $new_module);
 219                      }
 220                  }
 221              }
 222  
 223              return;
 224          }
 225  
 226          foreach ($parents as $parent)
 227          {
 228              $data['parent_id'] = $parent;
 229  
 230              // The "manual" way
 231              if (!$this->exists($class, false, $parent))
 232              {
 233                  throw new \phpbb\db\migration\exception('MODULE_NOT_EXIST', $parent);
 234              }
 235  
 236              if ($this->exists($class, $parent, $data['module_langname']))
 237              {
 238                  throw new \phpbb\db\migration\exception('MODULE_EXISTS', $data['module_langname']);
 239              }
 240  
 241              $module_data = array(
 242                  'module_enabled'    => (isset($data['module_enabled'])) ? $data['module_enabled'] : 1,
 243                  'module_display'    => (isset($data['module_display'])) ? $data['module_display'] : 1,
 244                  'module_basename'    => (isset($data['module_basename'])) ? $data['module_basename'] : '',
 245                  'module_class'        => $class,
 246                  'parent_id'            => (int) $parent,
 247                  'module_langname'    => (isset($data['module_langname'])) ? $data['module_langname'] : '',
 248                  'module_mode'        => (isset($data['module_mode'])) ? $data['module_mode'] : '',
 249                  'module_auth'        => (isset($data['module_auth'])) ? $data['module_auth'] : '',
 250              );
 251  
 252              try
 253              {
 254                  $this->module_manager->update_module_data($module_data);
 255  
 256                  // Success
 257                  $module_log_name = ((isset($this->user->lang[$data['module_langname']])) ? $this->user->lang[$data['module_langname']] : $data['module_langname']);
 258                  $phpbb_log->add('admin', (isset($user->data['user_id'])) ? $user->data['user_id'] : ANONYMOUS, $user->ip, 'LOG_MODULE_ADD', false, array($module_log_name));
 259  
 260                  // Move the module if requested above/below an existing one
 261                  if (isset($data['before']) && $data['before'])
 262                  {
 263                      $before_mode = $before_langname = '';
 264                      if (is_array($data['before']))
 265                      {
 266                          // Restore legacy-legacy behaviour from phpBB 3.0
 267                          list($before_mode, $before_langname) = $data['before'];
 268                      }
 269                      else
 270                      {
 271                          // Legacy behaviour from phpBB 3.1+
 272                          $before_langname = $data['before'];
 273                      }
 274  
 275                      $sql = 'SELECT left_id
 276                      FROM ' . $this->modules_table . "
 277                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
 278                          AND parent_id = " . (int) $parent . "
 279                          AND module_langname = '" . $this->db->sql_escape($before_langname) . "'"
 280                          . (($before_mode) ? " AND module_mode = '" . $this->db->sql_escape($before_mode) . "'" : '');
 281                      $result = $this->db->sql_query($sql);
 282                      $to_left = (int) $this->db->sql_fetchfield('left_id');
 283                      $this->db->sql_freeresult($result);
 284  
 285                      $sql = 'UPDATE ' . $this->modules_table . "
 286                      SET left_id = left_id + 2, right_id = right_id + 2
 287                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
 288                          AND left_id >= $to_left
 289                          AND left_id < {$module_data['left_id']}";
 290                      $this->db->sql_query($sql);
 291  
 292                      $sql = 'UPDATE ' . $this->modules_table . "
 293                      SET left_id = $to_left, right_id = " . ($to_left + 1) . "
 294                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
 295                          AND module_id = {$module_data['module_id']}";
 296                      $this->db->sql_query($sql);
 297                  }
 298                  else if (isset($data['after']) && $data['after'])
 299                  {
 300                      $after_mode = $after_langname = '';
 301                      if (is_array($data['after']))
 302                      {
 303                          // Restore legacy-legacy behaviour from phpBB 3.0
 304                          list($after_mode, $after_langname) = $data['after'];
 305                      }
 306                      else
 307                      {
 308                          // Legacy behaviour from phpBB 3.1+
 309                          $after_langname = $data['after'];
 310                      }
 311  
 312                      $sql = 'SELECT right_id
 313                      FROM ' . $this->modules_table . "
 314                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
 315                          AND parent_id = " . (int) $parent . "
 316                          AND module_langname = '" . $this->db->sql_escape($after_langname) . "'"
 317                          . (($after_mode) ? " AND module_mode = '" . $this->db->sql_escape($after_mode) . "'" : '');
 318                      $result = $this->db->sql_query($sql);
 319                      $to_right = (int) $this->db->sql_fetchfield('right_id');
 320                      $this->db->sql_freeresult($result);
 321  
 322                      $sql = 'UPDATE ' . $this->modules_table . "
 323                      SET left_id = left_id + 2, right_id = right_id + 2
 324                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
 325                          AND left_id >= $to_right
 326                          AND left_id < {$module_data['left_id']}";
 327                      $this->db->sql_query($sql);
 328  
 329                      $sql = 'UPDATE ' . $this->modules_table . '
 330                      SET left_id = ' . ($to_right + 1) . ', right_id = ' . ($to_right + 2) . "
 331                      WHERE module_class = '" . $this->db->sql_escape($class) . "'
 332                          AND module_id = {$module_data['module_id']}";
 333                      $this->db->sql_query($sql);
 334                  }
 335              }
 336              catch (module_exception $e)
 337              {
 338                  // Error
 339                  throw new \phpbb\db\migration\exception('MODULE_ERROR', $e->getMessage());
 340              }
 341          }
 342  
 343          // Clear the Modules Cache
 344          $this->module_manager->remove_cache_file($class);
 345      }
 346  
 347      /**
 348      * Module Remove
 349      *
 350      * Remove a module
 351      *
 352      * @param string $class The module class(acp|mcp|ucp)
 353      * @param int|string|bool $parent The parent module_id|module_langname(0 for no parent).
 354      *     Use false to ignore the parent check and check class wide.
 355      * @param int|string $module The module id|module_langname
 356      *     specify that here
 357      * @return null
 358      * @throws \phpbb\db\migration\exception
 359      */
 360  	public function remove($class, $parent = 0, $module = '')
 361      {
 362          // Imitation of module_add's "automatic" and "manual" method so the uninstaller works from the same set of instructions for umil_auto
 363          if (is_array($module))
 364          {
 365              if (isset($module['module_langname']))
 366              {
 367                  // Manual Method
 368                  return $this->remove($class, $parent, $module['module_langname']);
 369              }
 370  
 371              // Failed.
 372              if (!isset($module['module_basename']))
 373              {
 374                  throw new \phpbb\db\migration\exception('MODULE_NOT_EXIST');
 375              }
 376  
 377              // Automatic method
 378              $basename = $module['module_basename'];
 379              $module_info = $this->get_module_info($class, $basename);
 380  
 381              foreach ($module_info['modes'] as $mode => $info)
 382              {
 383                  if (!isset($module['modes']) || in_array($mode, $module['modes']))
 384                  {
 385                      $this->remove($class, $parent, $info['title']);
 386                  }
 387              }
 388          }
 389          else
 390          {
 391              if (!$this->exists($class, $parent, $module, true))
 392              {
 393                  return;
 394              }
 395  
 396              $parent_sql = '';
 397              if ($parent !== false)
 398              {
 399                  $parents = (array) $this->get_parent_module_id($parent, $module);
 400                  $parent_sql = 'AND ' . $this->db->sql_in_set('parent_id', $parents);
 401              }
 402  
 403              $module_ids = array();
 404              if (!is_numeric($module))
 405              {
 406                  $sql = 'SELECT module_id
 407                      FROM ' . $this->modules_table . "
 408                      WHERE module_langname = '" . $this->db->sql_escape($module) . "'
 409                          AND module_class = '" . $this->db->sql_escape($class) . "'
 410                          $parent_sql";
 411                  $result = $this->db->sql_query($sql);
 412                  while ($module_id = $this->db->sql_fetchfield('module_id'))
 413                  {
 414                      $module_ids[] = (int) $module_id;
 415                  }
 416                  $this->db->sql_freeresult($result);
 417              }
 418              else
 419              {
 420                  $module_ids[] = (int) $module;
 421              }
 422  
 423              foreach ($module_ids as $module_id)
 424              {
 425                  $this->module_manager->delete_module($module_id, $class);
 426              }
 427  
 428              $this->module_manager->remove_cache_file($class);
 429          }
 430      }
 431  
 432      /**
 433      * {@inheritdoc}
 434      */
 435  	public function reverse()
 436      {
 437          $arguments = func_get_args();
 438          $original_call = array_shift($arguments);
 439  
 440          $call = false;
 441          switch ($original_call)
 442          {
 443              case 'add':
 444                  $call = 'remove';
 445              break;
 446  
 447              case 'remove':
 448                  $call = 'add';
 449              break;
 450  
 451              case 'reverse':
 452                  // Reversing a reverse is just the call itself
 453                  $call = array_shift($arguments);
 454              break;
 455          }
 456  
 457          if ($call)
 458          {
 459              return call_user_func_array(array(&$this, $call), $arguments);
 460          }
 461      }
 462  
 463      /**
 464      * Wrapper for \acp_modules::get_module_infos()
 465      *
 466      * @param string $class Module Class
 467      * @param string $basename Module Basename
 468      * @return array Module Information
 469      * @throws \phpbb\db\migration\exception
 470      */
 471  	protected function get_module_info($class, $basename)
 472      {
 473          $module = $this->module_manager->get_module_infos($class, $basename, true);
 474  
 475          if (empty($module))
 476          {
 477              throw new \phpbb\db\migration\exception('MODULE_INFO_FILE_NOT_EXIST', $class, $basename);
 478          }
 479  
 480          return array_pop($module);
 481      }
 482  
 483      /**
 484      * Get the list of installed module categories
 485      *    key - module_id
 486      *    value - module_langname
 487      *
 488      * @return null
 489      */
 490  	protected function get_categories_list()
 491      {
 492          // Select the top level categories
 493          // and 2nd level [sub]categories
 494          $sql = 'SELECT m2.module_id, m2.module_langname
 495              FROM ' . $this->modules_table . ' m1, ' . $this->modules_table . " m2
 496              WHERE m1.parent_id = 0
 497                  AND (m1.module_id = m2.module_id OR m2.parent_id = m1.module_id)
 498              ORDER BY m1.module_id, m2.module_id ASC";
 499  
 500          $result = $this->db->sql_query($sql);
 501          while ($row = $this->db->sql_fetchrow($result))
 502          {
 503              $this->module_categories[(int) $row['module_id']] = $row['module_langname'];
 504          }
 505          $this->db->sql_freeresult($result);
 506      }
 507  
 508      /**
 509      * Get parent module id
 510      *
 511      * @param string|int $parent_id The parent module_id|module_langname
 512      * @param int|string|array $data The module_id, module_langname for existance checking or module data array for adding
 513      * @param bool $throw_exception The flag indicating if exception should be thrown on error
 514      * @return mixed The int parent module_id, an array of int parent module_id values or false
 515      * @throws \phpbb\db\migration\exception
 516      */
 517  	public function get_parent_module_id($parent_id, $data = '', $throw_exception = true)
 518      {
 519          // Allow '' to be sent as 0
 520          $parent_id = $parent_id ?: 0;
 521  
 522          if (!is_numeric($parent_id))
 523          {
 524              // Refresh the $module_categories array
 525              $this->get_categories_list();
 526  
 527              // Search for the parent module_langname
 528              $ids = array_keys($this->module_categories, $parent_id);
 529  
 530              switch (count($ids))
 531              {
 532                  // No parent with the given module_langname exist
 533                  case 0:
 534                      if ($throw_exception)
 535                      {
 536                          throw new \phpbb\db\migration\exception('MODULE_NOT_EXIST', $parent_id);
 537                      }
 538  
 539                      return false;
 540                  break;
 541  
 542                  // Return the module id
 543                  case 1:
 544                      return (int) $ids[0];
 545                  break;
 546  
 547                  default:
 548                      // This represents the old behaviour of phpBB 3.0
 549                      return $ids;
 550                  break;
 551              }
 552          }
 553  
 554          return $parent_id;
 555      }
 556  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1