Passed
Push — master ( 4f60cb...b7eab9 )
by Michael
08:23 queued 14s
created

XoopsModule::insertProfileFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * XOOPS Kernel Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 */
17
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
18
19
/**
20
 * A Module
21
 *
22
 * @package kernel
23
 * @author  Kazumi Ono <[email protected]>
24
 */
25
class XoopsModule extends XoopsObject
26
{
27
    /**
28
     *
29
     * @var string
30
     */
31
    public $modinfo;
32
    /**
33
     *
34
     * @var array
35
     */
36
    public $adminmenu;
37
    /**
38
     *
39
     * @var array
40
     */
41
    public $_msg;
42
43
    //PHP 8.2 Dynamic properties deprecated
44
    public $mid;
45
    public $name;
46
    public $version;
47
    public $last_update;
48
    public $weight;
49
    public $isactive;
50
    public $dirname;
51
    public $hasmain;
52
    public $hasadmin;
53
    public $hassearch;
54
    public $hasconfig;
55
    public $hascomments;
56
    // RMV-NOTIFY
57
    public $hasnotification;
58
59
    /**
60
     * Constructor
61
     */
62
    public function __construct()
63
    {
64
        parent::__construct();
65
        $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
66
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
67
        $this->initVar('version', XOBJ_DTYPE_TXTBOX, null, false);
68
        $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
69
        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
70
        $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
71
        $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
72
        $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
73
        $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
74
        $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
75
        $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
76
        $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
77
        // RMV-NOTIFY
78
        $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
79
    }
80
81
    /**
82
     * Load module info
83
     *
84
     * @param string  $dirname Directory Name
85
     * @param boolean $verbose
86
     */
87
    public function loadInfoAsVar($dirname, $verbose = true)
88
    {
89
        $dirname = basename($dirname);
90
        if (!isset($this->modinfo)) {
91
            $this->loadInfo($dirname, $verbose);
92
        }
93
        $this->setVar('name', $this->modinfo['name'], true);
94
        $this->setVar('version', $this->modinfo['version'], true);
95
        $this->setVar('dirname', $this->modinfo['dirname'], true);
96
        $hasmain     = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
97
        $hasadmin    = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
98
        $hassearch   = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0;
99
        $hasconfig   = ((isset($this->modinfo['config']) && \is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0;
100
        $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
101
        // RMV-NOTIFY
102
        $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0;
103
        $this->setVar('hasmain', $hasmain);
104
        $this->setVar('hasadmin', $hasadmin);
105
        $this->setVar('hassearch', $hassearch);
106
        $this->setVar('hasconfig', $hasconfig);
107
        $this->setVar('hascomments', $hascomments);
108
        // RMV-NOTIFY
109
        $this->setVar('hasnotification', $hasnotification);
110
    }
111
112
    /**
113
     * add a message
114
     *
115
     * @param string $str message to add
116
     * @access public
117
     */
118
    public function setMessage($str)
119
    {
120
        $this->_msg[] = trim($str);
121
    }
122
123
    /**
124
     * return the messages for this object as an array
125
     *
126
     * @return array an array of messages
127
     * @access public
128
     */
129
    public function getMessages()
130
    {
131
        return $this->_msg;
132
    }
133
134
    /**
135
     * Set module info
136
     *
137
     * @param  string $name
138
     * @param  mixed  $value
139
     * @return bool
140
     **/
141
    public function setInfo($name, $value)
142
    {
143
        if (empty($name)) {
144
            $this->modinfo = $value;
145
        } else {
146
            $this->modinfo[$name] = $value;
147
        }
148
149
        return true;
150
    }
151
152
    /**
153
     * Get module info
154
     *
155
     * @param  string $name
156
     * @return array|string    Array of module information.
157
     *                     If {@link $name} is set, returns a single module information item as string.
158
     */
159
    public function &getInfo($name = null)
160
    {
161
        if (!isset($this->modinfo)) {
162
            $this->loadInfo($this->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('dirname') can also be of type array and array; however, parameter $dirname of XoopsModule::loadInfo() 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

162
            $this->loadInfo(/** @scrutinizer ignore-type */ $this->getVar('dirname'));
Loading history...
163
        }
164
        if (isset($name)) {
165
            if (isset($this->modinfo[$name])) {
166
                return $this->modinfo[$name];
167
            }
168
            $return = false;
169
170
            return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return returns the type false which is incompatible with the documented return type array|string.
Loading history...
171
        }
172
173
        return $this->modinfo;
174
    }
175
176
    /**
177
     * Get status
178
     *
179
     * @return string
180
     */
181
    public function getStatus()
182
    {
183
        return substr(strrchr($this->getVar('version'), '-'), 1);
184
    }
185
186
    /**
187
     * Compares two "XOOPS-standardized" version number strings.
188
     *
189
     * @param  string $version1
190
     * @param  string $version2
191
     * @param  string $operator
192
     * @return boolean The function will return true if the relationship is the one specified by the operator, false otherwise.
193
     */
194
    public function versionCompare($version1 = '', $version2 = '', $operator = '<')
195
    {
196
        $version1 = strtolower($version1);
197
        $version2 = strtolower($version2);
198
        if (false !== strpos($version2, '-stable')) {
199
            $version2 = substr($version2, 0, strpos($version2, '-stable'));
200
        }
201
        if (false !== strpos($version1, '-stable')) {
202
            $version1 = substr($version1, 0, strpos($version1, '-stable'));
203
        }
204
        return version_compare($version1, $version2, $operator);
0 ignored issues
show
Bug Best Practice introduced by
The expression return version_compare($..., $version2, $operator) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
205
    }
206
207
    /**
208
     * Get a link to the modules main page
209
     *
210
     * @return string FALSE on fail
211
     */
212
    public function mainLink()
213
    {
214
        if ($this->getVar('hasmain') == 1) {
215
            $ret = '<a href="' . XOOPS_URL . '/modules/' . $this->getVar('dirname') . '/">' . $this->getVar('name') . '</a>';
216
217
            return $ret;
218
        }
219
220
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
221
    }
222
223
    /**
224
     * Get links to the subpages
225
     *
226
     * @return string
227
     */
228
    public function subLink()
229
    {
230
        $ret = [];
231
        if ($this->getInfo('sub') && \is_array($this->getInfo('sub'))) {
232
            foreach ($this->getInfo('sub') as $submenu) {
233
                $ret[] = [
234
                    'name' => $submenu['name'],
235
                    'url'  => $submenu['url'],
236
237
                    'icon' => $submenu['icon']?? '',
238
239
                ];
240
            }
241
        }
242
243
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type array|array<mixed,array<string,mixed|string>> which is incompatible with the documented return type string.
Loading history...
244
    }
245
246
    /**
247
     * Load the admin menu for the module
248
     */
249
    public function loadAdminMenu()
250
    {
251
        $adminmenu = [];
252
        if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) {
0 ignored issues
show
Bug introduced by
Are you sure $this->getInfo('adminmenu') of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

252
        if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . /** @scrutinizer ignore-type */ $this->getInfo('adminmenu'))) {
Loading history...
253
            include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu');
254
        }
255
        $this->adminmenu = & $adminmenu;
256
    }
257
258
    /**
259
     * Get the admin menu for the module
260
     *
261
     * @return array
262
     */
263
    public function &getAdminMenu()
264
    {
265
        if (!isset($this->adminmenu)) {
266
            $this->loadAdminMenu();
267
        }
268
269
        return $this->adminmenu;
270
    }
271
272
    /**
273
     * Load the module info for this module
274
     *
275
     * @param string $dirname Module directory
276
     * @param bool   $verbose Give an error on fail?
277
     *
278
     * @return bool true if loaded
279
     */
280
    public function loadInfo($dirname, $verbose = true)
281
    {
282
        static $modVersions;
283
        $dirname = basename($dirname);
284
        if (isset($modVersions[$dirname])) {
285
            $this->modinfo = $modVersions[$dirname];
286
287
            return true;
288
        }
289
        global $xoopsConfig;
290
        if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) {
291
            include_once $file;
292
        } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) {
293
            include_once $file;
294
        }
295
296
        if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) {
297
            if (false !== (bool) $verbose) {
298
                echo "Module File for $dirname Not Found!";
299
            }
300
301
            return false;
302
        }
