Utility::fieldExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
use XoopsModules\Newbb;
6
use XoopsModules\Newbb\Common;
7
use XoopsModules\Newbb\Constants;
8
9
/**
10
 * Class Utility
11
 */
12
class Utility extends Common\SysUtility
13
{
14
    //--------------- Custom module methods -----------------------------
15
16
    /**
17
     * Verify that a mysql table exists
18
     *
19
     * @param $tablename
20
     * @return bool
21
     * @copyright (c) Hervé Thouzard
22
     * @package       News
23
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
24
     */
25
    public function tableExists($tablename)
26
    {
27
        global $xoopsDB;
28
        /** @var \XoopsMySQLDatabase $xoopsDB */
29
        $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'");
30
31
        return ($xoopsDB->getRowsNum($result) > 0);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, 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

31
        return ($xoopsDB->getRowsNum(/** @scrutinizer ignore-type */ $result) > 0);
Loading history...
32
    }
33
34
    /**
35
     * Verify that a field exists inside a mysql table
36
     *
37
     * @param $fieldname
38
     * @param $table
39
     * @return bool
40
     * @package       News
41
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
42
     * @copyright (c) Hervé Thouzard
43
     */
44
    public function fieldExists($fieldname, $table)
45
    {
46
        global $xoopsDB;
47
        $result = $xoopsDB->queryF("SHOW COLUMNS FROM   $table LIKE '$fieldname'");
48
49
        return ($xoopsDB->getRowsNum($result) > 0);
50
    }
51
52
    /**
53
     * Add a field to a mysql table
54
     *
55
     * @param $field
56
     * @param $table
57
     * @return bool|\mysqli_result
58
     * @package       News
59
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
60
     * @copyright (c) Hervé Thouzard
61
     */
62
    public function addField($field, $table)
63
    {
64
        global $xoopsDB;
65
        $result = $xoopsDB->queryF('ALTER TABLE ' . $table . " ADD $field");
66
67
        return $result;
68
    }
69
70
    /**
71
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
72
     *
73
     * @param string $folder Le chemin complet du répertoire à vérifier
74
     */
75
    public static function prepareFolder($folder)
76
    {
77
        try {
78
            if (!@\mkdir($folder) && !\is_dir($folder)) {
79
                throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder));
80
            }
81
            file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
82
        } catch (\Exception $e) {
83
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br>';
84
        }
85
    }
86
87
    public static function cleanCache()
88
    {
89
        $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
90
        if (\method_exists($cacheHelper, 'clear')) {
91
            $cacheHelper->clear();
92
93
            return;
94
        }
95
        // for 2.5 systems, clear everything
96
        require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php';
97
        $maintenance = new \SystemMaintenance();
98
        $cacheList   = [
99
            3, // xoops_cache
100
        ];
101
        $maintenance->CleanCache($cacheList);
102
        \xoops_setActiveModules();
103
    }
104
105
    /**
106
     * Checks if a user is admin of NewBB
107
     *
108
     * @return bool
109
     */
110
    public static function userIsAdmin()
111
    {
112
        /** @var Newbb\Helper $helper */
113
        $helper = \XoopsModules\Newbb\Helper::getInstance();
114
115
        static $newbbIsAdmin;
116
117
        if (isset($newbbIsAdmin)) {
118
            return $newbbIsAdmin;
119
        }
120
121
        if (!$GLOBALS['xoopsUser']) {
122
            $newbbIsAdmin = false;
123
        } else {
124
            $newbbIsAdmin = $GLOBALS['xoopsUser']->isAdmin($helper->getModule()->getVar('mid'));
125
        }
126
127
        return $newbbIsAdmin;
128
    }
129
}
130