includes/functions_module.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 02:49:04 +0400
changeset 19 6d3ca1857104
parent 0 ceef6d64c21d
permissions -rw-r--r--
RU translate fix
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Class handling all types of 'plugins' (a future term)
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class p_master
indvdum@0
    24
{
indvdum@0
    25
	var $p_id;
indvdum@0
    26
	var $p_class;
indvdum@0
    27
	var $p_name;
indvdum@0
    28
	var $p_mode;
indvdum@0
    29
	var $p_parent;
indvdum@0
    30
indvdum@0
    31
	var $include_path = false;
indvdum@0
    32
	var $active_module = false;
indvdum@0
    33
	var $active_module_row_id = false;
indvdum@0
    34
	var $acl_forum_id = false;
indvdum@0
    35
	var $module_ary = array();
indvdum@0
    36
indvdum@0
    37
	/**
indvdum@0
    38
	* Constuctor
indvdum@0
    39
	* Set module include path
indvdum@0
    40
	*/
indvdum@0
    41
	function p_master($include_path = false)
indvdum@0
    42
	{
indvdum@0
    43
		global $phpbb_root_path;
indvdum@0
    44
indvdum@0
    45
		$this->include_path = ($include_path !== false) ? $include_path : $phpbb_root_path . 'includes/';
indvdum@0
    46
indvdum@0
    47
		// Make sure the path ends with /
indvdum@0
    48
		if (substr($this->include_path, -1) !== '/')
indvdum@0
    49
		{
indvdum@0
    50
			$this->include_path .= '/';
indvdum@0
    51
		}
indvdum@0
    52
	}
indvdum@0
    53
indvdum@0
    54
	/**
indvdum@0
    55
	* Set custom include path for modules
indvdum@0
    56
	* Schema for inclusion is include_path . modulebase
indvdum@0
    57
	*
indvdum@0
    58
	* @param string $include_path include path to be used.
indvdum@0
    59
	* @access public
indvdum@0
    60
	*/
indvdum@0
    61
	function set_custom_include_path($include_path)
indvdum@0
    62
	{
indvdum@0
    63
		$this->include_path = $include_path;
indvdum@0
    64
indvdum@0
    65
		// Make sure the path ends with /
indvdum@0
    66
		if (substr($this->include_path, -1) !== '/')
indvdum@0
    67
		{
indvdum@0
    68
			$this->include_path .= '/';
indvdum@0
    69
		}
indvdum@0
    70
	}
indvdum@0
    71
indvdum@0
    72
	/**
indvdum@0
    73
	* List modules
indvdum@0
    74
	*
indvdum@0
    75
	* This creates a list, stored in $this->module_ary of all available
indvdum@0
    76
	* modules for the given class (ucp, mcp and acp). Additionally
indvdum@0
    77
	* $this->module_y_ary is created with indentation information for
indvdum@0
    78
	* displaying the module list appropriately. Only modules for which
indvdum@0
    79
	* the user has access rights are included in these lists.
indvdum@0
    80
	*/
indvdum@0
    81
	function list_modules($p_class)
indvdum@0
    82
	{
indvdum@0
    83
		global $auth, $db, $user, $cache;
indvdum@0
    84
		global $config, $phpbb_root_path, $phpEx;
indvdum@0
    85
indvdum@0
    86
		// Sanitise for future path use, it's escaped as appropriate for queries
indvdum@0
    87
		$this->p_class = str_replace(array('.', '/', '\\'), '', basename($p_class));
indvdum@0
    88
indvdum@0
    89
		// Get cached modules
indvdum@0
    90
		if (($this->module_cache = $cache->get('_modules_' . $this->p_class)) === false)
indvdum@0
    91
		{
indvdum@0
    92
			// Get modules
indvdum@0
    93
			$sql = 'SELECT *
indvdum@0
    94
				FROM ' . MODULES_TABLE . "
indvdum@0
    95
				WHERE module_class = '" . $db->sql_escape($this->p_class) . "'
indvdum@0
    96
				ORDER BY left_id ASC";
indvdum@0
    97
			$result = $db->sql_query($sql);
indvdum@0
    98
indvdum@0
    99
			$rows = array();
indvdum@0
   100
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   101
			{
indvdum@0
   102
				$rows[$row['module_id']] = $row;
indvdum@0
   103
			}
indvdum@0
   104
			$db->sql_freeresult($result);
indvdum@0
   105
indvdum@0
   106
			$this->module_cache = array();
indvdum@0
   107
			foreach ($rows as $module_id => $row)
indvdum@0
   108
			{
indvdum@0
   109
				$this->module_cache['modules'][] = $row;
indvdum@0
   110
				$this->module_cache['parents'][$row['module_id']] = $this->get_parents($row['parent_id'], $row['left_id'], $row['right_id'], $rows);
indvdum@0
   111
			}
indvdum@0
   112
			unset($rows);
indvdum@0
   113
indvdum@0
   114
			$cache->put('_modules_' . $this->p_class, $this->module_cache);
indvdum@0
   115
		}
indvdum@0
   116
indvdum@0
   117
		if (empty($this->module_cache))
indvdum@0
   118
		{
indvdum@0
   119
			$this->module_cache = array('modules' => array(), 'parents' => array());
indvdum@0
   120
		}
indvdum@0
   121
indvdum@0
   122
		// We "could" build a true tree with this function - maybe mod authors want to use this...
indvdum@0
   123
		// Functions for traversing and manipulating the tree are not available though
indvdum@0
   124
		// We might re-structure the module system to use true trees in 3.2.x...
indvdum@0
   125
		// $tree = $this->build_tree($this->module_cache['modules'], $this->module_cache['parents']);
indvdum@0
   126
indvdum@0
   127
		// Clean up module cache array to only let survive modules the user can access
indvdum@0
   128
		$right_id = false;
indvdum@0
   129
		foreach ($this->module_cache['modules'] as $key => $row)
indvdum@0
   130
		{
indvdum@0
   131
			// Not allowed to view module?
indvdum@0
   132
			if (!$this->module_auth($row['module_auth']))
indvdum@0
   133
			{
indvdum@0
   134
				unset($this->module_cache['modules'][$key]);
indvdum@0
   135
				continue;
indvdum@0
   136
			}
indvdum@0
   137
indvdum@0
   138
			// Category with no members, ignore
indvdum@0
   139
			if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']))
indvdum@0
   140
			{
indvdum@0
   141
				unset($this->module_cache['modules'][$key]);
indvdum@0
   142
				continue;
indvdum@0
   143
			}
indvdum@0
   144
indvdum@0
   145
			// Skip branch
indvdum@0
   146
			if ($right_id !== false)
indvdum@0
   147
			{
indvdum@0
   148
				if ($row['left_id'] < $right_id)
indvdum@0
   149
				{
indvdum@0
   150
					unset($this->module_cache['modules'][$key]);
indvdum@0
   151
					continue;
indvdum@0
   152
				}
indvdum@0
   153
indvdum@0
   154
				$right_id = false;
indvdum@0
   155
			}
indvdum@0
   156
indvdum@0
   157
			// Not enabled?
indvdum@0
   158
			if (!$row['module_enabled'])
indvdum@0
   159
			{
indvdum@0
   160
				// If category is disabled then disable every child too
indvdum@0
   161
				unset($this->module_cache['modules'][$key]);
indvdum@0
   162
				$right_id = $row['right_id'];
indvdum@0
   163
				continue;
indvdum@0
   164
			}
indvdum@0
   165
		}
indvdum@0
   166
indvdum@0
   167
		// Re-index (this is needed, else we are not able to array_slice later)
indvdum@0
   168
		$this->module_cache['modules'] = array_merge($this->module_cache['modules']);
indvdum@0
   169
indvdum@0
   170
		// Include MOD _info files for populating language entries within the menus
indvdum@0
   171
		$this->add_mod_info($this->p_class);
indvdum@0
   172
indvdum@0
   173
		// Now build the module array, but exclude completely empty categories...
indvdum@0
   174
		$right_id = false;
indvdum@0
   175
		$names = array();
indvdum@0
   176
indvdum@0
   177
		foreach ($this->module_cache['modules'] as $key => $row)
indvdum@0
   178
		{
indvdum@0
   179
			// Skip branch
indvdum@0
   180
			if ($right_id !== false)
indvdum@0
   181
			{
indvdum@0
   182
				if ($row['left_id'] < $right_id)
indvdum@0
   183
				{
indvdum@0
   184
					continue;
indvdum@0
   185
				}
indvdum@0
   186
indvdum@0
   187
				$right_id = false;
indvdum@0
   188
			}
indvdum@0
   189
indvdum@0
   190
			// Category with no members on their way down (we have to check every level)
indvdum@0
   191
			if (!$row['module_basename'])
indvdum@0
   192
			{
indvdum@0
   193
				$empty_category = true;
indvdum@0
   194
indvdum@0
   195
				// We go through the branch and look for an activated module
indvdum@0
   196
				foreach (array_slice($this->module_cache['modules'], $key + 1) as $temp_row)
indvdum@0
   197
				{
indvdum@0
   198
					if ($temp_row['left_id'] > $row['left_id'] && $temp_row['left_id'] < $row['right_id'])
indvdum@0
   199
					{
indvdum@0
   200
						// Module there
indvdum@0
   201
						if ($temp_row['module_basename'] && $temp_row['module_enabled'])
indvdum@0
   202
						{
indvdum@0
   203
							$empty_category = false;
indvdum@0
   204
							break;
indvdum@0
   205
						}
indvdum@0
   206
						continue;
indvdum@0
   207
					}
indvdum@0
   208
					break;
indvdum@0
   209
				}
indvdum@0
   210
indvdum@0
   211
				// Skip the branch
indvdum@0
   212
				if ($empty_category)
indvdum@0
   213
				{
indvdum@0
   214
					$right_id = $row['right_id'];
indvdum@0
   215
					continue;
indvdum@0
   216
				}
indvdum@0
   217
			}
indvdum@0
   218
indvdum@0
   219
			$depth = sizeof($this->module_cache['parents'][$row['module_id']]);
indvdum@0
   220
indvdum@0
   221
			// We need to prefix the functions to not create a naming conflict
indvdum@0
   222
indvdum@0
   223
			// Function for building 'url_extra'
indvdum@0
   224
			$url_func = '_module_' . $row['module_basename'] . '_url';
indvdum@0
   225
indvdum@0
   226
			// Function for building the language name
indvdum@0
   227
			$lang_func = '_module_' . $row['module_basename'] . '_lang';
indvdum@0
   228
indvdum@0
   229
			// Custom function for calling parameters on module init (for example assigning template variables)
indvdum@0
   230
			$custom_func = '_module_' . $row['module_basename'];
indvdum@0
   231
indvdum@0
   232
			$names[$row['module_basename'] . '_' . $row['module_mode']][] = true;
indvdum@0
   233
indvdum@0
   234
			$module_row = array(
indvdum@0
   235
				'depth'		=> $depth,
indvdum@0
   236
indvdum@0
   237
				'id'		=> (int) $row['module_id'],
indvdum@0
   238
				'parent'	=> (int) $row['parent_id'],
indvdum@0
   239
				'cat'		=> ($row['right_id'] > $row['left_id'] + 1) ? true : false,
indvdum@0
   240
indvdum@0
   241
				'is_duplicate'	=> ($row['module_basename'] && sizeof($names[$row['module_basename'] . '_' . $row['module_mode']]) > 1) ? true : false,
indvdum@0
   242
indvdum@0
   243
				'name'		=> (string) $row['module_basename'],
indvdum@0
   244
				'mode'		=> (string) $row['module_mode'],
indvdum@0
   245
				'display'	=> (int) $row['module_display'],
indvdum@0
   246
indvdum@0
   247
				'url_extra'	=> (function_exists($url_func)) ? $url_func($row['module_mode'], $row) : '',
indvdum@0
   248
indvdum@0
   249
				'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']),
indvdum@0
   250
				'langname'	=> $row['module_langname'],
indvdum@0
   251
indvdum@0
   252
				'left'		=> $row['left_id'],
indvdum@0
   253
				'right'		=> $row['right_id'],
indvdum@0
   254
			);
indvdum@0
   255
indvdum@0
   256
			if (function_exists($custom_func))
indvdum@0
   257
			{
indvdum@0
   258
				$custom_func($row['module_mode'], $module_row);
indvdum@0
   259
			}
indvdum@0
   260
indvdum@0
   261
			$this->module_ary[] = $module_row;
indvdum@0
   262
		}
