source: trunk/plugins/Linker/scan.php @ 974

Last change on this file since 974 was 926, checked in by gogo, 12 years ago

Allow specification of base url to use (so that a subdirectory can be used as $dir and return appropriate urls to the files (prefixing the url)).

  • Property svn:eol-style set to native
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 3.1 KB
Line 
1<?php
2    // /home/username/foo/public_html/bar
3    $dir          = dirname(__FILE__)."/../..";
4   
5    // http://example.com/bar (or relative url, or semi absolute)
6    $url       = '';
7   
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
12
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())
18    {
19      extract($passed_data);     
20    }
21
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)
27    {
28      if(isset($_REQUEST[$k]))
29      {
30        if(get_magic_quotes_gpc())
31        {
32          $_REQUEST[$k] = stripslashes($_REQUEST[$k]);
33        }
34        $hash .= $k . '=' . $_REQUEST[$k];
35        $$k = $_REQUEST[$k];
36      }
37    }
38
39    if($hash)
40    {
41      session_start();
42      if(!isset($_SESSION[sha1($hash)]))
43      {
44        ?>
45        [ ];
46        <?php
47        exit;
48      }
49    }
50    //------------------------------------------------------------------------
51
52    // Neither dir nor url should have trailing slash
53    $dir = preg_replace('/\/$/', '', $dir);
54    $url = preg_replace('/\/$/', '', $url);
55   
56    function scan($dir, $durl = '')
57    {
58      global $include, $exclude, $dirinclude, $direxclude;
59      static $seen = array();
60
61      $files = array();
62
63      $dir = realpath($dir);
64      if(isset($seen[$dir]))
65      {
66        return $files;
67      }
68      $seen[$dir] = TRUE;
69      $dh = @opendir($dir);
70
71
72      while($dh && ($file = readdir($dh)))
73      {
74        if($file !== '.' && $file !== '..')
75        {
76          $path = realpath($dir . '/' . $file);
77          $url  = $durl . '/' . $file;
78
79          if(($dirinclude && !preg_match($dirinclude, $url)) || ($direxclude && preg_match($direxclude, $url))) continue;
80          if(is_dir($path))
81          {
82            if($subdir = scan($path, $url))
83            {
84              $files[] = array('url'=>$url, 'children'=>$subdir);
85            }
86          }
87          elseif(is_file($path))
88          {
89            if(($include && !preg_match($include, $url)) || ($exclude && preg_match($exclude, $url))) continue;
90            $files[] = array('url'=>$url);
91          }
92
93        }
94      }
95      @closedir($dh);
96      return dirsort($files);
97    }
98
99    function dirsort($files)
100    {
101      usort($files, 'dircomp');
102      return $files;
103    }
104
105    function dircomp($a, $b)
106    {
107      if(isset($a['children']) && !isset($b['children'])) return -1;
108      if(isset($b['children']) && !isset($a['children'])) return 1;
109     
110      return strcmp(strtolower($a['url']), strtolower($b['url']));
111    }
112   
113    echo xinha_to_js(scan($dir,$url));
114?>
Note: See TracBrowser for help on using the repository browser.