303
        include $file;
304
        $modVersions[$dirname] = $modversion;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modversion does not exist. Did you maybe mean $modVersions?
Loading history...
305
        $this->modinfo         = $modVersions[$dirname];
306
307
        return true;
308
    }
309
310
    /**
311
     * Search contents within a module
312
     *
313
     * @param  string  $term
314
     * @param  string  $andor 'AND' or 'OR'
315
     * @param  integer $limit
316
     * @param  integer $offset
317
     * @param  integer $userid
318
     * @return mixed   Search result.
319
     */
320
    public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
321
    {
322
        if ($this->getVar('hassearch') != 1) {
323
            return false;
324
        }
325
        $search = & $this->getInfo('search');
326
        if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
327
            return false;
328
        }
329
        if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) {
330
            include_once $file;
331
        } else {
332
            return false;
333
        }
334
        if (function_exists($search['func'])) {
335
            $func = $search['func'];
336
337
            return $func($term, $andor, $limit, $offset, $userid);
338
        }
339
340
        return false;
341
    }
342
343
    /**
344
     * Returns Class Base Variable mid
345
     * @param  string $format
346
     * @return mixed
347
     */
348
    public function id($format = 'N')
349
    {
350
        return $this->getVar('mid', $format);
351
    }
352
353
    /**
354
     * Returns Class Base Variable mid
355
     * @param  string $format
356
     * @return mixed
357
     */
