| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
// phpcs:ignoreFile -- third party library |
| 6 |
/* == DOCUMENTATION == |
| 7 |
For documentation and updates, visit http://abeautifulsite.net/notebook.php?article=21 |
| 8 |
*/ |
| 9 |
|
| 10 |
function php_file_tree($directory, $return_link, $extensions = array()) { |
| 11 |
// Generates a valid XHTML list of all directories, sub-directories, and files in $directory |
| 12 |
// Remove trailing slash |
| 13 |
$siteURL = $directory . '/'; |
| 14 |
$code = ""; |
| 15 |
if (substr($directory, -1) == "/") |
| 16 |
$directory = substr($directory, 0, strlen($directory) - 1); |
| 17 |
$code .= php_file_tree_dir($siteURL, $directory, $return_link, $extensions); |
| 18 |
return $code; |
| 19 |
} |
| 20 |
|
| 21 |
function php_file_tree_dir($siteURL, $directory, $return_link, $extensions = array(), $first_call = true) { |
| 22 |
// Recursive function called by php_file_tree() to list directories/files |
| 23 |
$php_file_tree = ""; |
| 24 |
|
| 25 |
$wp_all_backup_exclude_dir = explode("|", get_option('wp_all_backup_exclude_dir')); |
| 26 |
|
| 27 |
// Get and sort directories/files |
| 28 |
if (function_exists("scandir")) |
| 29 |
$file = scandir($directory); |
| 30 |
else |
| 31 |
$file = php4_scandir($directory); |
| 32 |
natcasesort($file); |
| 33 |
// Make directories first |
| 34 |
$files = $dirs = array(); |
| 35 |
foreach ($file as $this_file) { |
| 36 |
if (is_dir("$directory/$this_file")) |
| 37 |
$dirs[] = $this_file; |
| 38 |
else |
| 39 |
$files[] = $this_file; |
| 40 |
} |
| 41 |
$file = array_merge($dirs, $files); |
| 42 |
|
| 43 |
// Filter unwanted extensions |
| 44 |
if (!empty($extensions)) { |
| 45 |
foreach (array_keys($file) as $key) { |
| 46 |
if (!is_dir("$directory/$file[$key]")) { |
| 47 |
$ext = substr($file[$key], strrpos($file[$key], ".") + 1); |
| 48 |
if (!in_array($ext, $extensions)) |
| 49 |
unset($file[$key]); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (count($file) > 2) { // Use 2 instead of 0 to account for . and .. "directories" |
| 55 |
$php_file_tree = "<ul"; |
| 56 |
if ($first_call) { |
| 57 |
$php_file_tree .= " class=\"php-file-tree\""; |
| 58 |
$first_call = false; |
| 59 |
} |
| 60 |
$php_file_tree .= ">"; |
| 61 |
foreach ($file as $this_file) { |
| 62 |
if ($this_file != "." && $this_file != "..") { |
| 63 |
$exclude_dir_file = str_replace($siteURL, '', "$directory/$this_file"); |
| 64 |
if (in_array($exclude_dir_file, $wp_all_backup_exclude_dir)) { |
| 65 |
$exclude_file = 'checked'; |
| 66 |
} else { |
| 67 |
$exclude_file = ''; |
| 68 |
} |
| 69 |
|
| 70 |
if (is_dir("$directory/$this_file")) { |
| 71 |
// Directory |
| 72 |
$php_file_tree .= "<li class=\"pft-directory\"><input type='checkbox' name='wpall_exclude[]' $exclude_file value='$exclude_dir_file'><a >" . htmlspecialchars($this_file) . "</a>"; |
| 73 |
$php_file_tree .= php_file_tree_dir($siteURL, "$directory/$this_file", $return_link, $extensions, false); |
| 74 |
$php_file_tree .= "</li>"; |
| 75 |
} else { |
| 76 |
// File |
| 77 |
// Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers) |
| 78 |
$ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); |
| 79 |
$link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link); |
| 80 |
$php_file_tree .= "<li class=\"pft-file " . strtolower($ext) . "\"><input type='checkbox' name='wpall_exclude[]' $exclude_file value='$exclude_dir_file'> <a>" . htmlspecialchars($this_file) . "</a></li>"; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
$php_file_tree .= "</ul>"; |
| 85 |
} |
| 86 |
return $php_file_tree; |
| 87 |
} |
| 88 |
|
| 89 |
// For PHP4 compatibility |
| 90 |
function php4_scandir($dir) { |
| 91 |
$dh = opendir($dir); |
| 92 |
while (false !== ($filename = readdir($dh))) { |
| 93 |
$files[] = $filename; |
| 94 |
} |
| 95 |
sort($files); |
| 96 |
return($files); |
| 97 |
} |