aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/functions_module.php
blob: cdb2cd6fa55963b62390cf2a10afb774fe6847cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/** 
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @package phpBB3
* Module Class handling all types of modules
*/
class module
{
	var $id = 0;
	var $filename;
	var $mode;
	var $module_ary = array();
	var $module_y_ary = array();
	var $module_url = '';
	var $module_path = '';
	var $acl_forum_id = false;

	// Private methods, should not be overwritten
	function create($module_type, $module_path, $module_url, $selected_module = false)
	{
		global $template, $auth, $db, $user, $config, $phpbb_root_path, $phpEx;

		$active = $category = false;
		$this->module_url = htmlspecialchars($module_url);
		// TODO Do some checking here?
		$this->module_path = $module_path;

		if (file_exists($phpbb_root_path . 'cache/' . $module_type . '_modules.'.$phpEx))
		{
			include($phpbb_root_path . 'cache/' . $module_type . '_modules.'.$phpEx);
		}

		$sql = 'SELECT *
			FROM ' . MODULES_TABLE . "
			WHERE module_type = '" . $db->sql_escape($module_type) . "'
				AND module_enabled = 1
				$sql_and
			ORDER BY left_id";
		$result = $db->sql_query($sql);

		$padding = $i = 0;
		while ($row = $db->sql_fetchrow($result))
		{
			// Authorisation is required ... not authed, skip
			if ($row['module_auth'])
			{
				$is_auth = false;
				eval('$is_auth = (int) (' . preg_replace(array('#acl_([a-z_]+)(,\$id)?#e', '#\$id#', '#cfg_([a-z_]+)#e'), array('(int) $auth->acl_get("\\1"\\2)', '$this->acl_forum_id', '(int) $config["\\1"]'), trim($row['module_auth'])) . ');');
				if (!$is_auth)
				{
					continue;
				}
			}

			if ($row['module_cat'] && ($row['left_id'] + 1 == $row['right_id']))
			{
				// Categoriy with no members, ignore
				continue;
			}

			if ($row['left_id'] < $right)
			{
				$padding++;
				$padding_store[$row['parent_id']] = $padding;
			}
			else if ($row['left_id'] > $right + 1)
			{
				$padding = $padding_store[$row['parent_id']];
			}

			$right = $row['right_id'];

			$this->module_y_ary[$i] 			= ($padding) ? $padding : 0;

			$this->module_ary[$i]['id'] 		= (int) $row['module_id'];
			$this->module_ary[$i]['name'] 		= (function_exists($row['module_filename'])) ? $row['module_filename']($row['module_name']) : ((!empty($user->lang[$row['module_name']])) ? $user->lang[$row['module_name']] : ucfirst(str_replace('_', ' ', strtolower($row['module_name']))));
			$this->module_ary[$i]['filename']	= (string) $row['module_filename'];
			$this->module_ary[$i]['parent'] 	= (int) $row['parent_id'];
			$this->module_ary[$i]['category']	= (bool) $row['module_cat'];
			$this->module_ary[$i]['hilit'] 		= (bool) $row['module_hilit'];
			$this->module_ary[$i]['active'] 	= false;

			// If this is a module and it's selected, active
			// If this is a category and the module is the first within it, active
			// If no category or module selected, go active for first module in first category
			if (!$active && ((!$row['module_cat'] && $row['module_id'] == $selected_module) || $row['parent_id'] === $category || (!$selected_module && !$row['module_cat'])))
			{
				$this->module_ary[$i]['active'] = true;

				$this->id		= (int) $row['module_id'];
				$this->parent	= (int) $row['parent_id'];
				$this->filename = (string) $row['module_filename'];
				$this->mode 	= (string) $row['module_mode'];

				$active = true;
			}
			else if ($row['module_cat'] && $row['module_id'] == $selected_module)
			{
				$category = $row['module_id'];
			}

			$i++;
		}
		$db->sql_freeresult($result);
	}

	function load($mode = false, $run = true)
	{
		global $phpbb_root_path, $phpEx;

		if (!class_exists($this->filename))
		{
			require_once($phpbb_root_path . "$this->module_path/$this->filename.$phpEx");

			if ($run)
			{
				if (!empty($mode))
				{
					$this->mode = $mode;
				}

				eval("\$this->module = new $this->filename(\$this->id, \$this->mode);");
				if (method_exists($this->module, 'init'))
				{
					$this->module->init();
				}
			}
		}
	}

	// Displays the appropriate template with the given title
	function display($page_title, $tpl_name)
	{
		global $template;

		$current_padding = 0;
		$linear_offset 	= 'l_block1';
		$tabular_offset = 't_block2';

		// Generate the list of modules, we'll do this in two ways ...
		// 1) In a linear fashion
		// 2) In a combined tabbed + linear fashion ... tabs for the categories
		//    and a linear list for subcategories/items
		foreach ($this->module_ary as $row_id => $item_ary)
		{
			$padding = $this->module_y_ary[$row_id];

			if ($padding > $current_padding)
			{
				$linear_offset = $linear_offset . '.l_block' . ($padding + 1);
				$tabular_offset = ($padding + 1 > 2) ? $tabular_offset . '.t_block' . ($padding + 1) : $tabular_offset;
			}
			else if ($padding < $current_padding)
			{
				for ($i = $current_padding - $padding; $i > 0; $i--)
				{
					$linear_offset = substr($linear_offset, 0, strrpos($linear_offset, '.'));
					$tabular_offset = ($i + $padding > 1) ? substr($tabular_offset, 0, strrpos($tabular_offset, '.')) : $tabular_offset;
				}
			}

			$current_padding = $padding;

			// Only output a categories items if it's currently selected
			if (!$padding || ($padding && $item_ary['parent'] == $this->parent))
			{
				$use_tabular_offset = (!$padding) ? 't_block1' : $tabular_offset;
				$template->assign_block_vars($use_tabular_offset, array(
					'L_TITLE'		=> $item_ary['name'],
					'S_SELECTED'	=> ($item_ary['id'] == $this->parent || $item_ary['active']) ? true : false,
					'U_TITLE'		=> $this->module_url . '&amp;i=' . $item_ary['id'])
				);
			}

			$template->assign_block_vars($linear_offset, array(
				'L_TITLE'		=> $item_ary['name'],
				'S_SELECTED'	=> ($item_ary['id'] == $this->parent || $item_ary['active']) ? true : false,
				'U_TITLE'		=> $this->module_url . '&amp;i=' . $item_ary['id'])
			);
		}

		page_header($page_title);

		$template->set_filenames(array(
			'body' => $tpl_name . '_' . $this->mode . '.html')
		);

		page_footer();
	}

	//
	// Public methods to be overwritten by modules
	//
	function module()
	{
		// Module name
		// Module filename
		// Module description
		// Module version
		// Module compatibility
		return false;
	}

	function init()
	{
		return false;
	}
}

?>