phpBB
Statistics
| Revision:

root / trunk / phpBB / includes / functions_module.php

History | View | Annotate | Download (24.7 kB)

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