1<?php
2
3/**
4 * Helper class for Administration backend.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @author    Anatoliy Belsky <anatoliy.belsky@mayflower.de>
13 * @copyright 2010-2020 phpMyFAQ Team
14 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15 * @link      https://www.phpmyfaq.de
16 * @since     2010-01-19
17 */
18
19namespace phpMyFAQ\Helper;
20
21use phpMyFAQ\User;
22
23/**
24 * Class Administration
25 *
26 * @package phpMyFAQ\Helper
27 */
28class AdministrationHelper
29{
30    /**
31     * Array with permissions.
32     *
33     * @var array
34     */
35    private $permission = [];
36
37    /**
38     * Adds a menu entry according to user permissions.
39     * ',' stands for 'or', '*' stands for 'and'.
40     *
41     * @param string $restrictions Restrictions
42     * @param string $action       Action parameter
43     * @param string $caption      Caption
44     * @param string $active       Active
45     * @param bool   $checkPerm    Check permission (default: true)
46     *
47     * @return string
48     */
49    public function addMenuEntry(
50        string $restrictions = '',
51        string $action = '',
52        string $caption = '',
53        $active = '',
54        bool $checkPerm = true
55    ): string {
56        global $PMF_LANG;
57
58        if ($action != '') {
59            $action = 'action=' . $action;
60        }
61
62        if (isset($PMF_LANG[$caption])) {
63            $renderedCaption = $PMF_LANG[$caption];
64        } else {
65            $renderedCaption = 'No string for ' . $caption;
66        }
67
68        $output = sprintf(
69            '<a class="collapse-item" href="?%s">%s</a>%s',
70            $action,
71            $renderedCaption,
72            "\n"
73        );
74
75        if ($checkPerm) {
76            return $this->evaluatePermission($restrictions) ? $output : '';
77        } else {
78            return $output;
79        }
80    }
81
82    /**
83     * Parse and check a permission string.
84     *
85     * Permissions are glued with each other as follows
86     * - '+' stands for 'or'
87     * - '*' stands for 'and'
88     *
89     * No braces will be parsed, only simple expressions
90     *
91     * @param string $restrictions
92     *
93     * @return  bool
94     * @example right1*right2+right3+right4*right5
95     */
96    private function evaluatePermission(string $restrictions): bool
97    {
98        if (false !== strpos($restrictions, '+')) {
99            $hasPermission = false;
100            foreach (explode('+', $restrictions) as $restriction) {
101                $hasPermission = $hasPermission || $this->evaluatePermission($restriction);
102                if ($hasPermission) {
103                    break;
104                }
105            }
106        } elseif (false !== strpos($restrictions, '*')) {
107            $hasPermission = true;
108            foreach (explode('*', $restrictions) as $restriction) {
109                if (!isset($this->permission[$restriction]) || !$this->permission[$restriction]) {
110                    $hasPermission = false;
111                    break;
112                }
113            }
114        } else {
115            $hasPermission = strlen($restrictions) > 0 &&
116                isset($this->permission[$restrictions]) &&
117                $this->permission [$restrictions];
118        }
119
120        return $hasPermission;
121    }
122
123    /**
124     * Setter for permission array.
125     *
126     * @param User $user
127     */
128    public function setUser(User $user): void
129    {
130        // read all rights, set them FALSE
131        $allRights = $user->perm->getAllRightsData();
132        foreach ($allRights as $right) {
133            $this->permission[$right['name']] = false;
134        }
135        // check user rights, set them TRUE
136        $allUserRights = $user->perm->getAllUserRights($user->getUserId());
137        if (false !== $allUserRights) {
138            foreach ($allRights as $right) {
139                if (in_array($right['right_id'], $allUserRights)) {
140                    $this->permission[$right['name']] = true;
141                }
142            }
143        }
144        // If user is super admin, give all rights
145        if ($user->isSuperAdmin()) {
146            foreach ($allRights as $right) {
147                $this->permission[$right['name']] = true;
148            }
149        }
150    }
151
152    /**
153     * @param string $metaRobots
154     *
155     * @return string
156     */
157    public function renderMetaRobotsDropdown(string $metaRobots): string
158    {
159        $html = '';
160        $values = [
161            'index, follow',
162            'index, nofollow',
163            'noindex, follow',
164            'noindex, nofollow',
165        ];
166
167        foreach ($values as $value) {
168            $html .= sprintf(
169                '<option%s>%s</option>',
170                ($value === $metaRobots) ? ' selected' : '',
171                $value
172            );
173        }
174
175        return $html;
176    }
177}
178