indvdum@0
   263
indvdum@0
   264
		unset($this->module_cache['modules'], $names);
indvdum@0
   265
	}
indvdum@0
   266
indvdum@0
   267
	/**
indvdum@0
   268
	* Check if a certain main module is accessible/loaded
indvdum@0
   269
	* By giving the module mode you are able to additionally check for only one mode within the main module
indvdum@0
   270
	*
indvdum@0
   271
	* @param string $module_basename The module base name, for example logs, reports, main (for the mcp).
indvdum@0
   272
	* @param mixed $module_mode The module mode to check. If provided the mode will be checked in addition for presence.
indvdum@0
   273
	*
indvdum@0
   274
	* @return bool Returns true if module is loaded and accessible, else returns false
indvdum@0
   275
	*/
indvdum@0
   276
	function loaded($module_basename, $module_mode = false)
indvdum@0
   277
	{
indvdum@0
   278
		if (empty($this->loaded_cache))
indvdum@0
   279
		{
indvdum@0
   280
			$this->loaded_cache = array();
indvdum@0
   281
indvdum@0
   282
			foreach ($this->module_ary as $row)
indvdum@0
   283
			{
indvdum@0
   284
				if (!$row['name'])
indvdum@0
   285
				{
indvdum@0
   286
					continue;
indvdum@0
   287
				}
indvdum@0
   288
indvdum@0
   289
				if (!isset($this->loaded_cache[$row['name']]))
indvdum@0
   290
				{
indvdum@0
   291
					$this->loaded_cache[$row['name']] = array();
indvdum@0
   292
				}
indvdum@0
   293
indvdum@0
   294
				if (!$row['mode'])
indvdum@0
   295
				{
indvdum@0
   296
					continue;
indvdum@0
   297
				}
indvdum@0
   298
indvdum@0
   299
				$this->loaded_cache[$row['name']][$row['mode']] = true;
indvdum@0
   300
			}
indvdum@0
   301
		}
indvdum@0
   302
indvdum@0
   303
		if ($module_mode === false)
indvdum@0
   304
		{
indvdum@0
   305
			return (isset($this->loaded_cache[$module_basename])) ? true : false;
indvdum@0
   306
		}
indvdum@0
   307
indvdum@0
   308
		return (!empty($this->loaded_cache[$module_basename][$module_mode])) ? true : false;
indvdum@0
   309
	}
