Completed
Branch master (db7223)
by Michael
04:38 queued 02:32
created

onupdate.php ➔ xoops_module_pre_update_gbook()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 18
rs 9.2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 30 and the first side effect is on line 22.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project http://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @author       XOOPS Development Team
17
 */
18
19
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
20
    || !$GLOBALS['xoopsUser']->IsAdmin()
21
) {
22
    exit('Restricted access' . PHP_EOL);
23
}
24
25
/**
26
 * @param string $tablename
27
 *
28
 * @return bool
29
 */
30
function tableExists($tablename)
0 ignored issues
show
Coding Style introduced by
tableExists 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...
31
{
32
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
33
34
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) ? true : false;
35
}
36
37
/**
38
 *
39
 * Prepares system prior to attempting to install module
40
 * @param XoopsModule $module {@link XoopsModule}
41
 *
42
 * @return bool true if ready to install, false if not
43
 */
44
function xoops_module_pre_update_gbook(XoopsModule $module)
45
{
46
    $moduleDirName = basename(dirname(__DIR__));
47
    $className = ucfirst($moduleDirName) . 'Utilities';
48
    if (!class_exists($className)) {
49
        xoops_load('utilities', $moduleDirName);
50
    }
51
    //check for minimum XOOPS version
52
    if (!$className::checkXoopsVer($module)) {
53
        return false;
54
    }
55
56
    // check for minimum PHP version
57
    if (!$className::checkPHPVer($module)) {
58
        return false;
59
    }
60
    return true;
61
}
62
63
/**
64
 *
65
 * Performs tasks required during update of the module
66
 * @param XoopsModule $module {@link XoopsModule}
67
 * @param null        $previousVersion
68
 *
69
 * @return bool true if update successful, false if not
70
 */
71
72
function xoops_module_update_gbook(XoopsModule $module, $previousVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_gbook 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...
73
{
74
    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...
75
    if ($previousVersion < 111) {
76
        // delete old HTML template files ============================
77
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/');
78 View Code Duplication
        if (is_dir($templateDirectory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            $templateList = array_diff(scandir($templateDirectory), array('..', '.'));
80
            foreach ($templateList as $k => $v) {
81
                $fileInfo = new SplFileInfo($templateDirectory . $v);
82
                if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
83
                    if (file_exists($templateDirectory . $v)) {
84
                        unlink($templateDirectory . $v);
85
                    }
86
                }
87
            }
88
        }
89
        // delete old block html template files ============================
90
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n')
91
                                                     . '/templates/blocks/');
92 View Code Duplication
        if (is_dir($templateDirectory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            $templateList = array_diff(scandir($templateDirectory), array('..', '.'));
94
            foreach ($templateList as $k => $v) {
95
                $fileInfo = new SplFileInfo($templateDirectory . $v);
96
                if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
97
                    if (file_exists($templateDirectory . $v)) {
98
                        unlink($templateDirectory . $v);
99
                    }
100
                }
101
            }
102
        }
103
        //delete old files: ===================
104
        $oldFiles = array(
105
            '/assets/images/logo_module.png',
106
            '/templates/gbook.css'
107
        );
108
109
        foreach (array_keys($oldFiles) as $file) {
110
            if (is_file($file)) {
111
                unlink($GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . $oldFiles[$file]));
112
            }
113
        }
114
115
        //delete .html entries from the tpl table
116
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname',
117
                                                                                                          'n')
118
               . "' AND `tpl_file` LIKE '%.html%'";
119
        $xoopsDB->queryF($sql);
120
121
        // Load class XoopsFile ====================
122
        xoops_load('XoopsFile');
123
124
        //delete /images directory ============
125
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
126
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
127
        $folderHandler->delete($imagesDirectory);
128
    }
129
130
    $gpermHandler = xoops_getHandler('groupperm');
131
132
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
133
}
134