SuicoController::getPermissions()   F
last analyzed

Complexity

Conditions 14
Paths 1920

Size

Total Lines 67
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 46
c 2
b 1
f 0
dl 0
loc 67
rs 2.1
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
/**
37
 * Class SuicoController
38
 */
39
class SuicoController extends \XoopsObject
40
{
41
    public $db;
42
    public $user;
43
    public $isOwner;
44
    public $isUser;
45
    public $isAnonym;
46
    public $isFriend;
47
    public $uidOwner;
48
    public $nameOwner;
49
    public $owner;
50
    public $albumFactory;
51
    public $visitorsFactory;
52
    public $audioFactory;
53
    public $videosFactory;
54
    public $friendrequestFactory;
55
    public $friendshipsFactory;
56
    public $relgroupusersFactory;
57
    public $suspensionsFactory;
58
    public $groupsFactory;
59
    public $notesFactory;
60
    public $configsFactory;
61
    public $section;
62
    public $privilegeLevel;
63
    public $isSuspended;
64
    public $helper;
65
    public $isSelfRequest;
66
    public $isOtherRequest;
67
68
    /**
69
     * Constructor
70
     *
71
     * @param \XoopsDatabase $xoopsDatabase
72
     * @param                $user
73
     * @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...
74
     */
75
    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

75
    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...
76
    {
77
        $this->helper         = Helper::getInstance();
78
        $this->db             = $xoopsDatabase;
79
        $this->user           = $user;
80
        $this->isOwner        = 0;
81
        $this->isAnonym       = 1;
82
        $this->isFriend       = 0;
83
        $this->isUser         = 0;
84
        $this->isSelfRequest  = 0;
85
        $this->isOtherRequest = 0;
86
        $this->createFactories();
87
        $this->getPermissions();
88
        $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

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

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