indvdum@0
   310
indvdum@0
   311
	/**
indvdum@0
   312
	* Check module authorisation
indvdum@0
   313
	*/
indvdum@0
   314
	function module_auth($module_auth, $forum_id = false)
indvdum@0
   315
	{
indvdum@0
   316
		global $auth, $config;
indvdum@0
   317
indvdum@0
   318
		$module_auth = trim($module_auth);
indvdum@0
   319
indvdum@0
   320
		// Generally allowed to access module if module_auth is empty
indvdum@0
   321
		if (!$module_auth)
indvdum@0
   322
		{
indvdum@0
   323
			return true;
indvdum@0
   324
		}
indvdum@0
   325
indvdum@0
   326
		// With the code below we make sure only those elements get eval'd we really want to be checked
indvdum@0
   327
		preg_match_all('/(?:
indvdum@0
   328
			"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"         |
indvdum@0
   329
			\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     |
indvdum@0
   330
			[(),]                                  |
indvdum@0
   331
			[^\s(),]+)/x', $module_auth, $match);
indvdum@0
   332
indvdum@0
   333
		$tokens = $match[0];
indvdum@0
   334
		for ($i = 0, $size = sizeof($tokens); $i < $size; $i++)
indvdum@0
   335
		{
indvdum@0
   336
			$token = &$tokens[$i];
indvdum@0
   337
indvdum@0
   338
			switch ($token)
indvdum@0
   339
			{
indvdum@0
   340
				case ')':
indvdum@0
   341
				case '(':
indvdum@0
   342
				case '&&':
indvdum@0
   343
				case '||':
indvdum@0
   344
				case ',':
indvdum@0
   345
				break;
indvdum@0
   346
indvdum@0
   347
				default:
indvdum@0
   348
					if (!preg_match('#(?:acl_([a-z0-9_]+)(,\$id)?)|(?:\$id)|(?:aclf_([a-z0-9_]+))|(?:cfg_([a-z0-9_]+))|(?:request_([a-zA-Z0-9_]+))#', $token))
indvdum@0
   349
					{
indvdum@0
   350
						$token = '';
indvdum@0
   351
					}
indvdum@0
   352
				break;
indvdum@0
   353
			}
indvdum@0
   354
		}
indvdum@0
   355
indvdum@0
   356
		$module_auth = implode(' ', $tokens);
indvdum@0
   357
indvdum@0
   358
		// Make sure $id seperation is working fine
indvdum@0
   359
		$module_auth = str_replace(' , ', ',', $module_auth);
indvdum@0
   360
indvdum@0
   361
		$forum_id = ($forum_id === false) ? $this->acl_forum_id : $forum_id;
indvdum@0
   362
indvdum@0
   363
		$is_auth = false;
indvdum@0
   364
		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) . ');');