358
    public function mid($format = '')
359
    {
360
        return $this->getVar('mid', $format);
361
    }
362
363
    /**
364
     * Returns Class Base Variable name
365
     * @param  string $format
366
     * @return mixed
367
     */
368
    public function name($format = '')
369
    {
370
        return $this->getVar('name', $format);
371
    }
372
373
    /**
374
     * Returns Class Base Variable version
375
     * @param  string $format
376
     * @return mixed
377
     */
378
    public function version($format = '')
379
    {
380
        return $this->getVar('version', $format);
381
    }
382
383
    /**
384
     * Returns Class Base Variable last_update
385
     * @param  string $format
386
     * @return mixed
387
     */
388
    public function last_update($format = '')
389
    {
390
        return $this->getVar('last_update', $format);
391
    }
392
393
    /**
394
     * Returns Class Base Variable weight
395
     * @param  string $format
396
     * @return mixed
397
     */
398
    public function weight($format = '')
399
    {
400
        return $this->getVar('weight', $format);
401
    }
402
403
    /**
404
     * Returns Class Base Variable isactive
405
     * @param  string $format
406
     * @return mixed
407
     */
408
    public function isactive($format = '')
409
    {
410
        return $this->getVar('isactive', $format);
411
    }
412
413
    /**
414
     * Returns Class Base Variable dirname
415
     * @param  string $format
416
     * @return mixed
417
     */
418
    public function dirname($format = '')
419
    {
420
        return $this->getVar('dirname', $format);
421
    }
422
423
    /**
424
     * Returns Class Base Variable hasmain
425
     * @param  string $format
426
     * @return mixed
427
     */
428
    public function hasmain($format = '')
429
    {
430
        return $this->getVar('hasmain', $format);
431
    }
432
433
    /**
434
     * Returns Class Base Variable hasadmin
435
     * @param  string $format
436
     * @return mixed
437
     */
438
    public function hasadmin($format = '')
439
    {
440
        return $this->getVar('hasadmin', $format);
441
    }
442
443
    /**
444
     * Returns Class Base Variable hassearch
445
     * @param  string $format
446
     * @return mixed
447
     */
448
    public function hassearch($format = '')
449
    {
450
        return $this->getVar('hassearch', $format);
451
    }
452
453
    /**
454
     * Returns Class Base Variable hasconfig
455
     * @param  string $format
456
     * @return mixed
457
     */
458
    public function hasconfig($format = '')
