Passed
Pull Request — master (#70)
by Pierre-Henry
03:15
created

class/PermissionHandler.php (10 issues)

1
<?php namespace XoopsModules\Newbb;
2
3
/**
4
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
use XoopsModules\Newbb;
14
15
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
16
17
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
18
define('NEWBB_HANDLER_PERMISSION', 1);
19
20
// Initializing XoopsGroupPermHandler if not loaded yet
21
if (!class_exists('XoopsGroupPermHandler')) {
22
    require_once $GLOBALS['xoops']->path('kernel/groupperm.php');
23
}
24
25
/**
26
 * Class PermissionHandler
27
 */
28
class PermissionHandler extends \XoopsGroupPermHandler
0 ignored issues
show
The type XoopsGroupPermHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
{
30
    /** @var \Xmf\Module\Helper\Cache */
31
    protected $cacheHelper;
32
33
    /** @var array */
34
    private $_handler;
35
36
    /**
37
     * @param $db
38
     */
39
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
    {
41
        $this->cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
42
43
        $this->db = $db;
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        parent::__construct($db);
45
    }
46
47
    /**
48
     * @param $name
49
     * @return mixed
50
     */
51
    public function loadHandler($name)
52
    {
53
        if (!isset($this->_handler[$name])) {
54
//            require_once __DIR__ . "/permission.{$name}.php";
55
            $className             = '\\XoopsModules\\Newbb\\Permission' . ucfirst($name) . 'Handler';
56
            $this->_handler[$name] = new $className($this->db);
57
        }
58
59
        return $this->_handler[$name];
60
    }
61
62
    /**
63
     * @param  bool $fullname
64
     * @return mixed
65
     */
66
    public function getValidForumPerms($fullname = false)
67
    {
68
        $handler = $this->loadHandler('forum');
69
70
        return $handler->getValidPerms($fullname);
71
    }
72
73
    /**
74
     * @param  int  $forum
75
     * @param  bool $topic_locked
76
     * @param  bool $isAdmin
77
     * @return mixed
78
     */
79
    public function getPermissionTable($forum = 0, $topic_locked = false, $isAdmin = false)
80
    {
81
        $handler = $this->loadHandler('forum');
82
        $perm    = $handler->getPermissionTable($forum, $topic_locked, $isAdmin);
83
84
        return $perm;
85
    }
86
87
    /**
88
     * @param $forum_id
89
     * @return mixed
90
     */
91
    public function deleteByForum($forum_id)
92
    {
93
        $this->cacheHelper->delete('permission_forum');
94
        $handler = $this->loadHandler('forum');
95
96
        return $handler->deleteByForum($forum_id);
97
    }
98
99
    /**
100
     * @param $cat_id
101
     * @return mixed
102
     */
103
    public function deleteByCategory($cat_id)
104
    {
105
        $this->cacheHelper->delete('permission_category');
106
        $handler = $this->loadHandler('category');
107
108
        return $handler->deleteByCategory($cat_id);
109
    }
110
111
    /**
112
     * @param        $category
113
     * @param  array $groups
114
     * @return mixed
115
     */
116
    public function setCategoryPermission($category, array $groups = [])
117
    {
118
        $this->cacheHelper->delete('permission_category');
119
        $handler = $this->loadHandler('category');
120
121
        return $handler->setCategoryPermission($category, $groups);
122
    }
123
124
    /**
125
     * @param         $type
126
     * @param  string $gperm_name
127
     * @param  int    $id
128
     * @return bool
129
     */
130
    public function getPermission($type, $gperm_name = 'access', $id = 0)
131
    {
132
        global $xoopsModule;
133
        $ret = false;
134
        if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $xoopsModule->getVar('dirname')) {
135
            $ret = true;
136
        }
137
138
        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
0 ignored issues
show
The constant XoopsModules\Newbb\XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
139
        if (!$groups) {
140
            $ret = false;
141
        }
142
        if (!$allowed_groups = $this->getGroups("{$type}_{$gperm_name}", $id)) {
143
            $ret = false;
144
        }
145
146
        if (count(array_intersect($allowed_groups, $groups)) > 0) {
147
            $ret = true;
148
        }
149
150
        return $ret;
151
    }
152
153
    /**
154
     * @param  string $perm_name
155
     * @return array
156
     */
157
    public function &getCategories($perm_name = 'access')
158
    {
159
        $ret = $this->getAllowedItems('category', "category_{$perm_name}");
160
161
        return $ret;
162
    }
163
164
    /**
165
     * @param  string $perm_name
166
     * @return array
167
     */
168
    public function getForums($perm_name = 'access')
169
    {
170
        $ret = $this->getAllowedItems('forum', "forum_{$perm_name}");
171
172
        return $ret;
173
    }
174
175
    /**
176
     * @param $type
177
     * @param $perm_name
178
     * @return array
179
     */
180
    public function getAllowedItems($type, $perm_name)
181
    {
182
        $ret = [];
183
184
        $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [XOOPS_GROUP_ANONYMOUS];
0 ignored issues
show
The constant XoopsModules\Newbb\XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
185
        if (count($groups) < 1) {
186
            return $ret;
187
        }
188
189
        if (!$_cachedPerms = $this->loadPermData($perm_name, $type)) {
190
            return $ret;
191
        }
192
193
        $allowed_items = [];
194
        foreach ($_cachedPerms as $id => $allowed_groups) {
195
            if (0 == $id || empty($allowed_groups)) {
196
                continue;
197
            }
198
199
            if (array_intersect($groups, $allowed_groups)) {
200
                $allowed_items[$id] = 1;
201
            }
202
        }
203
        unset($_cachedPerms);
204
        $ret = array_keys($allowed_items);
205
206
        return $ret;
207
    }
208
209
    /**
210
     * @param        $gperm_name
211
     * @param  int   $id
212
     * @return array
213
     */
214
    public function getGroups($gperm_name, $id = 0)
215
    {
216
        $_cachedPerms = $this->loadPermData($gperm_name);
217
        $groups       = empty($_cachedPerms[$id]) ? [] : array_unique($_cachedPerms[$id]);
218
        unset($_cachedPerms);
219
220
        return $groups;
221
    }
222
223
    /**
224
     * @param  string $perm_name
225
     * @return array
226
     */
227
    public function createPermData($perm_name = 'forum_all')
228
    {
229
        global $xoopsModule;
230
        /** @var \XoopsModuleHandler $moduleHandler */
231
        $perms = [];
232
233
        if (is_object($xoopsModule) && 'newbb' === $xoopsModule->getVar('dirname')) {
234
            $modid = $xoopsModule->getVar('mid');
235
        } else {
236
            $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
            $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
237
            $module        = $moduleHandler->getByDirname('newbb');
238
            $modid         = $module->getVar('mid');
239
            unset($module);
240
        }
241
242
        if (in_array($perm_name, ['forum_all', 'category_all'], true)) {
243
            /** @var \XoopsMemberHandler $memberHandler */
244
            $memberHandler = xoops_getHandler('member');
245
            $groups        = array_keys($memberHandler->getGroupList());
246
247
            $type          = ('category_all' === $perm_name) ? 'category' : 'forum';
248
            $objectHandler = Newbb\Helper::getInstance()->getHandler($type);
249
            $object_ids    = $objectHandler->getIds();
250
            foreach ($object_ids as $item_id) {
251
                $perms[$perm_name][$item_id] = $groups;
252
            }
253
        } else {
254
            $gpermHandler = xoops_getHandler('groupperm');
255
            $criteria     = new \CriteriaCompo(new \Criteria('gperm_modid', $modid));
0 ignored issues
show
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
256
            if (!empty($perm_name) && 'forum_all' !== $perm_name && 'category_all' !== $perm_name) {
257
                $criteria->add(new \Criteria('gperm_name', $perm_name));
258
            }
259
            $permissions = $this->getObjects($criteria);
260
261
            foreach ($permissions as $gperm) {
262
                $item_id                                         = $gperm->getVar('gperm_itemid');
263
                $group_id                                        = (int)$gperm->getVar('gperm_groupid');
264
                $perms[$gperm->getVar('gperm_name')][$item_id][] = $group_id;
265
            }
266
        }
267
        if (count($perms) > 0) {
268
            foreach (array_keys($perms) as $perm) {
269
                $this->cacheHelper->write("permission_{$perm}", $perms[$perm]);
270
            }
271
        }
272
        $ret = !empty($perm_name) ? @$perms[$perm_name] : $perms;
273
274
        return $ret;
275
    }
276
277
    /**
278
     * @param  string $perm_name
279
     * @return array|mixed|null
280
     */
281
    public function &loadPermData($perm_name = 'forum_access')
282
    {
283
        if (!$perms = $this->cacheHelper->read("permission_{$perm_name}")) {
284
            $perms = $this->createPermData($perm_name);
285
        }
286
287
        return $perms;
288
    }
289
290
    /**
291
     * @param       $perm
292
     * @param       $itemid
293
     * @param       $groupid
294
     * @param  null $mid
295
     * @return bool
296
     */
297
    public function validateRight($perm, $itemid, $groupid, $mid = null)
298
    {
299
        if (empty($mid)) {
300
            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
301
                $mid = $GLOBALS['xoopsModule']->getVar('mid');
302
            } else {
303
                /** @var \XoopsModuleHandler $moduleHandler */
304
                $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

304
                $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
305
                $mod           = $moduleHandler->getByDirname('newbb');
306
                $mid           = $mod->getVar('mid');
307
                unset($mod);
308
            }
309
        }
310
        if ($this->myCheckRight($perm, $itemid, $groupid, $mid)) {
311
            return true;
312
        }
313
        $this->cacheHelper->delete('permission');
314
        $this->addRight($perm, $itemid, $groupid, $mid);
315
316
        return true;
317
    }
