6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Class handling all types of 'plugins' (a future term)
31 var $include_path = false;
32 var $active_module = false;
33 var $active_module_row_id = false;
34 var $acl_forum_id = false;
35 var $module_ary = array();
39 * Set module include path
41 public function __construct($include_path = false)
43 $this->include_path
= ($include_path !== false) ?
$include_path : PHPBB_ROOT_PATH
. 'modules/';
45 // Make sure the path ends with /
46 if (substr($this->include_path
, -1) !== '/')
48 $this->include_path
.= '/';
53 * Set custom include path for modules
54 * Schema for inclusion is include_path . modulebase
56 * @param string $include_path include path to be used.
59 public function set_custom_include_path($include_path)
61 $this->include_path
= $include_path;
63 // Make sure the path ends with /
64 if (substr($this->include_path
, -1) !== '/')
66 $this->include_path
.= '/';
73 * This creates a list, stored in $this->module_ary of all available
74 * modules for the given class (ucp, mcp and acp). Additionally
75 * $this->module_y_ary is created with indentation information for
76 * displaying the module list appropriately. Only modules for which
77 * the user has access rights are included in these lists.
79 function list_modules($p_class)
81 // Sanitise for future path use, it's escaped as appropriate for queries
82 $this->p_class
= str_replace(array('.', '/', '\\'), '', basename($p_class));
85 if (($this->module_cache
= phpbb
::$acm->get('modules_' . $this->p_class
)) === false)
89 FROM ' . MODULES_TABLE
. "
90 WHERE module_class = '" . phpbb
::$db->sql_escape($this->p_class
) . "'
91 ORDER BY left_id ASC";
92 $result = phpbb
::$db->sql_query($sql);
95 while ($row = phpbb
::$db->sql_fetchrow($result))
97 $rows[$row['module_id']] = $row;
99 phpbb
::$db->sql_freeresult($result);
101 $this->module_cache
= array();
102 foreach ($rows as $module_id => $row)
104 $this->module_cache
['modules'][] = $row;
105 $this->module_cache
['parents'][$row['module_id']] = $this->get_parents($row['parent_id'], $row['left_id'], $row['right_id'], $rows);
109 phpbb
::$acm->put('modules_' . $this->p_class
, $this->module_cache
);
112 if (empty($this->module_cache
))
114 $this->module_cache
= array('modules' => array(), 'parents' => array());
117 // We "could" build a true tree with this function - maybe mod authors want to use this...
118 // Functions for traversing and manipulating the tree are not available though
119 // We might re-structure the module system to use true trees in 3.2.x...
120 // $tree = $this->build_tree($this->module_cache['modules'], $this->module_cache['parents']);
122 // Clean up module cache array to only let survive modules the user can access
124 foreach ($this->module_cache
['modules'] as $key => $row)
126 // Not allowed to view module?
127 if (!$this->module_auth($row['module_auth']))
129 unset($this->module_cache
['modules'][$key]);
133 // Category with no members, ignore
134 if (!$row['module_basename'] && ($row['left_id'] +
1 == $row['right_id']))
136 unset($this->module_cache
['modules'][$key]);
141 if ($right_id !== false)
143 if ($row['left_id'] < $right_id)
145 unset($this->module_cache
['modules'][$key]);
153 if (!$row['module_enabled'])
155 // If category is disabled then disable every child too
156 unset($this->module_cache
['modules'][$key]);
157 $right_id = $row['right_id'];
162 // Re-index (this is needed, else we are not able to array_slice later)
163 $this->module_cache
['modules'] = array_merge($this->module_cache
['modules']);
165 // Include MOD _info files for populating language entries within the menus
166 $this->add_mod_info($this->p_class
);
168 // Now build the module array, but exclude completely empty categories...
172 foreach ($this->module_cache
['modules'] as $key => $row)
175 if ($right_id !== false)
177 if ($row['left_id'] < $right_id)
185 // Category with no members on their way down (we have to check every level)
186 if (!$row['module_basename'])
188 $empty_category = true;
190 // We go through the branch and look for an activated module
191 foreach (array_slice($this->module_cache
['modules'], $key +
1) as $temp_row)
193 if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
196 if ($temp_row['module_basename'] && $temp_row['module_enabled'])
198 $empty_category = false;
209 $right_id = $row['right_id'];
214 $depth = sizeof($this->module_cache
['parents'][$row['module_id']]);
216 // We need to prefix the functions to not create a naming conflict
218 // Function for building 'url_extra'
219 $url_func = '_module_' . $row['module_basename'] . '_url';
221 // Function for building the language name
222 $lang_func = '_module_' . $row['module_basename'] . '_lang';
224 // Custom function for calling parameters on module init (for example assigning template variables)
225 $custom_func = '_module_' . $row['module_basename'];
227 $names[$row['module_basename'] . '_' . $row['module_mode']][] = true;
232 'id' => (int) $row['module_id'],
233 'parent' => (int) $row['parent_id'],
234 'cat' => ($row['right_id'] > $row['left_id'] +
1) ?
true : false,
236 'is_duplicate' => ($row['module_basename'] && sizeof($names[$row['module_basename'] . '_' . $row['module_mode']]) > 1) ?
true : false,
238 'name' => (string) $row['module_basename'],
239 'mode' => (string) $row['module_mode'],
240 'display' => (int) $row['module_display'],
242 'url_extra' => (function_exists($url_func)) ?
$url_func($row['module_mode'], $row) : '',
244 'lang' => ($row['module_basename'] && function_exists($lang_func)) ?
$lang_func($row['module_mode'], $row['module_langname']) : phpbb
::$user->lang($row['module_langname']),
245 'langname' => $row['module_langname'],
247 'left' => $row['left_id'],
248 'right' => $row['right_id'],
251 if (function_exists($custom_func))
253 $custom_func($row['module_mode'], $module_row);
256 $this->module_ary
[] = $module_row;
259 unset($this->module_cache
['modules'], $names);
263 * Check if a certain main module is accessible/loaded
264 * By giving the module mode you are able to additionally check for only one mode within the main module
266 * @param string $module_basename The module base name, for example logs, reports, main (for the mcp).
267 * @param mixed $module_mode The module mode to check. If provided the mode will be checked in addition for presence.
269 * @return bool Returns true if module is loaded and accessible, else returns false
271 function loaded($module_basename, $module_mode = false)
273 if (empty($this->loaded_cache
))
275 $this->loaded_cache
= array();
277 foreach ($this->module_ary
as $row)
284 if (!isset($this->loaded_cache
[$row['name']]))
286 $this->loaded_cache
[$row['name']] = array();
294 $this->loaded_cache
[$row['name']][$row['mode']] = true;
298 if ($module_mode === false)
300 return (isset($this->loaded_cache
[$module_basename])) ?
true : false;
303 return (!empty($this->loaded_cache
[$module_basename][$module_mode])) ?
true : false;
307 * Check module authorisation
309 function module_auth($module_auth, $forum_id = false)
311 $module_auth = trim($module_auth);
313 // Generally allowed to access module if module_auth is empty
319 // With the code below we make sure only those elements get eval'd we really want to be checked
321 "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
322 \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' |
324 [^\s(),]+)/x', $module_auth, $match);
327 for ($i = 0, $size = sizeof($tokens); $i < $size; $i++
)
329 $token = &$tokens[$i];
341 if (!preg_match('#(?:acl_([a-z0-9_]+)(,\$id)?)|(?:\$id)|(?:aclf_([a-z0-9_]+))|(?:cfg_([a-z0-9_]+))|(?:request_([a-zA-Z0-9_]+))#', $token))
349 $module_auth = implode(' ', $tokens);
351 // Make sure $id seperation is working fine
352 $module_auth = str_replace(' , ', ',', $module_auth);
354 $forum_id = ($forum_id === false) ?
$this->acl_forum_id
: $forum_id;
357 eval('$is_auth = (int) (' . preg_replace(array('#acl_([a-z0-9_]+)(,\$id)?#', '#\$id#', '#aclf_([a-z0-9_]+)#', '#cfg_([a-z0-9_]+)#', '#request_([a-zA-Z0-9_]+)#'), array('(int) phpbb::$acl->acl_get(\'\\1\'\\2)', '(int) $forum_id', '(int) phpbb::$acl->acl_getf_global(\'\\1\')', '(int) phpbb::$config[\'\\1\']', 'phpbb_request::variable(\'\\1\', false)'), $module_auth) . ');');
365 function set_active($id = false, $mode = false)
368 $this->active_module
= false;
370 if (request_var('icat', ''))
373 $id = request_var('icat', '');
377 foreach ($this->module_ary
as $row_id => $item_ary)
379 // If this is a module and it's selected, active
380 // If this is a category and the module is the first within it, active
381 // If this is a module and no mode selected, select first mode
382 // If no category or module selected, go active for first module in first category
384 (($item_ary['name'] === $id ||
$item_ary['id'] === (int) $id) && (($item_ary['mode'] == $mode && !$item_ary['cat']) ||
($icat && $item_ary['cat']))) ||
385 ($item_ary['parent'] === $category && !$item_ary['cat'] && !$icat && $item_ary['display']) ||
386 (($item_ary['name'] === $id ||
$item_ary['id'] === (int) $id) && !$mode && !$item_ary['cat']) ||
387 (!$id && !$mode && !$item_ary['cat'] && $item_ary['display'])
390 if ($item_ary['cat'])
398 $this->p_id
= $item_ary['id'];
399 $this->p_parent
= $item_ary['parent'];
400 $this->p_name
= $item_ary['name'];
401 $this->p_mode
= $item_ary['mode'];
402 $this->p_left
= $item_ary['left'];
403 $this->p_right
= $item_ary['right'];
405 $this->module_cache
['parents'] = $this->module_cache
['parents'][$this->p_id
];
406 $this->active_module
= $item_ary['id'];
407 $this->active_module_row_id
= $row_id;
411 else if (($item_ary['cat'] && $item_ary['id'] === (int) $id) ||
($item_ary['parent'] === $category && $item_ary['cat']))
413 $category = $item_ary['id'];
419 * Loads currently active module
421 * This method loads a given module, passing it the relevant id and mode.
423 function load_active($mode = false, $module_url = false, $execute_module = true)
425 $module_path = $this->include_path
. $this->p_class
;
426 $icat = request_var('icat', '');
428 if ($this->active_module
=== false)
430 trigger_error('Module not accessible', E_USER_ERROR
);
433 if (!class_exists("{$this->p_class}_$this->p_name"))
435 if (!file_exists("$module_path/{$this->p_class}_$this->p_name." . PHP_EXT
))
437 trigger_error("Cannot find module $module_path/{$this->p_class}_$this->p_name." . PHP_EXT
, E_USER_ERROR
);
440 include("$module_path/{$this->p_class}_$this->p_name." . PHP_EXT
);
442 if (!class_exists("{$this->p_class}_$this->p_name"))
444 trigger_error("Module file $module_path/{$this->p_class}_$this->p_name." . PHP_EXT
. " does not contain correct class [{$this->p_class}_$this->p_name]", E_USER_ERROR
);
449 $this->p_mode
= $mode;
452 // Create a new instance of the desired module ... if it has a
453 // constructor it will of course be executed
454 $instance = "{$this->p_class}_$this->p_name";
456 $this->module
= new $instance($this);
458 // We pre-define the action parameter we are using all over the place
459 if (defined('IN_ADMIN'))
461 // Is first module automatically enabled a duplicate and the category not passed yet?
462 if (!$icat && $this->module_ary
[$this->active_module_row_id
]['is_duplicate'])
464 $icat = $this->module_ary
[$this->active_module_row_id
]['parent'];
467 // Not being able to overwrite ;)
468 $this->module
->u_action
= phpbb
::$url->append_sid(PHPBB_ADMIN_PATH
. 'index.' . PHP_EXT
, "i={$this->p_name}") . (($icat) ?
'&icat=' . $icat : '') . "&mode={$this->p_mode}";
472 // If user specified the module url we will use it...
473 if ($module_url !== false)
475 $this->module
->u_action
= $module_url;
479 $this->module
->u_action
= PHPBB_ROOT_PATH
. ((phpbb
::$user->page
['page_dir']) ? phpbb
::$user->page
['page_dir'] . '/' : '') . phpbb
::$user->page
['page_name'];
482 $this->module
->u_action
= phpbb
::$url->append_sid($this->module
->u_action
, "i={$this->p_name}") . (($icat) ?
'&icat=' . $icat : '') . "&mode={$this->p_mode}";
485 // Add url_extra parameter to u_action url
486 if (!empty($this->module_ary
) && $this->active_module
!== false && $this->module_ary
[$this->active_module_row_id
]['url_extra'])
488 $this->module
->u_action
.= $this->module_ary
[$this->active_module_row_id
]['url_extra'];
491 // Assign the module path for re-usage
492 $this->module
->module_path
= $module_path . '/';
494 // Execute the main method for the new instance, we send the module id and mode as parameters
495 // Users are able to call the main method after this function to be able to assign additional parameters manually
498 $this->module
->main($this->p_name
, $this->p_mode
);
506 * Appending url parameter to the currently active module.
508 * This function is called for adding specific url parameters while executing the current module.
509 * It is doing the same as the _module_{name}_url() function, apart from being able to be called after
510 * having dynamically parsed specific parameters. This allows more freedom in choosing additional parameters.
511 * One example can be seen in /includes/mcp/mcp_notes.php - $this->p_master->adjust_url() call.
513 * @param string $url_extra Extra url parameters, e.g.: &u=$user_id
516 function adjust_url($url_extra)
518 if (empty($this->module_ary
[$this->active_module_row_id
]))
523 $row = &$this->module_ary
[$this->active_module_row_id
];
525 // We check for the same url_extra in $row['url_extra'] to overcome doubled additions...
526 if (strpos($row['url_extra'], $url_extra) === false)
528 $row['url_extra'] .= $url_extra;
533 * Check if a module is active
535 function is_active($id, $mode = false)
537 // If we find a name by this id and being enabled we have our active one...
538 foreach ($this->module_ary
as $row_id => $item_ary)
540 if (($item_ary['name'] === $id ||
$item_ary['id'] === (int) $id) && $item_ary['display'])
542 if ($mode === false ||
$mode === $item_ary['mode'])
555 function get_parents($parent_id, $left_id, $right_id, &$all_parents)
561 foreach ($all_parents as $module_id => $row)
563 if ($row['left_id'] < $left_id && $row['right_id'] > $right_id)
565 $parents[$module_id] = $row['parent_id'];
568 if ($row['left_id'] > $left_id)
581 function get_branch($left_id, $right_id, $remaining)
585 foreach ($remaining as $key => $row)
587 if ($row['left_id'] > $left_id && $row['left_id'] < $right_id)
599 * Build true binary tree from given array
602 function build_tree(&$modules, &$parents)
606 foreach ($modules as $row)
610 if ($row['parent_id'])
612 // Go through the tree to find our branch
613 $parent_tree = $parents[$row['module_id']];
615 foreach ($parent_tree as $id => $value)
617 if (!isset($branch[$id]) && isset($branch['child']))
619 $branch = &$branch['child'];
621 $branch = &$branch[$id];
623 $branch = &$branch['child'];
626 $branch[$row['module_id']] = $row;
627 if (!isset($branch[$row['module_id']]['child']))
629 $branch[$row['module_id']]['child'] = array();
637 * Build navigation structure
639 function assign_tpl_vars($module_url)
641 $current_id = $right_id = false;
643 // Make sure the module_url has a question mark set, effectively determining the delimiter to use
644 $delim = (strpos($module_url, '?') === false) ?
'?' : '&';
646 $current_padding = $current_depth = 0;
647 $linear_offset = 'l_block1';
648 $tabular_offset = 't_block2';
650 // Generate the list of modules, we'll do this in two ways ...
651 // 1) In a linear fashion
652 // 2) In a combined tabbed + linear fashion ... tabs for the categories
653 // and a linear list for subcategories/items
654 foreach ($this->module_ary
as $row_id => $item_ary)
656 // Skip hidden modules
657 if (!$item_ary['display'])
663 if ($right_id !== false)
665 if ($item_ary['left'] < $right_id)
673 // Category with no members on their way down (we have to check every level)
674 if (!$item_ary['name'])
676 $empty_category = true;
678 // We go through the branch and look for an activated module
679 foreach (array_slice($this->module_ary
, $row_id +
1) as $temp_row)
681 if ($temp_row['left'] > $item_ary['left'] && $temp_row['left'] < $item_ary['right'])
683 // Module there and displayed?
684 if ($temp_row['name'] && $temp_row['display'])
686 $empty_category = false;
697 $right_id = $item_ary['right'];
702 // Select first id we can get
703 if (!$current_id && (isset($this->module_cache
['parents'][$item_ary['id']]) ||
$item_ary['id'] == $this->p_id
))
705 $current_id = $item_ary['id'];
708 $depth = $item_ary['depth'];
710 if ($depth > $current_depth)
712 $linear_offset = $linear_offset . '.l_block' . ($depth +
1);
713 $tabular_offset = ($depth +
1 > 2) ?
$tabular_offset . '.t_block' . ($depth +
1) : $tabular_offset;
715 else if ($depth < $current_depth)
717 for ($i = $current_depth - $depth; $i > 0; $i--)
719 $linear_offset = substr($linear_offset, 0, strrpos($linear_offset, '.'));
720 $tabular_offset = ($i +
$depth > 1) ?
substr($tabular_offset, 0, strrpos($tabular_offset, '.')) : $tabular_offset;
724 $u_title = $module_url . $delim . 'i=' . (($item_ary['cat']) ?
$item_ary['id'] : $item_ary['name'] . (($item_ary['is_duplicate']) ?
'&icat=' . $current_id : '') . '&mode=' . $item_ary['mode']);
726 // Was not allowed in categories before - /*!$item_ary['cat'] && */
727 $u_title .= (isset($item_ary['url_extra'])) ?
$item_ary['url_extra'] : '';
729 // Only output a categories items if it's currently selected
730 if (!$depth ||
($depth && (in_array($item_ary['parent'], array_values($this->module_cache
['parents'])) ||
$item_ary['parent'] == $this->p_parent
)))
732 $use_tabular_offset = (!$depth) ?
't_block1' : $tabular_offset;
735 'L_TITLE' => $item_ary['lang'],
736 'S_SELECTED' => (isset($this->module_cache
['parents'][$item_ary['id']]) ||
$item_ary['id'] == $this->p_id
) ?
true : false,
737 'U_TITLE' => $u_title
740 phpbb
::$template->assign_block_vars($use_tabular_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER
)));
744 'L_TITLE' => $item_ary['lang'],
745 'S_SELECTED' => (isset($this->module_cache
['parents'][$item_ary['id']]) ||
$item_ary['id'] == $this->p_id
) ?
true : false,
746 'U_TITLE' => $u_title
749 phpbb
::$template->assign_block_vars($linear_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER
)));
751 $current_depth = $depth;
756 * Returns desired template name
758 function get_tpl_name()
760 return $this->module
->tpl_name
. '.html';
764 * Returns the desired page title
766 function get_page_title()
768 if (!isset($this->module
->page_title
))
773 return phpbb
::$user->lang($this->module
->page_title
);
777 * Load module as the current active one without the need for registering it
779 function load($class, $name, $mode = false)
781 $this->p_class
= $class;
782 $this->p_name
= $name;
784 // Set active module to true instead of using the id
785 $this->active_module
= true;
787 $this->load_active($mode);
793 function display($page_title, $display_online_list = true)
795 page_header($page_title, $display_online_list);
797 phpbb
::$template->set_filenames(array(
798 'body' => $this->get_tpl_name())
805 * Toggle whether this module will be displayed or not
807 function set_display($id, $mode = false, $display = true)
809 foreach ($this->module_ary
as $row_id => $item_ary)
811 if (($item_ary['name'] === $id ||
$item_ary['id'] === (int) $id) && (!$mode ||
$item_ary['mode'] === $mode))
813 $this->module_ary
[$row_id]['display'] = (int) $display;
819 * Add custom MOD info language file
821 function add_mod_info($module_class)
823 if (file_exists(phpbb
::$user->lang_path
. phpbb
::$user->lang_name
. '/mods'))
825 $add_files = array();
827 $dir = @opendir
(phpbb
::$user->lang_path
. phpbb
::$user->lang_name
. '/mods');
831 while (($entry = readdir($dir)) !== false)
833 if (strpos($entry, 'info_' . strtolower($module_class) . '_') === 0 && substr(strrchr($entry, '.'), 1) == PHP_EXT
)
835 $add_files[] = 'mods/' . substr(basename($entry), 0, -(strlen(PHP_EXT
) +
1));
841 if (sizeof($add_files))
843 phpbb
::$user->add_lang($add_files);