459
    {
460
        return $this->getVar('hasconfig', $format);
461
    }
462
463
    /**
464
     * Returns Class Base Variable hascomments
465
     * @param  string $format
466
     * @return mixed
467
     */
468
    public function hascomments($format = '')
469
    {
470
        return $this->getVar('hascomments', $format);
471
    }
472
473
    /**
474
     * Returns Class Base Variable hasnotification
475
     * @param  string $format
476
     * @return mixed
477
     */
478
    public function hasnotification($format = '')
479
    {
480
        return $this->getVar('hasnotification', $format);
481
    }
482
483
    /**
484
     * @param $dirname
485
     *
486
     * @return mixed
487
     */
488
    public static function getByDirname($dirname)
489
    {
490
        /** @var XoopsModuleHandler $modhandler */
491
        $modhandler = xoops_getHandler('module');
492
        $inst       = $modhandler->getByDirname($dirname);
493
494
        return $inst;
495
    }
496
497
498
    /**
499
     * Returns Class Base Variable icon
500
     * @param  string $format
501
     * @return mixed
502
     */
503
    public function icon($format = '')
504
    {
505
        return $this->getVar('icon', $format);
506
    }
507
508
509
    ##################### Deprecated Methods ######################
510
511
    /**#@+
512
     * @deprecated
513
     */
514
    public function checkAccess()
515
    {
516
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
517
518
        return false;
519
    }
520
521
    /**
522
     * @param string $type
523
     *
524
     * @return bool
525
     */
526
    public function loadLanguage($type = 'main')
0 ignored issues
show
Unused Code introduced by
The parameter $type 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

526
    public function loadLanguage(/** @scrutinizer ignore-unused */ $type = 'main')

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...
527
    {
528
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
529
530
        return false;
531
    }
532
533
    /**
534
     * @return bool
535
     */
536
    public function loadErrorMessages()
537
    {
538
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
539
540
        return false;
541
    }
542
543
    /**
544
     * @return bool
545
     */
546
    public function getCurrentPage()
547
    {
548
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
549
550
        return false;
551
    }
552
553
    /**
554
     * @param array $admingroups
555
     * @param array $accessgroups
556
     *
557
     * @return bool
558
     */
559
    public function install($admingroups = [], $accessgroups = [])
0 ignored issues
show
Unused Code introduced by
The parameter $accessgroups 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

559
    public function install($admingroups = [], /** @scrutinizer ignore-unused */ $accessgroups = [])

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...
Unused Code introduced by
The parameter $admingroups 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

559
    public function install(/** @scrutinizer ignore-unused */ $admingroups = [], $accessgroups = [])

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...
560
    {
561
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
562
563
        return false;
564
    }
565
566
    /**
567
     * @return bool
568
     */
569
    public function update()
570
    {
571
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
572
573
        return false;
574
    }
575
576
    /**
577
     * @return bool
578
     */
579
    public function insert()
580
    {
581
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
582
583
        return false;
584
    }
585
586
    /**
587
     * @return bool
588
     */
589
    public function executeSQL()
590
    {
591
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
592
593
        return false;
594
    }
595
596
    /**
597
     * @return bool
598
     */
599
    public function insertTemplates()
600
    {
601
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
602
603
        return false;
604
    }
605
606
    /**
607
     * @param      $template
608
     * @param bool $block
609
     *
610
     * @return bool
611
     */
612
    public function gettemplate($template, $block = false)
0 ignored issues
show
Unused Code introduced by
The parameter $block 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

612
    public function gettemplate($template, /** @scrutinizer ignore-unused */ $block = false)

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...
Unused Code introduced by
The parameter $template 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

612
    public function gettemplate(/** @scrutinizer ignore-unused */ $template, $block = false)

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...
613
    {
614
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
615
616
        return false;
617
    }
618
619
    /**
620
     * @return bool
621
     */
622
    public function insertBlocks()
623
    {
624
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
625
626
        return false;
627
    }
628
629
    /**
630
     * @return bool
631
     */
632
    public function insertConfigCategories()
633
    {
634
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
635
636
        return false;
637
    }
