Issues (621)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

class/Common/FilesManagement.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace XoopsModules\Smallworld\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright   XOOPS Project (https://xoops.org)
17
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @author      mamba <[email protected]>
19
 */
20
trait FilesManagement
21
{
22
    /**
23
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
24
     *
25
     * @param string $folder The full path of the directory to check
26
     *
27
     * @return void
28
     * @throws \RuntimeException
29
     */
30
    public static function createFolder($folder)
31
    {
32
        try {
33
            if (!file_exists($folder)) {
34
                if (!is_dir($folder) && !mkdir($folder) && !is_dir($folder)) {
35
                    throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
36
                }
37
38
                file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
39
            }
40
        } catch (\Exception $e) {
41
            echo 'Caught exception: ', $e->getMessage(), '<br>';
42
        }
43
    }
44
45
    /**
46
     * @param $file
47
     * @param $folder
48
     * @return bool
49
     */
50
    public static function copyFile($file, $folder)
51
    {
52
        return copy($file, $folder);
53
    }
54
55
    /**
56
     * @param $src
57
     * @param $dst
58
     */
59
    public static function recurseCopy($src, $dst)
60
    {
61
        $dir = opendir($src);
62
        //        @mkdir($dst);
63
        if (!@mkdir($dst) && !is_dir($dst)) {
64
            throw new \RuntimeException('The directory ' . $dst . ' could not be created.');
65
        }
66
        while (false !== ($file = readdir($dir))) {
67
            if (('.' !== $file) && ('..' !== $file)) {
68
                if (is_dir($src . '/' . $file)) {
69
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
70
                } else {
71
                    copy($src . '/' . $file, $dst . '/' . $file);
72
                }
73
            }
74
        }
75
        closedir($dir);
76
    }
77
78
    /**
79
     * Copy a file, or recursively copy a folder and its contents
80
     * @param string $source Source path
81
     * @param string $dest   Destination path
82
     * @return      bool     Returns true on success, false on failure
83
     * @author      Aidan Lister <[email protected]>
84
     * @version     1.0.1
85
     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
86
     */
87
    public static function xcopy($source, $dest)
88
    {
89
        // Check for symlinks
90
        if (is_link($source)) {
91
            return symlink(readlink($source), $dest);
92
        }
93
94
        // Simple copy for a file
95
        if (is_file($source)) {
96
            return copy($source, $dest);
97
        }
98
99
        // Make destination directory
100
        if (!is_dir($dest)) {
101
            if (!mkdir($dest) && !is_dir($dest)) {
102
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $dest));
103
            }
104
        }
105
106
        // Loop through the folder
107
        $dir = dir($source);
108
        if (@is_dir($dir)) {
109
            while (false !== $entry = $dir->read()) {
110
                // Skip pointers
111
                if ('.' === $entry || '..' === $entry) {
112
                    continue;
113
                }
114
                // Deep copy directories
115
                self::xcopy("$source/$entry", "$dest/$entry");
116
            }
117
            // Clean up
118
            $dir->close();
119
        }
120
121
        return true;
122
    }
123
124
    /**
125
     * Remove files and (sub)directories
126
     *
127
     * @param string $src source directory to delete
128
     *
129
     * @return bool true on success
130
     * @uses \Xmf\Module\Helper::isUserAdmin()
131
     *
132
     * @uses \Xmf\Module\Helper::getHelper()
133
     */
134
    public static function deleteDirectory($src)
135
    {
136
        // Only continue if user is a 'global' Admin
137
        if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
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...
138
            return false;
139
        }
140
141
        $success = true;
142
        // remove old files
143
        $dirInfo = new \SplFileInfo($src);
144
        // validate is a directory
145
        if ($dirInfo->isDir()) {
146
            $fileList = array_diff(scandir($src, SCANDIR_SORT_NONE), ['..', '.']);
147
            foreach ($fileList as $k => $v) {
148
                $fileInfo = new \SplFileInfo("{$src}/{$v}");
149
                if ($fileInfo->isDir()) {
150
                    // recursively handle subdirectories
151
                    if (!$success = self::deleteDirectory($fileInfo->getRealPath())) {
152
                        break;
153
                    }
154
                } elseif (!($success = unlink($fileInfo->getRealPath()))) {
155
                    break;
156
                }
157
            }
158
            // now delete this (sub)directory if all the files are gone
159
            if ($success) {
160
                $success = rmdir($dirInfo->getRealPath());
161
            }
162
        } else {
163
            // input is not a valid directory
164
            $success = false;
165
        }