318
319
    /**
320
     * Check permission (directly)
321
     *
322
     * @param string $gperm_name   Name of permission
323
     * @param int    $gperm_itemid ID of an item
324
     * @param        int           /array $gperm_groupid A group ID or an array of group IDs
325
     * @param int    $gperm_modid  ID of a module
326
     *
327
     * @return bool TRUE if permission is enabled
328
     */
329
    public function myCheckRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
330
    {
331
        $ret      = false;
332
        $criteria = new \CriteriaCompo(new \Criteria('gperm_modid', $gperm_modid));
333
        $criteria->add(new \Criteria('gperm_name', $gperm_name));
334
        $gperm_itemid = (int)$gperm_itemid;
335
        if ($gperm_itemid > 0) {
336
            $criteria->add(new \Criteria('gperm_itemid', $gperm_itemid));
337
        }
338
        if (is_array($gperm_groupid)) {
339
            $criteria2 = new \CriteriaCompo();
340
            foreach ($gperm_groupid as $gid) {
341
                $criteria2->add(new \Criteria('gperm_groupid', $gid), 'OR');
342
            }
343
            $criteria->add($criteria2);
344
        } else {
345
            $criteria->add(new \Criteria('gperm_groupid', $gperm_groupid));
346
        }
347
        if ($this->getCount($criteria) > 0) {
348
            $ret = true;
349
        }
350
351
        return $ret;
352
    }