638
639
    /**
640
     * @return bool
641
     */
642
    public function insertConfig()
643
    {
644
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
645
646
        return false;
647
    }
648
649
    /**
650
     * @return bool
651
     */
652
    public function insertProfileFields()
653
    {
654
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
655
656
        return false;
657
    }
658
659
    /**
660
     * @param     $type
661
     * @param int $state
662
     *
663
     * @return bool
664
     */
665
    public function executeScript($type, $state = 2)
0 ignored issues
show
Unused Code introduced by
The parameter $type 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

665
    public function executeScript(/** @scrutinizer ignore-unused */ $type, $state = 2)

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...
Unused Code introduced by
The parameter $state 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

665
    public function executeScript($type, /** @scrutinizer ignore-unused */ $state = 2)

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...
666
    {
667
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
668
669
        return false;
670
    }
671
672
    /**
673
     * @param $groups
674
     * @param $type
675
     *
676
     * @return bool
677
     */
678
    public function insertGroupPermissions($groups, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $groups 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

678
    public function insertGroupPermissions(/** @scrutinizer ignore-unused */ $groups, $type)

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...
Unused Code introduced by
The parameter $type 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

678
    public function insertGroupPermissions($groups, /** @scrutinizer ignore-unused */ $type)

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...
679
    {
680
        $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
681
682
        return false;
683
    }
684
    /**#@-*/
685
}
686
687
/**
688
 * XOOPS module handler class.
689
 *
690
 * This class is responsible for providing data access mechanisms to the data source
691
 * of XOOPS module class objects.
692
 *
693
 * @package       kernel
694
 * @author        Kazumi Ono <[email protected]>
695
 * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org
696
 *
697
 * @todo Why is this not a XoopsPersistableObjectHandler?
698
 */
