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

AboutUtility::rrmdir()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 31
Code Lines 17

Duplication

Lines 3
Ratio 9.68 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 7
nop 1
dl 3
loc 31
rs 4.909
c 0
b 0
f 0
1
<?php
2
/*
3
 About Utility Class Definition
4
5
 You may not change or alter any portion of this comment or credits of
6
 supporting developers from this source code or any supporting source code
7
 which is considered copyrighted (c) material of the original comment or credit
8
 authors.
9
10
 This program is distributed in the hope that it will be useful, but
11
 WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
/**
15
 * Module:  About
16
 *
17
 * @package::    \module\about\class
18
 * @license      http://www.fsf.org/copyleft/gpl.html GNU public license
19
 * @copyright    http://xoops.org 2001-2017 &copy; XOOPS Project
20
 * @author       ZySpec <[email protected]>
21
 * @author       Mamba <[email protected]>
22
 * @since::      File available since version 1.54
23
 */
24
25
 /**
26
  * AboutUtility
27
  *
28
  * Static utility class to provide common functionality
29
  *
30
  */
31
class AboutUtility
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...
32
{
33
    /**
34
     *
35
     * Verifies XOOPS version meets minimum requirements for this module
36
     * @static
37
     * @param XoopsModule
38
     *
39
     * @return bool true if meets requirements, false if not
40
     */
41
    public static function checkVerXoops(XoopsModule $module)
42
    {
43
        xoops_loadLanguage('admin', $module->dirname());
44
        //check for minimum XOOPS version
45
        $currentVer  = substr(XOOPS_VERSION, 6); // get the numeric part of string
46
        $currArray   = explode('.', $currentVer);
47
        $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
48
        $reqArray    = explode('.', $requiredVer);
49
        $success     = true;
50
        foreach ($reqArray as $k => $v) {
51
            if (isset($currArray[$k])) {
52
                if ($currArray[$k] > $v) {
53
                    break;
54
                } elseif ($currArray[$k] == $v) {
55
                    continue;
56
                } else {
57
                    $success = false;
58
                    break;
59
                }
60
            } else {
61
                if ((int)$v > 0) { // handles versions like x.x.x.0_RC2
62
                    $success = false;
63
                    break;
64
                }
65
            }
66
        }
67
68
        if (false === $success) {
69
            $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
70
        }
71
72
        return $success;
73
    }
74
    /**
75
     *
76
     * Verifies PHP version meets minimum requirements for this module
77
     * @static
78
     * @param XoopsModule
79
     *
80
     * @return bool true if meets requirements, false if not
81
     */
82
    public static function checkVerPHP(XoopsModule $module)
83
    {
84
        xoops_loadLanguage('admin', $module->dirname());
85
        // check for minimum PHP version
86
        $success = true;
87
        $verNum  = phpversion();
88
        $reqVer  = $module->getInfo('min_php');
89
        if ((false !== $reqVer) && ('' !== $reqVer)) {
90
            if (version_compare($verNum, (string)$reqVer, '<')) {
91
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_PHP, $reqVer, $verNum));
92
                $success = false;
93
            }
94
        }
95
        return $success;
96
    }
97
    /**
98
     *
99
     * Remove files and (sub)directories
100
     *
101
     * @param string $src source directory to delete
102
     *
103
     * @see Xmf\Module\Helper::getHelper()
104
     * @see Xmf\Module\Helper::isUserAdmin()
105
     *
106
     * @return bool true on success
107
     */
108
    public static function deleteDirectory($src)
0 ignored issues
show
Coding Style introduced by
deleteDirectory 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...
109
    {
110
        // Only continue if user is a 'global' Admin
111 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
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...
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...
112
            return false;
113
        }
114
115
        $success = true;
116
        // remove old files
117
        $dirInfo = new SplFileInfo($src);
118
        // validate is a directory
119
        if ($dirInfo->isDir()) {
120
            $fileList = array_diff(scandir($src), array('..', '.'));
121
            foreach ($fileList as $k => $v) {
122
                $fileInfo = new SplFileInfo("{$src}/{$v}");
123
                if ($fileInfo->isDir()) {
124
                    // recursively handle subdirectories
125
                    if (!$success = self::deleteDirectory($fileInfo->getRealPath())) {
126
                        break;
127
                    }
128
                } else {
129
                    // delete the file
130
                    if (!($success = unlink($fileInfo->getRealPath()))) {
131
                        break;
132
                    }
133
                }
134
            }
135
            // now delete this (sub)directory if all the files are gone
136
            if ($success) {
137
                $success = rmdir($dirInfo->getRealPath());
138
            }
139
        } else {
140
            // input is not a valid directory
141
            $success = false;
142
        }
