| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
function patternToRegExp($pattern){ |
| 22 |
$rxp = "^"; |
| 23 |
$c = ""; |
| 24 |
$len = strlen($pattern); |
| 25 |
for ($i = 0; $i < $len; $i++) { |
| 26 |
$c = $pattern[$i]; |
| 27 |
switch ($c) { |
| 28 |
case '\\': |
| 29 |
$rxp = $rxp.$c; |
| 30 |
$i++; |
| 31 |
$rxp = $rxp.$pattern[$i]; |
| 32 |
break; |
| 33 |
case '*': |
| 34 |
$rxp = $rxp.".*"; break; |
| 35 |
case '?': |
| 36 |
$rxp = $rxp."."; break; |
| 37 |
case '$': |
| 38 |
case '^': |
| 39 |
case '/': |
| 40 |
case '+': |
| 41 |
case '.': |
| 42 |
case '|': |
| 43 |
case '(': |
| 44 |
case ')': |
| 45 |
case '{': |
| 46 |
case '}': |
| 47 |
case '[': |
| 48 |
case ']': |
| 49 |
$rxp = $rxp."\\"; |
| 50 |
default: |
| 51 |
$rxp = $rxp.$c; |
| 52 |
} |
| 53 |
} |
| 54 |
return "(".$rxp."$)"; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
function getAllfiles($dir, $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles) { |
| 68 |
|
| 69 |
|
| 70 |
$files = array(); |
| 71 |
$dirHandle = opendir($rootDir."/".$dir); |
| 72 |
if ($dirHandle) { |
| 73 |
while($file = readdir($dirHandle)) { |
| 74 |
if ($file) { |
| 75 |
if ($file != ".." && $file != ".") { |
| 76 |
$path = $dir."/".$file; |
| 77 |
$fileObj = generateFileObj($file, $dir, $rootDir,$expand,$showHiddenFiles); |
| 78 |
if (is_dir($rootDir."/".$path)) { |
| 79 |
if ($recurse) { |
| 80 |
if ($showHiddenFiles || $fileObj["name"][0] != '.') { |
| 81 |
$subfiles = getAllfiles($path,$rootDir,$recurse,$dirsOnly,$expand,$showHiddenFiles); |
| 82 |
$length = count($subfiles); |
| 83 |
for ($i = 0; $i < $length; $i++) { |
| 84 |
$files[] = $subfiles[$i]; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
if (!$dirsOnly || $fileObj["directory"]) { |
| 90 |
if ($showHiddenFiles || $fileObj["name"][0] !== '.') { |
| 91 |
$files[] = $fileObj; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
closedir($dirHandle); |
| 99 |
return $files; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
function generateFileObj($file, $dir, $rootDir, $expand, $showHiddenFiles) { |
| 121 |
|
| 122 |
|
| 123 |
$path = $file; |
| 124 |
if ($dir != "." && $dir != "./") { |
| 125 |
$path = $dir."/".$file; |
| 126 |
} |
| 127 |
|
| 128 |
$fullPath = $rootDir."/".$path; |
| 129 |
|
| 130 |
$atts = stat($fullPath); |
| 131 |
|
| 132 |
$rootPath = realPath($rootDir); |
| 133 |
$resolvedDir = realPath($rootDir."/".$dir); |
| 134 |
$resolvedFullPath = realPath($fullPath); |
| 135 |
|
| 136 |
|
| 137 |
if (strcmp($rootPath, $resolvedDir) === 0) { |
| 138 |
$dir = "."; |
| 139 |
} else { |
| 140 |
$dir = substr($resolvedDir, (strlen($rootPath) + 1), strlen($resolvedDir)); |
| 141 |
$dir = "./".str_replace("\\","/",$dir); |
| 142 |
} |
| 143 |
if (strcmp($rootPath, $resolvedFullPath) === 0) { |
| 144 |
$path = "."; |
| 145 |
} else { |
| 146 |
$path = substr($resolvedFullPath, (strlen($rootPath) + 1), strlen($resolvedFullPath)); |
| 147 |
$path = "./".str_replace("\\","/",$path); |
| 148 |
} |
| 149 |
|
| 150 |
$fObj = array(); |
| 151 |
$fObj["name"] = $file; |
| 152 |
$fObj["parentDir"] = $dir; |
| 153 |
$fObj["path"] = $path; |
| 154 |
$fObj["directory"] = is_dir($fullPath); |
| 155 |
$fObj["size"] = filesize($fullPath); |
| 156 |
$fObj["modified"] = $atts[9]; |
| 157 |
|
| 158 |
if (is_dir($fullPath)) { |
| 159 |
$children = array(); |
| 160 |
$dirHandle = opendir($fullPath); |
| 161 |
while($cFile = readdir($dirHandle)) { |
| 162 |
if ($cFile) { |
| 163 |
if ($cFile != ".." && $cFile != ".") { |
| 164 |
if ($showHiddenFiles || $cFile[0] != '.') { |
| 165 |
if (!$expand) { |
| 166 |
$children[] = $cFile; |
| 167 |
}else{ |
| 168 |
$children[] = generateFileObj($cFile, $path, $rootDir, $expand, $showHiddenFiles); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
closedir($dirHandle); |
| 175 |
$fObj["children"] = $children; |
| 176 |
} |
| 177 |
return $fObj; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
class FieldComparator { |
| 185 |
var $field; |
| 186 |
var $descending = false; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
function FieldComparator($f, $d) { |
| 194 |
$this->field = $f; |
| 195 |
$this->descending = $d; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
function compare($fileA,$fileB){ |
| 204 |
$f = $this->field; |
| 205 |
$a = $fileA[$f]; |
| 206 |
$b = $fileB[$f]; |
| 207 |
|
| 208 |
$ret = 0; |
| 209 |
if (is_string($a) && is_string($b)) { |
| 210 |
$ret = strcmp($a,$b); |
| 211 |
} else if($a > $b || $a === null){ |
| 212 |
$ret = 1; |
| 213 |
}else if($a < $b || $b === null){ |
| 214 |
$ret = -1; |
| 215 |
} |
| 216 |
|
| 217 |
if (property_exists($this, "descending") && $this->descending == true) { |
| 218 |
$ret = $ret * -1; |
| 219 |
} |
| 220 |
|
| 221 |
if ($ret > 0) { |
| 222 |
$ret = 1; |
| 223 |
} else if ($ret < 0) { |
| 224 |
$ret = -1; |
| 225 |
} |
| 226 |
return $ret; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
class CompoundComparator { |
| 235 |
|
| 236 |
var $comparators = array(); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
function compare($a, $b) { |
| 245 |
$ret = 0; |
| 246 |
$size = count($this->comparators); |
| 247 |
for ($i = 0; $i < $size; $i++) { |
| 248 |
$comp = $this->comparators[$i]; |
| 249 |
$ret = $comp->compare($a, $b); |
| 250 |
if ($ret != 0) { |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
return $ret; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
function addComparator($comp){ |
| 262 |
$this->comparators[] = $comp; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
function createComparator($sortSpec) { |
| 272 |
|
| 273 |
$comparator = new CompoundComparator(); |
| 274 |
$size = count($sortSpec); |
| 275 |
for ($i = 0; $i < $size; $i++) { |
| 276 |
$sort = $sortSpec[$i]; |
| 277 |
$desc = false; |
| 278 |
if(property_exists($sort, "descending")){ |
| 279 |
$desc = $sort->descending; |
| 280 |
} |
| 281 |
$fileComp = new FieldComparator($sort->attribute,$desc); |
| 282 |
$comparator->addComparator($fileComp); |
| 283 |
} |
| 284 |
return $comparator; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
function matchFiles($query, $patterns, $ignoreCase, $dir, $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles) { |
| 297 |
$files = array(); |
| 298 |
$fullDir = $rootDir."/".$dir; |
| 299 |
|
| 300 |
if ($fullDir != null && is_dir($fullDir)) { |
| 301 |
|
| 302 |
$dirHandle = opendir($fullDir); |
| 303 |
while ($file = readdir($dirHandle)) { |
| 304 |
if ($file != "." && $file != "..") { |
| 305 |
$item = generateFileObj($file, $dir, $rootDir, $expand,$showHiddenFiles); |
| 306 |
$keys = array_keys($patterns); |
| 307 |
$total = count($keys); |
| 308 |
for ($i = 0; $i < $total; $i++) { |
| 309 |
$key = $keys[$i]; |
| 310 |
$pattern = $query[$key]; |
| 311 |
$matched = containsValue($item,$key,$query[$key],$patterns[$key], $ignoreCase); |
| 312 |
if (!$matched) { |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
if ($matched) { |
| 317 |
if (!$dirsOnly || $item["directory"]) { |
| 318 |
if ($showHiddenFiles || $item["name"][0] != '.') { |
| 319 |
$files[] = $item; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (is_dir($rootDir."/".$item["path"]) && $recurse) { |
| 325 |
if ($showHiddenFiles || $item["name"][0] != '.') { |
| 326 |
$files = array_merge($files, matchFiles($query, $patterns, $ignoreCase, $item["path"], $rootDir, $recurse, $dirsOnly, $expand, $showHiddenFiles)); |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
closedir($dirHandle); |
| 332 |
} |
| 333 |
return $files; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
function containsValue($item, $attr, $value, $rExp, $ignoreCase) { |
| 346 |
$matched = false; |
| 347 |
$possibleValue = $item[$attr]; |
| 348 |
if ($possibleValue === null && $value === null) { |
| 349 |
$matched = true; |
| 350 |
} else { |
| 351 |
if ($rExp != null && is_string($possibleValue)) { |
| 352 |
if ($ignoreCase) { |
| 353 |
$matched = eregi($rExp, $possibleValue); |
| 354 |
} else { |
| 355 |
$matched = ereg($rExp, $possibleValue); |
| 356 |
} |
| 357 |
|
| 358 |
} else { |
| 359 |
if ($value != null && $possibleValue != null) { |
| 360 |
$matched = ($value == $possibleValue); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
return $matched; |
| 365 |
} |
| 366 |
|