| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 4 |
if (!headers_sent()) { |
| 5 |
header('HTTP/1.1 403 Forbidden'); |
| 6 |
} |
| 7 |
die("Protected By WebTotem!"); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* WebTotem file info class for Wordpress. |
| 12 |
*/ |
| 13 |
class WebTotemFileInfo{ |
| 14 |
|
| 15 |
/** |
| 16 |
* Checks if the file scanner is usable. |
| 17 |
* |
| 18 |
* @link https://www.php.net/manual/en/class.splfileobject.php |
| 19 |
* |
| 20 |
* @return bool True if PHP class "SplFileObject" is available. |
| 21 |
*/ |
| 22 |
public static function isSplAvailable() |
| 23 |
{ |
| 24 |
return (bool) ( |
| 25 |
class_exists('SplFileObject') |
| 26 |
&& class_exists('FilesystemIterator') |
| 27 |
&& class_exists('RecursiveIteratorIterator') |
| 28 |
&& class_exists('RecursiveDirectoryIterator') |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Reads a directory and retrieves all its files. |
| 34 |
* |
| 35 |
* @param string $directory Where to execute the scanner. |
| 36 |
* @param string $filterby Either "file" or "directory". |
| 37 |
* @return array List of files in the specified directory. |
| 38 |
*/ |
| 39 |
public function getDirectoryTree($directory = '', $filterby = 'file', $recursively = true) |
| 40 |
{ |
| 41 |
$files = []; |
| 42 |
|
| 43 |
if (is_dir($directory) && self::isSplAvailable()) { |
| 44 |
$objects = []; |
| 45 |
|
| 46 |
// @codeCoverageIgnoreStart |
| 47 |
try { |
| 48 |
if ($recursively) { |
| 49 |
$flags = FilesystemIterator::KEY_AS_PATHNAME; |
| 50 |
$flags |= FilesystemIterator::CURRENT_AS_FILEINFO; |
| 51 |
$flags |= FilesystemIterator::SKIP_DOTS; |
| 52 |
$flags |= FilesystemIterator::UNIX_PATHS; |
| 53 |
$objects = new RecursiveIteratorIterator( |
| 54 |
new RecursiveDirectoryIterator($directory, $flags), |
| 55 |
RecursiveIteratorIterator::SELF_FIRST, |
| 56 |
RecursiveIteratorIterator::CATCH_GET_CHILD |
| 57 |
); |
| 58 |
} else { |
| 59 |
$objects = new DirectoryIterator($directory); |
| 60 |
} |
| 61 |
} catch (RuntimeException $exception) { |
| 62 |
/* ignore failure */ |
| 63 |
} |
| 64 |
// @codeCoverageIgnoreEnd |
| 65 |
|
| 66 |
foreach ($objects as $fifo) { |
| 67 |
$filepath = $fifo->getRealPath(); |
| 68 |
|
| 69 |
/* check files and directories */ |
| 70 |
if ($this->isIgnoredPath($filepath)) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
try { |
| 75 |
/* check only files */ |
| 76 |
if ($fifo->isFile() |
| 77 |
&& $filterby === 'file' |
| 78 |
) { |
| 79 |
$files[] = $filepath; |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
/* check only directories */ |
| 84 |
if ($fifo->isDir() |
| 85 |
&& $filterby === 'directory' |
| 86 |
) { |
| 87 |
$files[] = $filepath; |
| 88 |
continue; |
| 89 |
} |
| 90 |
} catch (RuntimeException $e) { |
| 91 |
// Skip failure |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
sort($files); |
| 96 |
} |
| 97 |
|
| 98 |
return $files; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Ignores files specified by the admins. |
| 103 |
* |
| 104 |
* @param string $path Path to the file or directory. |
| 105 |
* @return bool True if the path has to be ignored. |
| 106 |
*/ |
| 107 |
private function isIgnoredPath($path) |
| 108 |
{ |
| 109 |
$shouldBeIgnored = false; |
| 110 |
$ignored_directories = [ |
| 111 |
'/wt-security/lib/modules/logs/Scan.php' |
| 112 |
]; |
| 113 |
|
| 114 |
foreach ($ignored_directories as $ignored) { |
| 115 |
if (strpos($path, $ignored) !== false) { |
| 116 |
$shouldBeIgnored = true; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return $shouldBeIgnored; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the content of a file. |
| 126 |
* |
| 127 |
* If the file does not exists or is not readable the method will return |
| 128 |
* false. Make sure that you double check this with a condition using triple |
| 129 |
* equals in order to avoid ambiguous results when the file exists, is |
| 130 |
* readable, but is empty. |
| 131 |
* |
| 132 |
* @param string $path Relative or absolute path of the file. |
| 133 |
* @return string Content of the file, false if not accessible. |
| 134 |
*/ |
| 135 |
public static function fileContent($path = '') |
| 136 |
{ |
| 137 |
return (string) (is_readable($path) ? @file_get_contents($path) : ''); |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|