aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/module/module_manager.php
blob: 00df33f62f017f0034b73d89a529286f4e507d14 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\module;

use phpbb\module\exception\module_exception;
use phpbb\module\exception\module_not_found_exception;

class module_manager
{
	/**
	 * @var \phpbb\cache\driver\driver_interface
	 */
	protected $cache;

	/**
	 * @var \phpbb\db\driver\driver_interface
	 */
	protected $db;

	/**
	 * @var \phpbb\extension\manager
	 */
	protected $extension_manager;

	/**
	 * @var string
	 */
	protected $modules_table;

	/**
	 * @var string
	 */
	protected $phpbb_root_path;

	/**
	 * @var string
	 */
	protected $php_ext;

	/**
	 * Constructor
	 *
	 * @param \phpbb\cache\driver\driver_interface	$cache				Cache driver
	 * @param \phpbb\db\driver\driver_interface		$db					Database driver
	 * @param \phpbb\extension\manager				$ext_manager		Extension manager
	 * @param string								$modules_table		Module database table's name
	 * @param string								$phpbb_root_path	Path to phpBB's root
	 * @param string								$php_ext			Extension of PHP files
	 */
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\extension\manager $ext_manager, $modules_table, $phpbb_root_path, $php_ext)
	{
		$this->cache				= $cache;
		$this->db					= $db;
		$this->extension_manager	= $ext_manager;
		$this->modules_table		= $modules_table;
		$this->phpbb_root_path		= $phpbb_root_path;
		$this->php_ext				= $php_ext;
	}

	/**
	 * Get row for specified module
	 *
	 * @param int		$module_id		ID of the module
	 * @param string	$module_class	Class of the module (acp, ucp, mcp etc...)
	 *
	 * @return array	Array of data fetched from the database
	 *
	 * @throws \phpbb\module\exception\module_not_found_exception	When there is no module with $module_id
	 */
	public function get_module_row($module_id, $module_class)
	{
		$module_id = (int) $module_id;

		$sql = 'SELECT *
			FROM ' . $this->modules_table . "
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND module_id = $module_id";
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		if (!$row)
		{
			throw new module_not_found_exception('NO_MODULE');
		}

		return $row;
	}

	/**
	 * Get available module information from module files
	 *
	 * @param string	$module_class		Class of the module (acp, ucp, mcp etc...)
	 * @param string	$module				ID of module
	 * @param bool		$use_all_available	Use all available instead of just all
	 *										enabled extensions
	 *
	 * @return array	Array with module information gathered from module info files.
	 */
	public function get_module_infos($module_class, $module = '', $use_all_available = false)
	{
		$directory = $this->phpbb_root_path . 'includes/' . $module_class . '/info/';
		$fileinfo = array();

		$finder = $this->extension_manager->get_finder($use_all_available);

		$modules = $finder
			->extension_suffix('_module')
			->extension_directory("/$module_class")
			->core_path("includes/$module_class/info/")
			->core_prefix($module_class . '_')
			->get_classes(true);

		foreach ($modules as $cur_module)
		{
			// Skip entries we do not need if we know the module we are
			// looking for
			if ($module && strpos(str_replace('\\', '_', $cur_module), $module) === false && $module !== $cur_module)
			{
				continue;
			}

			$info_class = preg_replace('/_module$/', '_info', $cur_module);

			// If the class does not exist it might be following the old
			// format. phpbb_acp_info_acp_foo needs to be turned into
			// acp_foo_info and the respective file has to be included
			// manually because it does not support auto loading
			$old_info_class_file = str_replace("phpbb_{$module_class}_info_", '', $cur_module);
			$old_info_class = $old_info_class_file . '_info';

			if (class_exists($old_info_class))
			{
				$info_class = $old_info_class;
			}
			else if (!class_exists($info_class))
			{
				$info_class = $old_info_class;

				// need to check class exists again because previous checks triggered autoloading
				if (!class_exists($info_class) && file_exists($directory . $old_info_class_file . '.' . $this->php_ext))
				{
					include($directory . $old_info_class_file . '.' . $this->php_ext);
				}
			}

			if (class_exists($info_class))
			{
				$info = new $info_class();
				$module_info = $info->module();

				$main_class = (isset($module_info['filename'])) ? $module_info['filename'] : $cur_module;

				$fileinfo[$main_class] = $module_info;
			}
		}

		ksort($fileinfo);

		return $fileinfo;
	}

	/**
	 * Get module branch
	 *
	 * @param int		$module_id		ID of the module
	 * @param string	$module_class	Class of the module (acp, ucp, mcp etc...)
	 * @param string	$type			Type of branch (Expected values: all, parents or children)
	 * @param bool		$include_module	Whether or not to include the specified module with $module_id
	 *
	 * @return array	Returns an array containing the modules in the specified branch type.
	 */
	public function get_module_branch($module_id, $module_class, $type = 'all', $include_module = true)
	{
		$module_id = (int) $module_id;

		switch ($type)
		{
			case 'parents':
				$condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
			break;

			case 'children':
				$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
			break;

			default:
				$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
			break;
		}

		$rows = array();

		$sql = 'SELECT m2.*
			FROM ' . $this->modules_table . ' m1
			LEFT JOIN ' . $this->modules_table . " m2 ON ($condition)
			WHERE m1.module_class = '" . $this->db->sql_escape($module_class) . "'
				AND m2.module_class = '" . $this->db->sql_escape($module_class) . "'
				AND m1.module_id = $module_id
			ORDER BY m2.left_id";
		$result = $this->db->sql_query($sql);

		while ($row = $this->db->sql_fetchrow($result))
		{
			if (!$include_module && $row['module_id'] == $module_id)
			{
				continue;
			}

			$rows[] = $row;
		}
		$this->db->sql_freeresult($result);

		return $rows;
	}

	/**
	 * Remove modules cache file
	 *
	 * @param string	$module_class	Class of the module (acp, ucp, mcp etc...)
	 */
	public function remove_cache_file($module_class)
	{
		// Sanitise for future path use, it's escaped as appropriate for queries
		$cache_class = str_replace(array('.', '/', '\\'), '', basename($module_class));
		$this->cache->destroy('_modules_' . $cache_class);
		$this->cache->destroy('sql', $this->modules_table);
	}

	/**
	 * Update/Add module
	 *
	 * @param array	&$module_data	The module data
	 *
	 * @throws \phpbb\module\exception\module_not_found_exception	When parent module or the category is not exist
	 */
	public function update_module_data(&$module_data)
	{
		if (!isset($module_data['module_id']))
		{
			// no module_id means we're creating a new category/module
			if ($module_data['parent_id'])
			{
				$sql = 'SELECT left_id, right_id
					FROM ' . $this->modules_table . "
					WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
						AND module_id = " . (int) $module_data['parent_id'];
				$result = $this->db->sql_query($sql);
				$row = $this->db->sql_fetchrow($result);
				$this->db->sql_freeresult($result);

				if (!$row)
				{
					throw new module_not_found_exception('PARENT_NOT_EXIST');
				}

				// Workaround
				$row['left_id'] = (int) $row['left_id'];
				$row['right_id'] = (int) $row['right_id'];

				$sql = 'UPDATE ' . $this->modules_table . "
					SET left_id = left_id + 2, right_id = right_id + 2
					WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
						AND left_id > {$row['right_id']}";
				$this->db->sql_query($sql);

				$sql = 'UPDATE ' . $this->modules_table . "
					SET right_id = right_id + 2
					WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
						AND {$row['left_id']} BETWEEN left_id AND right_id";
				$this->db->sql_query($sql);

				$module_data['left_id'] = (int) $row['right_id'];
				$module_data['right_id'] = (int) $row['right_id'] + 1;
			}
			else
			{
				$sql = 'SELECT MAX(right_id) AS right_id
					FROM ' . $this->modules_table . "
					WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'";
				$result = $this->db->sql_query($sql);
				$row = $this->db->sql_fetchrow($result);
				$this->db->sql_freeresult($result);

				$module_data['left_id'] = (int) $row['right_id'] + 1;
				$module_data['right_id'] = (int) $row['right_id'] + 2;
			}

			$sql = 'INSERT INTO ' . $this->modules_table . ' ' . $this->db->sql_build_array('INSERT', $module_data);
			$this->db->sql_query($sql);

			$module_data['module_id'] = $this->db->sql_nextid();
		}
		else
		{
			$row = $this->get_module_row($module_data['module_id'], $module_data['module_class']);

			if ($module_data['module_basename'] && !$row['module_basename'])
			{
				// we're turning a category into a module
				$branch = $this->get_module_branch($module_data['module_id'], $module_data['module_class'], 'children', false);

				if (count($branch))
				{
					throw new module_not_found_exception('NO_CATEGORY_TO_MODULE');
				}
			}

			if ($row['parent_id'] != $module_data['parent_id'])
			{
				$this->move_module($module_data['module_id'], $module_data['parent_id'], $module_data['module_class']);
			}

			$update_ary = $module_data;
			unset($update_ary['module_id']);

			$sql = 'UPDATE ' . $this->modules_table . '
				SET ' . $this->db->sql_build_array('UPDATE', $update_ary) . "
				WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
					AND module_id = " . (int) $module_data['module_id'];
			$this->db->sql_query($sql);
		}
	}

	/**
	 * Move module around the tree
	 *
	 * @param int		$from_module_id	ID of the current parent module
	 * @param int		$to_parent_id	ID of the target parent module
	 * @param string	$module_class	Class of the module (acp, ucp, mcp etc...)
	 *
	 * @throws \phpbb\module\exception\module_not_found_exception	If the module specified to move modules from does not
	 * 																have any children.
	 */
	public function move_module($from_module_id, $to_parent_id, $module_class)
	{
		$moved_modules = $this->get_module_branch($from_module_id, $module_class, 'children');

		if (empty($moved_modules))
		{
			throw new module_not_found_exception();
		}

		$from_data = $moved_modules[0];
		$diff = count($moved_modules) * 2;

		$moved_ids = array();
		for ($i = 0, $size = count($moved_modules); $i < $size; ++$i)
		{
			$moved_ids[] = $moved_modules[$i]['module_id'];
		}

		// Resync parents
		$sql = 'UPDATE ' . $this->modules_table . "
			SET right_id = right_id - $diff
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND left_id < " . (int) $from_data['right_id'] . '
				AND right_id > ' . (int) $from_data['right_id'];
		$this->db->sql_query($sql);

		// Resync righthand side of tree
		$sql = 'UPDATE ' . $this->modules_table . "
			SET left_id = left_id - $diff, right_id = right_id - $diff
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND left_id > " . (int) $from_data['right_id'];
		$this->db->sql_query($sql);

		if ($to_parent_id > 0)
		{
			$to_data = $this->get_module_row($to_parent_id, $module_class);

			// Resync new parents
			$sql = 'UPDATE ' . $this->modules_table . "
				SET right_id = right_id + $diff
				WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
					AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
					AND ' . $this->db->sql_in_set('module_id', $moved_ids, true);
			$this->db->sql_query($sql);

			// Resync the righthand side of the tree
			$sql = 'UPDATE ' . $this->modules_table . "
				SET left_id = left_id + $diff, right_id = right_id + $diff
				WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
					AND left_id > " . (int) $to_data['right_id'] . '
					AND ' . $this->db->sql_in_set('module_id', $moved_ids, true);
			$this->db->sql_query($sql);

			// Resync moved branch
			$to_data['right_id'] += $diff;
			if ($to_data['right_id'] > $from_data['right_id'])
			{
				$diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
			}
			else
			{
				$diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
			}
		}
		else
		{
			$sql = 'SELECT MAX(right_id) AS right_id
				FROM ' . $this->modules_table . "
				WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
					AND " . $this->db->sql_in_set('module_id', $moved_ids, true);
			$result = $this->db->sql_query($sql);
			$row = $this->db->sql_fetchrow($result);
			$this->db->sql_freeresult($result);

			$diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
		}

		$sql = 'UPDATE ' . $this->modules_table . "
			SET left_id = left_id $diff, right_id = right_id $diff
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND " . $this->db->sql_in_set('module_id', $moved_ids);
		$this->db->sql_query($sql);
	}

	/**
	 * Remove module from tree
	 *
	 * @param int		$module_id		ID of the module to delete
	 * @param string	$module_class	Class of the module (acp, ucp, mcp etc...)
	 *
	 * @throws \phpbb\module\exception\module_exception	When the specified module cannot be removed
	 */
	public function delete_module($module_id, $module_class)
	{
		$module_id = (int) $module_id;

		$row = $this->get_module_row($module_id, $module_class);

		$branch = $this->get_module_branch($module_id, $module_class, 'children', false);

		if (count($branch))
		{
			throw new module_exception('CANNOT_REMOVE_MODULE');
		}

		// If not move
		$diff = 2;
		$sql = 'DELETE FROM ' . $this->modules_table . "
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND module_id = $module_id";
		$this->db->sql_query($sql);

		$row['right_id'] = (int) $row['right_id'];
		$row['left_id'] = (int) $row['left_id'];

		// Resync tree
		$sql = 'UPDATE ' . $this->modules_table . "
			SET right_id = right_id - $diff
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
		$this->db->sql_query($sql);

		$sql = 'UPDATE ' . $this->modules_table . "
			SET left_id = left_id - $diff, right_id = right_id - $diff
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND left_id > {$row['right_id']}";
		$this->db->sql_query($sql);
	}

	/**
	 * Move module position by $steps up/down
	 *
	 * @param array		$module_row		Array of module data
	 * @param string	$module_class	Class of the module (acp, ucp, mcp etc...)
	 * @param string	$action			Direction of moving (valid values: move_up or move_down)
	 * @param int		$steps			Number of steps to move module
	 *
	 * @return string	Returns the language name of the module
	 *
	 * @throws \phpbb\module\exception\module_not_found_exception	When the specified module does not exists
	 */
	public function move_module_by($module_row, $module_class, $action = 'move_up', $steps = 1)
	{
		/**
		 * Fetch all the siblings between the module's current spot
		 * and where we want to move it to. If there are less than $steps
		 * siblings between the current spot and the target then the
		 * module will move as far as possible
		 */
		$sql = 'SELECT module_id, left_id, right_id, module_langname
			FROM ' . $this->modules_table . "
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND parent_id = " . (int) $module_row['parent_id'] . '
				AND ' . (($action == 'move_up') ? 'right_id < ' . (int) $module_row['right_id'] . ' ORDER BY right_id DESC' : 'left_id > ' . (int) $module_row['left_id'] . ' ORDER BY left_id ASC');
		$result = $this->db->sql_query_limit($sql, $steps);

		$target = array();
		while ($row = $this->db->sql_fetchrow($result))
		{
			$target = $row;
		}
		$this->db->sql_freeresult($result);

		if (!count($target))
		{
			// The module is already on top or bottom
			throw new module_not_found_exception();
		}

		/**
		 * $left_id and $right_id define the scope of the nodes that are affected by the move.
		 * $diff_up and $diff_down are the values to substract or add to each node's left_id
		 * and right_id in order to move them up or down.
		 * $move_up_left and $move_up_right define the scope of the nodes that are moving
		 * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
		 */
		if ($action == 'move_up')
		{
			$left_id = (int) $target['left_id'];
			$right_id = (int) $module_row['right_id'];

			$diff_up = (int) ($module_row['left_id'] - $target['left_id']);
			$diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);

			$move_up_left = (int) $module_row['left_id'];
			$move_up_right = (int) $module_row['right_id'];
		}
		else
		{
			$left_id = (int) $module_row['left_id'];
			$right_id = (int) $target['right_id'];

			$diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
			$diff_down = (int) ($target['right_id'] - $module_row['right_id']);

			$move_up_left = (int) ($module_row['right_id'] + 1);
			$move_up_right = (int) $target['right_id'];
		}

		// Now do the dirty job
		$sql = 'UPDATE ' . $this->modules_table . "
			SET left_id = left_id + CASE
				WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
				ELSE {$diff_down}
			END,
			right_id = right_id + CASE
				WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
				ELSE {$diff_down}
			END
			WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
				AND left_id BETWEEN {$left_id} AND {$right_id}
				AND right_id BETWEEN {$left_id} AND {$right_id}";
		$this->db->sql_query($sql);

		$this->remove_cache_file($module_class);

		return $target['module_langname'];
	}
}