Add Xinha editor
[booki.git] / site_media / xinha / plugins / Linker / scan.php
blob0a7a7d8e0f6116747110ea24a7fb9d9e3bf0f86c
1 <?php
2 // /home/username/foo/public_html/bar
3 $dir = dirname(__FILE__)."/../..";
5 // http://example.com/bar (or relative url, or semi absolute)
6 $url = '';
8 $include = '/\.(php|shtml|html|htm|shtm|cgi|txt|doc|pdf|rtf|xls|csv)$/';
9 $exclude = '';
10 $dirinclude = '';
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
15 // as the items above
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 //------------------------------------------------------------------------
25 $hash = '';
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];
35 $$k = $_REQUEST[$k];
39 if($hash)
41 session_start();
42 if(!isset($_SESSION[sha1($hash)]))
45 [ ];
46 <?php
47 exit;
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();
61 $files = array();
63 $dir = realpath($dir);
64 if(isset($seen[$dir]))
66 return $files;
68 $seen[$dir] = TRUE;
69 $dh = @opendir($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;
80 if(is_dir($path))
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);
95 @closedir($dh);
96 return dirsort($files);
99 function dirsort($files)
101 usort($files, 'dircomp');
102 return $files;
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));