699
class XoopsModuleHandler extends XoopsObjectHandler
700
{
701
    /**
702
     * holds an array of cached module references, indexed by module id
703
     *
704
     * @var array
705
     * @access private
706
     */
707
    public $_cachedModule_mid = [];
708
709
    /**
710
     * holds an array of cached module references, indexed by module dirname
711
     *
712
     * @var array
713
     * @access private
714
     */
715
    public $_cachedModule_dirname = [];
716
717
    /**
718
     * Create a new {@link XoopsModule} object
719
     *
720
     * @param  boolean $isNew Flag the new object as "new"
721
     * @return XoopsModule
722
     */
723
    public function create($isNew = true)
724
    {
725
        $module = new XoopsModule();
726
        if ($isNew) {
727
            $module->setNew();
728
        }
729
730
        return $module;
731
    }
732
733
    /**
734
     * Load a module from the database
735
     *
736
     * @param  int $id ID of the module
737
     * @return XoopsObject|false false on fail
738
     */
739
    public function get($id)
740
    {
741
        static $_cachedModule_dirname;
742
        static $_cachedModule_mid;
743
        $id     = (int) $id;
744
        $module = false;
745
        if ($id > 0) {
746
            if (!empty($_cachedModule_mid[$id])) {
747
                return $_cachedModule_mid[$id];
748
            } else {
749
                $sql = 'SELECT * FROM ' . $this->db->prefix('modules') . ' WHERE mid = ' . $id;
750
                $result = $this->db->query($sql);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

750
                /** @scrutinizer ignore-call */ 
751
                $result = $this->db->query($sql);
Loading history...
751
                if (!$this->db->isResultSet($result)) {
752
                    return $module;
753
                }
754
                $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
The method getRowsNum() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

754
                /** @scrutinizer ignore-call */ 
755
                $numrows = $this->db->getRowsNum($result);
Loading history...
755
                if ($numrows == 1) {
756
                    $module = new XoopsModule();
757
                    $myrow  = $this->db->fetchArray($result);
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

757
                    /** @scrutinizer ignore-call */ 
758
                    $myrow  = $this->db->fetchArray($result);
Loading history...
758
                    $module->assignVars($myrow);
759
                    $_cachedModule_mid[$id]                            = &$module;
760
                    $_cachedModule_dirname[$module->getVar('dirname')] = &$module;
761
762
                    return $module;
763
                }
764
            }
765
        }
766
767
        return $module;
768
    }
769
770
    /**
771
     * Load a module by its dirname
772
     *
773
     * @param  string $dirname
774
     * @return XoopsModule|FALSE on fail
775
     */
776
    public function getByDirname($dirname)
777
    {
778
        $dirname = basename($dirname);
779
        // Sanitize $dirname to prevent SQL injection
780
        $dirname = $this->db->escape(trim($dirname));
0 ignored issues
show
Bug introduced by
The method escape() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

780
        /** @scrutinizer ignore-call */ 
781
        $dirname = $this->db->escape(trim($dirname));
Loading history...
781
782
        //could not we check for spaces instead??
783
        if (strpos(strtolower($dirname), ' union ')) {
784
            return false;
785
        }
786
        static $_cachedModule_mid;
787
        static $_cachedModule_dirname;
788
        if (!empty($_cachedModule_dirname[$dirname])) {
789
            return $_cachedModule_dirname[$dirname];
790
        } else {
791
            $module = false;
792
            $sql    = 'SELECT * FROM ' . $this->db->prefix('modules') . ' WHERE dirname = ?';
793
            $stmt   = $this->db->conn->prepare($sql);
794
            $stmt->bind_param('s', $dirname);
795
            $success = $stmt->execute();
796
            if (!$success) {
797
                return $module;
798
            }
799
            $result = $stmt->get_result();
800
801
            if (!$this->db->isResultSet($result)) {
802
                return $module;
803
            }
804
            $numrows = $this->db->getRowsNum($result);
805
            if ($numrows == 1) {
806
                $module = new XoopsModule();
807
                $myrow  = $this->db->fetchArray($result);
808
                $module->assignVars($myrow);
809
                $_cachedModule_dirname[$dirname]           = & $module;
810
                $_cachedModule_mid[$module->getVar('mid')] = & $module;
811
            }
812
813
            return $module;
814
        }
815
    }
816
817
    /**
818
     * Write a module to the database
819
     *
820
     * @param  XoopsObject|XoopsModule $module a XoopsModule object
821
     *
822
     * @return bool true on success, otherwise false
823
     */
824
    public function insert(XoopsObject $module)
825
    {
826
        $className = 'XoopsModule';
827
        if (!($module instanceof $className)) {
828
            return false;
829
        }
830
        if (!$module->isDirty()) {
831
            return true;
832
        }
833
        if (!$module->cleanVars()) {
834
            return false;
835
        }
836
        foreach ($module->cleanVars as $k => $v) {
837
            ${$k} = $v;
838
        }
839
        if ($module->isNew()) {
840
            $mid = $this->db->genId('modules_mid_seq');
0 ignored issues
show
Bug introduced by
The method genId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

840
            /** @scrutinizer ignore-call */ 
841
            $mid = $this->db->genId('modules_mid_seq');
Loading history...
841
            $sql = sprintf('INSERT INTO %s (mid, name, version, last_update, weight, isactive, dirname, hasmain, hasadmin, hassearch, hasconfig, hascomments, hasnotification) VALUES (%u, %s, %s, %u, %u, %u, %s, %u, %u, %u, %u, %u, %u)', $this->db->prefix('modules'), $mid, $this->db->quoteString($name), $this->db->quoteString($version), time(), $weight, 1, $this->db->quoteString($dirname), $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $hasnotification seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $hassearch seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $hasconfig seems to be never defined.
Loading history...
Bug introduced by
The method quoteString() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

841
            $sql = sprintf('INSERT INTO %s (mid, name, version, last_update, weight, isactive, dirname, hasmain, hasadmin, hassearch, hasconfig, hascomments, hasnotification) VALUES (%u, %s, %s, %u, %u, %u, %s, %u, %u, %u, %u, %u, %u)', $this->db->prefix('modules'), $mid, $this->db->/** @scrutinizer ignore-call */ quoteString($name), $this->db->quoteString($version), time(), $weight, 1, $this->db->quoteString($dirname), $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification);
Loading history...
Comprehensibility Best Practice introduced by
The variable $dirname seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $hasadmin seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $version seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $hasmain seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $hascomments seems to be never defined.
Loading history...
842
        } else {
843
            $sql = sprintf('UPDATE %s SET name = %s, dirname = %s, version = %s, last_update = %u, weight = %u, isactive = %u, hasmain = %u, hasadmin = %u, hassearch = %u, hasconfig = %u, hascomments = %u, hasnotification = %u WHERE mid = %u', $this->db->prefix('modules'), $this->db->quoteString($name), $this->db->quoteString($dirname), $this->db->quoteString($version), time(), $weight, $isactive, $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification, $mid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $isactive seems to be never defined.
Loading history...
844
        }
845
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
846
            return false;
847
        }
848
        if (empty($mid)) {
849
            $mid = $this->db->getInsertId();
0 ignored issues
show
Bug introduced by
The method getInsertId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

849
            /** @scrutinizer ignore-call */ 
850
            $mid = $this->db->getInsertId();
Loading history...
850
        }
851
        $module->assignVar('mid', $mid);
852
        if (!empty($this->_cachedModule_dirname[$dirname])) {
853
            unset($this->_cachedModule_dirname[$dirname]);
854
        }
855
        if (!empty($this->_cachedModule_mid[$mid])) {
856
            unset($this->_cachedModule_mid[$mid]);
857
        }
858
859
        return true;
860
    }
861
862
    /**
863
     * Delete a module from the database
864
     *
865
     * @param  XoopsObject|XoopsModule $module a XoopsModule object
866
     *
867
     * @return bool true on success, otherwise false
868
     */
869
    public function delete(XoopsObject $module)
870
    {
871
        $className = 'XoopsModule';
872
        if (!($module instanceof $className)) {
873
            return false;
874
        }
875
        $sql = sprintf('DELETE FROM %s WHERE mid = %u', $this->db->prefix('modules'), $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
It seems like $module->getVar('mid') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

875
        $sql = sprintf('DELETE FROM %s WHERE mid = %u', $this->db->prefix('modules'), /** @scrutinizer ignore-type */ $module->getVar('mid'));
Loading history...
876
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
877
            return false;
878
        }
879
        // delete admin permissions assigned for this module
880
        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
881
        $this->db->query($sql);
882
        // delete read permissions assigned for this module
883
        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
884
        $this->db->query($sql);
885
886
        $sql = sprintf('SELECT block_id FROM %s WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
887
        $result = $this->db->query($sql);
888
        if ($this->db->isResultSet($result)) {
889
            $block_id_arr = [];
890
            /** @var array $myrow */
891
            while (false !== ($myrow = $this->db->fetchArray($result))) {
892
                $block_id_arr[] = $myrow['block_id'];
893
            }
894
        }
895
        // loop through block_id_arr
896
        if (isset($block_id_arr)) {
897
            foreach ($block_id_arr as $i) {
898
                $sql = sprintf('SELECT block_id FROM %s WHERE module_id != %u AND block_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
899
                $result2 = $this->db->query($sql);
900
                if ($this->db->isResultSet($result2)) {
901
                    if (0 < $this->db->getRowsNum($result2)) {
902
                        // this block has other entries, so delete the entry for this module
903
                        $sql = sprintf('DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
904
                        $this->db->query($sql);
905
                    } else {
906
                        // this block doesn't have other entries, so disable the block and let it show on top page only. otherwise, this block will not display anymore on block admin page!
907
                        $sql = sprintf('UPDATE %s SET visible = 0 WHERE bid = %u', $this->db->prefix('newblocks'), $i);
908
                        $this->db->query($sql);
909
                        $sql = sprintf('UPDATE %s SET module_id = -1 WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
910
                        $this->db->query($sql);
911
                    }
912
                }
913
            }
914
        }
915
916
        $dirname = (string) $module->getVar('dirname');
917
        $mid = (int) $module->getVar('mid');
918
919
        if (!empty($this->_cachedModule_dirname[$dirname])) {
920
            unset($this->_cachedModule_dirname[$dirname]);
921
        }
922
        if (!empty($this->_cachedModule_mid[$mid])) {
923
            unset($this->_cachedModule_mid[$mid]);
924
        }
925
926
        return true;
927
    }
928
929
    /**
930
     * Load some modules
931
     *
932
     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement}
933
     * @param  boolean         $id_as_key Use the ID as key into the array
934
     * @return array
935
     */
936
    public function getObjects(?CriteriaElement $criteria = null, $id_as_key = false)
937
    {
938
        if (func_num_args() > 0) {
939
            $criteria = func_get_arg(0);
940
        }
941
942
        if (version_compare(PHP_VERSION, '8.0.0', '>=')) {
943
            $criteria = $criteria ?? null;
944
        }
945
946
        if (func_num_args() > 0) {
947
            $criteria = func_get_arg(0);
948
        }
949
950
        if (version_compare(PHP_VERSION, '8.0.0', '>=')) {
951
            $criteria = $criteria ?? null; // Explicitly set to null if not provided
952
        }
953
954
        $ret   = [];
955
        $limit = $start = 0;
956
        $sql   = 'SELECT * FROM ' . $this->db->prefix('modules');
957
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
958
            $sql .= ' ' . $criteria->renderWhere();
959
            $sql .= ' ORDER BY weight ' . $criteria->getOrder() . ', mid ASC';
960
            $limit = $criteria->getLimit();
961
            $start = $criteria->getStart();
962
        }
963
        $result = $this->db->query($sql, $limit, $start);
964
        if (!$this->db->isResultSet($result)) {
965
            return $ret;
966
        }
967
        /** @var array $myrow */
968
        while (false !== ($myrow = $this->db->fetchArray($result))) {
969
            $module = new XoopsModule();
970
            $module->assignVars($myrow);
971
            if (!$id_as_key) {
972
                $ret[] = & $module;
973
            } else {
974
                $ret[$myrow['mid']] = & $module;
975
            }
976
            unset($module);
977
        }
978
979
        return $ret;
980
    }
981
982
    /**
983
     * Count some modules
984
     *
985
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
986
     * @return int
987
     */
988
    public function getCount(?CriteriaElement $criteria = null)
989
    {
990
        if (func_num_args() > 0) {
991
            $criteria = func_get_arg(0);
992
        }
993
994
        if (version_compare(PHP_VERSION, '8.0.0', '>=')) {
995
            $criteria = $criteria ?? null;
996
        }
997
998
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('modules');
999
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
1000
            $sql .= ' ' . $criteria->renderWhere();
1001
        }
1002
        $result = $this->db->query($sql);
1003
        if (!$this->db->isResultSet($result)) {
1004
            return 0;
1005
        }
1006
        [$count] = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

1006
        /** @scrutinizer ignore-call */ 
1007
        [$count] = $this->db->fetchRow($result);
Loading history...
1007
1008
        return (int) $count;
1009
    }
1010
1011
    /**
1012
     * returns an array of module names
1013
     *
1014
     * @param  CriteriaElement $criteria
1015
     * @param  boolean         $dirname_as_key if true, array keys will be module directory names
1016
     *                                         if false, array keys will be module id
1017
     * @return array
1018
     */
1019
    public function getList(?CriteriaElement $criteria = null, $dirname_as_key = false)
1020
    {
1021
1022
        if (func_num_args() > 0) {
1023
            $criteria = func_get_arg(0);
1024
        }
1025
1026
        if (version_compare(PHP_VERSION, '8.0.0', '>=')) {
1027
            $criteria = $criteria ?? null; // Explicitly set to null if not provided
1028
        }
1029
1030
1031
        $ret     = [];
1032
        $modules = $this->getObjects($criteria, true);
1033
        foreach (array_keys($modules) as $i) {
1034
            if (!$dirname_as_key) {
1035
                $ret[$i] = $modules[$i]->getVar('name');
1036
            } else {
1037
                $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
1038
            }
1039
        }
1040
1041
        return $ret;
1042
    }
1043
}
1044