includes/functions_module.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_module.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,883 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Class handling all types of 'plugins' (a future term)
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class p_master
    1.27 +{
    1.28 +	var $p_id;
    1.29 +	var $p_class;
    1.30 +	var $p_name;
    1.31 +	var $p_mode;
    1.32 +	var $p_parent;
    1.33 +
    1.34 +	var $include_path = false;
    1.35 +	var $active_module = false;
    1.36 +	var $active_module_row_id = false;
    1.37 +	var $acl_forum_id = false;
    1.38 +	var $module_ary = array();
    1.39 +
    1.40 +	/**
    1.41 +	* Constuctor
    1.42 +	* Set module include path
    1.43 +	*/
    1.44 +	function p_master($include_path = false)
    1.45 +	{
    1.46 +		global $phpbb_root_path;
    1.47 +
    1.48 +		$this->include_path = ($include_path !== false) ? $include_path : $phpbb_root_path . 'includes/';
    1.49 +
    1.50 +		// Make sure the path ends with /
    1.51 +		if (substr($this->include_path, -1) !== '/')
    1.52 +		{
    1.53 +			$this->include_path .= '/';
    1.54 +		}
    1.55 +	}
    1.56 +
    1.57 +	/**
    1.58 +	* Set custom include path for modules
    1.59 +	* Schema for inclusion is include_path . modulebase
    1.60 +	*
    1.61 +	* @param string $include_path include path to be used.
    1.62 +	* @access public
    1.63 +	*/
    1.64 +	function set_custom_include_path($include_path)
    1.65 +	{
    1.66 +		$this->include_path = $include_path;
    1.67 +
    1.68 +		// Make sure the path ends with /
    1.69 +		if (substr($this->include_path, -1) !== '/')
    1.70 +		{
    1.71 +			$this->include_path .= '/';
    1.72 +		}
    1.73 +	}
    1.74 +
    1.75 +	/**
    1.76 +	* List modules
    1.77 +	*
    1.78 +	* This creates a list, stored in $this->module_ary of all available
    1.79 +	* modules for the given class (ucp, mcp and acp). Additionally
    1.80 +	* $this->module_y_ary is created with indentation information for
    1.81 +	* displaying the module list appropriately. Only modules for which
    1.82 +	* the user has access rights are included in these lists.
    1.83 +	*/
    1.84 +	function list_modules($p_class)
    1.85 +	{
    1.86 +		global $auth, $db, $user, $cache;
    1.87 +		global $config, $phpbb_root_path, $phpEx;
    1.88 +
    1.89 +		// Sanitise for future path use, it's escaped as appropriate for queries
    1.90 +		$this->p_class = str_replace(array('.', '/', '\\'), '', basename($p_class));
    1.91 +
    1.92 +		// Get cached modules
    1.93 +		if (($this->module_cache = $cache->get('_modules_' . $this->p_class)) === false)
    1.94 +		{
    1.95 +			// Get modules
    1.96 +			$sql = 'SELECT *
    1.97 +				FROM ' . MODULES_TABLE . "
    1.98 +				WHERE module_class = '" . $db->sql_escape($this->p_class) . "'
    1.99 +				ORDER BY left_id ASC";
   1.100 +			$result = $db->sql_query($sql);
   1.101 +
   1.102 +			$rows = array();
   1.103 +			while ($row = $db->sql_fetchrow($result))
   1.104 +			{
   1.105 +				$rows[$row['module_id']] = $row;
   1.106 +			}
   1.107 +			$db->sql_freeresult($result);
   1.108 +
   1.109 +			$this->module_cache = array();
   1.110 +			foreach ($rows as $module_id => $row)
   1.111 +			{
   1.112 +				$this->module_cache['modules'][] = $row;
   1.113 +				$this->module_cache['parents'][$row['module_id']] = $this->get_parents($row['parent_id'], $row['left_id'], $row['right_id'], $rows);
   1.114 +			}
   1.115 +			unset($rows);
   1.116 +
   1.117 +			$cache->put('_modules_' . $this->p_class, $this->module_cache);
   1.118 +		}
   1.119 +
   1.120 +		if (empty($this->module_cache))
   1.121 +		{
   1.122 +			$this->module_cache = array('modules' => array(), 'parents' => array());
   1.123 +		}
   1.124 +
   1.125 +		// We "could" build a true tree with this function - maybe mod authors want to use this...
   1.126 +		// Functions for traversing and manipulating the tree are not available though
   1.127 +		// We might re-structure the module system to use true trees in 3.2.x...
   1.128 +		// $tree = $this->build_tree($this->module_cache['modules'], $this->module_cache['parents']);
   1.129 +
   1.130 +		// Clean up module cache array to only let survive modules the user can access
   1.131 +		$right_id = false;
   1.132 +		foreach ($this->module_cache['modules'] as $key => $row)
   1.133 +		{
   1.134 +			// Not allowed to view module?
   1.135 +			if (!$this->module_auth($row['module_auth']))
   1.136 +			{
   1.137 +				unset($this->module_cache['modules'][$key]);
   1.138 +				continue;
   1.139 +			}
   1.140 +
   1.141 +			// Category with no members, ignore
   1.142 +			if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']))
   1.143 +			{
   1.144 +				unset($this->module_cache['modules'][$key]);
   1.145 +				continue;
   1.146 +			}
   1.147 +
   1.148 +			// Skip branch
   1.149 +			if ($right_id !== false)
   1.150 +			{
   1.151 +				if ($row['left_id'] < $right_id)
   1.152 +				{
   1.153 +					unset($this->module_cache['modules'][$key]);
   1.154 +					continue;
   1.155 +				}
   1.156 +
   1.157 +				$right_id = false;
   1.158 +			}
   1.159 +
   1.160 +			// Not enabled?
   1.161 +			if (!$row['module_enabled'])
   1.162 +			{
   1.163 +				// If category is disabled then disable every child too
   1.164 +				unset($this->module_cache['modules'][$key]);
   1.165 +				$right_id = $row['right_id'];
   1.166 +				continue;
   1.167 +			}
   1.168 +		}
   1.169 +
   1.170 +		// Re-index (this is needed, else we are not able to array_slice later)
   1.171 +		$this->module_cache['modules'] = array_merge($this->module_cache['modules']);
   1.172 +
   1.173 +		// Include MOD _info files for populating language entries within the menus
   1.174 +		$this->add_mod_info($this->p_class);
   1.175 +
   1.176 +		// Now build the module array, but exclude completely empty categories...
   1.177 +		$right_id = false;
   1.178 +		$names = array();
   1.179 +
   1.180 +		foreach ($this->module_cache['modules'] as $key => $row)
   1.181 +		{
   1.182 +			// Skip branch
   1.183 +			if ($right_id !== false)
   1.184 +			{
   1.185 +				if ($row['left_id'] < $right_id)
   1.186 +				{
   1.187 +					continue;
   1.188 +				}
   1.189 +
   1.190 +				$right_id = false;
   1.191 +			}
   1.192 +
   1.193 +			// Category with no members on their way down (we have to check every level)
   1.194 +			if (!$row['module_basename'])
   1.195 +			{
   1.196 +				$empty_category = true;
   1.197 +
   1.198 +				// We go through the branch and look for an activated module
   1.199 +				foreach (array_slice($this->module_cache['modules'], $key + 1) as $temp_row)
   1.200 +				{
   1.201 +					if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
   1.202 +					{
   1.203 +						// Module there
   1.204 +						if ($temp_row['module_basename'] && $temp_row['module_enabled'])
   1.205 +						{
   1.206 +							$empty_category = false;
   1.207 +							break;
   1.208 +						}
   1.209 +						continue;
   1.210 +					}
   1.211 +					break;
   1.212 +				}
   1.213 +
   1.214 +				// Skip the branch
   1.215 +				if ($empty_category)
   1.216 +				{
   1.217 +					$right_id = $row['right_id'];
   1.218 +					continue;
   1.219 +				}
   1.220 +			}
   1.221 +
   1.222 +			$depth = sizeof($this->module_cache['parents'][$row['module_id']]);
   1.223 +
   1.224 +			// We need to prefix the functions to not create a naming conflict
   1.225 +
   1.226 +			// Function for building 'url_extra'
   1.227 +			$url_func = '_module_' . $row['module_basename'] . '_url';
   1.228 +
   1.229 +			// Function for building the language name
   1.230 +			$lang_func = '_module_' . $row['module_basename'] . '_lang';
   1.231 +
   1.232 +			// Custom function for calling parameters on module init (for example assigning template variables)
   1.233 +			$custom_func = '_module_' . $row['module_basename'];
   1.234 +
   1.235 +			$names[$row['module_basename'] . '_' . $row['module_mode']][] = true;
   1.236 +
   1.237 +			$module_row = array(
   1.238 +				'depth'		=> $depth,
   1.239 +
   1.240 +				'id'		=> (int) $row['module_id'],
   1.241 +				'parent'	=> (int) $row['parent_id'],
   1.242 +				'cat'		=> ($row['right_id'] > $row['left_id'] + 1) ? true : false,
   1.243 +
   1.244 +				'is_duplicate'	=> ($row['module_basename'] && sizeof($names[$row['module_basename'] . '_' . $row['module_mode']]) > 1) ? true : false,
   1.245 +
   1.246 +				'name'		=> (string) $row['module_basename'],
   1.247 +				'mode'		=> (string) $row['module_mode'],
   1.248 +				'display'	=> (int) $row['module_display'],
   1.249 +
   1.250 +				'url_extra'	=> (function_exists($url_func)) ? $url_func($row['module_mode'], $row) : '',
   1.251 +
   1.252 +				'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']),
   1.253 +				'langname'	=> $row['module_langname'],
   1.254 +
   1.255 +				'left'		=> $row['left_id'],
   1.256 +				'right'		=> $row['right_id'],
   1.257 +			);
   1.258 +
   1.259 +			if (function_exists($custom_func))
   1.260 +			{
   1.261 +				$custom_func($row['module_mode'], $module_row);
   1.262 +			}
   1.263 +
   1.264 +			$this->module_ary[] = $module_row;
   1.265 +		}
   1.266 +
   1.267 +		unset($this->module_cache['modules'], $names);
   1.268 +	}
   1.269 +
   1.270 +	/**
   1.271 +	* Check if a certain main module is accessible/loaded
   1.272 +	* By giving the module mode you are able to additionally check for only one mode within the main module
   1.273 +	*
   1.274 +	* @param string $module_basename The module base name, for example logs, reports, main (for the mcp).
   1.275 +	* @param mixed $module_mode The module mode to check. If provided the mode will be checked in addition for presence.
   1.276 +	*
   1.277 +	* @return bool Returns true if module is loaded and accessible, else returns false
   1.278 +	*/
   1.279 +	function loaded($module_basename, $module_mode = false)
   1.280 +	{
   1.281 +		if (empty($this->loaded_cache))
   1.282 +		{
   1.283 +			$this->loaded_cache = array();
   1.284 +
   1.285 +			foreach ($this->module_ary as $row)
   1.286 +			{
   1.287 +				if (!$row['name'])
   1.288 +				{
   1.289 +					continue;
   1.290 +				}
   1.291 +
   1.292 +				if (!isset($this->loaded_cache[$row['name']]))
   1.293 +				{
   1.294 +					$this->loaded_cache[$row['name']] = array();
   1.295 +				}
   1.296 +
   1.297 +				if (!$row['mode'])
   1.298 +				{
   1.299 +					continue;
   1.300 +				}
   1.301 +
   1.302 +				$this->loaded_cache[$row['name']][$row['mode']] = true;
   1.303 +			}
   1.304 +		}
   1.305 +
   1.306 +		if ($module_mode === false)
   1.307 +		{
   1.308 +			return (isset($this->loaded_cache[$module_basename])) ? true : false;
   1.309 +		}
   1.310 +
   1.311 +		return (!empty($this->loaded_cache[$module_basename][$module_mode])) ? true : false;
   1.312 +	}
   1.313 +
   1.314 +	/**
   1.315 +	* Check module authorisation
   1.316 +	*/
   1.317 +	function module_auth($module_auth, $forum_id = false)
   1.318 +	{
   1.319 +		global $auth, $config;
   1.320 +
   1.321 +		$module_auth = trim($module_auth);
   1.322 +
   1.323 +		// Generally allowed to access module if module_auth is empty
   1.324 +		if (!$module_auth)
   1.325 +		{
   1.326 +			return true;
   1.327 +		}
   1.328 +
   1.329 +		// With the code below we make sure only those elements get eval'd we really want to be checked
   1.330 +		preg_match_all('/(?:
   1.331 +			"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
   1.332 +			\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
   1.333 +			[(),]                                  |
   1.334 +			[^\s(),]+)/x', $module_auth, $match);
   1.335 +
   1.336 +		$tokens = $match[0];
   1.337 +		for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
   1.338 +		{
   1.339 +			$token = &$tokens[$i];
   1.340 +
   1.341 +			switch ($token)
   1.342 +			{
   1.343 +				case ')':
   1.344 +				case '(':
   1.345 +				case '&&':
   1.346 +				case '||':
   1.347 +				case ',':
   1.348 +				break;
   1.349 +
   1.350 +				default:
   1.351 +					if (!preg_match('#(?:acl_([a-z0-9_]+)(,\$id)?)|(?:\$id)|(?:aclf_([a-z0-9_]+))|(?:cfg_([a-z0-9_]+))|(?:request_([a-zA-Z0-9_]+))#', $token))
   1.352 +					{
   1.353 +						$token = '';
   1.354 +					}
   1.355 +				break;
   1.356 +			}
   1.357 +		}
   1.358 +
   1.359 +		$module_auth = implode(' ', $tokens);
   1.360 +
   1.361 +		// Make sure $id seperation is working fine
   1.362 +		$module_auth = str_replace(' , ', ',', $module_auth);
   1.363 +
   1.364 +		$forum_id = ($forum_id === false) ? $this->acl_forum_id : $forum_id;
   1.365 +
   1.366 +		$is_auth = false;
   1.367 +		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) $auth->acl_get(\'\\1\'\\2)', '(int) $forum_id', '(int) $auth->acl_getf_global(\'\\1\')', '(int) $config[\'\\1\']', '!empty($_REQUEST[\'\\1\'])'), $module_auth) . ');');
   1.368 +
   1.369 +		return $is_auth;
   1.370 +	}
   1.371 +
   1.372 +	/**
   1.373 +	* Set active module
   1.374 +	*/
   1.375 +	function set_active($id = false, $mode = false)
   1.376 +	{
   1.377 +		$icat = false;
   1.378 +		$this->active_module = false;
   1.379 +
   1.380 +		if (request_var('icat', ''))
   1.381 +		{
   1.382 +			$icat = $id;
   1.383 +			$id = request_var('icat', '');
   1.384 +		}
   1.385 +
   1.386 +		$category = false;
   1.387 +		foreach ($this->module_ary as $row_id => $item_ary)
   1.388 +		{
   1.389 +			// If this is a module and it's selected, active
   1.390 +			// If this is a category and the module is the first within it, active
   1.391 +			// If this is a module and no mode selected, select first mode
   1.392 +			// If no category or module selected, go active for first module in first category
   1.393 +			if (
   1.394 +				(($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && (($item_ary['mode'] == $mode && !$item_ary['cat']) || ($icat && $item_ary['cat']))) ||
   1.395 +				($item_ary['parent'] === $category && !$item_ary['cat'] && !$icat && $item_ary['display']) ||
   1.396 +				(($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && !$mode && !$item_ary['cat']) ||
   1.397 +				(!$id && !$mode && !$item_ary['cat'] && $item_ary['display'])
   1.398 +				)
   1.399 +			{
   1.400 +				if ($item_ary['cat'])
   1.401 +				{
   1.402 +					$id = $icat;
   1.403 +					$icat = false;
   1.404 +
   1.405 +					continue;
   1.406 +				}
   1.407 +
   1.408 +				$this->p_id		= $item_ary['id'];
   1.409 +				$this->p_parent	= $item_ary['parent'];
   1.410 +				$this->p_name	= $item_ary['name'];
   1.411 +				$this->p_mode 	= $item_ary['mode'];
   1.412 +				$this->p_left	= $item_ary['left'];
   1.413 +				$this->p_right	= $item_ary['right'];
   1.414 +
   1.415 +				$this->module_cache['parents'] = $this->module_cache['parents'][$this->p_id];
   1.416 +				$this->active_module = $item_ary['id'];
   1.417 +				$this->active_module_row_id = $row_id;
   1.418 +
   1.419 +				break;
   1.420 +			}
   1.421 +			else if (($item_ary['cat'] && $item_ary['id'] === (int) $id) || ($item_ary['parent'] === $category && $item_ary['cat']))
   1.422 +			{
   1.423 +				$category = $item_ary['id'];
   1.424 +			}
   1.425 +		}
   1.426 +	}
   1.427 +
   1.428 +	/**
   1.429 +	* Loads currently active module
   1.430 +	*
   1.431 +	* This method loads a given module, passing it the relevant id and mode.
   1.432 +	*/
   1.433 +	function load_active($mode = false, $module_url = false, $execute_module = true)
   1.434 +	{
   1.435 +		global $phpbb_root_path, $phpbb_admin_path, $phpEx, $user;
   1.436 +
   1.437 +		$module_path = $this->include_path . $this->p_class;
   1.438 +		$icat = request_var('icat', '');
   1.439 +
   1.440 +		if ($this->active_module === false)
   1.441 +		{
   1.442 +			trigger_error('Module not accessible', E_USER_ERROR);
   1.443 +		}
   1.444 +
   1.445 +		if (!class_exists("{$this->p_class}_$this->p_name"))
   1.446 +		{
   1.447 +			if (!file_exists("$module_path/{$this->p_class}_$this->p_name.$phpEx"))
   1.448 +			{
   1.449 +				trigger_error("Cannot find module $module_path/{$this->p_class}_$this->p_name.$phpEx", E_USER_ERROR);
   1.450 +			}
   1.451 +
   1.452 +			include("$module_path/{$this->p_class}_$this->p_name.$phpEx");
   1.453 +
   1.454 +			if (!class_exists("{$this->p_class}_$this->p_name"))
   1.455 +			{
   1.456 +				trigger_error("Module file $module_path/{$this->p_class}_$this->p_name.$phpEx does not contain correct class [{$this->p_class}_$this->p_name]", E_USER_ERROR);
   1.457 +			}
   1.458 +
   1.459 +			if (!empty($mode))
   1.460 +			{
   1.461 +				$this->p_mode = $mode;
   1.462 +			}
   1.463 +
   1.464 +			// Create a new instance of the desired module ... if it has a
   1.465 +			// constructor it will of course be executed
   1.466 +			$instance = "{$this->p_class}_$this->p_name";
   1.467 +
   1.468 +			$this->module = new $instance($this);
   1.469 +
   1.470 +			// We pre-define the action parameter we are using all over the place
   1.471 +			if (defined('IN_ADMIN'))
   1.472 +			{
   1.473 +				// Is first module automatically enabled a duplicate and the category not passed yet?
   1.474 +				if (!$icat && $this->module_ary[$this->active_module_row_id]['is_duplicate'])
   1.475 +				{
   1.476 +					$icat = $this->module_ary[$this->active_module_row_id]['parent'];
   1.477 +				}
   1.478 +
   1.479 +				// Not being able to overwrite ;)
   1.480 +				$this->module->u_action = append_sid("{$phpbb_admin_path}index.$phpEx", "i={$this->p_name}") . (($icat) ? '&amp;icat=' . $icat : '') . "&amp;mode={$this->p_mode}";
   1.481 +			}
   1.482 +			else
   1.483 +			{
   1.484 +				// If user specified the module url we will use it...
   1.485 +				if ($module_url !== false)
   1.486 +				{
   1.487 +					$this->module->u_action = $module_url;
   1.488 +				}
   1.489 +				else
   1.490 +				{
   1.491 +					$this->module->u_action = $phpbb_root_path . (($user->page['page_dir']) ? $user->page['page_dir'] . '/' : '') . $user->page['page_name'];
   1.492 +				}
   1.493 +
   1.494 +				$this->module->u_action = append_sid($this->module->u_action, "i={$this->p_name}") . (($icat) ? '&amp;icat=' . $icat : '') . "&amp;mode={$this->p_mode}";
   1.495 +			}
   1.496 +
   1.497 +			// Add url_extra parameter to u_action url
   1.498 +			if (!empty($this->module_ary) && $this->active_module !== false && $this->module_ary[$this->active_module_row_id]['url_extra'])
   1.499 +			{
   1.500 +				$this->module->u_action .= $this->module_ary[$this->active_module_row_id]['url_extra'];
   1.501 +			}
   1.502 +
   1.503 +			// Assign the module path for re-usage
   1.504 +			$this->module->module_path = $module_path . '/';
   1.505 +
   1.506 +			// Execute the main method for the new instance, we send the module id and mode as parameters
   1.507 +			// Users are able to call the main method after this function to be able to assign additional parameters manually
   1.508 +			if ($execute_module)
   1.509 +			{
   1.510 +				$this->module->main($this->p_name, $this->p_mode);
   1.511 +			}
   1.512 +
   1.513 +			return;
   1.514 +		}
   1.515 +	}
   1.516 +
   1.517 +	/**
   1.518 +	* Appending url parameter to the currently active module.
   1.519 +	*
   1.520 +	* This function is called for adding specific url parameters while executing the current module.
   1.521 +	* It is doing the same as the _module_{name}_url() function, apart from being able to be called after
   1.522 +	* having dynamically parsed specific parameters. This allows more freedom in choosing additional parameters.
   1.523 +	* One example can be seen in /includes/mcp/mcp_notes.php - $this->p_master->adjust_url() call.
   1.524 +	*
   1.525 +	* @param string $url_extra Extra url parameters, e.g.: &amp;u=$user_id
   1.526 +	*
   1.527 +	*/
   1.528 +	function adjust_url($url_extra)
   1.529 +	{
   1.530 +		if (empty($this->module_ary[$this->active_module_row_id]))
   1.531 +		{
   1.532 +			return;
   1.533 +		}
   1.534 +
   1.535 +		$row = &$this->module_ary[$this->active_module_row_id];
   1.536 +
   1.537 +		// We check for the same url_extra in $row['url_extra'] to overcome doubled additions...
   1.538 +		if (strpos($row['url_extra'], $url_extra) === false)
   1.539 +		{
   1.540 +			$row['url_extra'] .= $url_extra;
   1.541 +		}
   1.542 +	}
   1.543 +
   1.544 +	/**
   1.545 +	* Check if a module is active
   1.546 +	*/
   1.547 +	function is_active($id, $mode = false)
   1.548 +	{
   1.549 +		// If we find a name by this id and being enabled we have our active one...
   1.550 +		foreach ($this->module_ary as $row_id => $item_ary)
   1.551 +		{
   1.552 +			if (($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && $item_ary['display'])
   1.553 +			{
   1.554 +				if ($mode === false || $mode === $item_ary['mode'])
   1.555 +				{
   1.556 +					return true;
   1.557 +				}
   1.558 +			}
   1.559 +		}
   1.560 +
   1.561 +		return false;
   1.562 +	}
   1.563 +
   1.564 +	/**
   1.565 +	* Get parents
   1.566 +	*/
   1.567 +	function get_parents($parent_id, $left_id, $right_id, &$all_parents)
   1.568 +	{
   1.569 +		global $db;
   1.570 +
   1.571 +		$parents = array();
   1.572 +
   1.573 +		if ($parent_id > 0)
   1.574 +		{
   1.575 +			foreach ($all_parents as $module_id => $row)
   1.576 +			{
   1.577 +				if ($row['left_id'] < $left_id && $row['right_id'] > $right_id)
   1.578 +				{
   1.579 +					$parents[$module_id] = $row['parent_id'];
   1.580 +				}
   1.581 +
   1.582 +				if ($row['left_id'] > $left_id)
   1.583 +				{
   1.584 +					break;
   1.585 +				}
   1.586 +			}
   1.587 +		}
   1.588 +
   1.589 +		return $parents;
   1.590 +	}
   1.591 +
   1.592 +	/**
   1.593 +	* Get tree branch
   1.594 +	*/
   1.595 +	function get_branch($left_id, $right_id, $remaining)
   1.596 +	{
   1.597 +		$branch = array();
   1.598 +
   1.599 +		foreach ($remaining as $key => $row)
   1.600 +		{
   1.601 +			if ($row['left_id'] > $left_id && $row['left_id'] < $right_id)
   1.602 +			{
   1.603 +				$branch[] = $row;
   1.604 +				continue;
   1.605 +			}
   1.606 +			break;
   1.607 +		}
   1.608 +
   1.609 +		return $branch;
   1.610 +	}
   1.611 +
   1.612 +	/**
   1.613 +	* Build true binary tree from given array
   1.614 +	* Not in use
   1.615 +	*/
   1.616 +	function build_tree(&$modules, &$parents)
   1.617 +	{
   1.618 +		$tree = array();
   1.619 +
   1.620 +		foreach ($modules as $row)
   1.621 +		{
   1.622 +			$branch = &$tree;
   1.623 +
   1.624 +			if ($row['parent_id'])
   1.625 +			{
   1.626 +				// Go through the tree to find our branch
   1.627 +				$parent_tree = $parents[$row['module_id']];
   1.628 +
   1.629 +				foreach ($parent_tree as $id => $value)
   1.630 +				{
   1.631 +					if (!isset($branch[$id]) && isset($branch['child']))
   1.632 +					{
   1.633 +						$branch = &$branch['child'];
   1.634 +					}
   1.635 +					$branch = &$branch[$id];
   1.636 +				}
   1.637 +				$branch = &$branch['child'];
   1.638 +			}
   1.639 +
   1.640 +			$branch[$row['module_id']] = $row;
   1.641 +			if (!isset($branch[$row['module_id']]['child']))
   1.642 +			{
   1.643 +				$branch[$row['module_id']]['child'] = array();
   1.644 +			}
   1.645 +		}
   1.646 +
   1.647 +		return $tree;
   1.648 +	}
   1.649 +
   1.650 +	/**
   1.651 +	* Build navigation structure
   1.652 +	*/
   1.653 +	function assign_tpl_vars($module_url)
   1.654 +	{
   1.655 +		global $template;
   1.656 +
   1.657 +		$current_id = $right_id = false;
   1.658 +
   1.659 +		// Make sure the module_url has a question mark set, effectively determining the delimiter to use
   1.660 +		$delim = (strpos($module_url, '?') === false) ? '?' : '&amp;';
   1.661 +
   1.662 +		$current_padding = $current_depth = 0;
   1.663 +		$linear_offset 	= 'l_block1';
   1.664 +		$tabular_offset = 't_block2';
   1.665 +
   1.666 +		// Generate the list of modules, we'll do this in two ways ...
   1.667 +		// 1) In a linear fashion
   1.668 +		// 2) In a combined tabbed + linear fashion ... tabs for the categories
   1.669 +		//    and a linear list for subcategories/items
   1.670 +		foreach ($this->module_ary as $row_id => $item_ary)
   1.671 +		{
   1.672 +			// Skip hidden modules
   1.673 +			if (!$item_ary['display'])
   1.674 +			{
   1.675 +				continue;
   1.676 +			}
   1.677 +
   1.678 +			// Skip branch
   1.679 +			if ($right_id !== false)
   1.680 +			{
   1.681 +				if ($item_ary['left'] < $right_id)
   1.682 +				{
   1.683 +					continue;
   1.684 +				}
   1.685 +
   1.686 +				$right_id = false;
   1.687 +			}
   1.688 +
   1.689 +			// Category with no members on their way down (we have to check every level)
   1.690 +			if (!$item_ary['name'])
   1.691 +			{
   1.692 +				$empty_category = true;
   1.693 +
   1.694 +				// We go through the branch and look for an activated module
   1.695 +				foreach (array_slice($this->module_ary, $row_id + 1) as $temp_row)
   1.696 +				{
   1.697 +					if ($temp_row['left'] > $item_ary['left'] && $temp_row['left'] < $item_ary['right'])
   1.698 +					{
   1.699 +						// Module there and displayed?
   1.700 +						if ($temp_row['name'] && $temp_row['display'])
   1.701 +						{
   1.702 +							$empty_category = false;
   1.703 +							break;
   1.704 +						}
   1.705 +						continue;
   1.706 +					}
   1.707 +					break;
   1.708 +				}
   1.709 +
   1.710 +				// Skip the branch
   1.711 +				if ($empty_category)
   1.712 +				{
   1.713 +					$right_id = $item_ary['right'];
   1.714 +					continue;
   1.715 +				}
   1.716 +			}
   1.717 +
   1.718 +			// Select first id we can get
   1.719 +			if (!$current_id && (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id))
   1.720 +			{
   1.721 +				$current_id = $item_ary['id'];
   1.722 +			}
   1.723 +
   1.724 +			$depth = $item_ary['depth'];
   1.725 +
   1.726 +			if ($depth > $current_depth)
   1.727 +			{
   1.728 +				$linear_offset = $linear_offset . '.l_block' . ($depth + 1);
   1.729 +				$tabular_offset = ($depth + 1 > 2) ? $tabular_offset . '.t_block' . ($depth + 1) : $tabular_offset;
   1.730 +			}
   1.731 +			else if ($depth < $current_depth)
   1.732 +			{
   1.733 +				for ($i = $current_depth - $depth; $i > 0; $i--)
   1.734 +				{
   1.735 +					$linear_offset = substr($linear_offset, 0, strrpos($linear_offset, '.'));
   1.736 +					$tabular_offset = ($i + $depth > 1) ? substr($tabular_offset, 0, strrpos($tabular_offset, '.')) : $tabular_offset;
   1.737 +				}
   1.738 +			}
   1.739 +
   1.740 +			$u_title = $module_url . $delim . 'i=' . (($item_ary['cat']) ? $item_ary['id'] : $item_ary['name'] . (($item_ary['is_duplicate']) ? '&amp;icat=' . $current_id : '') . '&amp;mode=' . $item_ary['mode']);
   1.741 +
   1.742 +			// Was not allowed in categories before - /*!$item_ary['cat'] && */
   1.743 +			$u_title .= (isset($item_ary['url_extra'])) ? $item_ary['url_extra'] : '';
   1.744 +
   1.745 +			// Only output a categories items if it's currently selected
   1.746 +			if (!$depth || ($depth && (in_array($item_ary['parent'], array_values($this->module_cache['parents'])) || $item_ary['parent'] == $this->p_parent)))
   1.747 +			{
   1.748 +				$use_tabular_offset = (!$depth) ? 't_block1' : $tabular_offset;
   1.749 +
   1.750 +				$tpl_ary = array(
   1.751 +					'L_TITLE'		=> $item_ary['lang'],
   1.752 +					'S_SELECTED'	=> (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id) ? true : false,
   1.753 +					'U_TITLE'		=> $u_title
   1.754 +				);
   1.755 +
   1.756 +				$template->assign_block_vars($use_tabular_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
   1.757 +			}
   1.758 +
   1.759 +			$tpl_ary = array(
   1.760 +				'L_TITLE'		=> $item_ary['lang'],
   1.761 +				'S_SELECTED'	=> (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id) ? true : false,
   1.762 +				'U_TITLE'		=> $u_title
   1.763 +			);
   1.764 +
   1.765 +			$template->assign_block_vars($linear_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
   1.766 +
   1.767 +			$current_depth = $depth;
   1.768 +		}
   1.769 +	}
   1.770 +
   1.771 +	/**
   1.772 +	* Returns desired template name
   1.773 +	*/
   1.774 +	function get_tpl_name()
   1.775 +	{
   1.776 +		return $this->module->tpl_name . '.html';
   1.777 +	}
   1.778 +
   1.779 +	/**
   1.780 +	* Returns the desired page title
   1.781 +	*/
   1.782 +	function get_page_title()
   1.783 +	{
   1.784 +		global $user;
   1.785 +
   1.786 +		if (!isset($this->module->page_title))
   1.787 +		{
   1.788 +			return '';
   1.789 +		}
   1.790 +
   1.791 +		return (isset($user->lang[$this->module->page_title])) ? $user->lang[$this->module->page_title] : $this->module->page_title;
   1.792 +	}
   1.793 +
   1.794 +	/**
   1.795 +	* Load module as the current active one without the need for registering it
   1.796 +	*/
   1.797 +	function load($class, $name, $mode = false)
   1.798 +	{
   1.799 +		$this->p_class = $class;
   1.800 +		$this->p_name = $name;
   1.801 +
   1.802 +		// Set active module to true instead of using the id
   1.803 +		$this->active_module = true;
   1.804 +
   1.805 +		$this->load_active($mode);
   1.806 +	}
   1.807 +
   1.808 +	/**
   1.809 +	* Display module
   1.810 +	*/
   1.811 +	function display($page_title, $display_online_list = true)
   1.812 +	{
   1.813 +		global $template, $user;
   1.814 +
   1.815 +		// Generate the page
   1.816 +		if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
   1.817 +		{
   1.818 +			adm_page_header($page_title);
   1.819 +		}
   1.820 +		else
   1.821 +		{
   1.822 +			page_header($page_title, $display_online_list);
   1.823 +		}
   1.824 +
   1.825 +		$template->set_filenames(array(
   1.826 +			'body' => $this->get_tpl_name())
   1.827 +		);
   1.828 +
   1.829 +		if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
   1.830 +		{
   1.831 +			adm_page_footer();
   1.832 +		}
   1.833 +		else
   1.834 +		{
   1.835 +			page_footer();
   1.836 +		}
   1.837 +	}
   1.838 +
   1.839 +	/**
   1.840 +	* Toggle whether this module will be displayed or not
   1.841 +	*/
   1.842 +	function set_display($id, $mode = false, $display = true)
   1.843 +	{
   1.844 +		foreach ($this->module_ary as $row_id => $item_ary)
   1.845 +		{
   1.846 +			if (($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && (!$mode || $item_ary['mode'] === $mode))
   1.847 +			{
   1.848 +				$this->module_ary[$row_id]['display'] = (int) $display;
   1.849 +			}
   1.850 +		}
   1.851 +	}
   1.852 +
   1.853 +	/**
   1.854 +	* Add custom MOD info language file
   1.855 +	*/
   1.856 +	function add_mod_info($module_class)
   1.857 +	{
   1.858 +		global $user, $phpEx;
   1.859 +
   1.860 +		if (file_exists($user->lang_path . $user->lang_name . '/mods'))
   1.861 +		{
   1.862 +			$add_files = array();
   1.863 +
   1.864 +			$dir = @opendir($user->lang_path . $user->lang_name . '/mods');
   1.865 +
   1.866 +			if ($dir)
   1.867 +			{
   1.868 +				while (($entry = readdir($dir)) !== false)
   1.869 +				{
   1.870 +					if (strpos($entry, 'info_' . strtolower($module_class) . '_') === 0 && substr(strrchr($entry, '.'), 1) == $phpEx)
   1.871 +					{
   1.872 +						$add_files[] = 'mods/' . substr(basename($entry), 0, -(strlen($phpEx) + 1));
   1.873 +					}
   1.874 +				}
   1.875 +				closedir($dir);
   1.876 +			}
   1.877 +
   1.878 +			if (sizeof($add_files))
   1.879 +			{
   1.880 +				$user->add_lang($add_files);
   1.881 +			}
   1.882 +		}
   1.883 +	}
   1.884 +}
   1.885 +
   1.886 +?>
   1.887 \ No newline at end of file