-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.php
72 lines (63 loc) · 2.02 KB
/
find.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
require_once 'parse_config.php';
require_once 'internal_or_external.php';
function find_container($d){
global $config;
return( dir_search($d, $config->image_base) );
}
function dir_search($name, $dir){
if( false === ($d = opendir($dir)) ){
return( FALSE );
}
// should really only do this if $dir contains a '/'
if( opendir( "$dir/$name" ) ){
return( "$dir/$name" );
}
while( false !== ($e = readdir($d)) ){
if( $e == '.' || $e == '..' ){
continue;
}
if( is_dir( "$dir/$e" ) ){
if( $e == $name ){
return( "$dir/$e" );
}
else{
if( false !== ($r = dir_search( $name, "$dir/$e" )) ){
return( $r );
};
}
}
}
// if we get here, we did not find anything
return( FALSE );
}
// apparently realpath sucks.
// http://stackoverflow.com/questions/4049856/replace-phps-realpath
function absolutePath($path) {
$isEmptyPath = (strlen($path) == 0);
$isRelativePath = ($path{0} != '/');
$isWindowsPath = !(strpos($path, ':') === false);
if (($isEmptyPath || $isRelativePath) && !$isWindowsPath)
$path= getcwd().DIRECTORY_SEPARATOR.$path;
// resolve path parts (single dot, double dot and double delimiters)
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$pathParts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutePathParts = array();
foreach ($pathParts as $part) {
if ($part == '.')
continue;
if ($part == '..') {
array_pop($absolutePathParts);
} else {
$absolutePathParts[] = $part;
}
}
$path = implode(DIRECTORY_SEPARATOR, $absolutePathParts);
// resolve any symlinks
if (file_exists($path) && linkinfo($path)>0)
$path = readlink($path);
// put initial separator that could have been lost
$path= (!$isWindowsPath ? '/'.$path : $path);
return $path;
}
?>