indvdum@0
   365
indvdum@0
   366
		return $is_auth;
indvdum@0
   367
	}
indvdum@0
   368
indvdum@0
   369
	/**
indvdum@0
   370
	* Set active module
indvdum@0
   371
	*/
indvdum@0
   372
	function set_active($id = false, $mode = false)
indvdum@0
   373
	{
indvdum@0
   374
		$icat = false;
indvdum@0
   375
		$this->active_module = false;
indvdum@0
   376
indvdum@0
   377
		if (request_var('icat', ''))
indvdum@0
   378
		{
indvdum@0
   379
			$icat = $id;
indvdum@0
   380
			$id = request_var('icat', '');
indvdum@0
   381
		}
indvdum@0
   382
indvdum@0
   383
		$category = false;
indvdum@0
   384
		foreach ($this->module_ary as $row_id => $item_ary)
indvdum@0
   385
		{
indvdum@0
   386
			// If this is a module and it's selected, active
indvdum@0
   387
			// If this is a category and the module is the first within it, active
indvdum@0
   388
			// If this is a module and no mode selected, select first mode
indvdum@0
   389
			// If no category or module selected, go active for first module in first category
indvdum@0
   390
			if (
indvdum@0
   391
				(($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && (($item_ary['mode'] == $mode && !$item_ary['cat']) || ($icat && $item_ary['cat']))) ||
indvdum@0
   392
				($item_ary['parent'] === $category && !$item_ary['cat'] && !$icat && $item_ary['display']) ||
indvdum@0
   393
				(($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && !$mode && !$item_ary['cat']) ||
indvdum@0
   394
				(!$id && !$mode && !$item_ary['cat'] && $item_ary['display'])
indvdum@0
   395
				)
indvdum@0
   396
			{
indvdum@0
   397
				if ($item_ary['cat'])
indvdum@0
   398
				{
indvdum@0
   399
					$id = $icat;
indvdum@0
   400
					$icat = false;
indvdum@0
   401
indvdum@0
   402
					continue;
indvdum@0
   403
				}
indvdum@0
   404
indvdum@0
   405
				$this->p_id		= $item_ary['id'];
indvdum@0
   406
				$this->p_parent	= $item_ary['parent'];
indvdum@0
   407
				$this->p_name	= $item_ary['name'];
indvdum@0
   408
				$this->p_mode 	= $item_ary['mode'];
indvdum@0
   409
				$this->p_left	= $item_ary['left'];
indvdum@0
   410
				$this->p_right	= $item_ary['right'];
indvdum@0
   411
indvdum@0
   412
				$this->module_cache['parents'] = $this->module_cache['parents'][$this->p_id];
indvdum@0
   413
				$this->active_module = $item_ary['id'];
indvdum@0
   414
				$this->active_module_row_id = $row_id;
indvdum@0
   415
indvdum@0
   416
				break;
indvdum@0
   417
			}
indvdum@0
   418
			else if (($item_ary['cat'] && $item_ary['id'] === (int) $id) || ($item_ary['parent'] === $category && $item_ary['cat']))
indvdum@0
   419
			{
indvdum@0
   420
				$category = $item_ary['id'];
indvdum@0
   421
			}
indvdum@0
   422
		}
indvdum@0
   423
	}
indvdum@0
   424
indvdum@0
   425
	/**
indvdum@0
   426
	* Loads currently active module
indvdum@0
   427
	*
indvdum@0
   428
	* This method loads a given module, passing it the relevant id and mode.
indvdum@0
   429
	*/
indvdum@0
   430
	function load_active($mode = false, $module_url = false, $execute_module = true)
indvdum@0
   431
	{
indvdum@0
   432
		global $phpbb_root_path, $phpbb_admin_path, $phpEx, $user;
indvdum@0
   433
indvdum@0
   434
		$module_path = $this->include_path . $this->p_class;
indvdum@0
   435
		$icat = request_var('icat', '');
indvdum@0
   436
indvdum@0
   437
		if ($this->active_module === false)
indvdum@0
   438
		{
indvdum@0
   439
			trigger_error('Module not accessible', E_USER_ERROR);
indvdum@0
   440
		}
indvdum@0
   441
indvdum@0
   442
		if (!class_exists("{$this->p_class}_$this->p_name"))
indvdum@0
   443
		{
indvdum@0
   444
			if (!file_exists("$module_path/{$this->p_class}_$this->p_name.$phpEx"))
indvdum@0
   445
			{
indvdum@0
   446
				trigger_error("Cannot find module $module_path/{$this->p_class}_$this->p_name.$phpEx", E_USER_ERROR);
indvdum@0
   447
			}
indvdum@0
   448
indvdum@0
   449
			include("$module_path/{$this->p_class}_$this->p_name.$phpEx");
indvdum@0
   450
indvdum@0
   451
			if (!class_exists("{$this->p_class}_$this->p_name"))
indvdum@0
   452
			{
indvdum@0
   453
				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);
indvdum@0
   454
			}
indvdum@0
   455
indvdum@0
   456
			if (!empty($mode))
indvdum@0
   457
			{
indvdum@0
   458
				$this->p_mode = $mode;
indvdum@0
   459
			}
indvdum@0
   460
indvdum@0
   461
			// Create a new instance of the desired module ... if it has a
indvdum@0
   462
			// constructor it will of course be executed
indvdum@0
   463
			$instance = "{$this->p_class}_$this->p_name";
indvdum@0
   464
indvdum@0
   465
			$this->module = new $instance($this);
indvdum@0
   466
indvdum@0
   467
			// We pre-define the action parameter we are using all over the place
indvdum@0
   468
			if (defined('IN_ADMIN'))
indvdum@0
   469
			{
indvdum@0
   470
				// Is first module automatically enabled a duplicate and the category not passed yet?
indvdum@0
   471
				if (!$icat && $this->module_ary[$this->active_module_row_id]['is_duplicate'])
indvdum@0
   472
				{
indvdum@0
   473
					$icat = $this->module_ary[$this->active_module_row_id]['parent'];
indvdum@0
   474
				}
indvdum@0
   475
indvdum@0
   476
				// Not being able to overwrite ;)
indvdum@0
   477
				$this->module->u_action = append_sid("{$phpbb_admin_path}index.$phpEx", "i={$this->p_name}") . (($icat) ? '&amp;icat=' . $icat : '') . "&amp;mode={$this->p_mode}";
indvdum@0
   478
			}
indvdum@0
   479
			else
indvdum@0
   480
			{
indvdum@0
   481
				// If user specified the module url we will use it...
indvdum@0
   482
				if ($module_url !== false)
indvdum@0
   483
				{
indvdum@0
   484
					$this->module->u_action = $module_url;
indvdum@0
   485
				}
indvdum@0
   486
				else
indvdum@0
   487
				{
indvdum@0
   488
					$this->module->u_action = $phpbb_root_path . (($user->page['page_dir']) ? $user->page['page_dir'] . '/' : '') . $user->page['page_name'];
indvdum@0
   489
				}
indvdum@0
   490
indvdum@0
   491
				$this->module->u_action = append_sid($this->module->u_action, "i={$this->p_name}") . (($icat) ? '&amp;icat=' . $icat : '') . "&amp;mode={$this->p_mode}";
indvdum@0
   492
			}
indvdum@0
   493
indvdum@0
   494
			// Add url_extra parameter to u_action url
indvdum@0
   495
			if (!empty($this->module_ary) && $this->active_module !== false && $this->module_ary[$this->active_module_row_id]['url_extra'])
indvdum@0
   496
			{
indvdum@0
   497
				$this->module->u_action .= $this->module_ary[$this->active_module_row_id]['url_extra'];
indvdum@0
   498
			}
indvdum@0
   499
indvdum@0
   500
			// Assign the module path for re-usage
indvdum@0
   501
			$this->module->module_path = $module_path . '/';
indvdum@0
   502
indvdum@0
   503
			// Execute the main method for the new instance, we send the module id and mode as parameters
indvdum@0
   504
			// Users are able to call the main method after this function to be able to assign additional parameters manually
indvdum@0
   505
			if ($execute_module)
indvdum@0
   506
			{
indvdum@0
   507
				$this->module->main($this->p_name, $this->p_mode);
indvdum@0
   508
			}
indvdum@0
   509
indvdum@0
   510
			return;
indvdum@0
   511
		}
indvdum@0
   512
	}
indvdum@0
   513
indvdum@0
   514
	/**
indvdum@0
   515
	* Appending url parameter to the currently active module.
indvdum@0
   516
	*
indvdum@0
   517
	* This function is called for adding specific url parameters while executing the current module.
indvdum@0
   518
	* It is doing the same as the _module_{name}_url() function, apart from being able to be called after
indvdum@0
   519
	* having dynamically parsed specific parameters. This allows more freedom in choosing additional parameters.
indvdum@0
   520
	* One example can be seen in /includes/mcp/mcp_notes.php - $this->p_master->adjust_url() call.
indvdum@0
   521
	*
indvdum@0
   522
	* @param string $url_extra Extra url parameters, e.g.: &amp;u=$user_id
indvdum@0
   523
	*
indvdum@0
   524
	*/
indvdum@0
   525
	function adjust_url($url_extra)
indvdum@0
   526
	{
indvdum@0
   527
		if (empty($this->module_ary[$this->active_module_row_id]))
indvdum@0
   528
		{
indvdum@0
   529
			return;
indvdum@0
   530
		}
indvdum@0
   531
indvdum@0
   532
		$row = &$this->module_ary[$this->active_module_row_id];
indvdum@0
   533
indvdum@0
   534
		// We check for the same url_extra in $row['url_extra'] to overcome doubled additions...
indvdum@0
   535
		if (strpos($row['url_extra'], $url_extra) === false)
indvdum@0
   536
		{
indvdum@0
   537
			$row['url_extra'] .= $url_extra;
indvdum@0
   538
		}
indvdum@0
   539
	}
indvdum@0
   540
indvdum@0
   541
	/**
indvdum@0
   542
	* Check if a module is active
indvdum@0
   543
	*/
indvdum@0
   544
	function is_active($id, $mode = false)
indvdum@0
   545
	{
indvdum@0
   546
		// If we find a name by this id and being enabled we have our active one...
indvdum@0
   547
		foreach ($this->module_ary as $row_id => $item_ary)
indvdum@0
   548
		{
indvdum@0
   549
			if (($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && $item_ary['display'])
indvdum@0
   550
			{
indvdum@0
   551
				if ($mode === false || $mode === $item_ary['mode'])
indvdum@0
   552
				{
indvdum@0
   553
					return true;
indvdum@0
   554
				}
indvdum@0
   555
			}
indvdum@0
   556
		}
indvdum@0
   557
indvdum@0
   558
		return false;
indvdum@0
   559
	}
indvdum@0
   560
indvdum@0
   561
	/**
indvdum@0
   562
	* Get parents
indvdum@0
   563
	*/
indvdum@0
   564
	function get_parents($parent_id, $left_id, $right_id, &$all_parents)
indvdum@0
   565
	{
indvdum@0
   566
		global $db;
indvdum@0
   567
indvdum@0
   568
		$parents = array();
indvdum@0
   569
indvdum@0
   570
		if ($parent_id > 0)
indvdum@0
   571
		{
indvdum@0
   572
			foreach ($all_parents as $module_id => $row)
indvdum@0
   573
			{
indvdum@0
   574
				if ($row['left_id'] < $left_id && $row['right_id'] > $right_id)
indvdum@0
   575
				{
indvdum@0
   576
					$parents[$module_id] = $row['parent_id'];
indvdum@0
   577
				}
indvdum@0
   578
indvdum@0
   579
				if ($row['left_id'] > $left_id)
indvdum@0
   580
				{
indvdum@0
   581
					break;
indvdum@0
   582
				}
indvdum@0
   583
			}
indvdum@0
   584
		}
indvdum@0
   585
indvdum@0
   586
		return $parents;
indvdum@0
   587
	}
indvdum@0
   588
indvdum@0
   589
	/**
indvdum@0
   590
	* Get tree branch
indvdum@0
   591
	*/
indvdum@0
   592
	function get_branch($left_id, $right_id, $remaining)
indvdum@0
   593
	{
indvdum@0
   594
		$branch = array();
indvdum@0
   595
indvdum@0
   596
		foreach ($remaining as $key => $row)
indvdum@0
   597
		{
indvdum@0
   598
			if ($row['left_id'] > $left_id && $row['left_id'] < $right_id)
indvdum@0
   599
			{
indvdum@0
   600
				$branch[] = $row;
indvdum@0
   601
				continue;
indvdum@0
   602
			}
indvdum@0
   603
			break;
indvdum@0
   604
		}
indvdum@0
   605
indvdum@0
   606
		return $branch;
indvdum@0
   607
	}
indvdum@0
   608
indvdum@0
   609
	/**
indvdum@0
   610
	* Build true binary tree from given array
indvdum@0
   611
	* Not in use
indvdum@0
   612
	*/
indvdum@0
   613
	function build_tree(&$modules, &$parents)
indvdum@0
   614
	{
indvdum@0
   615
		$tree = array();
indvdum@0
   616
indvdum@0
   617
		foreach ($modules as $row)
indvdum@0
   618
		{
indvdum@0
   619
			$branch = &$tree;
indvdum@0
   620
indvdum@0
   621
			if ($row['parent_id'])
indvdum@0
   622
			{
indvdum@0
   623
				// Go through the tree to find our branch
indvdum@0
   624
				$parent_tree = $parents[$row['module_id']];
indvdum@0
   625
indvdum@0
   626
				foreach ($parent_tree as $id => $value)
indvdum@0
   627
				{
indvdum@0
   628
					if (!isset($branch[$id]) && isset($branch['child']))
indvdum@0
   629
					{
indvdum@0
   630
						$branch = &$branch['child'];
indvdum@0
   631
					}
indvdum@0
   632
					$branch = &$branch[$id];
indvdum@0
   633
				}
indvdum@0
   634
				$branch = &$branch['child'];
indvdum@0
   635
			}
indvdum@0
   636
indvdum@0
   637
			$branch[$row['module_id']] = $row;
indvdum@0
   638
			if (!isset($branch[$row['module_id']]['child']))
indvdum@0
   639
			{
indvdum@0
   640
				$branch[$row['module_id']]['child'] = array();
indvdum@0
   641
			}
indvdum@0
   642
		}
indvdum@0
   643
indvdum@0
   644
		return $tree;
indvdum@0
   645
	}
indvdum@0
   646
indvdum@0
   647
	/**
indvdum@0
   648
	* Build navigation structure
indvdum@0
   649
	*/
indvdum@0
   650
	function assign_tpl_vars($module_url)
indvdum@0
   651
	{
indvdum@0
   652
		global $template;
indvdum@0
   653
indvdum@0
   654
		$current_id = $right_id = false;
indvdum@0
   655
indvdum@0
   656
		// Make sure the module_url has a question mark set, effectively determining the delimiter to use
indvdum@0
   657
		$delim = (strpos($module_url, '?') === false) ? '?' : '&amp;';
indvdum@0
   658
indvdum@0
   659
		$current_padding = $current_depth = 0;
indvdum@0
   660
		$linear_offset 	= 'l_block1';
indvdum@0
   661
		$tabular_offset = 't_block2';
indvdum@0
   662
indvdum@0
   663
		// Generate the list of modules, we'll do this in two ways ...
indvdum@0
   664
		// 1) In a linear fashion
indvdum@0
   665
		// 2) In a combined tabbed + linear fashion ... tabs for the categories
indvdum@0
   666
		//    and a linear list for subcategories/items
indvdum@0
   667
		foreach ($this->module_ary as $row_id => $item_ary)
indvdum@0
   668
		{
indvdum@0
   669
			// Skip hidden modules
indvdum@0
   670
			if (!$item_ary['display'])
indvdum@0
   671
			{
indvdum@0
   672
				continue;
indvdum@0
   673
			}
indvdum@0
   674
indvdum@0
   675
			// Skip branch
indvdum@0
   676
			if ($right_id !== false)
indvdum@0
   677
			{
indvdum@0
   678
				if ($item_ary['left'] < $right_id)
indvdum@0
   679
				{
indvdum@0
   680
					continue;
indvdum@0
   681
				}
indvdum@0
   682
indvdum@0
   683
				$right_id = false;
indvdum@0
   684
			}
indvdum@0
   685
indvdum@0
   686
			// Category with no members on their way down (we have to check every level)
indvdum@0
   687
			if (!$item_ary['name'])
indvdum@0
   688
			{
indvdum@0
   689
				$empty_category = true;
indvdum@0
   690
indvdum@0
   691
				// We go through the branch and look for an activated module
indvdum@0
   692
				foreach (array_slice($this->module_ary, $row_id + 1) as $temp_row)
indvdum@0
   693
				{
indvdum@0
   694
					if ($temp_row['left'] > $item_ary['left'] && $temp_row['left'] < $item_ary['right'])
indvdum@0
   695
					{
indvdum@0
   696
						// Module there and displayed?
indvdum@0
   697
						if ($temp_row['name'] && $temp_row['display'])
indvdum@0
   698
						{
indvdum@0
   699
							$empty_category = false;
indvdum@0
   700
							break;
indvdum@0
   701
						}
indvdum@0
   702
						continue;
indvdum@0
   703
					}
indvdum@0
   704
					break;
indvdum@0
   705
				}
indvdum@0
   706
indvdum@0
   707
				// Skip the branch
indvdum@0
   708
				if ($empty_category)
indvdum@0
   709
				{
indvdum@0
   710
					$right_id = $item_ary['right'];
indvdum@0
   711
					continue;
indvdum@0
   712
				}
indvdum@0
   713
			}
indvdum@0
   714
indvdum@0
   715
			// Select first id we can get
indvdum@0
   716
			if (!$current_id && (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id))
indvdum@0
   717
			{
indvdum@0
   718
				$current_id = $item_ary['id'];
indvdum@0
   719
			}
indvdum@0
   720
indvdum@0
   721
			$depth = $item_ary['depth'];
indvdum@0
   722
indvdum@0
   723
			if ($depth > $current_depth)
indvdum@0
   724
			{
indvdum@0
   725
				$linear_offset = $linear_offset . '.l_block' . ($depth + 1);
indvdum@0
   726
				$tabular_offset = ($depth + 1 > 2) ? $tabular_offset . '.t_block' . ($depth + 1) : $tabular_offset;
indvdum@0
   727
			}
indvdum@0
   728
			else if ($depth < $current_depth)
indvdum@0
   729
			{
indvdum@0
   730
				for ($i = $current_depth - $depth; $i > 0; $i--)
indvdum@0
   731
				{
indvdum@0
   732
					$linear_offset = substr($linear_offset, 0, strrpos($linear_offset, '.'));
indvdum@0
   733
					$tabular_offset = ($i + $depth > 1) ? substr($tabular_offset, 0, strrpos($tabular_offset, '.')) : $tabular_offset;
indvdum@0
   734
				}
indvdum@0
   735
			}
indvdum@0
   736
indvdum@0
   737
			$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']);
indvdum@0
   738
indvdum@0
   739
			// Was not allowed in categories before - /*!$item_ary['cat'] && */
indvdum@0
   740
			$u_title .= (isset($item_ary['url_extra'])) ? $item_ary['url_extra'] : '';
indvdum@0
   741
indvdum@0
   742
			// Only output a categories items if it's currently selected
indvdum@0
   743
			if (!$depth || ($depth && (in_array($item_ary['parent'], array_values($this->module_cache['parents'])) || $item_ary['parent'] == $this->p_parent)))
indvdum@0
   744
			{
indvdum@0
   745
				$use_tabular_offset = (!$depth) ? 't_block1' : $tabular_offset;
indvdum@0
   746
indvdum@0
   747
				$tpl_ary = array(
indvdum@0
   748
					'L_TITLE'		=> $item_ary['lang'],
indvdum@0
   749
					'S_SELECTED'	=> (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id) ? true : false,
indvdum@0
   750
					'U_TITLE'		=> $u_title
indvdum@0
   751
				);
indvdum@0
   752
indvdum@0
   753
				$template->assign_block_vars($use_tabular_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
indvdum@0
   754
			}
indvdum@0
   755
indvdum@0
   756
			$tpl_ary = array(
indvdum@0
   757
				'L_TITLE'		=> $item_ary['lang'],
indvdum@0
   758
				'S_SELECTED'	=> (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id) ? true : false,
indvdum@0
   759
				'U_TITLE'		=> $u_title
indvdum@0
   760
			);
indvdum@0
   761
indvdum@0
   762
			$template->assign_block_vars($linear_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
indvdum@0
   763
indvdum@0
   764
			$current_depth = $depth;
indvdum@0
   765
		}
indvdum@0
   766
	}
indvdum@0
   767
indvdum@0
   768
	/**
indvdum@0
   769
	* Returns desired template name
indvdum@0
   770
	*/
indvdum@0
   771
	function get_tpl_name()
indvdum@0
   772
	{
indvdum@0
   773
		return $this->module->tpl_name . '.html';
indvdum@0
   774
	}
indvdum@0
   775
indvdum@0
   776
	/**
indvdum@0
   777
	* Returns the desired page title
indvdum@0
   778
	*/
indvdum@0
   779
	function get_page_title()
indvdum@0
   780
	{
indvdum@0
   781
		global $user;
indvdum@0
   782
indvdum@0
   783
		if (!isset($this->module->page_title))
indvdum@0
   784
		{
indvdum@0
   785
			return '';
indvdum@0
   786
		}
indvdum@0
   787
indvdum@0
   788
		return (isset($user->lang[$this->module->page_title])) ? $user->lang[$this->module->page_title] : $this->module->page_title;
indvdum@0
   789
	}
indvdum@0
   790
indvdum@0
   791
	/**
indvdum@0
   792
	* Load module as the current active one without the need for registering it
indvdum@0
   793
	*/
indvdum@0
   794
	function load($class, $name, $mode = false)
indvdum@0
   795
	{
indvdum@0
   796
		$this->p_class = $class;
indvdum@0
   797
		$this->p_name = $name;
indvdum@0
   798
indvdum@0
   799
		// Set active module to true instead of using the id
indvdum@0
   800
		$this->active_module = true;
indvdum@0
   801
indvdum@0
   802
		$this->load_active($mode);
indvdum@0
   803
	}
indvdum@0
   804
indvdum@0
   805
	/**
indvdum@0
   806
	* Display module
indvdum@0
   807
	*/
indvdum@0
   808
	function display($page_title, $display_online_list = true)
indvdum@0
   809
	{
indvdum@0
   810
		global $template, $user;
indvdum@0
   811
indvdum@0
   812
		// Generate the page
indvdum@0
   813
		if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
indvdum@0
   814
		{
indvdum@0
   815
			adm_page_header($page_title);
indvdum@0
   816
		}
indvdum@0
   817
		else
indvdum@0
   818
		{
indvdum@0
   819
			page_header($page_title, $display_online_list);
indvdum@0
   820
		}
indvdum@0
   821
indvdum@0
   822
		$template->set_filenames(array(
indvdum@0
   823
			'body' => $this->get_tpl_name())
indvdum@0
   824
		);
indvdum@0
   825
indvdum@0
   826
		if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin'])
indvdum@0
   827
		{
indvdum@0
   828
			adm_page_footer();
indvdum@0
   829
		}
indvdum@0
   830
		else
indvdum@0
   831
		{
indvdum@0
   832
			page_footer();
indvdum@0
   833
		}
indvdum@0
   834
	}
indvdum@0
   835
indvdum@0
   836
	/**
indvdum@0
   837
	* Toggle whether this module will be displayed or not
indvdum@0
   838
	*/
indvdum@0
   839
	function set_display($id, $mode = false, $display = true)
indvdum@0
   840
	{
indvdum@0
   841
		foreach ($this->module_ary as $row_id => $item_ary)
indvdum@0
   842
		{
indvdum@0
   843
			if (($item_ary['name'] === $id || $item_ary['id'] === (int) $id) && (!$mode || $item_ary['mode'] === $mode))
indvdum@0
   844
			{
indvdum@0
   845
				$this->module_ary[$row_id]['display'] = (int) $display;
indvdum@0
   846
			}
indvdum@0
   847
		}
indvdum@0
   848
	}
indvdum@0
   849
indvdum@0
   850
	/**
indvdum@0
   851
	* Add custom MOD info language file
indvdum@0
   852
	*/
indvdum@0
   853
	function add_mod_info($module_class)
indvdum@0
   854
	{
indvdum@0
   855
		global $user, $phpEx;
indvdum@0
   856
indvdum@0
   857
		if (file_exists($user->lang_path . $user->lang_name . '/mods'))
indvdum@0
   858
		{
indvdum@0
   859
			$add_files = array();
indvdum@0
   860
indvdum@0
   861
			$dir = @opendir($user->lang_path . $user->lang_name . '/mods');
indvdum@0
   862
indvdum@0
   863
			if ($dir)
indvdum@0
   864
			{
indvdum@0
   865
				while (($entry = readdir($dir)) !== false)
indvdum@0
   866
				{
indvdum@0
   867
					if (strpos($entry, 'info_' . strtolower($module_class) . '_') === 0 && substr(strrchr($entry, '.'), 1) == $phpEx)
indvdum@0
   868
					{
indvdum@0
   869
						$add_files[] = 'mods/' . substr(basename($entry), 0, -(strlen($phpEx) + 1));
indvdum@0
   870
					}
indvdum@0
   871
				}
indvdum@0
   872
				closedir($dir);
indvdum@0
   873
			}
indvdum@0
   874
indvdum@0
   875
			if (sizeof($add_files))
indvdum@0
   876
			{
indvdum@0
   877
				$user->add_lang($add_files);
indvdum@0
   878
			}
indvdum@0
   879
		}
indvdum@0
   880
	}
indvdum@0
   881
}
indvdum@0
   882
indvdum@0
   883
?>