1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8/**
9 * \brief Show current permissions in a convenient way
10 * \author zaufi <zaufi@sendmail.ru>
11 */
12require_once('lib/debug/debugger-ext.php');
13
14/**
15 * \brief Show current permissions in a convenient way
16 */
17class DbgPermissions extends DebuggerCommand
18{
19	/// \b Must have function to announce command name in debugger console
20	function name()
21	{
22		return 'perm';
23	}
24
25	/// \b Must have function to provide help to debugger console
26	function description()
27	{
28		return 'Show current permissions in a convenient way';
29	}
30
31	/// \b Must have function to provide help to debugger console
32	function syntax()
33	{
34		return 'perm [partial-name]';
35	}
36
37	/// \b Must have function to show example of usage of given command
38	function example()
39	{
40		return 'perm' . "\n" . 'perm admin' . "\n" . 'perm .*_comments$';
41	}
42
43	/// Execute command with given set of arguments.
44	function execute($params)
45	{
46		$userlib = TikiLib::lib('user');
47		$smarty = TikiLib::lib('smarty');
48
49		$this->set_result_type(TPL_RESULT);
50
51		$this->set_result_tpl('debug/tiki-debug_permissions.tpl');
52		// Is regex to match against var name given?
53		$p = explode(' ', trim($params));
54		$mask = count($p) > 0 ? str_replace('$', '', trim($p[0])) : '';
55		// Get list of all vars
56		$tpl_vars = $smarty->getTemplateVars();
57		// Get descriptions for all permissions
58		$pd = $userlib->get_permissions();
59		$descriptions = [];
60
61		foreach ($pd['data'] as $p) {
62			$descriptions[$p['permName']] = $p['permDesc'];
63		}
64
65		// convert to vector of names, filter permissions only
66		$perms = [];
67		$len = strlen($mask);
68
69		foreach ($tpl_vars as $key => $val) {
70			if ((! $len || $len && preg_match('/' . $mask . '/', $key)) && preg_match('/tiki_p_/', $key)) {
71				$perms[] = [
72					'name' => $key,
73					'value' => $val,
74					'description' => isset($descriptions[$key]) ? $descriptions[$key] : 'No description'
75				];
76			}
77		}
78
79		return $perms;
80	}
81}
82
83/// Class factory to create instances of defined commands
84function dbg_command_factory_perm()
85{
86	return new DbgPermissions();
87}
88