166
167
        return $success;
168
    }
169
170
    /**
171
     * Recursively remove directory
172
     *
173
     * @todo currently won't remove directories with hidden files, should it?
174
     *
175
     * @param string $src directory to remove (delete)
176
     *
177
     * @return bool true on success
178
     */
179
    public static function rrmdir($src)
180
    {
181
        // Only continue if user is a 'global' Admin
182
        if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
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...
183
            return false;
184
        }
185
186
        // If source is not a directory stop processing
187
        if (!is_dir($src)) {
188
            return false;
189
        }
190
191
        $success = true;
0 ignored issues
show
$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...
192
193
        // Open the source directory to read in files
194
        $iterator = new \DirectoryIterator($src);
195
        foreach ($iterator as $fObj) {
196
            if ($fObj->isFile()) {
197
                $filename = $fObj->getPathname();
198
                $fObj     = null; // clear this iterator object to close the file
199
                if (!unlink($filename)) {
200
                    return false; // couldn't delete the file
201
                }
202
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
203
                // Try recursively on directory
204
                self::rrmdir($fObj->getPathname());
205
            }
206
        }
207
        $iterator = null;   // clear iterator Obj to close file/directory
0 ignored issues
show
$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...
208
        return rmdir($src); // remove the directory & return results
209
    }
210
211
    /**
212
     * Recursively move files from one directory to another
213
     *
214
     * @param string $src  - Source of files being moved
215
     * @param string $dest - Destination of files being moved
216
     *
217
     * @return bool true on success
218
     */
219 View Code Duplication
    public static function rmove($src, $dest)
0 ignored issues
show
This method 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...
220
    {
221
        // Only continue if user is a 'global' Admin
222
        if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
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...
223
            return false;
224
        }
225
226
        // If source is not a directory stop processing
227
        if (!is_dir($src)) {
228
            return false;
229
        }
230
231
        // If the destination directory does not exist and could not be created stop processing
232
        if (!is_dir($dest) && !mkdir($dest) && !is_dir($dest)) {
233
            return false;
234
        }
235
236
        // Open the source directory to read in files
237
        $iterator = new \DirectoryIterator($src);
238
        foreach ($iterator as $fObj) {
239
            if ($fObj->isFile()) {
240
                rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
241
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
242
                // Try recursively on directory
243
                self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
244
                //                rmdir($fObj->getPath()); // now delete the directory
245
            }
246
        }
247
        $iterator = null;   // clear iterator Obj to close file/directory
0 ignored issues
show
$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...
248
        return rmdir($src); // remove the directory & return results
249
    }
250
251
    /**
252
     * Recursively copy directories and files from one directory to another
253
     *
254
     * @param string $src  - Source of files being moved
255
     * @param string $dest - Destination of files being moved
256
     *
257
     * @return bool true on success
258
     * @uses \Xmf\Module\Helper::isUserAdmin()
259
     *
260
     * @uses \Xmf\Module\Helper::getHelper()
261
     */
262 View Code Duplication
    public static function rcopy($src, $dest)
0 ignored issues
show
This method 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...
263
    {
264
        // Only continue if user is a 'global' Admin
265
        if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
0 ignored issues
show
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...
266
            return false;
267
        }
268
269
        // If source is not a directory stop processing
270
        if (!is_dir($src)) {
271
            return false;
272
        }
273
274
        // If the destination directory does not exist and could not be created stop processing
275
        if (!is_dir($dest) && !mkdir($dest) && !is_dir($dest)) {
276
            return false;
277
        }
278
279
        // Open the source directory to read in files
280
        $iterator = new \DirectoryIterator($src);
281
        foreach ($iterator as $fObj) {
282
            if ($fObj->isFile()) {
283
                copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
284
            } elseif (!$fObj->isDot() && $fObj->isDir()) {
285
                self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
286
            }
287
        }
288
289
        return true;
290
    }
291
}
292