Completed
Push — master ( e95032...d1a68a )
by Michael
15s queued 13s
created

XoopsfaqUtility::checkVerPHP()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/*
3
 Xoopsfaq 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:  myQuiz
16
 *
17
 * @package::    \module\xoopsfaq\class
18
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
19
 * @copyright    http://xoops.org 2001-2017 &copy; XOOPS Project
20
 * @author       zyspec
21
 * @author       Mamba
22
 * @since::      File available since version 4.10
23
 */
24
25
 /**
26
  * XoopsfaqUtility
27
  *
28
  * Static utility class to provide common functionality
29
  *
30
  */
31
class XoopsfaqUtility
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($module)
42
    {
43
        $currentVersion = strtolower(str_replace('XOOPS ', '', XOOPS_VERSION));
44
        $requiredVersion = strtolower($module->getInfo('min_xoops'));
45
        $vc = version_compare ($currentVersion , $requiredVersion);
46
        $success = ($vc>=0);
47
        if (false === $success) {
48
            xoops_loadLanguage('admin', $module->dirname());
49
            $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_XOOPS, $requiredVersion, $currentVersion));
50
        }
51
52
        return $success;
53
    }
54
    /**
55
     *
56
     * Verifies PHP version meets minimum requirements for this module
57
     * @static
58
     * @param XoopsModule $module
59
     *
60
     * @return bool true if meets requirements, false if not
61
     */
62
    public static function checkVerPHP($module)
63
    {
64
        xoops_loadLanguage('admin', $module->dirname());
65
        // check for minimum PHP version
66
        $success = true;
67
        $verNum  = PHP_VERSION;
68
        $reqVer  = $module->getInfo('min_php');
69
        if ((false !== $reqVer) && ('' !== $reqVer)) {
70
            if (version_compare($verNum, (string)$reqVer, '<')) {
71
                $module->setErrors(sprintf(_AM_XOOPSFAQ_ERROR_BAD_PHP, $reqVer, $verNum));
72
                $success = false;
73
            }
74
        }
75
        return $success;
76
    }
77
    /**
78
     *
79
     * Remove files and (sub)directories
80
     *
81
     * @param string $src source directory to delete
82
     *
83
     * @see Xmf\Module\Helper::getHelper()
84
     * @see Xmf\Module\Helper::isUserAdmin()
85
     *
86
     * @return bool true on success
87
     */
88
    public static function deleteDirectory($src)
89
    {
90
        // Only continue if user is a 'global' Admin
91 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...
92
            return false;
93
        }
94
95
        $success = true;
96
        // remove old files
97
        $dirInfo = new SplFileInfo($src);
98
        // validate is a directory
99
        if ($dirInfo->isDir()) {
100
            $fileList = array_diff(scandir($src), array('..', '.'));
101
            foreach ($fileList as $k => $v) {
102
                $fileInfo = new SplFileInfo($src . '/' . $v);
103
                if ($fileInfo->isDir()) {
104
                    // recursively handle subdirectories
105
                    if (!$success = static::deleteDirectory($fileInfo->getRealPath())) {
106
                        break;
107
                    }
108
                } else {
109
                    // delete the file
110
                    if (!($success = unlink($fileInfo->getRealPath()))) {
111
                        break;
112
                    }
113
                }
114
            }
115
            // now delete this (sub)directory if all the files are gone
116
            if ($success) {
117
                $success = rmdir($dirInfo->getRealPath());
118
            }
119
        } else {
120
            // input is not a valid directory
121
            $success = false;
122
        }
123
        return $success;
124
    }
125
126
    /**
127
     *
128
     * Recursively remove directory
129
     *
130
     * @todo currently won't remove directories with hidden files, should it?
131
     *
132
     * @param string $src directory to remove (delete)
133
     *
134
     * @return bool true on success
135
     */
136
    public static function rrmdir($src)
137
    {
138
        // Only continue if user is a 'global' Admin
139 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...
140
            return false;
141
        }
142
143
        // If source is not a directory stop processing
144
        if (!is_dir($src)) {
145
            return false;
146
        }
147
148
        // Open the source directory to read in files
149
        $iterator = new DirectoryIterator($src);
150
        foreach ($iterator as $fObj) {
151
            if ($fObj->isFile()) {
152
                $filename = $fObj->getPathname();
153
                $fObj = null; // clear this iterator object to close the file
154
                if (!unlink($filename)) {
155
                    return false; // couldn't delete the file
156
                }
157
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
158
                // Try recursively on directory
159
                static::rrmdir($fObj->getPathname());
160
            }
161
        }
