| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
$rootDir = "../.."; |
| 5 |
|
| 6 |
require_once("filestore_funcs.php"); |
| 7 |
|
| 8 |
|
| 9 |
$query = false; |
| 10 |
if (array_key_exists("query", $_GET)) { |
| 11 |
$query = $_GET['query']; |
| 12 |
$query = str_replace("\\\"", "\"", $query); |
| 13 |
$query = json_decode($query, true); |
| 14 |
} |
| 15 |
|
| 16 |
$queryOptions = json_decode("{}"); |
| 17 |
$deep = false; |
| 18 |
$ignoreCase = false; |
| 19 |
if (array_key_exists("queryOptions", $_GET)) { |
| 20 |
$queryOptions = $_GET['queryOptions']; |
| 21 |
$queryOptions = str_replace("\\\"", "\"", $queryOptions); |
| 22 |
$queryOptions = json_decode($queryOptions); |
| 23 |
if (property_exists($queryOptions, "deep")) { |
| 24 |
$deep = $queryOptions->deep; |
| 25 |
} |
| 26 |
if (property_exists($queryOptions, "ignoreCase")) { |
| 27 |
$ignoreCase = $queryOptions->ignoreCase; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
$expand = false; |
| 33 |
$dirsOnly = false; |
| 34 |
$showHiddenFiles = false; |
| 35 |
$options = array(); |
| 36 |
if (array_key_exists("options", $_GET)) { |
| 37 |
$options = $_GET['options']; |
| 38 |
$options = str_replace("\\\"", "\"", $options); |
| 39 |
$options = json_decode($options); |
| 40 |
if (array_search("expand", $options) > -1) { |
| 41 |
$expand = true; |
| 42 |
} |
| 43 |
if (array_search("dirsOnly", $options) > -1) { |
| 44 |
$dirsOnly = true; |
| 45 |
} |
| 46 |
if (array_search("showHiddenFiles", $options) > -1) { |
| 47 |
$showHiddenFiles = true; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
$path = false; |
| 54 |
if (array_key_exists("path", $_GET)) { |
| 55 |
$path = $_GET['path']; |
| 56 |
} |
| 57 |
|
| 58 |
if (!is_string($path)) { |
| 59 |
|
| 60 |
$files = array(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
$patterns = array(); |
| 65 |
if (is_array($query)) { |
| 66 |
|
| 67 |
$keys = array_keys($query); |
| 68 |
$total = count($keys); |
| 69 |
if ($total > 0) { |
| 70 |
for ($i = 0; $i < $total; $i++) { |
| 71 |
$key = $keys[$i]; |
| 72 |
$pattern = $query[$key]; |
| 73 |
if (is_string($pattern)) { |
| 74 |
$patterns[$key] = patternToRegExp($pattern); |
| 75 |
} |
| 76 |
} |
| 77 |
$files = matchFiles($query, $patterns, $ignoreCase, ".", $rootDir, $deep, $dirsOnly, $expand, $showHiddenFiles); |
| 78 |
} else { |
| 79 |
$files = getAllFiles(".",$rootDir,$deep,$dirsOnly,$expand,$showHiddenFiles); |
| 80 |
} |
| 81 |
}else{ |
| 82 |
$files = getAllFiles(".",$rootDir,$deep,$dirsOnly,$expand,$showHiddenFiles); |
| 83 |
} |
| 84 |
|
| 85 |
$total = count($files); |
| 86 |
|
| 87 |
|
| 88 |
$sortSpec = false; |
| 89 |
if (array_key_exists("sort", $_GET)) { |
| 90 |
$sortSpec = $_GET['sort']; |
| 91 |
$sortSpec = str_replace("\\\"", "\"", $sortSpec); |
| 92 |
$sortSpec = json_decode($sortSpec); |
| 93 |
} |
| 94 |
|
| 95 |
if ($sortSpec != null) { |
| 96 |
$comparator = createComparator($sortSpec); |
| 97 |
usort($files,array($comparator, "compare")); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if (array_key_exists("start", $_GET)) { |
| 102 |
$start = $_GET['start']; |
| 103 |
if (!is_numeric($start)) { |
| 104 |
$start = 0; |
| 105 |
} |
| 106 |
$files = array_slice($files, $start); |
| 107 |
} |
| 108 |
if (array_key_exists("count", $_GET)) { |
| 109 |
$count = $_GET['count']; |
| 110 |
if (!is_numeric($count)) { |
| 111 |
$count = $total; |
| 112 |
} |
| 113 |
$files = array_slice($files, 0, $count); |
| 114 |
} |
| 115 |
|
| 116 |
$result; |
| 117 |
$result->total = $total; |
| 118 |
$result->items = $files; |
| 119 |
header("Content-Type", "text/json"); |
| 120 |
print("/* ".json_encode($result)." */"); |
| 121 |
} else { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
$rootPath = realPath($rootDir); |
| 127 |
$fullPath = realPath($rootPath."/".$path); |
| 128 |
|
| 129 |
if ($fullPath !== false) { |
| 130 |
if (strpos($fullPath,$rootPath) === 0) { |
| 131 |
|
| 132 |
if (strlen($fullPath) == strlen($rootPath)) { |
| 133 |
$path = "."; |
| 134 |
} else { |
| 135 |
|
| 136 |
$path = substr($fullPath,(strlen($rootPath) + 1),strlen($fullPath)); |
| 137 |
$path = str_replace("\\", "/", $path); |
| 138 |
} |
| 139 |
|
| 140 |
if (file_exists($fullPath)) { |
| 141 |
$arr = split("/", $path); |
| 142 |
$size = count($arr); |
| 143 |
|
| 144 |
if ($size > 0) { |
| 145 |
$fName = $arr[$size - 1]; |
| 146 |
if ($size == 1) { |
| 147 |
print("Setting path to: ."); |
| 148 |
$path = "."; |
| 149 |
} else { |
| 150 |
$path = $arr[0]; |
| 151 |
} |
| 152 |
for ($i = 1; $i < ($size - 1); $i++) { |
| 153 |
$path = $path."/".$arr[$i]; |
| 154 |
} |
| 155 |
$file = generateFileObj($fName, $path, $rootDir, $expand,$showHiddenFiles); |
| 156 |
header("Content-Type", "text/json"); |
| 157 |
print("/* ".json_encode($file)." */"); |
| 158 |
} else { |
| 159 |
header("HTTP/1.0 404 Not Found"); |
| 160 |
header("Status: 404 Not Found"); |
| 161 |
print("<b>Cannot access file: [".htmlentities($path)."]<b>"); |
| 162 |
} |
| 163 |
} else { |
| 164 |
header("HTTP/1.0 404 Not Found"); |
| 165 |
header("Status: 404 Not Found"); |
| 166 |
print("<b>Cannot access file: [".htmlentities($path)."]<b>"); |
| 167 |
} |
| 168 |
} else { |
| 169 |
header("HTTP/1.0 403 Forbidden"); |
| 170 |
header("Status: 403 Forbidden"); |
| 171 |
print("<b>Cannot access file: [".htmlentities($path)."]. It is outside of the root of the file service.<b>"); |
| 172 |
} |
| 173 |
} else { |
| 174 |
header("HTTP/1.0 404 Not Found"); |
| 175 |
header("Status: 404 Not Found"); |
| 176 |
print("<b>Cannot access file: [".htmlentities($path)."]<b>"); |
| 177 |
} |
| 178 |
} |
| 179 |
?> |