Completed
Push — master ( 2b5506...d00cae )
by Michael
02:15
created

NewbbUtility::fieldExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class NewbbUtility
5
 */
6
class NewbbUtility
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * Verify that a mysql table exists
10
     *
11
     * @package       News
12
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
13
     * @copyright (c) Hervé Thouzard
14
     * @param $tablename
15
     * @return bool
16
     */
17
    public function tableExists($tablename)
18
    {
19
        global $xoopsDB;
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...
20
        /** @var \XoopsMySQLDatabase $xoopsDB */
21
        $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'");
22
23
        return ($xoopsDB->getRowsNum($result) > 0);
24
    }
25
26
    /**
27
     * Verify that a field exists inside a mysql table
28
     *
29
     * @package       News
30
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
31
     * @copyright (c) Hervé Thouzard
32
     * @param $fieldname
33
     * @param $table
34
     * @return bool
35
     */
36
    public function fieldExists($fieldname, $table)
37
    {
38
        global $xoopsDB;
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...
39
        $result = $xoopsDB->queryF("SHOW COLUMNS FROM   $table LIKE '$fieldname'");
40
41
        return ($xoopsDB->getRowsNum($result) > 0);
42
    }
43
44
    /**
45
     * Add a field to a mysql table
46
     *
47
     * @package       News
48
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
49
     * @copyright (c) Hervé Thouzard
50
     * @param $field
51
     * @param $table
52
     * @return bool|\mysqli_result
53
     */
54
    public function addField($field, $table)
55
    {
56
        global $xoopsDB;
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...
57
        $result = $xoopsDB->queryF('ALTER TABLE ' . $table . " ADD $field;");
58
59
        return $result;
60
    }
61
62
    /**
63
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
64
     *
65
     * @param string $folder Le chemin complet du répertoire à vérifier
66
     *
67
     * @return void
68
     */
69
    public static function prepareFolder($folder)
70
    {
71
        try {
72
            if (!@mkdir($folder) && !is_dir($folder)) {
73
                throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
74
            } else {
75
                file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
76
            }
77
        } catch (Exception $e) {
78
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br>';
79
        }
80
    }
81
        
82
    public static function cleanCache()
83
    {
84
          $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
85
            if (method_exists($cacheHelper,'clear')) {
86
                $cacheHelper->clear();
87
            return;
88
          }
89
    // for 2.5 systems, clear everything
90
          require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php';
91
          $maintenance = new SystemMaintenance();
92
            $cacheList = [
93
            3, // xoops_cache
94
            ];
95
        $maintenance->CleanCache($cacheList);
96
        xoops_setActiveModules();
97
    }
98
99
    /**
100
     * Checks if a user is admin of NewBB
101
     *
102
     * @return boolean
103
     */
104
    public static function userIsAdmin()
0 ignored issues
show
Coding Style introduced by
userIsAdmin uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
105
    {
106
        $helper = Newbb::getInstance();
107
108
        static $newbbIsAdmin;
109
110
        if (isset($newbbIsAdmin)) {
111
            return $newbbIsAdmin;
112
        }
113
114
        if (!$GLOBALS['xoopsUser']) {
115
            $newbbIsAdmin = false;
116
        } else {
117
            $newbbIsAdmin = $GLOBALS['xoopsUser']->isAdmin($helper->getModule()->getVar('mid'));
118
        }
119
120
        return $newbbIsAdmin;
121
    }
122
}
123