1<?php
2global $wiki_home,$htmlokay_directories,$htmlokay_abs_path,$htmlokay_path;
3if (!defined('ACCESS_DIR')) define('ACCESS_DIR',realpath(dirname(__FILE__).'/').'/conf/access/');
4
5function init() {
6  global $wiki_home, $htmlokay_path,$htmlokay_abs_path;
7  global $htmlokay_directories;
8
9    $htmlokay_directories = array();
10    $htmlokay_path = rawurldecode($_REQUEST['path']);
11    $htmlokay_abs_path = rawurldecode($_REQUEST['abs_path']);
12    $wiki_home = $htmlokay_path;
13
14    $wiki_home = rtrim($wiki_home, '/');
15    $htmlokay_directories[$wiki_home]['name'] = 'root';
16    $htmlokay_directories[$wiki_home]['namespace'] = 'root';
17    $wiki_home = ltrim($wiki_home, '/');
18    $wiki_home = preg_quote($wiki_home, '/');
19}
20
21
22function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null){
23  $subdirectories=opendir($base);
24  while (($subdirectory=readdir($subdirectories))!==false){
25    $path=$base.$subdirectory;
26    if (is_file($path)){
27      if ($fileFunc!==null) $fileFunc($path);
28    }else{
29      if ($dirFunc!==null) $dirFunc($path);
30      if (($subdirectory!='.') && ($subdirectory!='..')){
31        traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
32      }
33      if ($afterDirFunc!==null) $afterDirFunc($path);
34    }
35  }
36           closedir($subdirectories);
37}
38
39
40function get_namespace($path) {
41  global $wiki_home;
42     $namespace_string =  trim($path, '/');
43     $namespace_string = preg_replace('/^' . $wiki_home . '/', "", $namespace_string);
44     $namespace_string = preg_replace('%/%',':',$namespace_string);
45     return ltrim($namespace_string, ':');
46
47}
48
49function outputPath($path){
50  global $htmlokay_directories;
51
52  $name = basename($path);
53  if($name == '.' || $name == '..') return;
54
55  if(is_dir($path)){
56     $htmlokay_directories[$path] = array();
57     $htmlokay_directories[$path]['name'] = $name;
58     $htmlokay_directories[$path]['namespace'] = get_namespace($path);
59     $htmlokay_directories[$path]['files'] = array();
60  }
61  elseif(is_file($path)){
62     $dir = dirname($path);
63     $htmlokay_directories[$dir]['files'][] = $name;
64  }
65}
66
67
68function get_file_options($dir) {
69 global $htmlokay_directories;
70
71     $options = array();
72      $files = $htmlokay_directories[$dir]['files'];
73
74    // currently these two options are the same -- may change in future
75      if(count($files) == 0) {
76           $options[] =  'No files found:none:selected';
77             $options[] = 'ALL:all' ;
78      }
79      else {
80             $options[] ='No Files Selected:none:selected';
81             $options[] = 'ALL:all' ;
82      }
83       foreach($files as $file) {
84       $options[] = "$file:$file";
85       }
86
87
88    return $options;
89}
90
91  /**
92    * This function first checks to see whether the current namespace has a an access file
93    *        if not, it goes back, one directory at a time, and tests whether an access file exists for that namespace
94    *        If one does exits, it returns the filespec and the current namespace will
95    *        be governed by this access file
96    *        This comes into play if the found access file has set its filespace parameter to 'all'
97    */
98    function get_access_file($access_dir, $namespace)
99    {
100        global $htmlokay_path;
101        $access_dir = rtrim($access_dir, '/');
102        $file = $access_dir . '/' . $namespace;
103        if (file_exists($file))
104        {
105            return $file;
106        }
107
108        $dirs = explode('#', $namespace);
109        foreach($dirs as $dir)
110        {
111            array_pop($dirs);
112            $new_dir = implode('#', $dirs);
113            $file = $access_dir . '/' . $new_dir;
114            if (file_exists($file) && is_file($file))
115            {
116                return $file;
117            }
118        }
119        return $access_dir . '/' . $namespace;
120    }
121
122 function access_process() {
123     global $wiki_home,$htmlokay_directories,$htmlokay_abs_path,$htmlokay_path;
124     init();
125     traverseDirTree($htmlokay_path,'outputpath','outputpath');
126    $options = get_file_options($htmlokay_abs_path);
127    foreach($options as $option) {
128       echo "$option|";
129    }
130  }
131
132function access_data() {
133    global $wiki_home,$htmlokay_directories,$htmlokay_abs_path,$htmlokay_path;
134    $namespace_descriptor = $htmlokay_directories[$htmlokay_abs_path]['namespace'];
135    if($namespace_descriptor == 'root')
136       $namespace_descriptor = '_ROOT_';
137    else {
138     $namespace_descriptor = preg_replace('/\:/','#', $namespace_descriptor);
139    }
140    $data = array();
141    $access_file =   get_access_file(ACCESS_DIR, $namespace_descriptor);
142    if(file_exists($access_file)) {
143     $str = file_get_contents ($access_file);
144     $data = unserialize($str);
145    }
146
147    $output="";
148    foreach($data as $name=>$val) {
149        if(is_array($val)) {
150           $output.= "$name=>(";
151           foreach($val as $key=>$item) {
152               if(is_string($key)) {
153                     $key = $key . ':';
154               }
155               else $key = "";
156               $output .= "{$key}$item,";
157           }
158           $output = rtrim($output, ',');
159           $output .= ');;';
160         }
161    }
162   $output =rtrim($output, ';');
163   echo '%%' .$output;
164 }
165
166  access_process();
167  access_data() ;
168  flush();
169
170?>
171
172