Passed
Push — master ( 33c64b...8d1135 )
by Michael
03:38
created

class/Utility.php (1 issue)

Severity
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
use XoopsModules\Newbb;
6
7
/**
8
 * Class Utility
9
 */
10
class Utility
11
{
12
    use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits
0 ignored issues
show
The trait XoopsModules\Newbb\Common\VersionChecks requires some properties which are not provided by XoopsModules\Newbb\Utility: $tag_name, $prerelease
Loading history...
13
14
    use Common\ServerStats; // getServerStats Trait
15
16
    use Common\FilesManagement; // Files Management Trait
17
18
    //--------------- Custom module methods -----------------------------
19
20
    /**
21
     * Verify that a mysql table exists
22
     *
23
     * @package       News
24
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
25
     * @copyright (c) Hervé Thouzard
26
     * @param $tablename
27
     * @return bool
28
     */
29
    public function tableExists($tablename)
30
    {
31
        global $xoopsDB;
32
        /** @var \XoopsMySQLDatabase $xoopsDB */
33
        $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'");
34
35
        return ($xoopsDB->getRowsNum($result) > 0);
36
    }
37
38
    /**
39
     * Verify that a field exists inside a mysql table
40
     *
41
     * @package       News
42
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
43
     * @copyright (c) Hervé Thouzard
44
     * @param $fieldname
45
     * @param $table
46
     * @return bool
47
     */
48
    public function fieldExists($fieldname, $table)
49
    {
50
        global $xoopsDB;
51
        $result = $xoopsDB->queryF("SHOW COLUMNS FROM   $table LIKE '$fieldname'");
52
53
        return ($xoopsDB->getRowsNum($result) > 0);
54
    }
55
56
    /**
57
     * Add a field to a mysql table
58
     *
59
     * @package       News
60
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
61
     * @copyright (c) Hervé Thouzard
62
     * @param $field
63
     * @param $table
64
     * @return bool|\mysqli_result
65
     */
66
    public function addField($field, $table)
67
    {
68
        global $xoopsDB;
69
        $result = $xoopsDB->queryF('ALTER TABLE ' . $table . " ADD $field");
70
71
        return $result;
72
    }
73
74
    /**
75
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
76
     *
77
     * @param string $folder Le chemin complet du répertoire à vérifier
78
     */
79
    public static function prepareFolder($folder)
80
    {
81
        try {
82
            if (!@mkdir($folder) && !is_dir($folder)) {
83
                throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
84
            }
85
            file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
86
        }
87
        catch (\Exception $e) {
88
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br>';
89
        }
90
    }
91
92
    public static function cleanCache()
93
    {
94
        $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
95
        if (method_exists($cacheHelper, 'clear')) {
96
            $cacheHelper->clear();
97
98
            return;
99
        }
100
        // for 2.5 systems, clear everything
101
        require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php';
102
        $maintenance = new \SystemMaintenance();
103
        $cacheList   = [
104
            3, // xoops_cache
105
        ];
106
        $maintenance->CleanCache($cacheList);
107
        xoops_setActiveModules();
108
    }
109
110
    /**
111
     * Checks if a user is admin of NewBB
112
     *
113
     * @return bool
114
     */
115
    public static function userIsAdmin()
116
    {
117
        /** @var Newbb\Helper $helper */
118
        $helper = Newbb\Helper::getInstance();
119
120
        static $newbbIsAdmin;
121
122
        if (isset($newbbIsAdmin)) {
123
            return $newbbIsAdmin;
124
        }
125
126
        if (!$GLOBALS['xoopsUser']) {
127
            $newbbIsAdmin = false;
128
        } else {
129
            $newbbIsAdmin = $GLOBALS['xoopsUser']->isAdmin($helper->getModule()->getVar('mid'));
130
        }
131
132
        return $newbbIsAdmin;
133
    }
134
}
135