2 // /home/username/foo/public_html/bar
3 $dir = dirname(__FILE__
)."/../..";
5 // http://example.com/bar (or relative url, or semi absolute)
8 $include = '/\.(php|shtml|html|htm|shtm|cgi|txt|doc|pdf|rtf|xls|csv)$/';
11 $direxclude = '/(^|\/)[._]|htmlarea/'; // Exclude the htmlarea tree by default
13 // New backend config data passing
14 // if data was passed using xinha_pass_to_backend() we extract and use it
16 require_once(realpath(dirname(__FILE__
) . '/../../contrib/php-xinha.php'));
17 if($passed_data = xinha_read_passed_data())
19 extract($passed_data);
22 // Old deprecated backend config data passing
23 // not described because you shouldn't use it.
24 //------------------------------------------------------------------------
26 foreach(explode(',', 'dir,include,exclude,dirinclude,direxclude') as $k)
28 if(isset($_REQUEST[$k]))
30 if(get_magic_quotes_gpc())
32 $_REQUEST[$k] = stripslashes($_REQUEST[$k]);
34 $hash .= $k . '=' . $_REQUEST[$k];
42 if(!isset($_SESSION[sha1($hash)]))
50 //------------------------------------------------------------------------
52 // Neither dir nor url should have trailing slash
53 $dir = preg_replace('/\/$/', '', $dir);
54 $url = preg_replace('/\/$/', '', $url);
56 function scan($dir, $durl = '')
58 global $include, $exclude, $dirinclude, $direxclude;
59 static $seen = array();
63 $dir = realpath($dir);
64 if(isset($seen[$dir]))
72 while($dh && ($file = readdir($dh)))
74 if($file !== '.' && $file !== '..')
76 $path = realpath($dir . '/' . $file);
77 $url = $durl . '/' . $file;
79 if(($dirinclude && !preg_match($dirinclude, $url)) ||
($direxclude && preg_match($direxclude, $url))) continue;
82 if($subdir = scan($path, $url))
84 $files[] = array('url'=>$url, 'children'=>$subdir);
87 elseif(is_file($path))
89 if(($include && !preg_match($include, $url)) ||
($exclude && preg_match($exclude, $url))) continue;
90 $files[] = array('url'=>$url);
96 return dirsort($files);
99 function dirsort($files)
101 usort($files, 'dircomp');
105 function dircomp($a, $b)
107 if(isset($a['children']) && !isset($b['children'])) return -1;
108 if(isset($b['children']) && !isset($a['children'])) return 1;
110 return strcmp(strtolower($a['url']), strtolower($b['url']));
113 echo xinha_to_js(scan($dir,$url));