Passed
Branch master (410c7b)
by Michael
03:30
created

SuicoController::getPermissions()   F

Complexity

Conditions 14
Paths 1920

Size

Total Lines 95
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 95
rs 2.1
c 1
b 0
f 0
cc 14
nc 1920
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
use Criteria;
18
use CriteriaCompo;
19
use Xmf\Request;
20
use XoopsDatabase;
21
use XoopsUser;
22
use XoopsUserHandler;
23
24
/**
25
 * @category        Module
26
 * @package         suico
27
 * @copyright       {@link https://xoops.org/ XOOPS Project}
28
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
29
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
30
 */
31
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
32
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
33
require_once XOOPS_ROOT_PATH . '/class/criteria.php';
34
require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
35
/**
36
 * Module classes
37
 */
38
//require_once __DIR__ . '/Image.php';
39
//require_once __DIR__ . '/Visitors.php';
40
//require_once __DIR__ . '/Video.php';
41
//require_once __DIR__ . '/Audio.php';
42
//require_once __DIR__ . '/Friendrequest.php';
43
//require_once __DIR__ . '/Friendship.php';
44
//require_once __DIR__ . '/Relgroupuser.php';
45
//require_once __DIR__ . '/Groups.php';
46
//require_once __DIR__ . '/Notes.php';
47
//require_once __DIR__ . '/Configs.php';
48
//require_once __DIR__ . '/Suspensions.php';
49
//if (str_replace('.', '', PHP_VERSION) > 499) {
50
//    require_once __DIR__ . '/Id3v1.php';
51
//}
52
53
/**
54
 * Class SuicoController
55
 */