162
        $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...
163
        return rmdir($src); // remove the directory & return results
164
    }
165
166
    /**
167
     * Recursively move files from one directory to another
168
     *
169
     * @param String $src - Source of files being moved
170
     * @param String $dest - Destination of files being moved
171
     *
172
     * @return bool true on success
173
     */
174
    public static function rmove($src, $dest)
175
    {
176
        // Only continue if user is a 'global' Admin
177 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...
178
            return false;
179
        }
180
181
        // If source is not a directory stop processing
182
        if (!is_dir($src)) {
183
            return false;
184
        }
185
186
        // If the destination directory does not exist and could not be created stop processing
187
        if (!is_dir($dest) && !mkdir($dest, 0755)) {
188
            return false;
189
        }
190
191
        // Open the source directory to read in files
192
        $iterator = new DirectoryIterator($src);
193 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...
194
            if ($fObj->isFile()) {
195
                rename($fObj->getPathname(), $dest . '/' . $fObj->getFilename());
196
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
197
                // Try recursively on directory
198
                static::rmove($fObj->getPathname(), $dest . '/' . $fObj->getFilename());
199
            }
200
        }
201
        $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...
202
        return rmdir($src); // remove the directory & return results
203
    }
204
205
    /**
206
     * Recursively copy directories and files from one directory to another
207
     *
208
     * @param string $src  - Source of files being moved
209
     * @param string $dest - Destination of files being moved
210
     *
211
     * @see Xmf\Module\Helper::getHelper()
212
     * @see Xmf\Module\Helper::isUserAdmin()
213
     *
214
     * @return bool true on success
215
     */
216
    public static function rcopy($src, $dest)
217
    {
218
        // Only continue if user is a 'global' Admin
219 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...
220
            return false;
221
        }
222
223
        // If source is not a directory stop processing
224
        if (!is_dir($src)) {
225
            return false;
226
        }
227
228
        // If the destination directory does not exist and could not be created stop processing
229
        if (!is_dir($dest) && !mkdir($dest, 0755)) {
230
            return false;
231
        }
232
233
        // Open the source directory to read in files
234
        $iterator = new DirectoryIterator($src);
235 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...
236
            if($fObj->isFile()) {
237
                copy($fObj->getPathname(), $dest . '/' . $fObj->getFilename());
238
            } else if(!$fObj->isDot() && $fObj->isDir()) {
239
                static::rcopy($fObj->getPathname(), $dest . '/' . $fObj-getFilename());
240
            }
241
        }
242
        return true;
243
    }
244
245
    /**
246
     * Render the icon links
247
     *
248
     * @param array $icon_array contains operation=>icon_name as key=>value
249
     * @param mixed $param      HTML parameter
250
     * @param mixed $value      HTML parameter value to set
251
     * @param mixed $extra      are any additional HTML attributes desired for the <a> tag
252
     * @return string
253
     */
254
    public static function renderIconLinks($icon_array = array(), $param, $value = null, $extra = null)
255
    {
256
        $moduleDirName = basename(dirname(__DIR__));
257
        xoops_loadLanguage('admin', $moduleDirName);
258
        $ret = '';
259
        if (null !== $value) {
260
            foreach($icon_array as $_op => $icon) {
261
                if (false === strpos($icon, '.')) {
262
                    $iconName = $icon;
263
                    $iconExt = 'png';
264
                } else {
265
                    $iconName = substr($icon, 0, strlen($icon)-strrchr($icon, '.'));
266
                    $iconExt  = substr(strrchr($icon, '.'), 1);
267
                }
268
                $url = (!is_numeric($_op)) ? $_op . '?' . $param . '=' . $value : xoops_getenv('PHP_SELF') . '?op=' . $iconName . '&amp;' . $param . '=' . $value;
269
                if (null !== $extra) {
270
                    $url .= ' ' . $extra;
271
                }
272
                $title = constant (htmlspecialchars(mb_strtoupper('_XO_LA_' . $iconName)));
273
                $img = '<img src="' . Xmf\Module\Admin::iconUrl($iconName . '.' . $iconExt, '16') . '"'
274
                     . ' title ="' . $title . '"'
275
                     . ' alt = "' . $title . '"'
276
                     . ' class="bnone middle">';
277
                $ret .= '<a href="' . $url . '"' . $extra . '>' . $img . '</a>';
278
            }
279
        }
280
        return $ret;
281
    }
282
}
283