Completed
Pull Request — master (#3)
by
unknown
01:52
created

action.module.php ➔ xoops_module_pre_install_xsitemap()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
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 42 and the first side effect is on line 31.

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
 * Module: xsitemap
13
 *
14
 * @package    module\xsitemap\includes
15
 * @author     Taiwen Jiang <[email protected]>
16
 * @author     ZySpec <[email protected]>
17
 * @copyright  http://xoops.org 2001-2017 XOOPS Project
18
 * @license    http://www.fsf.org/copyleft/gpl.html GNU public license
19
 * @link       http://xoops.org XOOPS
20
 * @since      1.00
21
 */
22
23
/**
24
 * @internal {Make sure you PROTECT THIS FILE}
25
 */
26
27
if ((!defined('XOOPS_ROOT_PATH'))
28
   || !($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...
29
   || !($GLOBALS['xoopsUser']->isAdmin()))
30
{
31
     exit("Restricted access" . PHP_EOL);
32
}
33
34
/**
35
 *
36
 * Prepares system prior to attempting to install module
37
 *
38
 * @param XoopsModule $module
39
 *
40
 * @return bool true if ready to install, false if not
41
 */
42 View Code Duplication
function xoops_module_pre_install_xsitemap(XoopsModule $module)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
43
{
44
    $utilsClass = ucfirst($moduleDirName) . 'Utilities';
0 ignored issues
show
Bug introduced by
The variable $moduleDirName does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
45
    if (!class_exists($utilsClass)) {
46
        xoops_load('utilities', $moduleDirName);
0 ignored issues
show
Bug introduced by
The variable $moduleDirName does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
47
    }
48
49
    $xoopsSuccess = $utilsClass::checkXoopsVer($module);
50
    $phpSuccess   = $utilsClass::checkPHPVer($module);
51
    return $xoopsSuccess && $phpSuccess;
52
}
53
54
/**
55
 *
56
 * Performs tasks required during installation of the module
57
 *
58
 * @param XoopsModule $module
59
 *
60
 * @return bool true if installation successful, false if not
61
 */
62
function xoops_module_install_xsitemap(&$module) {
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    return true; //
64
    /** @internal following code removed, it will fail because module not fully loaded/available until
65
     * after install, module now uses XOOPS preload instead */
66
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
    //28/08/2009 by urbanspaceman
68
    include_once $GLOBALS['xoops']->path("class/tree.php");
69
    include_once $GLOBALS['xoops']->path("modules/" . $module->dirname() . "/class/plugin.php");
70
    include_once $GLOBALS['xoops']->path("modules/" . $module->dirname() . "/include/functions.php");
71
    include_once $GLOBALS['xoops']->path("modules/" . $module->dirname(). "/class/dummy.php");
72
73
    //Create the xsitemap.xml file in the site root
74
    $xsitemap_show = xsitemap_generate_sitemap();
75
    return xsitemap_save($xsitemap_show) ? true : false;
76
    */
77
}
78
79
/**
80
 *
81
 * Prepares system prior to attempting to update module
82
 *
83
 * @param XoopsModule $module
84
 *
85
 * @return bool true if successfully ready to update module, false if not
86
 */
87 View Code Duplication
function xoops_module_pre_update_xsitemap($module)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
88
{
89
    $utilsClass = ucfirst($moduleDirName) . 'Utilities';
0 ignored issues
show
Bug introduced by
The variable $moduleDirName does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
90
    if (!class_exists($utilsClass)) {
91
        xoops_load('utilities', $moduleDirName);
0 ignored issues
show
Bug introduced by
The variable $moduleDirName does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
92
    }
93
94
    $xoopsSuccess = $utilsClass::checkXoopsVer($module);
95
    $phpSuccess   = $utilsClass::checkPHPVer($module);
96
    return $xoopsSuccess && $phpSuccess;
97
}
98
99
/**
100
 *
101
 * Functions to upgrade from previous version of the module
102
 *
103
 * @param XoopsModule $module
104
 * @param int $curr_version version number of module currently installed
0 ignored issues
show
Documentation introduced by
Should the type for parameter $curr_version not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
105
 *
106
 * @return bool true if successfully updated module, false if not
107
 */
108
function xoops_module_update_xsitemap($module, $curr_version = null)
0 ignored issues
show
Unused Code introduced by
The parameter $curr_version is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
{
110
/*======================================================================
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
    //----------------------------------------------------------------
112
    // Remove xSitemap uploads folder (and all subfolders) if they exist
113
    //----------------------------------------------------------------*
114
    $utilsClass = ucfirst($moduleDirName) . 'Utilities';
115
    if (!class_exists($utilsClass)) {
116
        xoops_load('utilities', $moduleDirName);
117
    }
118
119
    // Recursively delete directories
120
    $xsUploadDir = realpath(XOOPS_UPLOAD_PATH . "/" . $module->dirname());
121
    $success = $utilsClass::rrmdir($xsUploadDir);
122
    if (true !== $success) {
123
        \Xmf\Language::load('admin', $module->dirname());
124
        $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $xsUploadDir));
125
    }
126
    return $success;
127
======================================================================*/
128
129
    $moduleDirName = $module->getVar('dirname');
130
    $xsHelper = \Xmf\Module\Helper::getHelper($moduleDirName);
131
132
    $utilsClass = ucfirst($moduleDirName) . 'Utilities';
133
    if (!class_exists($utilsClass)) {
134
        xoops_load('utilities', $moduleDirName);
135
    }
136
    //-----------------------------------------------------------------------
137
    // Upgrade for Xsitemap < 1.54
138
    //-----------------------------------------------------------------------
139
140
    $success = true;
141
142
    $xsHelper->loadLanguage('modinfo');
143
    $xsHelper->loadLanguage('admin');
144
145
    if ($prev_version < 154) {
0 ignored issues
show
Bug introduced by
The variable $prev_version does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
146
        //----------------------------------------------------------------
147
        // Remove previous css & images directories since they've been relocated to ./assets
148
        // Also remove uploads directories since they're no longer used
149
        //----------------------------------------------------------------
150
        $old_directories = array($xsHelper->path('css/'),
151
            $xsHelper->path('js/'),
152
            $xsHelper->path('images/'),
153
            XOOPS_UPLOAD_PATH . "/" . $module->dirname()
154
        );
155 View Code Duplication
        foreach ($old_directories as $old_dir) {
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...
156
            $dirInfo = new SplFileInfo($old_dir);
157
            if ($dirInfo->isDir()) {
158
                // The directory exists so delete it
159
                if (false === $utilsClass::rrmdir($old_dir)) {
160
                    $xoopsModule->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir));
0 ignored issues
show
Bug introduced by
The variable $xoopsModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
161
                    return false;
162
                }
163
            }
164
            unset($dirInfo);
165
        }
166
167
        //-----------------------------------------------------------------------
168
        // Remove ./template/*.html (except index.html) files since they've
169
        // been replaced by *.tpl files
170
        // Note: this will also remove /template/xsitemap_style.html since it's no longer used
171
        //-----------------------------------------------------------------------
172
        $path       = $xsHelper->path('templates/');
173
        $unfiltered = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
174
        $iterator   = new RegexIterator($unfiltered, "/.*\.html/");
175
        foreach ($iterator as $name => $fObj) {
176
            if (($fObj->isFile()) && ('index.html' !== $fObj->getFilename())) {
177
                if (false === ($success = unlink($fObj->getPathname()))) {
178
                    $xoopsModule->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $fObj->getPathname()));
0 ignored issues
show
Bug introduced by
The variable $xoopsModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
179
                    return false;
180
                }
181
            }
182
        }
