Controller::getPermissions()   F
last analyzed

Complexity

Conditions 14
Paths 1920

Size

Total Lines 67
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 46
nc 1920
nop 0
dl 0
loc 67
rs 2.1
c 0
b 0
f 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 declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
use Criteria;
16
use CriteriaCompo;
17
use Xmf\Request;
18
use XoopsUser;
19
use XoopsUserHandler;
20
21
/**
22
 * @category        Module
23
 * @copyright       {@link https://xoops.org/ XOOPS Project}
24
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
26
 */
27
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
28
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
29
require_once XOOPS_ROOT_PATH . '/class/criteria.php';
30
require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
31
32
/**
33
 * Class Controller
34
 */
35
class Controller extends \XoopsObject
36
{
37
    public \XoopsDatabase $db;
38
    public                $user;
39
    public int            $isOwner;
40
    public int            $isUser;
41
    public int            $isAnonym;
42
    public int            $isFriend;
43
    public                $uidOwner;
44
    public $nameOwner;
45
    public $owner;
46
    public $albumFactory;
47
    public $visitorsFactory;
48
    public $audioFactory;
49
    public $videosFactory;
50
    public $friendrequestFactory;
51
    public $friendshipsFactory;
52
    public $relgroupusersFactory;
53
    public $suspensionsFactory;
54
    public $groupsFactory;
55
    public $notesFactory;
56
    public $configsFactory;
57
    public $section;
58
    public $privilegeLevel;
59
    public                $isSuspended;
60
    public Helper         $helper;
61
    public int            $isSelfRequest;
62
    public int            $isOtherRequest;
63
64
    /**
65
     * Constructor
66
     *
67
     * @param \XoopsDatabase $xoopsDatabase
68
     * @param                $user
69
     * @param null           $xoopsModule
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $xoopsModule is correct as it would always require null to be passed?
Loading history...
70
     */
71
    public function __construct(\XoopsDatabase $xoopsDatabase, $user, $xoopsModule = null)
0 ignored issues
show
Unused Code introduced by
The parameter $xoopsModule is not used and could be removed. ( Ignorable by Annotation )

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

71
    public function __construct(\XoopsDatabase $xoopsDatabase, $user, /** @scrutinizer ignore-unused */ $xoopsModule = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
        $this->helper         = Helper::getInstance();
74
        $this->db             = $xoopsDatabase;
75
        $this->user           = $user;
76
        $this->isOwner        = 0;
77
        $this->isAnonym       = 1;
78
        $this->isFriend       = 0;
79
        $this->isUser         = 0;
80
        $this->isSelfRequest  = 0;
81
        $this->isOtherRequest = 0;
82
        $this->createFactories();
83
        $this->getPermissions();
84
        $this->checkPrivilege('');
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Suico\Controller::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

84
        $this->/** @scrutinizer ignore-call */ 
85
               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...
85
        $this->checkSuspension();
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    public function checkSuspension(): void
92
    {
93
        $criteria_suspended = new Criteria('uid', $this->uidOwner);
94
        if (1 === $this->isSuspended) {
95
            $suspensions = $this->suspensionsFactory->getObjects($criteria_suspended);
96
            /** @var Suspensions $suspension */
97
            $suspension = $suspensions[0];
98
            if (\time() > $suspension->getVar('suspension_time')) {
99
                $suspension = $this->suspensionsFactory->create(false);
100
                $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 XoopsBlock 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

100
                $suspension->/** @scrutinizer ignore-call */ 
101
                             load($this->uidOwner);
Loading history...
101
                $this->owner->setVar('email', $suspension->getVar('old_email', 'n'));
102
                $this->owner->setVar('pass', $suspension->getVar('old_pass', 'n'));
103
                $this->owner->setVar('user_sig', $suspension->getVar('old_signature', 'n'));
104
                $userHandler = new XoopsUserHandler($this->db);
105
                $userHandler->insert($this->owner, true);
106
                $criteria = new Criteria('uid', $this->uidOwner);
107
                $this->suspensionsFactory->deleteAll($criteria);
108
            }
109
        }
110
    }
111
112
    /**
113
     * @return void
114
     */
115
    public function checkPrivilege()
116
    {
117
    }
118
119
    /**
120
     * Checkinf privilege levels
121
     *
122
     * @param int $privilegeNeeded 0 anonym 1 member 2 friend 3 owner
123
     * @return bool true if privilege enough
124
     */
125
    public function checkPrivilegeLevel(
126
        $privilegeNeeded = 1
127
    ) {
128
        return $privilegeNeeded <= $this->privilegeLevel;
129
    }
130
131
    /**
132
     * Set permissions according to user is logged or not , is owner or not etc..
133
     */
134
    public function getPermissions(): void
135
    {
136
        global $_GET, $xoopsUser;
137
        /**
138
         * @desc Check if the user uid exists if not redirect back to where he was
139
         */
140
        if (!empty($_GET['uid'])) {
141
            /** @var \XoopsMemberHandler $memberHandler */
142
            $memberHandler = \xoops_getHandler('member');
143
            $user          = $memberHandler->getUser(Request::getInt('uid', 0, 'GET'));
144
            if (!\is_object($user)) {
145
                \redirect_header('index.php', 3, \_MD_SUICO_USER_DOESNTEXIST);
146
            }
147
        }
148
        /**
149
         * If anonymous and uid not set then redirect to admins profile
150
         * Else redirects to own profile
151
         */
152
        if (empty($this->user)) {
153
            $this->isAnonym = 1;
154
            $this->isUser   = 0;
155
            if (!empty($_GET['uid'])) {
156
                $this->uidOwner = Request::getInt('uid', 0, 'GET');
157
            } else {
158
                $this->uidOwner = 1;
159
                $this->isOwner  = 0;
160
            }
161
        } else {
162
            $this->isAnonym = 0;
163
            $this->isUser   = 1;
164
            if (!empty($_GET['uid'])) {
165
                $this->uidOwner = Request::getInt('uid', 0, 'GET');
166
                $this->isOwner  = $this->user->getVar('uid') === Request::getInt('uid', 0, 'GET') ? 1 : 0;
167
            } else {
168
                $this->uidOwner = $this->user->getVar('uid');
169
                $this->isOwner  = 1;
170
            }
171
        }
172
        $this->owner        = new XoopsUser($this->uidOwner);
173
        $criteria_suspended = new Criteria('uid', $this->uidOwner);
174
        $this->isSuspended  = $this->suspensionsFactory->getCount($criteria_suspended) > 0 ? 1 : 0;
175
        if ('' === $this->owner->getVar('name')) {
176
            $this->nameOwner = $this->owner->getVar('uname');
177
        } else {
178
            $this->nameOwner = $this->owner->getVar('name');
179
        }
180
        //isFriend?
181
        $criteria_friends = new Criteria('friend1_uid', $this->uidOwner);
182
        if ($xoopsUser) {
183
            $criteriaIsfriend = new CriteriaCompo(new Criteria('friend2_uid', $this->user->getVar('uid')));
184
            $criteriaIsfriend->add($criteria_friends);
185
            $this->isFriend = $this->friendshipsFactory->getCount($criteriaIsfriend);
186
        } else {
187
            $this->isFriend = 0;
188
        }
189
        $this->privilegeLevel = 1;
190
        if (1 === $this->isAnonym) {
191
            $this->privilegeLevel = 1;
192
        }
193
        if (1 === $this->isUser) {
194
            $this->privilegeLevel = 2;
195
        }
196
        if (1 === $this->isFriend) {
197
            $this->privilegeLevel = 3;
198
        }
199
        if (1 === $this->isOwner) {
200
            $this->privilegeLevel = 4;
201
        }
202
    }
203
204
    /**
205
     * Get for each section the number of objects the user possess
206
     *
207
     * @return array(countGroups=>"",countPhotos=>"",countFriends=>"",countGroups=>"")
208
     */
209
    public function getNumbersSections()
210
    {
211
        $criteriaGroups            = new Criteria('rel_user_uid', $this->uidOwner);
212
        $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...
213
        $criteriaUid               = new Criteria('uid_owner', $this->uidOwner);
214
        $criteriaAlbum             = new CriteriaCompo($criteriaUid);
215
        if (0 === $this->isOwner) {
216
            $criteriaPrivate = new Criteria('private', 0);
217
            $criteriaAlbum->add($criteriaPrivate);
218
        }
219
        $nbSections['countPhotos']  = $this->albumFactory->getCount($criteriaAlbum);
220
        $criteriaFriends            = new Criteria('friend1_uid', $this->uidOwner);
221
        $nbSections['countFriends'] = $this->friendshipsFactory->getCount($criteriaFriends);
222
        $criteriaUidAudio           = new Criteria('uid_owner', $this->uidOwner);
223
        $nbSections['countAudios']  = $this->audioFactory->getCount($criteriaUidAudio);
224
        $criteriaUidVideo           = new Criteria('uid_owner', $this->uidOwner);
225
        $nbSections['countVideos']  = $this->videosFactory->getCount($criteriaUidVideo);
226
        $criteriaUidNotes           = new Criteria('note_to', $this->uidOwner);
227
        $nbSections['countNotes']   = $this->notesFactory->getCount($criteriaUidNotes);
228
229
        return $nbSections;
230
    }
231
232
    /**
233
     * This creates the module factories
234
     */
235
    public function createFactories(): void
236
    {
237
        $this->albumFactory         = new ImageHandler($this->db);
238
        $this->visitorsFactory      = new VisitorsHandler($this->db);
239
        $this->audioFactory         = new AudioHandler($this->db);
240
        $this->videosFactory        = new VideoHandler($this->db);
241
        $this->friendrequestFactory = new FriendrequestHandler($this->db);
242
        $this->friendshipsFactory   = new FriendshipHandler($this->db);
243
        $this->relgroupusersFactory = new RelgroupuserHandler($this->db);
244
        $this->notesFactory         = new NotesHandler($this->db);
245
        $this->groupsFactory        = new GroupsHandler($this->db);
246
        $this->configsFactory       = new ConfigsHandler($this->db);
247
        $this->suspensionsFactory   = new SuspensionsHandler($this->db);
248
    }
249
250
    /**
251
     * @param $section
252
     * @return int
253
     */
254
    public function checkPrivilegeBySection($section)
255
    {
256
        global $xoopsModuleConfig;
257
        $configsectionname = 'enable_' . $section;
258
        if (null !== $xoopsModuleConfig) {
259
            if (\array_key_exists($configsectionname, $xoopsModuleConfig)) {
260
                if (0 === $this->helper->getConfig($configsectionname)) {
261
                    return -1;
262
                }
263
            }
264
        }
265
        //  if ($section=="Notes" && $xoopsModuleConfig['enable_notes']==0){
266
        //          return false;
267
        //      }
268
        //      if ($section=="pictures" && $xoopsModuleConfig['enable_pictures']==0){
269
        //          return false;
270
        //      }
271
        //
272
        //      if ($section=="pictures" && $xoopsModuleConfig['enable_pictures']==0){
273
        //          return false;
274
        //      }
275
        $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

275
        $criteria = new Criteria('config_uid', /** @scrutinizer ignore-type */ $this->owner->getVar('uid'));
Loading history...
276
        if (1 === $this->configsFactory->getCount($criteria)) {
277
            $configs = $this->configsFactory->getObjects($criteria);
278
            $config  = $configs[0]->getVar($section);
279
            if (!$this->checkPrivilegeLevel($config)) {
280
                return 0;
281
            }
282
        }
283
284
        return 1;
285
    }
286
}
287