56
class SuicoController extends \XoopsObject
57
{
58
    public $db;
59
60
    public $user;
61
62
    public $isOwner;
63
64
    public $isUser;
65
66
    public $isAnonym;
67
68
    public $isFriend;
69
70
    public $uidOwner;
71
72
    public $nameOwner;
73
74
    public $owner;
75
76
    public $albumFactory;
77
78
    public $visitorsFactory;
79
80
    public $audioFactory;
81
82
    public $videosFactory;
83
84
    public $friendrequestFactory;
85
86
    public $friendshipsFactory;
87
88
    public $relgroupusersFactory;
89
90
    public $suspensionsFactory;
91
92
    public $groupsFactory;
93
94
    public $notesFactory;
95
96
    public $configsFactory;
97
98
    public $section;
99
100
    public $privilegeLevel;
101
102
    public $isSuspended;
103
104
    public $helper;
105
106
    public $isSelfRequest;
107
108
    public $isOtherRequest;
109
110
    /**
111
     * Constructor
112
     *
113
     * @param \XoopsDatabase $xoopsDatabase
114
     * @param                $user
115
     */
116
117
    public function __construct(
118
        XoopsDatabase $xoopsDatabase,
119
        $user
120
    ) {
121
        $this->helper = Helper::getInstance();
122
123
        $this->db = $xoopsDatabase;
124
125
        $this->user = $user;
126
127
        $this->isOwner = 0;
128
129
        $this->isAnonym = 1;
130
131
        $this->isFriend = 0;
132
133
        $this->isUser = 0;
134
135
        $this->isSelfRequest = 0;
136
137
        $this->isOtherRequest = 0;
138
139
        $this->createFactories();
140
141
        $this->getPermissions();
142
143
        $this->checkPrivilege('');
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Suico\Suico...oller::checkPrivilege() has too many arguments starting with ''. ( Ignorable by Annotation )

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

143
        $this->/** @scrutinizer ignore-call */ 
144
               checkPrivilege('');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
144
145
        $this->checkSuspension();
146
    }
147
148
    public function checkSuspension()
149
    {
150
        $criteria_suspended = new Criteria('uid', $this->uidOwner);
151
152
        if (1 === $this->isSuspended) {
153
            $suspensions = $this->suspensionsFactory->getObjects($criteria_suspended);
154
155
            $suspension = $suspensions[0];
156
157
            if (\time() > $suspension->getVar('suspension_time')) {
158
                $suspension = $this->suspensionsFactory->create(false);
159
160
                $suspension->load($this->uidOwner);
0 ignored issues
show
Bug introduced by
The method load() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Suico\Video or XoopsModules\Suico\Suspensions or XoopsModules\Suico\Notes or XoopsModules\Suico\Image or XoopsModules\Suico\Friendship or XoopsComments or XoopsModules\Suico\Visitors or XoopsModules\Suico\Relgroupuser or XoopsModules\Suico\Groups or XoopsModules\Suico\Configs or XoopsModules\Suico\Friendrequest or XoopsModules\Suico\Ishot or XoopsModules\Suico\Audio. ( Ignorable by Annotation )

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

160
                $suspension->/** @scrutinizer ignore-call */ 
161
                             load($this->uidOwner);
Loading history...
161
162
                $this->owner->setVar('email', $suspension->getVar('old_email', 'n'));
163
164
                $this->owner->setVar('pass', $suspension->getVar('old_pass', 'n'));
165
166
                $this->owner->setVar('user_sig', $suspension->getVar('old_signature', 'n'));
167
168
                $userHandler = new XoopsUserHandler($this->db);
169
170
                $userHandler->insert($this->owner, true);
171
172
                $criteria = new Criteria('uid', $this->uidOwner);
173
174
                $this->suspensionsFactory->deleteAll($criteria);
175
            }
176
        }
177
    }
178
179
    public function checkPrivilege()
180
    {
181
    }
182
183
    /**
184
     * Checkinf privilege levels
185
     *
186
     * @param int $privilegeNeeded 0 anonym 1 member 2 friend 3 owner
187
     * @return bool true if privilege enough
188
     */
189
190
    public function checkPrivilegeLevel(
191
        $privilegeNeeded = 0
192
    ) {
193
        return $privilegeNeeded <= $this->privilegeLevel;
194
    }
195
196
    /**
197
     * Set permissions according to user is logged or not , is owner or not etc..
198
     */
199
200
    public function getPermissions()
201
    {
202
        global $_GET, $xoopsUser;
203
204
        /**
205
         * @desc Check if the user uid exists if not redirect back to where he was
206
         */
207
208
        if (!empty($_GET['uid'])) {
209
            /** @var \XoopsMemberHandler $memberHandler */
210
211
            $memberHandler = \xoops_getHandler('member');
212
213
            $user = $memberHandler->getUser(Request::getInt('uid', 0, 'GET'));
214
215
            if (!\is_object($user)) {
216
                \redirect_header('index.php', 3, \_MD_SUICO_USER_DOESNTEXIST);
217
            }
218
        }
219
220
        /**
221
         * If anonym and uid not set then redirect to admins profile
222
         * Else redirects to own profile
223
         */
224
225
        if (empty($this->user)) {
226
            $this->isAnonym = 1;
227
228
            $this->isUser = 0;
229
230
            if (!empty($_GET['uid'])) {
231
                $this->uidOwner = Request::getInt('uid', 0, 'GET');
232
            } else {
233
                $this->uidOwner = 1;
234
235
                $this->isOwner = 0;
236
            }
237
        } else {
238
            $this->isAnonym = 0;
239
240
            $this->isUser = 1;
241
242
            if (!empty($_GET['uid'])) {
243
                $this->uidOwner = Request::getInt('uid', 0, 'GET');
244
245
                $this->isOwner = $this->user->getVar('uid') === Request::getInt('uid', 0, 'GET') ? 1 : 0;
246
            } else {
247
                $this->uidOwner = $this->user->getVar('uid');
248
249
                $this->isOwner = 1;
250
            }
251
        }
252
253
        $this->owner = new XoopsUser($this->uidOwner);
254
255
        $criteria_suspended = new Criteria('uid', $this->uidOwner);
256
257
        $this->isSuspended = $this->suspensionsFactory->getCount($criteria_suspended) > 0 ? 1 : 0;
258
259
        if ('' === $this->owner->getVar('name')) {
260
            $this->nameOwner = $this->owner->getVar('uname');
261
        } else {
262
            $this->nameOwner = $this->owner->getVar('name');
263
        }
264
265
        //isFriend?
266
267
        $criteria_friends = new Criteria('friend1_uid', $this->uidOwner);
268
269
        if (!$xoopsUser) {
270
            $this->isFriend = 0;
271
        } else {
272
            $criteria_isFriend = new CriteriaCompo(new Criteria('friend2_uid', $this->user->getVar('uid')));
273
274
            $criteria_isFriend->add($criteria_friends);
275
276
            $this->isFriend = $this->friendshipsFactory->getCount($criteria_isFriend);
277
        }
278
279
        $this->privilegeLevel = 0;
280
281
        if (1 === $this->isAnonym) {
282
            $this->privilegeLevel = 0;
283
        }
284
285
        if (1 === $this->isUser) {
286
            $this->privilegeLevel = 1;
287
        }
288
289
        if (1 === $this->isFriend) {
290
            $this->privilegeLevel = 2;
291
        }
292
293
        if (1 === $this->isOwner) {
294
            $this->privilegeLevel = 3;
295
        }
296
    }
297
298
    /**
299
     * Get for each section the number of objects the user possess
300
     *
301
     * @return array(countGroups=>"",countPhotos=>"",countFriends=>"",countGroups=>"")
302
     */
303
304
    public function getNumbersSections()
305
    {
306
        $criteriaGroups = new Criteria('rel_user_uid', $this->uidOwner);
307
308
        $nbSections['countGroups'] = $this->relgroupusersFactory->getCount($criteriaGroups);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$nbSections was never initialized. Although not strictly required by PHP, it is generally a good practice to add $nbSections = array(); before regardless.
Loading history...
309
310
        $criteriaUid = new Criteria('uid_owner', $this->uidOwner);
311
312
        $criteriaAlbum = new CriteriaCompo($criteriaUid);
313
314
        if (0 === $this->isOwner) {
315
            $criteriaPrivate = new Criteria('private', 0);
316
317
            $criteriaAlbum->add($criteriaPrivate);
318
        }
319
320
        $nbSections['countPhotos'] = $this->albumFactory->getCount($criteriaAlbum);
321
322
        $criteriaFriends = new Criteria('friend1_uid', $this->uidOwner);
323
324
        $nbSections['countFriends'] = $this->friendshipsFactory->getCount($criteriaFriends);
325
326
        $criteriaUidAudio = new Criteria('uid_owner', $this->uidOwner);
327
328
        $nbSections['countAudios'] = $this->audioFactory->getCount($criteriaUidAudio);
329
330
        $criteriaUidVideo = new Criteria('uid_owner', $this->uidOwner);
331
332
        $nbSections['countGroups'] = $this->videosFactory->getCount($criteriaUidVideo);
333
334
        $criteriaUidNotes = new Criteria('note_to', $this->uidOwner);
335
336
        $nbSections['countNotes'] = $this->notesFactory->getCount($criteriaUidNotes);
337
338
        return $nbSections;
339
    }
340
341
    /**
342
     * This creates the module factories
343
     */
344
345
    public function createFactories()
346
    {
347
        $this->albumFactory = new ImageHandler($this->db);
348
349
        $this->visitorsFactory = new VisitorsHandler($this->db);
350
351
        $this->audioFactory = new AudioHandler($this->db);
352
353
        $this->videosFactory = new VideoHandler($this->db);
354
355
        $this->friendrequestFactory = new FriendrequestHandler($this->db);
356
357
        $this->friendshipsFactory = new FriendshipHandler($this->db);
358
359
        $this->relgroupusersFactory = new RelgroupuserHandler($this->db);
360
361
        $this->notesFactory = new NotesHandler($this->db);
362
363
        $this->groupsFactory = new GroupsHandler($this->db);
364
365
        $this->configsFactory = new ConfigsHandler($this->db);
366
367
        $this->suspensionsFactory = new SuspensionsHandler($this->db);
368
    }
369
370
    /**
371
     * @param $section
372
     * @return int
373
     */
374
375
    public function checkPrivilegeBySection($section)
376
    {
377
        global $xoopsModuleConfig;
378
379
        $configsectionname = 'enable_' . $section;
380
381
        if (\array_key_exists($configsectionname, $xoopsModuleConfig)) {
382
            if (0 === $this->helper->getConfig($configsectionname)) {
383
                return -1;
384
            }
385
        }
386
387
        //  if ($section=="Notes" && $xoopsModuleConfig['enable_notes']==0){
388
389
        //          return false;
390
391
        //      }
392
393
        //      if ($section=="pictures" && $xoopsModuleConfig['enable_pictures']==0){
394
395
        //          return false;
396
397
        //      }
398
399
        //
400
401
        //      if ($section=="pictures" && $xoopsModuleConfig['enable_pictures']==0){
402
403
        //          return false;
404
405
        //      }
406
407
        $criteria = new Criteria('config_uid', $this->owner->getVar('uid'));
0 ignored issues
show
Bug introduced by
It seems like $this->owner->getVar('uid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

407
        $criteria = new Criteria('config_uid', /** @scrutinizer ignore-type */ $this->owner->getVar('uid'));
Loading history...
408
409
        if (1 === $this->configsFactory->getCount($criteria)) {
410
            $configs = $this->configsFactory->getObjects($criteria);
411
412
            $config = $configs[0]->getVar($section);
413
414
            if (!$this->checkPrivilegeLevel($config)) {
415
                return 0;
416
            }
417
        }
418
419
        return 1;
420
    }
421
}
422