183
184
        //-----------------------------------------------------------------------
185
        // Now remove a some misc files that were renamed or deprecated
186
        //-----------------------------------------------------------------------
187
        $oldFiles = array($xsHelper->path('include/install.php'),
188
                          $xsHelper->path('class/module.php'),
189
                          $xsHelper->path('class/menu.php')
190
        );
191
        foreach($oldFiles as $file) {
192
            if (is_file($file)) {
193 View Code Duplication
                if (false === ($delOk = unlink($file))) {
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...
194
                    $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $file));
195
                }
196
                $success = $success && $delOk;
197
            }
198
        }
199
    }
200
    return $success;
201
}
202
203
/**
204
 *
205
 * Function to perform before module uninstall
206
 *
207
 * @param XoopsModule $module
208
 *
209
 * @return bool true if successfully executed, false if not
210
 */
211
function xoops_module_pre_uninstall_xsitemap($module)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
212
{
213
    return true;
214
}
215
216
/**
217
 *
218
 * Function to complete upon module uninstall
219
 *
220
 * @param XoopsModule $module
221
 *
222
 * @return bool true if successfully executed uninstall of module, false if not
223
 */
224
function xoops_module_uninstall_xsitemap($module)
0 ignored issues
show
Coding Style introduced by
xoops_module_uninstall_xsitemap 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...
225
{
226
//    return true;
227
    $moduleDirName = $module->getVar('dirname');
228
    $xsHelper = \Xmf\Module\Helper::getHelper($moduleDirName);
229
    $utilsClass = ucfirst($moduleDirName) . 'Utilities';
230
    if (!class_exists($utilsClass)) {
231
        xoops_load('utilities', $moduleDirName);
232
    }
233
234
    $success = true;
235
    $xsHelper->loadLanguage('admin');
236
237
    //------------------------------------------------------------------
238
    // Remove xSitemap uploads folder (and all subfolders) if they exist
239
    //------------------------------------------------------------------
240
241
    $old_directories = array($GLOBALS['xoops']->path("uploads/{$moduleDirName}"));
242 View Code Duplication
    foreach ($old_directories as $old_dir) {
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...
243
        $dirInfo = new SplFileInfo($old_dir);
244
        if ($dirInfo->isDir()) {
245
            // The directory exists so delete it
246
            if (false === $utilsClass::rrmdir($old_dir)) {
247
                $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_DEL_PATH, $old_dir));
248
                $success = false;
249
            }
250
        }
251
        unset($dirInfo);
252
    }
253
    //------------------------------------------------------------------
254
    // Remove xsitemap.xml from XOOPS root folder if it exists
255
    //------------------------------------------------------------------
256
    $xmlfile = $GLOBALS['xoops']->path('xsitemap.xml');
257 View Code Duplication
    if (is_file($xmlfile)) {
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...
258
        if (false === ($delOk = unlink($xmlfile))) {
259
            $module->setErrors(sprintf(_AM_XSITEMAP_ERROR_BAD_REMOVE, $xmlfile));
260
        }
261
    }
262
    return $success && $delOk;
0 ignored issues
show
Bug introduced by
The variable $delOk does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
263
}
264