353
354
    /**
355
     * @param       $perm
356
     * @param       $itemid
357
     * @param       $groupid
358
     * @param  null $mid
359
     * @return bool
360
     */
361
    public function deleteRight($perm, $itemid, $groupid, $mid = null)
362
    {
363
        $this->cacheHelper->delete('permission');
364
        if (null === $mid) {
365
            if (is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
366
                $mid = $GLOBALS['xoopsModule']->getVar('mid');
367
            } else {
368
                /** @var \XoopsModuleHandler $moduleHandler */
369
                $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

369
                $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
370
                $mod           = $moduleHandler->getByDirname('newbb');
371
                $mid           = $mod->getVar('mid');
372
                unset($mod);
373
            }
374
        }
375
        if (is_callable('parent::deleteRight')) {
376
            return parent::deleteRight($perm, $itemid, $groupid, $mid);
377
        } else {
378
            $criteria = new \CriteriaCompo(new \Criteria('gperm_name', $perm));
379
            $criteria->add(new \Criteria('gperm_groupid', $groupid));
380
            $criteria->add(new \Criteria('gperm_itemid', $itemid));
381
            $criteria->add(new \Criteria('gperm_modid', $mid));
382
            $permsObject =& $this->getObjects($criteria);
383
            if (!empty($permsObject)) {
384
                foreach ($permsObject as $permObject) {
385
                    $this->delete($permObject);
386
                }
387
            }
388
            unset($criteria, $permsObject);
389
        }
390
391
        return true;
392
    }
393
394
    /**
395
     * @param        $forum
396
     * @param  int   $mid
397
     * @return mixed
398
     */
399
    public function applyTemplate($forum, $mid = 0)
400
    {
401
        $this->cacheHelper->delete('permission_forum');
402
        $handler = $this->loadHandler('forum');
403
404
        return $handler->applyTemplate($forum, $mid);
405
    }
406
407
    /**
408
     * @return mixed
409
     */
410
    public function getTemplate()
411
    {
412
        $handler  = $this->loadHandler('forum');
413
        $template = $handler->getTemplate();
414
415
        return $template;
416
    }
417
418
    /**
419
     * @param $perms
420
     * @return mixed
421
     */
422
    public function setTemplate($perms)
423
    {
424
        $handler = $this->loadHandler('forum');
425
426
        return $handler->setTemplate($perms);
427
    }
428
}
429