[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/includes/ -> functions_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  /**
  15  * @ignore
  16  */
  17  if (!defined('IN_PHPBB'))
  18  {
  19      exit;
  20  }
  21  
  22  /**
  23  * Class handling all types of 'plugins' (a future term)
  24  */
  25  class p_master
  26  {
  27      var $p_id;
  28      var $p_class;
  29      var $p_name;
  30      var $p_mode;
  31      var $p_parent;
  32  
  33      var $include_path = false;
  34      var $active_module = false;
  35      var $active_module_row_id = false;
  36      var $acl_forum_id = false;
  37      var $module_ary = array();
  38  
  39      /**
  40      * Constuctor
  41      * Set module include path
  42      */
  43  	function __construct($include_path = false)
  44      {
  45          global $phpbb_root_path;
  46  
  47          $this->include_path = ($include_path !== false) ? $include_path : $phpbb_root_path . 'includes/';
  48  
  49          // Make sure the path ends with /
  50          if (substr($this->include_path, -1) !== '/')
  51          {
  52              $this->include_path .= '/';
  53          }
  54      }
  55  
  56      /**
  57      * Set custom include path for modules
  58      * Schema for inclusion is include_path . modulebase
  59      *
  60      * @param string $include_path include path to be used.
  61      * @access public
  62      */
  63  	function set_custom_include_path($include_path)
  64      {
  65          $this->include_path = $include_path;
  66  
  67          // Make sure the path ends with /
  68          if (substr($this->include_path, -1) !== '/')
  69          {
  70              $this->include_path .= '/';
  71          }
  72      }
  73  
  74      /**
  75      * List modules
  76      *
  77      * This creates a list, stored in $this->module_ary of all available
  78      * modules for the given class (ucp, mcp and acp). Additionally
  79      * $this->module_y_ary is created with indentation information for
  80      * displaying the module list appropriately. Only modules for which
  81      * the user has access rights are included in these lists.
  82      */
  83  	function list_modules($p_class)
  84      {
  85          global $db, $user, $cache;
  86          global $phpbb_dispatcher;
  87  
  88          // Sanitise for future path use, it's escaped as appropriate for queries
  89          $this->p_class = str_replace(array('.', '/', '\\'), '', basename($p_class));
  90  
  91          // Get cached modules
  92          if (($this->module_cache = $cache->get('_modules_' . $this->p_class)) === false)
  93          {
  94              // Get modules
  95              $sql = 'SELECT *
  96                  FROM ' . MODULES_TABLE . "
  97                  WHERE module_class = '" . $db->sql_escape($this->p_class) . "'
  98                  ORDER BY left_id ASC";
  99              $result = $db->sql_query($sql);
 100  
 101              $rows = array();
 102              while ($row = $db->sql_fetchrow($result))
 103              {
 104                  $rows[$row['module_id']] = $row;
 105              }
 106              $db->sql_freeresult($result);
 107  
 108              $this->module_cache = array();
 109              foreach ($rows as $module_id => $row)
 110              {
 111                  $this->module_cache['modules'][] = $row;
 112                  $this->module_cache['parents'][$row['module_id']] = $this->get_parents($row['parent_id'], $row['left_id'], $row['right_id'], $rows);
 113              }
 114              unset($rows);
 115  
 116              $cache->put('_modules_' . $this->p_class, $this->module_cache);
 117          }
 118  
 119          if (empty($this->module_cache))
 120          {
 121              $this->module_cache = array('modules' => array(), 'parents' => array());
 122          }
 123  
 124          // We "could" build a true tree with this function - maybe mod authors want to use this...
 125          // Functions for traversing and manipulating the tree are not available though
 126          // We might re-structure the module system to use true trees in 4.0
 127          // $tree = $this->build_tree($this->module_cache['modules'], $this->module_cache['parents']);
 128  
 129          // Clean up module cache array to only let survive modules the user can access
 130          $right_id = false;
 131  
 132          $hide_categories = array();
 133          foreach ($this->module_cache['modules'] as $key => $row)
 134          {
 135              // When the module has no mode (category) we check whether it has visible children
 136              // before listing it as well.
 137              if (!$row['module_mode'])
 138              {
 139                  $hide_categories[(int) $row['module_id']] = $key;
 140              }
 141  
 142              // Not allowed to view module?
 143              if (!$this->module_auth_self($row['module_auth']))
 144              {
 145                  unset($this->module_cache['modules'][$key]);
 146                  continue;
 147              }
 148  
 149              // Category with no members, ignore
 150              if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']))
 151              {
 152                  unset($this->module_cache['modules'][$key]);
 153                  continue;
 154              }
 155  
 156              // Skip branch
 157              if ($right_id !== false)
 158              {
 159                  if ($row['left_id'] < $right_id)
 160                  {
 161                      unset($this->module_cache['modules'][$key]);
 162                      continue;
 163                  }
 164  
 165                  $right_id = false;
 166              }
 167  
 168              // Not enabled?
 169              if (!$row['module_enabled'])
 170              {
 171                  // If category is disabled then disable every child too
 172                  unset($this->module_cache['modules'][$key]);
 173                  $right_id = $row['right_id'];
 174                  continue;
 175              }
 176  
 177              if ($row['module_mode'])
 178              {
 179                  // The parent category has a visible child
 180                  // So remove it and all its parents from the hide array
 181                  unset($hide_categories[(int) $row['parent_id']]);
 182                  foreach ($this->module_cache['parents'][$row['module_id']] as $module_id => $row_id)
 183                  {
 184                      unset($hide_categories[$module_id]);
 185                  }
 186              }
 187          }
 188  
 189          foreach ($hide_categories as $module_id => $row_id)
 190          {
 191              unset($this->module_cache['modules'][$row_id]);
 192          }
 193  
 194          // Re-index (this is needed, else we are not able to array_slice later)
 195          $this->module_cache['modules'] = array_merge($this->module_cache['modules']);
 196  
 197          // Include MOD _info files for populating language entries within the menus
 198          $this->add_mod_info($this->p_class);
 199  
 200          // Now build the module array, but exclude completely empty categories...
 201          $right_id = false;
 202          $names = array();
 203  
 204          foreach ($this->module_cache['modules'] as $key => $row)
 205          {
 206              // Skip branch
 207              if ($right_id !== false)
 208              {
 209                  if ($row['left_id'] < $right_id)
 210                  {
 211                      continue;
 212                  }
 213  
 214                  $right_id = false;
 215              }
 216  
 217              // Category with no members on their way down (we have to check every level)
 218              if (!$row['module_basename'])
 219              {
 220                  $empty_category = true;
 221  
 222                  // We go through the branch and look for an activated module
 223                  foreach (array_slice($this->module_cache['modules'], $key + 1) as $temp_row)
 224                  {
 225                      if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
 226                      {
 227                          // Module there
 228                          if ($temp_row['module_basename'] && $temp_row['module_enabled'])
 229                          {
 230                              $empty_category = false;
 231                              break;
 232                          }
 233                          continue;
 234                      }
 235                      break;
 236                  }
 237  
 238                  // Skip the branch
 239                  if ($empty_category)
 240                  {
 241                      $right_id = $row['right_id'];
 242                      continue;
 243                  }
 244              }
 245  
 246              $depth = count($this->module_cache['parents'][$row['module_id']]);
 247  
 248              // We need to prefix the functions to not create a naming conflict
 249  
 250              // Function for building 'url_extra'
 251              $short_name = $this->get_short_name($row['module_basename']);
 252  
 253              $url_func = 'phpbb_module_' . $short_name . '_url';
 254              if (!function_exists($url_func))
 255              {
 256                  $url_func = '_module_' . $short_name . '_url';
 257              }
 258  
 259              // Function for building the language name
 260              $lang_func = 'phpbb_module_' . $short_name . '_lang';
 261              if (!function_exists($lang_func))
 262              {
 263                  $lang_func = '_module_' . $short_name . '_lang';
 264              }
 265  
 266              // Custom function for calling parameters on module init (for example assigning template variables)
 267              $custom_func = 'phpbb_module_' . $short_name;
 268              if (!function_exists($custom_func))
 269              {
 270                  $custom_func = '_module_' . $short_name;
 271              }
 272  
 273              $names[$row['module_basename'] . '_' . $row['module_mode']][] = true;
 274  
 275              $module_row = array(
 276                  'depth'        => $depth,
 277  
 278                  'id'        => (int) $row['module_id'],
 279                  'parent'    => (int) $row['parent_id'],
 280                  'cat'        => ($row['right_id'] > $row['left_id'] + 1) ? true : false,
 281  
 282                  'is_duplicate'    => ($row['module_basename'] && count($names[$row['module_basename'] . '_' . $row['module_mode']]) > 1) ? true : false,
 283  
 284                  'name'        => (string) $row['module_basename'],
 285                  'mode'        => (string) $row['module_mode'],
 286                  'display'    => (int) $row['module_display'],
 287  
 288                  'url_extra'    => (function_exists($url_func)) ? $url_func($row['module_mode'], $row) : '',
 289  
 290                  'lang'        => ($row['module_basename'] && function_exists($lang_func)) ? $lang_func($row['module_mode'], $row['module_langname']) : ((!empty($user->lang[$row['module_langname']])) ? $user->lang[$row['module_langname']] : $row['module_langname']),
 291                  'langname'    => $row['module_langname'],
 292  
 293                  'left'        => $row['left_id'],
 294                  'right'        => $row['right_id'],
 295              );
 296  
 297              if (function_exists($custom_func))
 298              {
 299                  $custom_func($row['module_mode'], $module_row);
 300              }
 301  
 302              /**
 303              * This event allows to modify parameters for building modules list
 304              *
 305              * @event core.modify_module_row
 306              * @var    string        url_func        Function for building 'url_extra'
 307              * @var    string        lang_func        Function for building the language name
 308              * @var    string        custom_func        Custom function for calling parameters on module init
 309              * @var    array        row                Array holding the basic module data
 310              * @var    array        module_row        Array holding the module display parameters
 311              * @since 3.1.0-b3
 312              */
 313              $vars = array('url_func', 'lang_func', 'custom_func', 'row', 'module_row');
 314              extract($phpbb_dispatcher->trigger_event('core.modify_module_row', compact($vars)));
 315  
 316              $this->module_ary[] = $module_row;
 317          }
 318  
 319          unset($this->module_cache['modules'], $names);
 320      }
 321  
 322      /**
 323      * Check if a certain main module is accessible/loaded
 324      * By giving the module mode you are able to additionally check for only one mode within the main module
 325      *
 326      * @param string $module_basename The module base name, for example logs, reports, main (for the mcp).
 327      * @param mixed $module_mode The module mode to check. If provided the mode will be checked in addition for presence.
 328      *
 329      * @return bool Returns true if module is loaded and accessible, else returns false
 330      */
 331  	function loaded($module_basename, $module_mode = false)
 332      {
 333          if (!$this->is_full_class($module_basename))
 334          {
 335              $module_basename = $this->p_class . '_' . $module_basename;
 336          }
 337  
 338          if (empty($this->loaded_cache))
 339          {
 340              $this->loaded_cache = array();
 341  
 342              foreach ($this->module_ary as $row)
 343              {
 344                  if (!$row['name'])
 345                  {
 346                      continue;
 347                  }
 348  
 349                  if (!isset($this->loaded_cache[$row['name']]))
 350                  {
 351                      $this->loaded_cache[$row['name']] = array();
 352                  }
 353  
 354                  if (!$row['mode'])
 355                  {
 356                      continue;
 357                  }
 358  
 359                  $this->loaded_cache[$row['name']][$row['mode']] = true;
 360              }
 361          }
 362  
 363          if ($module_mode === false)
 364          {
 365              return (isset($this->loaded_cache[$module_basename])) ? true : false;
 366          }
 367  
 368          return (!empty($this->loaded_cache[$module_basename][$module_mode])) ? true : false;
 369      }
 370  
 371      /**
 372      * Check module authorisation.
 373      *
 374      * This is a non-static version that uses $this->acl_forum_id
 375      * for the forum id.
 376      */
 377  	function module_auth_self($module_auth)
 378      {
 379          return self::module_auth($module_auth, $this->acl_forum_id);
 380      }
 381  
 382      /**
 383      * Check module authorisation.
 384      *
 385      * This is a static version, it must be given $forum_id.
 386      * See also module_auth_self.
 387      */
 388  	static function module_auth($module_auth, $forum_id)
 389      {
 390          global $auth, $config;
 391          global $request, $phpbb_extension_manager, $phpbb_dispatcher;
 392  
 393          $module_auth = trim($module_auth);
 394  
 395          // Generally allowed to access module if module_auth is empty
 396          if (!$module_auth)
 397          {
 398              return true;
 399          }
 400  
 401          // With the code below we make sure only those elements get eval'd we really want to be checked
 402          preg_match_all('/(?:
 403              "[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
 404              \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
 405              [(),]                                  |
 406              [^\s(),]+)/x', $module_auth, $match);
 407  
 408          // Valid tokens for auth and their replacements
 409          $valid_tokens = array(
 410              'acl_([a-z0-9_]+)(,\$id)?'        => '(int) $auth->acl_get(\'\\1\'\\2)',
 411              '\$id'                            => '(int) $forum_id',
 412              'aclf_([a-z0-9_]+)'                => '(int) $auth->acl_getf_global(\'\\1\')',
 413              'cfg_([a-z0-9_]+)'                => '(int) $config[\'\\1\']',
 414              'request_([a-zA-Z0-9_]+)'        => '$request->variable(\'\\1\', false)',
 415              'ext_([a-zA-Z0-9_/]+)'            => 'array_key_exists(\'\\1\', $phpbb_extension_manager->all_enabled())',
 416              'authmethod_([a-z0-9_\\\\]+)'        => '($config[\'auth_method\'] === \'\\1\')',
 417          );
 418  
 419          /**
 420          * Alter tokens for module authorisation check
 421          *
 422          * @event core.module_auth
 423          * @var    array    valid_tokens        Valid tokens and their auth check
 424          *                                    replacements
 425          * @var    string    module_auth            The module_auth of the current
 426          *                                     module
 427          * @var    int        forum_id            The current forum_id
 428          * @since 3.1.0-a3
 429          */
 430          $vars = array('valid_tokens', 'module_auth', 'forum_id');
 431          extract($phpbb_dispatcher->trigger_event('core.module_auth', compact($vars)));
 432  
 433          $tokens = $match[0];
 434          for ($i = 0, $size = count($tokens); $i < $size; $i++)
 435          {
 436              $token = &$tokens[$i];
 437  
 438              switch ($token)
 439              {
 440                  case ')':
 441                  case '(':
 442                  case '&&':
 443                  case '||':
 444                  case ',':
 445                  break;
 446  
 447                  default:
 448                      if (!preg_match('#(?:' . implode(')|(?:', array_keys($valid_tokens)) . ')#', $token))
 449                      {
 450                          $token = '';
 451                      }
 452                  break;
 453              }
 454          }
 455  
 456          $module_auth = implode(' ', $tokens);
 457  
 458          // Make sure $id separation is working fine
 459          $module_auth = str_replace(' , ', ',', $module_auth);
 460  
 461          $module_auth = preg_replace(
 462              // Array keys with # prepended/appended
 463              array_map(function($value) {
 464                  return '#' . $value . '#';
 465              }, array_keys($valid_tokens)),
 466              array_values($valid_tokens),
 467              $module_auth
 468          );
 469  
 470          $is_auth = false;
 471          // @codingStandardsIgnoreStart
 472          eval('$is_auth = (int) (' .    $module_auth . ');');
 473          // @codingStandardsIgnoreEnd
 474  
 475          return $is_auth;
 476      }
 477  
 478      /**
 479      * Set active module
 480      */
 481  	function set_active($id = false, $mode = false)
 482      {
 483          global $auth, $request, $user;
 484  
 485          $icat = false;
 486          $this->active_module = false;
 487  
 488          if ($request->variable('icat', ''))
 489          {
 490              $icat = $id;
 491              $id = $request->variable('icat', '');
 492          }
 493  
 494          // Restore the backslashes in class names
 495          if (strpos($id, '-') !== false)
 496          {
 497              $id = str_replace('-', '\\', $id);
 498          }
 499  
 500          if ($id && !is_numeric($id) && !$this->is_full_class($id))
 501          {
 502              $id = $this->p_class . '_' . $id;
 503          }
 504  
 505          // Fallback to acp main page for special test permission mode
 506          if ($this->p_class === 'acp' && $user->data['user_perm_from'] && $auth->acl_get('a_switchperm'))
 507          {
 508              $id = '';
 509              $mode = '';
 510              $icat = false;
 511          }
 512  
 513          $category = false;
 514          foreach ($this->module_ary as $row_id => $item_ary)
 515          {
 516              // If this is a module and it's selected, active
 517              // If this is a category and the module is the first within it, active
 518              // If this is a module and no mode selected, select first mode
 519              // If no category or module selected, go active for first module in first category
 520              if (
 521                  (($item_ary['name'] === $id || $item_ary['name'] === $this->p_class . '_' . $id || $item_ary['id'] === (int) $id) && (($item_ary['mode'] == $mode && !$item_ary['cat']) || ($icat && $item_ary['cat']))) ||
 522                  ($item_ary['parent'] === $category && !$item_ary['cat'] && !$icat && $item_ary['display']) ||
 523                  (($item_ary['name'] === $id || $item_ary['name'] === $this->p_class . '_' . $id || $item_ary['id'] === (int) $id) && !$mode && !$item_ary['cat']) ||
 524                  (!$id && !$mode && !$item_ary['cat'] && $item_ary['display'])
 525                  )
 526              {
 527                  if ($item_ary['cat'])
 528                  {
 529                      $id = $icat;
 530                      $icat = false;
 531  
 532                      continue;
 533                  }
 534  
 535                  $this->p_id        = $item_ary['id'];
 536                  $this->p_parent    = $item_ary['parent'];
 537                  $this->p_name    = $item_ary['name'];
 538                  $this->p_mode     = $item_ary['mode'];
 539                  $this->p_left    = $item_ary['left'];
 540                  $this->p_right    = $item_ary['right'];
 541  
 542                  $this->module_cache['parents'] = $this->module_cache['parents'][$this->p_id];
 543                  $this->active_module = $item_ary['id'];
 544                  $this->active_module_row_id = $row_id;
 545  
 546                  break;
 547              }
 548              else if (($item_ary['cat'] && $item_ary['id'] === (int) $id) || ($item_ary['parent'] === $category && $item_ary['cat']))
 549              {
 550                  $category = $item_ary['id'];
 551              }
 552          }
 553      }
 554  
 555      /**
 556      * Loads currently active module
 557      *
 558      * This method loads a given module, passing it the relevant id and mode.
 559      *
 560      * @param string|false $mode mode, as passed through to the module
 561      * @param string|false $module_url If supplied, we use this module url
 562      * @param bool $execute_module If true, at the end we execute the main method for the new instance
 563      */
 564  	function load_active($mode = false, $module_url = false, $execute_module = true)
 565      {
 566          global $phpbb_root_path, $phpbb_admin_path, $phpEx, $user, $template, $request;
 567  
 568          $module_path = $this->include_path . $this->p_class;
 569          $icat = $request->variable('icat', '');
 570  
 571          if ($this->active_module === false)
 572          {
 573              trigger_error('MODULE_NOT_ACCESS', E_USER_ERROR);
 574          }
 575  
 576          // new modules use the full class names, old ones are always called <type>_<name>, e.g. acp_board
 577          if (!class_exists($this->p_name))
 578          {
 579              if (!file_exists("$module_path/{$this->p_name}.$phpEx"))
 580              {
 581                  trigger_error($user->lang('MODULE_NOT_FIND', "$module_path/{$this->p_name}.$phpEx"), E_USER_ERROR);
 582              }
 583  
 584              include("$module_path/{$this->p_name}.$phpEx");
 585  
 586              if (!class_exists($this->p_name))
 587              {
 588                  trigger_error($user->lang('MODULE_FILE_INCORRECT_CLASS', "$module_path/{$this->p_name}.$phpEx", $this->p_name), E_USER_ERROR);
 589              }
 590          }
 591  
 592          if (!empty($mode))
 593          {
 594              $this->p_mode = $mode;
 595          }
 596  
 597          // Create a new instance of the desired module ...
 598          $class_name = $this->p_name;
 599  
 600          $this->module = new $class_name($this);
 601  
 602          // We pre-define the action parameter we are using all over the place
 603          if (defined('IN_ADMIN'))
 604          {
 605              /*
 606              * If this is an extension module, we'll try to automatically set
 607              * the style paths for the extension (the ext author can change them
 608              * if necessary).
 609              */
 610              $module_dir = explode('\\', get_class($this->module));
 611  
 612              // 0 vendor, 1 extension name, ...
 613              if (isset($module_dir[1]))
 614              {
 615                  $module_style_dir = $phpbb_root_path . 'ext/' . $module_dir[0] . '/' . $module_dir[1] . '/adm/style';
 616  
 617                  if (is_dir($module_style_dir))
 618                  {
 619                      $template->set_custom_style(array(
 620                          array(
 621                              'name'         => 'adm',
 622                              'ext_path'     => 'adm/style/',
 623                          ),
 624                      ), array($module_style_dir, $phpbb_admin_path . 'style'));
 625                  }
 626              }
 627  
 628              // Is first module automatically enabled a duplicate and the category not passed yet?
 629              if (!$icat && $this->module_ary[$this->active_module_row_id]['is_duplicate'])
 630              {
 631                  $icat = $this->module_ary[$this->active_module_row_id]['parent'];
 632              }
 633  
 634              // Not being able to overwrite ;)
 635              $this->module->u_action = append_sid("{$phpbb_admin_path}index.$phpEx", 'i=' . $this->get_module_identifier($this->p_name)) . (($icat) ? '&amp;icat=' . $icat : '') . "&amp;mode={$this->p_mode}";
 636          }
 637          else
 638          {
 639              /*
 640              * If this is an extension module, we'll try to automatically set
 641              * the style paths for the extension (the ext author can change them
 642              * if necessary).
 643              */
 644              $module_dir = explode('\\', get_class($this->module));
 645  
 646              // 0 vendor, 1 extension name, ...
 647              if (isset($module_dir[1]))
 648              {
 649                  $module_style_dir = 'ext/' . $module_dir[0] . '/' . $module_dir[1] . '/styles';
 650  
 651                  if (is_dir($phpbb_root_path . $module_style_dir))
 652                  {
 653                      $template->set_style(array($module_style_dir, 'styles'));
 654                  }
 655              }
 656  
 657              // If user specified the module url we will use it...
 658              if ($module_url !== false)
 659              {
 660                  $this->module->u_action = $module_url;
 661              }
 662              else
 663              {
 664                  $this->module->u_action = $phpbb_root_path . (($user->page['page_dir']) ? $user->page['page_dir'] . '/' : '') . $user->page['page_name'];
 665              }
 666  
 667              $this->module->u_action = append_sid($this->module->u_action, 'i=' . $this->get_module_identifier($this->p_name)) . (($icat) ? '&amp;icat=' . $icat : '') . "&amp;mode={$this->p_mode}";
 668          }
 669  
 670          // Add url_extra parameter to u_action url
 671          if (!empty($this->module_ary) && $this->active_module !== false && $this->module_ary[$this->active_module_row_id]['url_extra'])
 672          {
 673              $this->module->u_action .= '&amp;' . $this->module_ary[$this->active_module_row_id]['url_extra'];
 674          }
 675  
 676          // Assign the module path for re-usage
 677          $this->module->module_path = $module_path . '/';
 678  
 679          // Execute the main method for the new instance, we send the module id and mode as parameters
 680          // Users are able to call the main method after this function to be able to assign additional parameters manually
 681          if ($execute_module)
 682          {
 683              $short_name = preg_replace("#^{$this->p_class}_#", '', $this->p_name);
 684              $this->module->main($short_name, $this->p_mode);
 685          }
 686      }
 687  
 688      /**
 689      * Appending url parameter to the currently active module.
 690      *
 691      * This function is called for adding specific url parameters while executing the current module.
 692      * It is doing the same as the _module_{name}_url() function, apart from being able to be called after
 693      * having dynamically parsed specific parameters. This allows more freedom in choosing additional parameters.
 694      * One example can be seen in /includes/mcp/mcp_notes.php - $this->p_master->adjust_url() call.
 695      *
 696      * @param string $url_extra Extra url parameters, e.g.: &amp;u=$user_id
 697      *
 698      */
 699  	function adjust_url($url_extra)
 700      {
 701          if (empty($this->module_ary[$this->active_module_row_id]))
 702          {
 703              return;
 704          }
 705  
 706          $row = &$this->module_ary[$this->active_module_row_id];
 707  
 708          // We check for the same url_extra in $row['url_extra'] to overcome doubled additions...
 709          if (strpos($row['url_extra'], $url_extra) === false)
 710          {
 711              $row['url_extra'] .= $url_extra;
 712          }
 713      }
 714  
 715      /**
 716      * Check if a module is active
 717      */
 718  	function is_active($id, $mode = false)
 719      {
 720          // If we find a name by this id and being enabled we have our active one...
 721          foreach ($this->module_ary as $row_id => $item_ary)
 722          {
 723              if (($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && $item_ary['display'] || $item_ary['name'] === $this->p_class . '_' . $id)
 724              {
 725                  if ($mode === false || $mode === $item_ary['mode'])
 726                  {
 727                      return true;
 728                  }
 729              }
 730          }
 731  
 732          return false;
 733      }
 734  
 735      /**
 736      * Get parents
 737      */
 738  	function get_parents($parent_id, $left_id, $right_id, &$all_parents)
 739      {
 740          $parents = array();
 741  
 742          if ($parent_id > 0)
 743          {
 744              foreach ($all_parents as $module_id => $row)
 745              {
 746                  if ($row['left_id'] < $left_id && $row['right_id'] > $right_id)
 747                  {
 748                      $parents[$module_id] = $row['parent_id'];
 749                  }
 750  
 751                  if ($row['left_id'] > $left_id)
 752                  {
 753                      break;
 754                  }
 755              }
 756          }
 757  
 758          return $parents;
 759      }
 760  
 761      /**
 762      * Get tree branch
 763      */
 764  	function get_branch($left_id, $right_id, $remaining)
 765      {
 766          $branch = array();
 767  
 768          foreach ($remaining as $key => $row)
 769          {
 770              if ($row['left_id'] > $left_id && $row['left_id'] < $right_id)
 771              {
 772                  $branch[] = $row;
 773                  continue;
 774              }
 775              break;
 776          }
 777  
 778          return $branch;
 779      }
 780  
 781      /**
 782      * Build true binary tree from given array
 783      * Not in use
 784      */
 785  	function build_tree(&$modules, &$parents)
 786      {
 787          $tree = array();
 788  
 789          foreach ($modules as $row)
 790          {
 791              $branch = &$tree;
 792  
 793              if ($row['parent_id'])
 794              {
 795                  // Go through the tree to find our branch
 796                  $parent_tree = $parents[$row['module_id']];
 797  
 798                  foreach ($parent_tree as $id => $value)
 799                  {
 800                      if (!isset($branch[$id]) && isset($branch['child']))
 801                      {
 802                          $branch = &$branch['child'];
 803                      }
 804                      $branch = &$branch[$id];
 805                  }
 806                  $branch = &$branch['child'];
 807              }
 808  
 809              $branch[$row['module_id']] = $row;
 810              if (!isset($branch[$row['module_id']]['child']))
 811              {
 812                  $branch[$row['module_id']]['child'] = array();
 813              }
 814          }
 815  
 816          return $tree;
 817      }
 818  
 819      /**
 820      * Build navigation structure
 821      */
 822  	function assign_tpl_vars($module_url)
 823      {
 824          global $template;
 825  
 826          $current_id = $right_id = false;
 827  
 828          // Make sure the module_url has a question mark set, effectively determining the delimiter to use
 829          $delim = (strpos($module_url, '?') === false) ? '?' : '&amp;';
 830  
 831          $current_depth = 0;
 832          $linear_offset     = 'l_block1';
 833          $tabular_offset = 't_block2';
 834  
 835          // Generate the list of modules, we'll do this in two ways ...
 836          // 1) In a linear fashion
 837          // 2) In a combined tabbed + linear fashion ... tabs for the categories
 838          //    and a linear list for subcategories/items
 839          foreach ($this->module_ary as $row_id => $item_ary)
 840          {
 841              // Skip hidden modules
 842              if (!$item_ary['display'])
 843              {
 844                  continue;
 845              }
 846  
 847              // Skip branch
 848              if ($right_id !== false)
 849              {
 850                  if ($item_ary['left'] < $right_id)
 851                  {
 852                      continue;
 853                  }
 854  
 855                  $right_id = false;
 856              }
 857  
 858              // Category with no members on their way down (we have to check every level)
 859              if (!$item_ary['name'])
 860              {
 861                  $empty_category = true;
 862  
 863                  // We go through the branch and look for an activated module
 864                  foreach (array_slice($this->module_ary, $row_id + 1) as $temp_row)
 865                  {
 866                      if ($temp_row['left'] > $item_ary['left'] && $temp_row['left'] < $item_ary['right'])
 867                      {
 868                          // Module there and displayed?
 869                          if ($temp_row['name'] && $temp_row['display'])
 870                          {
 871                              $empty_category = false;
 872                              break;
 873                          }
 874                          continue;
 875                      }
 876                      break;
 877                  }
 878  
 879                  // Skip the branch
 880                  if ($empty_category)
 881                  {
 882                      $right_id = $item_ary['right'];
 883                      continue;
 884                  }
 885              }
 886  
 887              // Select first id we can get
 888              if (!$current_id && (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id))
 889              {
 890                  $current_id = $item_ary['id'];
 891              }
 892  
 893              $depth = $item_ary['depth'];
 894  
 895              if ($depth > $current_depth)
 896              {
 897                  $linear_offset = $linear_offset . '.l_block' . ($depth + 1);
 898                  $tabular_offset = ($depth + 1 > 2) ? $tabular_offset . '.t_block' . ($depth + 1) : $tabular_offset;
 899              }
 900              else if ($depth < $current_depth)
 901              {
 902                  for ($i = $current_depth - $depth; $i > 0; $i--)
 903                  {
 904                      $linear_offset = substr($linear_offset, 0, strrpos($linear_offset, '.'));
 905                      $tabular_offset = ($i + $depth > 1) ? substr($tabular_offset, 0, strrpos($tabular_offset, '.')) : $tabular_offset;
 906                  }
 907              }
 908  
 909              $u_title = $module_url . $delim . 'i=';
 910              // if the item has a name use it, else use its id
 911              if (empty($item_ary['name']))
 912              {
 913                  $u_title .= $item_ary['id'];
 914              }
 915              else
 916              {
 917                  // if the category has a name, then use it.
 918                  $u_title .= $this->get_module_identifier($item_ary['name']);
 919              }
 920              // If the item is not a category append the mode
 921              if (!$item_ary['cat'])
 922              {
 923                  if ($item_ary['is_duplicate'])
 924                  {
 925                      $u_title .= '&amp;icat=' . $current_id;
 926                  }
 927                  $u_title .= '&amp;mode=' . $item_ary['mode'];
 928              }
 929  
 930              // Was not allowed in categories before - /*!$item_ary['cat'] && */
 931              $u_title .= (isset($item_ary['url_extra']) && $item_ary['url_extra']) ? '&amp;' . $item_ary['url_extra'] : '';
 932  
 933              // Only output a categories items if it's currently selected
 934              if (!$depth || ($depth && (in_array($item_ary['parent'], array_values($this->module_cache['parents'])) || $item_ary['parent'] == $this->p_parent)))
 935              {
 936                  $use_tabular_offset = (!$depth) ? 't_block1' : $tabular_offset;
 937  
 938                  $tpl_ary = array(
 939                      'L_TITLE'        => $item_ary['lang'],
 940                      'S_SELECTED'    => (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id) ? true : false,
 941                      'U_TITLE'        => $u_title
 942                  );
 943  
 944                  if (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id)
 945                  {
 946                      $template->assign_block_vars('navlinks', array(
 947                          'BREADCRUMB_NAME'    => $item_ary['lang'],
 948                          'U_BREADCRUMB'        => $u_title,
 949                      ));
 950                  }
 951  
 952                  $template->assign_block_vars($use_tabular_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
 953              }
 954  
 955              $tpl_ary = array(
 956                  'L_TITLE'        => $item_ary['lang'],
 957                  'S_SELECTED'    => (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id) ? true : false,
 958                  'U_TITLE'        => $u_title
 959              );
 960  
 961              $template->assign_block_vars($linear_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
 962  
 963              $current_depth = $depth;
 964          }
 965      }
 966  
 967      /**
 968      * Returns desired template name
 969      */
 970  	function get_tpl_name()
 971      {
 972          return $this->module->tpl_name . '.html';
 973      }
 974  
 975      /**
 976      * Returns the desired page title
 977      */
 978  	function get_page_title()
 979      {
 980          global $user;
 981  
 982          if (!isset($this->module->page_title))
 983          {
 984              return '';
 985          }
 986  
 987          return (isset($user->lang[$this->module->page_title])) ? $user->lang[$this->module->page_title] : $this->module->page_title;
 988      }
 989  
 990      /**
 991      * Load module as the current active one without the need for registering it
 992      *
 993      * @param string $class module class (acp/mcp/ucp)
 994      * @param string $name module name (class name of the module, or its basename
 995      *                     phpbb_ext_foo_acp_bar_module, ucp_zebra or zebra)
 996      * @param string $mode mode, as passed through to the module
 997      *
 998      */
 999  	function load($class, $name, $mode = false)
1000      {
1001          // new modules use the full class names, old ones are always called <class>_<name>, e.g. acp_board
1002          // in the latter case this function may be called as load('acp', 'board')
1003          if (!class_exists($name) && substr($name, 0, strlen($class) + 1) !== $class . '_')
1004          {
1005              $name = $class . '_' . $name;
1006          }
1007  
1008          $this->p_class = $class;
1009          $this->p_name = $name;
1010  
1011          // Set active module to true instead of using the id
1012          $this->active_module = true;
1013  
1014          $this->load_active($mode);
1015      }
1016  
1017      /**
1018      * Display module
1019      */
1020  	function display($page_title, $display_online_list = false)
1021      {
1022          global $template, $user;
1023  
1024          // Generate the page
1025          if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
1026          {
1027              adm_page_header($page_title);
1028          }
1029          else
1030          {
1031              page_header($page_title, $display_online_list);
1032          }
1033  
1034          $template->set_filenames(array(
1035              'body' => $this->get_tpl_name())
1036          );
1037  
1038          if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
1039          {
1040              adm_page_footer();
1041          }
1042          else
1043          {
1044              page_footer();
1045          }
1046      }
1047  
1048      /**
1049      * Toggle whether this module will be displayed or not
1050      */
1051  	function set_display($id, $mode = false, $display = true)
1052      {
1053          foreach ($this->module_ary as $row_id => $item_ary)
1054          {
1055              if (($item_ary['name'] === $id || $item_ary['name'] === $this->p_class . '_' . $id || $item_ary['id'] === (int) $id) && (!$mode || $item_ary['mode'] === $mode))
1056              {
1057                  $this->module_ary[$row_id]['display'] = (int) $display;
1058              }
1059          }
1060      }
1061  
1062      /**
1063      * Add custom MOD info language file
1064      */
1065  	function add_mod_info($module_class)
1066      {
1067          global $config, $user, $phpEx, $phpbb_extension_manager;
1068  
1069          $finder = $phpbb_extension_manager->get_finder();
1070  
1071          // We grab the language files from the default, English and user's language.
1072          // So we can fall back to the other files like we do when using add_lang()
1073          $default_lang_files = $english_lang_files = $user_lang_files = array();
1074  
1075          // Search for board default language if it's not the user language
1076          if ($config['default_lang'] != $user->lang_name)
1077          {
1078              $default_lang_files = $finder
1079                  ->prefix('info_' . strtolower($module_class) . '_')
1080                  ->suffix(".$phpEx")
1081                  ->extension_directory('/language/' . basename($config['default_lang']))
1082                  ->core_path('language/' . basename($config['default_lang']) . '/mods/')
1083                  ->find();
1084          }
1085  
1086          // Search for english, if its not the default or user language
1087          if ($config['default_lang'] != 'en' && $user->lang_name != 'en')
1088          {
1089              $english_lang_files = $finder
1090                  ->prefix('info_' . strtolower($module_class) . '_')
1091                  ->suffix(".$phpEx")
1092                  ->extension_directory('/language/en')
1093                  ->core_path('language/en/mods/')
1094                  ->find();
1095          }
1096  
1097          // Find files in the user's language
1098          $user_lang_files = $finder
1099              ->prefix('info_' . strtolower($module_class) . '_')
1100              ->suffix(".$phpEx")
1101              ->extension_directory('/language/' . $user->lang_name)
1102              ->core_path('language/' . $user->lang_name . '/mods/')
1103              ->find();
1104  
1105          $lang_files = array_merge($english_lang_files, $default_lang_files, $user_lang_files);
1106          foreach ($lang_files as $lang_file => $ext_name)
1107          {
1108              $user->add_lang_ext($ext_name, $lang_file);
1109          }
1110      }
1111  
1112      /**
1113      * Retrieve shortened module basename for legacy basenames (with xcp_ prefix)
1114      *
1115      * @param string $basename A module basename
1116      * @return string The basename if it starts with phpbb_ or the basename with
1117      *                the current p_class (e.g. acp_) stripped.
1118      */
1119  	protected function get_short_name($basename)
1120      {
1121          if (substr($basename, 0, 6) === 'phpbb\\' || strpos($basename, '\\') !== false)
1122          {
1123              return $basename;
1124          }
1125  
1126          // strip xcp_ prefix from old classes
1127          return substr($basename, strlen($this->p_class) + 1);
1128      }
1129  
1130      /**
1131      * If the basename contains a \ we don't use that for the URL.
1132      *
1133      * Firefox is currently unable to correctly copy a urlencoded \
1134      * so users will be unable to post links to modules.
1135      * However we can replace them with dashes and re-replace them later
1136      *
1137      * @param    string    $basename    Basename of the module
1138      * @return        string    Identifier that should be used for
1139      *                        module link creation
1140      */
1141  	protected function get_module_identifier($basename)
1142      {
1143          if (strpos($basename, '\\') === false)
1144          {
1145              return $basename;
1146          }
1147  
1148          return str_replace('\\', '-', $basename);
1149      }
1150  
1151      /**
1152      * Checks whether the given module basename is a correct class name
1153      *
1154      * @param string $basename A module basename
1155      * @return bool True if the basename starts with phpbb_ or (x)cp_, false otherwise
1156      */
1157  	protected function is_full_class($basename)
1158      {
1159          return (strpos($basename, '\\') !== false || preg_match('/^(ucp|mcp|acp)_/', $basename));
1160      }
1161  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1