143
        return $success;
144
    }
145
146
    /**
147
     *
148
     * Recursively remove directory
149
     *
150
     * @todo currently won't remove directories with hidden files, should it?
151
     *
152
     * @param string $src directory to remove (delete)
153
     *
154
     * @return bool true on success
155
     */
156
    public static function rrmdir($src)
0 ignored issues
show
Coding Style introduced by
rrmdir 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...
157
    {
158
        // Only continue if user is a 'global' Admin
159 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
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...
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...
160
            return false;
161
        }
162
163
        // If source is not a directory stop processing
164
        if (!is_dir($src)) {
165
            return false;
166
        }
167
168
        $success = true;
0 ignored issues
show
Unused Code introduced by
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
169
170
        // Open the source directory to read in files
171
        $iterator = new DirectoryIterator($src);
172
       foreach ($iterator as $fObj) {
173
            if ($fObj->isFile()) {
174
                $filename = $fObj->getPathname();
175
                $fObj = null; // clear this iterator object to close the file
176
                if (!unlink($filename)) {
177
                    return false; // couldn't delete the file
178
                }
179
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
180
                // Try recursively on directory
181
                self::rrmdir($fObj->getPathname());
182
            }
183
        }
184
        $iterator = null;   // clear iterator Obj to close file/directory
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
185
        return rmdir($src); // remove the directory & return results
186
    }
187
188
    /**
189
     * Recursively move files from one directory to another
190
     *
191
     * @param string $src - Source of files being moved
192
     * @param string $dest - Destination of files being moved
193
     *
194
     * @return bool true on success
195
     */
196
    public static function rmove($src, $dest)
0 ignored issues
show
Coding Style introduced by
rmove 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...
197
    {
198
        // Only continue if user is a 'global' Admin
199 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
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...
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...
200
            return false;
201
        }
202
203
        // If source is not a directory stop processing
204
        if (!is_dir($src)) {
205
            return false;
206
        }
207
208
        // If the destination directory does not exist and could not be created stop processing
209
        if (!is_dir($dest) && !mkdir($dest, 0755)) {
210
            return false;
211
        }
212
213
        // Open the source directory to read in files
214
        $iterator = new DirectoryIterator($src);
215 View Code Duplication
        foreach ($iterator as $fObj) {
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...
216
            if ($fObj->isFile()) {
217
                rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
218
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
219
                // Try recursively on directory
220
                self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
221
//                rmdir($fObj->getPath()); // now delete the directory
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
222
            }
223
        }
224
        $iterator = null;   // clear iterator Obj to close file/directory
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
        return rmdir($src); // remove the directory & return results
226
    }
227
228
    /**
229
     * Recursively copy directories and files from one directory to another
230
     *
231
     * @param string $src  - Source of files being moved
232
     * @param string $dest - Destination of files being moved
233
     *
234
     * @see Xmf\Module\Helper::getHelper()
235
     * @see Xmf\Module\Helper::isUserAdmin()
236
     *
237
     * @return bool true on success
238
     */
239
    public static function rcopy($src, $dest)
0 ignored issues
show
Coding Style introduced by
rcopy 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...
240
    {
241
        // Only continue if user is a 'global' Admin
242 View Code Duplication
        if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
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...
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
            return false;
244
        }
245
246
        // If source is not a directory stop processing
247
        if (!is_dir($src)) {
248
            return false;
249
        }
250
251
        // If the destination directory does not exist and could not be created stop processing
252
        if (!is_dir($dest) && !mkdir($dest, 0755)) {
253
            return false;
254
        }
255
256
        // Open the source directory to read in files
257
        $iterator = new DirectoryIterator($src);
258 View Code Duplication
        foreach($iterator as $fObj) {
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...
259
            if($fObj->isFile()) {
260
                copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
261
            } else if(!$fObj->isDot() && $fObj->isDir()) {
262
                self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj-getFilename());
263
            }
264
        }
265
        return true;
266
    }
267
}
268