XoopsModules25x /
xoopsfaq
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php declare(strict_types=1); |
||||
| 2 | |||||
| 3 | namespace XoopsModules\Xoopsfaq\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 GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
||||
| 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 | final public static function createFolder(string $folder): void |
||||
| 28 | {
|
||||
| 29 | try {
|
||||
| 30 | if (!\is_dir($folder)) {
|
||||
| 31 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) {
|
||||
| 32 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder));
|
||||
| 33 | } |
||||
| 34 | |||||
| 35 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||||
| 36 | } |
||||
| 37 | } catch (\Exception $e) {
|
||||
| 38 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||||
| 39 | } |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | final public static function copyFile(string $file, string $folder): bool |
||||
| 43 | {
|
||||
| 44 | return \copy($file, $folder); |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | final public static function recurseCopy(string $src, string $dst): void |
||||
| 48 | {
|
||||
| 49 | $dir = \opendir($src); |
||||
| 50 | // @mkdir($dst); |
||||
| 51 | try {
|
||||
| 52 | if (!\mkdir($dst) && !\is_dir($dst)) {
|
||||
| 53 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.');
|
||||
| 54 | } |
||||
| 55 | } catch (\RuntimeException $e) {
|
||||
| 56 | echo 'Caught exception: ', $e->getMessage(), "<br>\n"; |
||||
| 57 | } |
||||
| 58 | while (false !== ($file = \readdir($dir))) {
|
||||
| 59 | if (('.' !== $file) && ('..' !== $file)) {
|
||||
| 60 | if (\is_dir($src . '/' . $file)) {
|
||||
| 61 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||||
| 62 | } else {
|
||||
| 63 | \copy($src . '/' . $file, $dst . '/' . $file); |
||||
| 64 | } |
||||
| 65 | } |
||||
| 66 | } |
||||
| 67 | \closedir($dir); |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * Remove files and (sub)directories |
||||
| 72 | * |
||||
| 73 | * @param string $src source directory to delete |
||||
| 74 | * |
||||
| 75 | * @return bool true on success |
||||
| 76 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||||
| 77 | * |
||||
| 78 | * @uses \Xmf\Module\Helper::getHelper() |
||||
| 79 | */ |
||||
| 80 | final public static function deleteDirectory(string $src): bool |
||||
| 81 | {
|
||||
| 82 | // Only continue if user is a 'global' Admin |
||||
| 83 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
|
||||
| 84 | return false; |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | $success = true; |
||||
| 88 | // remove old files |
||||
| 89 | $dirInfo = new \SplFileInfo($src); |
||||
| 90 | // validate is a directory |
||||
| 91 | if ($dirInfo->isDir()) {
|
||||
| 92 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||||
| 93 | foreach ($fileList as $k => $v) {
|
||||
| 94 | $fileInfo = new \SplFileInfo("{$src}/{$v}");
|
||||
| 95 | if ($fileInfo->isDir()) {
|
||||
| 96 | // recursively handle subdirectories |
||||
| 97 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) {
|
||||
| 98 | break; |
||||
| 99 | } |
||||
| 100 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) {
|
||||
| 101 | break; |
||||
| 102 | } |
||||
| 103 | } |
||||
| 104 | // now delete this (sub)directory if all the files are gone |
||||
| 105 | if ($success) {
|
||||
| 106 | $success = \rmdir($dirInfo->getRealPath()); |
||||
| 107 | } |
||||
| 108 | } else {
|
||||
| 109 | // input is not a valid directory |
||||
| 110 | $success = false; |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | return $success; |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * Recursively remove directory |
||||
| 118 | * |
||||
| 119 | * @todo currently won't remove directories with hidden files, should it? |
||||
| 120 | * |
||||
| 121 | * @param string $src directory to remove (delete) |
||||
| 122 | * |
||||
| 123 | * @return bool true on success |
||||
| 124 | */ |
||||
| 125 | final public static function rrmdir(string $src): bool |
||||
| 126 | {
|
||||
| 127 | // Only continue if user is a 'global' Admin |
||||
| 128 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
|
||||
| 129 | return false; |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | // If source is not a directory stop processing |
||||
| 133 | if (!\is_dir($src)) {
|
||||
| 134 | return false; |
||||
| 135 | } |
||||
| 136 | |||||
| 137 | $success = true; |
||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||
| 138 | |||||
| 139 | // Open the source directory to read in files |
||||
| 140 | $iterator = new \DirectoryIterator($src); |
||||
| 141 | foreach ($iterator as $fObj) {
|
||||
| 142 | if ($fObj->isFile()) {
|
||||
| 143 | $filename = $fObj->getPathname(); |
||||
| 144 | $fObj = null; // clear this iterator object to close the file |
||||
|
0 ignored issues
–
show
|
|||||
| 145 | if (!\unlink($filename)) {
|
||||
| 146 | return false; // couldn't delete the file |
||||
| 147 | } |
||||
| 148 | } elseif (!$fObj->isDot() && $fObj->isDir()) {
|
||||
| 149 | // Try recursively on directory |
||||
| 150 | self::rrmdir($fObj->getPathname()); |
||||
| 151 | } |
||||
| 152 | } |
||||
| 153 | $iterator = null; // clear iterator Obj to close file/directory |
||||
|
0 ignored issues
–
show
|
|||||
| 154 | |||||
| 155 | return \rmdir($src); // remove the directory & return results |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | /** |
||||
| 159 | * Recursively move files from one directory to another |
||||
| 160 | * |
||||
| 161 | * @param string $src - Source of files being moved |
||||
| 162 | * @param string $dest - Destination of files being moved |
||||
| 163 | * |
||||
| 164 | * @return bool true on success |
||||
| 165 | */ |
||||
| 166 | final public static function rmove(string $src, string $dest): bool |
||||
| 167 | {
|
||||
| 168 | // Only continue if user is a 'global' Admin |
||||
| 169 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
|
||||
| 170 | return false; |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | // If source is not a directory stop processing |
||||
| 174 | if (!\is_dir($src)) {
|
||||
| 175 | return false; |
||||
| 176 | } |
||||
| 177 | |||||
| 178 | // If the destination directory does not exist and could not be created stop processing |
||||
| 179 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) {
|
||||
| 180 | return false; |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | // Open the source directory to read in files |
||||
| 184 | $iterator = new \DirectoryIterator($src); |
||||
| 185 | foreach ($iterator as $fObj) {
|
||||
| 186 | if ($fObj->isFile()) {
|
||||
| 187 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
|
||||
| 188 | } elseif (!$fObj->isDot() && $fObj->isDir()) {
|
||||
| 189 | // Try recursively on directory |
||||
| 190 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
|
||||
| 191 | // rmdir($fObj->getPath()); // now delete the directory |
||||
| 192 | } |
||||
| 193 | } |
||||
| 194 | $iterator = null; // clear iterator Obj to close file/directory |
||||
|
0 ignored issues
–
show
|
|||||
| 195 | |||||
| 196 | return \rmdir($src); // remove the directory & return results |
||||
| 197 | } |
||||
| 198 | |||||
| 199 | /** |
||||
| 200 | * Recursively copy directories and files from one directory to another |
||||
| 201 | * |
||||
| 202 | * @param string $src - Source of files being moved |
||||
| 203 | * @param string $dest - Destination of files being moved |
||||
| 204 | * |
||||
| 205 | * @return bool true on success |
||||
| 206 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||||
| 207 | * |
||||
| 208 | * @uses \Xmf\Module\Helper::getHelper() |
||||
| 209 | */ |
||||
| 210 | final public function rcopy(string $src, string $dest): bool |
||||
| 211 | {
|
||||
| 212 | // Only continue if user is a 'global' Admin |
||||
| 213 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
|
||||
| 214 | return false; |
||||
| 215 | } |
||||
| 216 | |||||
| 217 | // If source is not a directory stop processing |
||||
| 218 | if (!\is_dir($src)) {
|
||||
| 219 | return false; |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | // If the destination directory does not exist and could not be created stop processing |
||||
| 223 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) {
|
||||
| 224 | return false; |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | // Open the source directory to read in files |
||||
| 228 | $iterator = new \DirectoryIterator($src); |
||||
| 229 | foreach ($iterator as $fObj) {
|
||||
| 230 | if ($fObj->isFile()) {
|
||||
| 231 | \copy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
|
||||
| 232 | } elseif (!$fObj->isDot() && $fObj->isDir()) {
|
||||
| 233 | self::rcopy($fObj->getPathname(), "{$dest}/" . $fObj->getFilename());
|
||||
|
0 ignored issues
–
show
The method
XoopsModules\Xoopsfaq\Co...ilesManagement::rcopy() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 234 | } |
||||
| 235 | } |
||||
| 236 | |||||
| 237 | return true; |
||||
| 238 | } |
||||
| 239 | } |
||||
| 240 |