Passed
Push — master ( 2744eb...81ba93 )
by Michael
02:46
created

class/Utility.php (1 issue)

1
<?php namespace XoopsModules\Smartfaq;
2
3
use Xmf\Request;
4
use XoopsModules\Smartfaq;
5
use XoopsModules\Smartfaq\Common;
6
7
/**
8
 * Class Utility
9
 */
10
class Utility
11
{
12
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
13
14
    use Common\ServerStats; // getServerStats Trait
15
16
    use Common\FilesManagement; // Files Management Trait
17
18
    //--------------- Custom module methods -----------------------------
19
20
21
22
    /**
23
     * @return mixed|null
24
     */
25
    public static function getModuleInfo()
26
    {
27
        static $smartModule;
28
        if (null === $smartModule) {
29
            global $xoopsModule;
30
            if (null !== $xoopsModule && is_object($xoopsModule) && 'smartfaq' === $xoopsModule->getVar('dirname')) {
31
                $smartModule = $xoopsModule;
32
            } else {
33
                $hModule     = xoops_getHandler('module');
34
                $smartModule = $hModule->getByDirname('smartfaq');
35
            }
36
        }
37
38
        return $smartModule;
39
    }
40
41
    /**
42
     * @return mixed
43
     */
44
    public static function getModuleConfig()
45
    {
46
        static $smartConfig;
47
        if (!$smartConfig) {
48
            global $xoopsModule;
49
            if (null !== $xoopsModule && is_object($xoopsModule) && 'smartfaq' === $xoopsModule->getVar('dirname')) {
50
                global $xoopsModuleConfig;
51
                $smartConfig = $xoopsModuleConfig;
52
            } else {
53
                $smartModule = self::getModuleInfo();
54
                $hModConfig  = xoops_getHandler('config');
55
                $smartConfig = $hModConfig->getConfigsByCat(0, $smartModule->getVar('mid'));
56
            }
57
        }
58
59
        return $smartConfig;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public static function getHelpPath()
66
    {
67
        $smartConfig = self::getModuleConfig();
68
        switch ($smartConfig['helppath_select']) {
69
            case 'docs.xoops.org':
70
                return 'http://docs.xoops.org/help/sfaqh/index.htm';
71
                break;
72
73
            case 'inside':
74
                return XOOPS_URL . '/modules/smartfaq/doc/';
75
                break;
76
77
            case 'custom':
78
                return $smartConfig['helppath_custom'];
79
                break;
80
        }
81
    }
82
83
    /**
84
     * @param  array $errors
85
     * @return string
86
     */
87
    public static function formatErrors($errors = [])
88
    {
89
        $ret = '';
90
        foreach ($errors as $key => $value) {
91
            $ret .= '<br> - ' . $value;
92
        }
93
94
        return $ret;
95
    }
96
97
    /**
98
     * @param         $categoryObj
99
     * @param  int    $selectedid
100
     * @param  int    $level
101
     * @param  string $ret
102
     * @return string
103
     */
104
    public static function addCategoryOption($categoryObj, $selectedid = 0, $level = 0, $ret = '')
105
    {
106
        // Creating the category handler object
107
        /** @var Smartfaq\CategoryHandler $categoryHandler */
108
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
109
110
        $spaces = '';
111
        for ($j = 0; $j < $level; ++$j) {
112
            $spaces .= '--';
113
        }
114
115
        $ret .= "<option value='" . $categoryObj->categoryid() . "'";
116
        if ($selectedid == $categoryObj->categoryid()) {
117
            $ret .= ' selected';
118
        }
119
        $ret .= '>' . $spaces . $categoryObj->name() . "</option>\n";
120
121
        $subCategoriesObj =& $categoryHandler->getCategories(0, 0, $categoryObj->categoryid());
122
        if (count($subCategoriesObj) > 0) {
123
            ++$level;
124
            foreach ($subCategoriesObj as $catID => $subCategoryObj) {
125
                $ret .= self::addCategoryOption($subCategoryObj, $selectedid, $level);
126
            }
127
        }
128
129
        return $ret;
130
    }
131
132
    /**
133
     * @param  int  $selectedid
134
     * @param  int  $parentcategory
135
     * @param  bool $allCatOption
136
     * @return string
137
     */
138
    public static function createCategorySelect($selectedid = 0, $parentcategory = 0, $allCatOption = true)
139
    {
140
        $ret = '' . _MB_SF_SELECTCAT . "&nbsp;<select name='options[]'>";
141
        if ($allCatOption) {
142
            $ret .= "<option value='0'";
143
            $ret .= '>' . _MB_SF_ALLCAT . "</option>\n";
144
        }
145
146
        // Creating the category handler object
147
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
148
149
        // Creating category objects
150
        $categoriesObj = $categoryHandler->getCategories(0, 0, $parentcategory);
151
152
        if (count($categoriesObj) > 0) {
153
            foreach ($categoriesObj as $catID => $categoryObj) {
154
                $ret .= self::addCategoryOption($categoryObj, $selectedid);
155
            }
156
        }
157
        $ret .= "</select>\n";
158
159
        return $ret;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public static function getStatusArray()
166
    {
167
        $result = [
168
            '1' => _AM_SF_STATUS1,
169
            '2' => _AM_SF_STATUS2,
170
            '3' => _AM_SF_STATUS3,
171
            '4' => _AM_SF_STATUS4,
172
            '5' => _AM_SF_STATUS5,
173
            '6' => _AM_SF_STATUS6,
174
            '7' => _AM_SF_STATUS7,
175
            '8' => _AM_SF_STATUS8
176
        ];
177
178
        return $result;
179
    }
180
181
    /**
182
     * @return bool
183
     */
184
    public static function hasModerator()
185
    {
186
        global $xoopsUser;
187
188
        if (!$xoopsUser) {
189
            $result = false;
190
        } else {
191
            /** @var Smartfaq\PermissionHandler $smartPermHandler */
192
            $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission');
193
194
            $categories = $smartPermHandler->getPermissions('moderation');
195
            if (0 == count($categories)) {
196
                $result = false;
197
            } else {
198
                $result = true;
199
            }
200
        }
201
202
        return $result;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public static function modFooter()
209
    {
210
        $smartModule = self::getModuleInfo();
211
212
        $modfootertxt = 'Module ' . $smartModule->getInfo('name') . ' - Version ' . $smartModule->getInfo('version') . '';
213
214
        $modfooter = "<a href='" . $smartModule->getInfo('support_site_url') . "' target='_blank'><img src='" . XOOPS_URL . "/modules/smartfaq/assets/images/sfcssbutton.gif' title='" . $modfootertxt . "' alt='" . $modfootertxt . "'></a>";
215
216
        return $modfooter;
217
    }
218
219
    /**
220
     * Checks if a user is admin of Smartfaq
221
     *
222
     * self::userIsAdmin()
223
     *
224
     * @return boolean : array with userids and uname
225
     */
226
    public static function userIsAdmin()
227
    {
228
        global $xoopsUser;
229
230
        $result = false;
231
232
        $smartModule = self::getModuleInfo();
233
        $module_id   = $smartModule->getVar('mid');
234
235
        if (!empty($xoopsUser)) {
236
            $groups =& $xoopsUser->getGroups();
237
            $result = in_array(XOOPS_GROUP_ADMIN, $groups) || $xoopsUser->isAdmin($module_id);
238
        }
239
240
        return $result;
241
    }
242
243
    /**
244
     * Checks if a user has access to a selected faq. If no item permissions are
245
     * set, access permission is denied. The user needs to have necessary category
246
     * permission as well.
247
     *
248
     * self::faqAccessGranted()
249
     *
250
     * @param $faqObj
251
     * @return int : -1 if no access, 0 if partialview and 1 if full access
252
     * @internal param int $faqid : faqid on which we are setting permissions
253
     * @internal param $integer $ categoryid : categoryid of the faq
254
     */
255
256
    // TODO : Move this function to Smartfaq\Faq class
257
    public static function faqAccessGranted($faqObj)
258
    {
259
        global $xoopsUser;
260
261
        if (self::userIsAdmin()) {
262
            $result = 1;
263
        } else {
264
            $result = -1;
265
266
            $groups = $xoopsUser ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
267
268
            $gpermHandler = xoops_getHandler('groupperm');
269
            $smartModule  = self::getModuleInfo();
270
            $module_id    = $smartModule->getVar('mid');
271
272
            // Do we have access to the parent category
273
            if ($gpermHandler->checkRight('category_read', $faqObj->categoryid(), $groups, $module_id)) {
274
                // Do we have access to the faq?
275
                if ($gpermHandler->checkRight('item_read', $faqObj->faqid(), $groups, $module_id)) {
276
                    $result = 1;
277
                } else { // No we don't !
278
                    // Check to see if we have partial view access
279
                    if (!is_object($xoopsUser) && $faqObj->partialView()) {
280
                        return 0;
281
                    }
282
                }
283
            } else { // No we don't !
284
                $result = false;
285
            }
286
        }
287
288
        return $result;
289
    }
290
291
    /**
292
     * Override FAQs permissions of a category by the category read permissions
293
     *
294
     *   self::overrideFaqsPermissions()
295
     *
296
     * @param  array   $groups     : group with granted permission
297
     * @param  integer $categoryid :
298
     * @return boolean|array : TRUE if the no errors occured
299
     */
300
    public static function overrideFaqsPermissions($groups, $categoryid)
301
    {
302
        global $xoopsDB;
303
304
        $result      = true;
305
        $smartModule = self::getModuleInfo();
306
        $module_id   = $smartModule->getVar('mid');
307
308
        $gpermHandler = xoops_getHandler('groupperm');
309
310
        $sql    = 'SELECT faqid FROM ' . $xoopsDB->prefix('smartfaq_faq') . " WHERE categoryid = '$categoryid' ";
311
        $result = $xoopsDB->queryF($sql);
312
313
        if ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) {
314
            while (false !== (list($faqid) = $xoopsDB->fetchRow($result))) {
315
                // First, if the permissions are already there, delete them
316
                $gpermHandler->deleteByModule($module_id, 'item_read', $faqid);
317
                // Save the new permissions
318
                if (count($groups) > 0) {
319
                    foreach ($groups as $group_id) {
320
                        $gpermHandler->addRight('item_read', $faqid, $group_id, $module_id);
321
                    }
322
                }
323
            }
324
        }
325
326
        return $result;
327
    }
328
329
    /**
330
     * Saves permissions for the selected faq
331
     *
332
     *   self::saveItemPermissions()
333
     *
334
     * @param  array   $groups : group with granted permission
335
     * @param  integer $itemID : faqid on which we are setting permissions
336
     * @return boolean : TRUE if the no errors occured
337
     */
338
    public static function saveItemPermissions($groups, $itemID)
339
    {
340
        $result      = true;
341
        $smartModule = self::getModuleInfo();
342
        $module_id   = $smartModule->getVar('mid');
343
344
        $gpermHandler = xoops_getHandler('groupperm');
345
        // First, if the permissions are already there, delete them
346
        $gpermHandler->deleteByModule($module_id, 'item_read', $itemID);
347
        // Save the new permissions
348
        if (count($groups) > 0) {
349
            foreach ($groups as $group_id) {
350
                $gpermHandler->addRight('item_read', $itemID, $group_id, $module_id);
351
            }
352
        }
353
354
        return $result;
355
    }
356
357
    /**
358
     * Saves permissions for the selected category
359
     *
360
     *   self::saveCategoryPermissions()
361
     *
362
     * @param  array   $groups     : group with granted permission
363
     * @param  integer $categoryid : categoryid on which we are setting permissions
364
     * @param  string  $perm_name  : name of the permission
365
     * @return boolean : TRUE if the no errors occured
366
     */
367
368
    public static function saveCategoryPermissions($groups, $categoryid, $perm_name)
369
    {
370
        $result      = true;
371
        $smartModule = self::getModuleInfo();
372
        $module_id   = $smartModule->getVar('mid');
373
374
        $gpermHandler = xoops_getHandler('groupperm');
375
        // First, if the permissions are already there, delete them
376
        $gpermHandler->deleteByModule($module_id, $perm_name, $categoryid);
377
        // Save the new permissions
378
        if (count($groups) > 0) {
379
            foreach ($groups as $group_id) {
380
                $gpermHandler->addRight($perm_name, $categoryid, $group_id, $module_id);
381
            }
382
        }
383
384
        return $result;
385
    }
386
387
    /**
388
     * Saves permissions for the selected category
389
     *
390
     *   self::saveModerators()
391
     *
392
     * @param  array   $moderators : moderators uids
393
     * @param  integer $categoryid : categoryid on which we are setting permissions
394
     * @return boolean : TRUE if the no errors occured
395
     */
396
397
    public static function saveModerators($moderators, $categoryid)
398
    {
399
        $result      = true;
400
        $smartModule = self::getModuleInfo();
401
        $module_id   = $smartModule->getVar('mid');
402
403
        $gpermHandler = xoops_getHandler('groupperm');
404
        // First, if the permissions are already there, delete them
405
        $gpermHandler->deleteByModule($module_id, 'category_moderation', $categoryid);
406
        // Save the new permissions
407
        if (count($moderators) > 0) {
408
            foreach ($moderators as $uid) {
409
                $gpermHandler->addRight('category_moderation', $categoryid, $uid, $module_id);
410
            }
411
        }
412
413
        return $result;
414
    }
415
416
    /**
417
     * @param  int $faqid
418
     * @return array
419
     */
420
    public static function retrieveFaqByID($faqid = 0)
421
    {
422
        $ret = [];
423
        global $xoopsDB;
424
425
        $result = $xoopsDB->queryF('SELECT * FROM ' . $xoopsDB->prefix('smartfaq_faq') . " WHERE faqid = '$faqid'");
426
        $ret    = $xoopsDB->fetchArray($result);
427
428
        return $ret;
429
    }
430
431
    /**
432
     * self::getAdminLinks()
433
     *
434
     * @param  integer $faqid
435
     * @param  bool    $open
436
     * @return string
437
     */
438
439
    // TODO : Move this to the Smartfaq\Faq class
440
    public static function getAdminLinks($faqid = 0, $open = false)
441
    {
442
        global $xoopsUser, $xoopsModule,  $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
443
        /** @var Smartfaq\Helper $helper */
444
        $helper = Smartfaq\Helper::getInstance();
445
446
        $adminLinks = '';
447
        $modulePath = XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/';
448
        $page       = $open ? 'question.php' : 'faq.php';
449
        if ($xoopsUser && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
450
            // Edit button
451
            $adminLinks .= "<a href='" . $modulePath . "admin/$page?op=mod&amp;faqid=" . $faqid . "'><img src='" . $modulePath . "assets/images/links/edit.gif'" . " title='" . _MD_SF_EDIT . "' alt='" . _MD_SF_EDIT . "'></a>";
452
            $adminLinks .= ' ';
453
            // Delete button
454
            $adminLinks .= "<a href='" . $modulePath . "admin/$page?op=del&amp;faqid=" . $faqid . "'><img src='" . $modulePath . "assets/images/links/delete.gif'" . " title='" . _MD_SF_DELETE . "' alt='" . _MD_SF_DELETE . "'></a>";
455
            $adminLinks .= ' ';
456
        }
457
        // Print button
458
        $adminLinks .= "<a href='" . $modulePath . 'print.php?faqid=' . $faqid . "'><img src='" . $modulePath . "assets/images/links/print.gif' title='" . _MD_SF_PRINT . "' alt='" . _MD_SF_PRINT . "'></a>";
459
        $adminLinks .= ' ';
460
        // Email button
461
        $maillink   = 'mailto:?subject=' . sprintf(_MD_SF_INTARTICLE, $xoopsConfig['sitename']) . '&amp;body=' . sprintf(_MD_SF_INTARTFOUND, $xoopsConfig['sitename']) . ':  ' . $modulePath . 'faq.php?faqid=' . $faqid;
462
        $adminLinks .= '<a href="' . $maillink . "\"><img src='" . $modulePath . "assets/images/links/friend.gif' title='" . _MD_SF_MAIL . "' alt='" . _MD_SF_MAIL . "'></a>";
463
        $adminLinks .= ' ';
464
        // Submit New Answer button
465
        if ($helper->getConfig('allownewanswer') && (is_object($xoopsUser) || $helper->getConfig('anonpost'))) {
466
            $adminLinks .= "<a href='" . $modulePath . 'answer.php?faqid=' . $faqid . "'><img src='" . $modulePath . "assets/images/links/newanswer.gif' title='" . _MD_SF_SUBMITANSWER . "' alt='" . _MD_SF_SUBMITANSWER . "'></a>";
467
            $adminLinks .= ' ';
468
        }
469
470
        return $adminLinks;
471
    }
472
473
    /**
474
     * self::getLinkedUnameFromId()
475
     *
476
     * @param  integer $userid Userid of poster etc
477
     * @param  integer $name   :  0 Use Usenamer 1 Use realname
478
     * @param  array   $users
479
     * @return string
480
     */
481
    public static function getLinkedUnameFromId($userid = 0, $name = 0, $users = [])
482
    {
483
        if (!is_numeric($userid)) {
484
            return $userid;
485
        }
486
487
        $userid = (int)$userid;
488
        if ($userid > 0) {
489
            if ($users == []) {
490
                //fetching users
491
                $memberHandler = xoops_getHandler('member');
492
                $user          = $memberHandler->getUser($userid);
493
            } else {
494
                if (!isset($users[$userid])) {
495
                    return $GLOBALS['xoopsConfig']['anonymous'];
496
                }
497
                $user =& $users[$userid];
498
            }
499
500
            if (is_object($user)) {
501
                $ts       = \MyTextSanitizer::getInstance();
502
                $username = $user->getVar('uname');
503
                $fullname = '';
504
505
                $fullname2 = $user->getVar('name');
506
507
                if ($name && !empty($fullname2)) {
508
                    $fullname = $user->getVar('name');
509
                }
510
                if (!empty($fullname)) {
511
                    $linkeduser = "$fullname [<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $userid . "'>" . $ts->htmlSpecialChars($username) . '</a>]';
512
                } else {
513
                    $linkeduser = "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $userid . "'>" . ucwords($ts->htmlSpecialChars($username)) . '</a>';
514
                }
515
516
                return $linkeduser;
517
            }
518
        }
519
520
        return $GLOBALS['xoopsConfig']['anonymous'];
521
    }
522
523
    /**
524
     * @param  string $url
525
     * @return mixed|string
526
     */
527
    public static function getXoopslink($url = '')
528
    {
529
        $xurl = $url;
530
        if (strlen($xurl) > 0) {
531
            if ($xurl[0] = '/') {
532
                $xurl = str_replace('/', '', $xurl);
533
            }
534
            $xurl = str_replace('{SITE_URL}', XOOPS_URL, $xurl);
535
        }
536
537
        //        $xurl = $url;
538
539
        return $xurl;
540
    }
541
542
    /**
543
     * @param string $tablename
544
     * @param string $iconname
545
     */
546
    public static function collapsableBar($tablename = '', $iconname = '')
547
    {
548
        ?>
549
        <script type="text/javascript"><!--
550
            function goto_URL(object) {
551
                window.location.href = object.options[object.selectedIndex].value;
552
            }
553
554
            function toggle(id) {
555
                if (document.getElementById) {
556
                    obj = document.getElementById(id);
557
                }
558
                if (document.all) {
559
                    obj = document.all[id];
560
                }
561
                if (document.layers) {
562
                    obj = document.layers[id];
563
                }
564
                if (obj) {
565
                    if (obj.style.display === "none") {
566
                        obj.style.display = "";
567
                    } else {
568
                        obj.style.display = "none";
569
                    }
570
                }
571
572
                return false;
573
            }
574
575
            var iconClose = new Image();
576
            iconClose.src = '../assets/images/icon/close12.gif';
577
            var iconOpen = new Image();
578
            iconOpen.src = '../assets/images/icon/open12.gif';
579
580
            function toggleIcon(iconName) {
581
                if (document.images[iconName].src == window.iconOpen.src) {
582
                    document.images[iconName].src = window.iconClose.src;
583
                }
584
                elseif(document.images[iconName].src == window.iconClose.src)
585
                {
586
                    document.images[iconName].src = window.iconOpen.src;
587
                }
588
589
                return;
590
            }
591
592
            //-->
593
        </script>
594
        <?php
595
        echo "<h3 style=\"color: #2F5376; margin: 6px 0 0 0; \"><a href='#' onClick=\"toggle('" . $tablename . "'); toggleIcon('" . $iconname . "');\">";
596
    }
597
}
598