/[docman2]/docman.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /docman.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32 by dpavlin, Mon Jul 29 14:17:05 2002 UTC revision 1.40 by dpavlin, Thu Jun 12 18:09:01 2003 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  /*  /*
4   *      Document Manager ][   *      Document Manager ][
5   *   *
6   *      Dobrica Pavlinusic <dpavlin@rot13.org>   *      Dobrica Pavlinusic <dpavlin@rot13.org>
7   *   *
8   *      License: GPL2   *      License: GPL2
9   *   *
10   *      Document Manager 1.x was based on   *      Document Manager 1.x was based on
11   *      Copyright 1999 by John Martin d/b/a www.ANYPORTAL.com   *      Copyright 1999 by John Martin d/b/a www.ANYPORTAL.com
12   *      PHP version Copyright 2000 by Stefan@Wiesendanger.org   *      PHP version Copyright 2000 by Stefan@Wiesendanger.org
13   *   *
14   *      For more info, please see web pages at   *      For more info, please see web pages at
15   *      http://www.rot13.org/~dpavlin/docman.html   *      http://www.rot13.org/~dpavlin/docman.html
16   *   *
17   */   */
18    
19    
20  //////////////////////////////////////////////////////////////////  //////////////////////////////////////////////////////////////////
21  // CONFIGURATION OPTIONS  // CONFIGURATION OPTIONS
22    
23          error_reporting(E_ALL) ;                // how verbose ?          error_reporting(E_ALL) ;                // how verbose ?
24    
25          // from where to include auth_*.php modules?          // try to do some guessing about configuration
26          $gblIncDir = "/data/docman2";          $script = $HTTP_SERVER_VARS["SCRIPT_FILENAME"];
27            if (is_link($script)) $script=readlink($script);
28          // force download on view (so it won't open in associated application)          $gblIncDir = dirname($script);
29          $gblForceDownload = 0;          $html = $gblIncDir."/html";
30    
31          // date format          // location of master docman configuration file
32          $gblDateFmt="Y-m-d";          $docman_conf = "/etc/docman.conf";
33  //      $gblDateFmt="D, F d, Y";          if (! file_exists($docman_conf)) {
34                    $error = "Can't find master configuration file <tt>$docman_conf</tt>. See <tt>docman2/doc/upgrade.html#docman_conf</tt> for more informations";
35          // time format  
36          $gblTimeFmt="H:i:s";                  error_log("docman: $error");
37  //      $gblTimeFmt="g:i:sA";                  Error("docman not installed completly",$error);
38            }
39          // Number of backup files to keep          include($docman_conf);
40          $gblNumBackups=3;  
41            if (! isset($gblIncDir)) {
42          // show red star if newer than ... days                  $error = "Can't findi <tt>\$gblIncDir</tt> in master configuration file <tt>$docman_conf</tt>. This variable should point to docman installation directory";
43          $gblModDays=1;  
44                    error_log("docman: $error");
45          // choose GifIcon below unless you have the M$                  Error("docman not installed completly",$error);
46          // WingDings font installed on your system          }
47    
48          $gblIcon="GifIcon";             // MockIcon or GifIcon          // location of html files
49            $html = $gblIncDir."/html";
50          // the directory below should be /icons/ or /icons/small/  
51          // on Apache; a set of icons is included in the distribution          // force download on view (so it won't open in associated application)
52            $gblForceDownload = 0;
53          $gblIconLocation="/icons/";  
54            // date format
55          // files you want to be able to edit in text mode          $gblDateFmt="Y-m-d";
56          // and view with (primitive) syntax highlighting  //      $gblDateFmt="D, F d, Y";
57    
58          $gblEditable = array( ".txt",".asa",".asp",".htm",".html",          // time format
59                                ".cfm",".php3",".php",".phtml",          $gblTimeFmt="H:i:s";
60                                ".shtml",".css" ) ;  //      $gblTimeFmt="g:i:sA";
61    
62          // files that will display as images on the detail page          // Number of backup files to keep
63          // (useless if your browser doesn't support them)          $gblNumBackups=3;
64    
65          $gblImages   = array( ".jpg",".jpeg",".gif",".png",".ico",          // show red star if newer than ... days
66                                ".bmp",".xbm") ;          $gblModDays=1;
67    
68          // which files to hide (separated by ,)          // choose GifIcon below unless you have the M$
69          $gblHide = "";          // WingDings font installed on your system
70    
71          // Where are users? (by default in .htusers file)          $gblIcon="GifIcon";             // MockIcon or GifIcon
72          $gblUsers = "file";  
73            // the directory below should be /icons/ or /icons/small/
74  //////////////////////////////////////////////////////////////////          // on Apache; a set of icons is included in the distribution
75    
76          $gblTitle = "Document Manager";          $gblIconLocation="/icons/";
77          $gblVersion = "2.0-pre1";  
78            // files you want to be able to edit in text mode
79          $secHash    = "";          // and view with (primitive) syntax highlighting
80    
81          // location of html files          $gblEditable = array( ".txt",".asa",".asp",".htm",".html",
82          $html = $gblIncDir."/html";                                ".cfm",".php3",".php",".phtml",
83                                  ".shtml",".css" ) ;
84          LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]);  
85            // files that will display as images on the detail page
86          // for security and configuration          // (useless if your browser doesn't support them)
87          $realm=$HTTP_SERVER_VARS["HTTP_HOST"];  
88            $gblImages   = array( ".jpg",".jpeg",".gif",".png",".ico",
89          $fsDocumentRoot = dirname($HTTP_SERVER_VARS["SCRIPT_FILENAME"]);                                ".bmp",".xbm") ;
90          if ($fsDocumentRoot == "") Error("Configuration error","Can't get SCRIPT_FILENAME from your web server. Please set <tt>\$fsDocumentRoot</tt> in <tt>\$</tt>",1);  
91            // which files to hide (separated by ,)
92          // globals for later          $gblHide = "";
93          $gblLogin = HTTP_SERVER_VAR("PHP_AUTH_USER");  
94          $gblPasswd = HTTP_SERVER_VAR("PHP_AUTH_PW");          // Where are users? (by default in .htusers file)
95            $gblUsers = "file";
96  //////////////////////////////////////////////////////////////////  
97    //////////////////////////////////////////////////////////////////
98  function LoadLanguage($lang) {  
99            $gblTitle = "Document Manager";
100          global $gblIncDir,$html;          $gblVersion = "2.0-pre4";
101    
102          if (file_exists($gblIncDir."/lang/$lang.php")) {          $secHash    = "";
103                  include($gblIncDir."/lang/$lang.php");  
104                  $html .= "-$lang";          // load language
105          } else {          if (isset($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"])) {
106                  include($gblIncDir."/lang/default.php");                  LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]);
107          }          }
108  }  
109            // does gettext locale function exits?
110  function StartHTML($title,$text="") {          if (! function_exists("_")) {
111                    function _($str) { return $str; }
112          global $html,$fsDocumentRoot,$gblTitle,$HTTP_SERVER_VARS;          }
113    
114          $host  = $HTTP_SERVER_VARS["HTTP_HOST"] ;          // for security and configuration
115          $self  = $HTTP_SERVER_VARS["PHP_SELF"] ;          $realm=$HTTP_SERVER_VARS["HTTP_HOST"];
116    
117          if (file_exists("$fsDocumentRoot/docman.css")) {          // globals for later
118                  $css=dirname($self)."/docman.css";          $gblLogin = HTTP_SERVER_VAR("PHP_AUTH_USER");
119          } else {          $gblPasswd = HTTP_SERVER_VAR("PHP_AUTH_PW");
120                  $css=$self."?STYLE=get";  
121          }  //////////////////////////////////////////////////////////////////
122    
123          include("$html/head.html");  function LoadLanguage($lang) {
124  }  
125            global $gblIncDir,$html;
126  //////////////////////////////////////////////////////////////////  
127            if (file_exists($gblIncDir."/lang/$lang.php")) {
128  function EndHTML() {                  include($gblIncDir."/lang/$lang.php");
129                    $html .= "-$lang";
130          global $gblDateFmt, $gblTimeFmt, $gblUserName, $PHP_SELF,          } else {
131                  $secHash, $gblVersion, $html,                  include($gblIncDir."/lang/default.php");
132                  $gblLogin,$gblPasswd;          }
133    }
134          $url = $PHP_SELF."?relogin=";  
135          if (isset($secHash) && $secHash != "") {  function StartHTML($title,$text="") {
136                  $url .= $secHash;  
137          } else {          global $html,$gblIncDir,$gblTitle,$HTTP_SERVER_VARS;
138                  $url .= md5($gblLogin.$gblPasswd);  
139          }          $host  = $HTTP_SERVER_VARS["HTTP_HOST"] ;
140          if ( (  (isset($gblLogin) && $gblLogin != "") ||          $self  = $HTTP_SERVER_VARS["PHP_SELF"] ;
141                  (!isset($gblLogin) || $gblLogin == "")  
142               ) && ($gblPasswd == "" || !isset($gblPasswd))) {          if (file_exists("$gblIncDir/docman.css")) {
143                  $url_title="login";                  $css = "";
144                  $url .= "&force_login=1";                  $d = dirname($self);
145          } else {                  if ($d != "/") $css = $d;
146                  $url_title="relogin";                  $css .= "/docman.css";
147          }          } else {
148          include("$html/footer.html");                  $css=$self."?STYLE=get";
149            }
150          global $debug;          include("$html/head.html");
151          if ($debug) print $debug;  }
152  } // end function EndHTML  
153    //////////////////////////////////////////////////////////////////
154  //////////////////////////////////////////////////////////////////  
155    function EndHTML() {
156  function DetailPage($fsRoot,$relDir,$fn) {  
157                    global $gblDateFmt, $gblTimeFmt, $gblUserName, $PHP_SELF,
158          global $gblEditable, $gblImages,                  $secHash, $gblVersion, $html,
159                  $gblDateFmt, $gblTimeFmt,                  $gblLogin,$gblPasswd;
160                  $gblPermNote,  
161                  $webRoot, $html,          $url = $PHP_SELF."?relogin=";
162                  $HTTP_SERVER_VARS ;          if (isset($secHash) && $secHash != "") {
163          $self = $HTTP_SERVER_VARS["PHP_SELF"] ;                  $url .= $secHash;
164            } else {
165          $relPath  = $relDir . "/" . $fn ;                  $url .= md5($gblLogin.$gblPasswd);
166          $fsPath   = $fsRoot . $relPath ;          }
167          $fsDir    = $fsRoot . $relDir ;          if ( (  (isset($gblLogin) && $gblLogin != "") ||
168                    (!isset($gblLogin) || $gblLogin == "")
169          $exists   = file_exists($fsPath) ;               ) && ($gblPasswd == "" || !isset($gblPasswd))) {
170          $ext      = strtolower(strrchr($relPath,".")) ;                  $url_title="login";
171          $editable = ( $ext=="" || strstr(join(" ",$gblEditable),$ext)) &&                  $url .= "&force_login=1";
172                  check_perm($relPath,trperm_w);          } else {
173          $writable = is_writeable($fsPath) && check_perm($relPath,trperm_w) ;                  $url_title="relogin";
174          $writable_dir = is_writeable($fsDir) && check_perm($relDir,trperm_w) ;          }
175          $file_lock = CheckLock($fsPath);          include("$html/footer.html");
176    
177          if (!$editable && !$exists)  #       global $debug;
178                  Error("Creation denied","Can't create <tt>$relPath</tt>") ;  #       if ($debug) print $debug;
179          if (!$exists && !$writable_dir )  } // end function EndHTML
180                  Error("Creation denied","Can't write in directory <tt>$relDir</tt> while creating <tt>$relPath</tt>for which user has permissions.",1);  
181    //////////////////////////////////////////////////////////////////
182          $text  = _("Use this page to view, modify or ") ;  
183          if (is_dir($fsPath)) {  function DetailPage($fsRoot,$relDir,$fn) {
184                  $text .=_("delete a directory on this ") ;          
185          } else {          global $gblEditable, $gblImages,
186                  $text .= _("delete a single document on this ") ;                  $gblDateFmt, $gblTimeFmt,
187          };                  $gblPermNote,
188          $text .= _("web site.") ;                        $webRoot, $html,
189          $title = "("._("Detail Page").")" ;                  $HTTP_SERVER_VARS ;
190          StartHTML($title, $text) ;          $self = $HTTP_SERVER_VARS["PHP_SELF"] ;
191    
192          print "<H3>".$relDir.$fn."</H3>";          $relPath  = $relDir . "/" . $fn ;
193            $fsPath   = $fsRoot . $relPath ;
194          if ($exists) {  // get file info          $fsDir    = $fsRoot . $relDir ;
195                  $fsize = filesize($fsPath) ;  
196                  $fmodified = date("$gblDateFmt $gblTimeFmt", filemtime($fsPath)) ;          $exists   = file_exists($fsPath) ;
197                  $faccessed = date("$gblDateFmt $gblTimeFmt", fileatime($fsPath)) ;          $ext      = strtolower(strrchr($relPath,".")) ;
198                  $fuid=fileowner($fsPath);          $editable = ( $ext=="" || strstr(join(" ",$gblEditable),$ext)) &&
199                  $fgid=filegroup($fsPath);                  check_perm($relPath,trperm_w);
200                  $userinfo = posix_getpwuid($fuid);          $writable = is_writeable($fsPath) && check_perm($relPath,trperm_w) ;
201                  $grpinfo = posix_getgrgid($fgid);          $writable_dir = is_writeable($fsDir) && check_perm($relDir,trperm_w) ;
202            $file_lock = CheckLock($fsPath);
203                  include("$html/DetailPage-file.html");  
204          }          if (!$editable && !$exists)
205                    Error("Creation denied","Can't create <tt>$relPath</tt>") ;
206          if ( !is_dir($fsPath) && $editable && ($writable || !$exists) && !$file_lock ) {          if (!$exists && !$writable_dir )
207                  $fh = fopen($fsPath,"a+") ;                  Error("Creation denied","Can't write in directory <tt>$relDir</tt> while creating <tt>$relPath</tt>for which user has permissions.",1);
208                  rewind($fh) ;  
209                  $fstr = fread($fh,filesize($fsPath)) ;          $text  = _("Use this page to view, modify or ") ;
210                  fclose($fh) ;          if (is_dir($fsPath)) {
211                  $fstr = htmlentities( $fstr ) ;                  $text .=_("delete a directory on this ") ;
212            } else {
213                  include("$html/DetailPage-edit.html");                  $text .= _("delete a single document on this ") ;
214          }          };
215          if ( !$file_lock && $ext!="" && strstr(join(' ',$gblImages),$ext) ) {            $text .= _("web site.") ;      
216                  $info  = getimagesize($fsPath) ;          $title = "("._("Detail Page").")" ;
217                  $tstr = "<IMG SRC=\"$self?A=V&D=".urlpath(dirname($relPath))."&F=".urlpath(basename($relPath))."\" BORDER=0 " ;          StartHTML($title, $text) ;
218                  $tstr .= $info[3] . " ALT=\"" . $fn . " - " ;  
219                  $tstr .= (int)(($fsize+1023)/1024) . "Kb\">" ;          print "<H3>".$relDir.$fn."</H3>";
220  //              echo htmlentities($tstr) . "<BR><BR>" . $tstr ;  
221                  echo $tstr ;          if ($exists) {  // get file info
222          }                  $fsize = filesize($fsPath) ;
223                    $fmodified = date("$gblDateFmt $gblTimeFmt", filemtime($fsPath)) ;
224                    $faccessed = date("$gblDateFmt $gblTimeFmt", fileatime($fsPath)) ;
225          print '<FORM ACTION="'.$self.'" METHOD="POST">                  $fuid=fileowner($fsPath);
226                  <INPUT TYPE="HIDDEN" NAME="DIR" VALUE="'.$relDir.'">                  $fgid=filegroup($fsPath);
227                  <INPUT TYPE="HIDDEN" NAME="FN" VALUE="'.$fn.'">                  $userinfo = posix_getpwuid($fuid);
228                  <INPUT TYPE="SUBMIT" NAME="POSTACTION" VALUE="CANCEL"><BR>                  $grpinfo = posix_getgrgid($fgid);
229                  ';  
230                    include("$html/DetailPage-file.html");
231          if ($file_lock && check_perm($relDir.$fn,trperm_w)) {          }
232                  include("$html/DetailPage-unlock.html");  
233          } // file_lock          if ( !is_dir($fsPath) && $editable && ($writable || !$exists) && !$file_lock ) {
234                    $fh = fopen($fsPath,"a+") ;
235          if (substr($fn,0,4) == ".del") {                  rewind($fh) ;
236                  $action="UNDELETE";                  $fstr = fread($fh,filesize($fsPath)) ;
237                  $desc="undelete previously deleted file";                  fclose($fh) ;
238          } else {                  $fstr = htmlentities( $fstr ) ;
239                  $action="DELETE";  
240                  $desc="delete";                  include("$html/DetailPage-edit.html");
241          }          }
242            if ( !$file_lock && $ext!="" && strstr(join(' ',$gblImages),$ext) ) {  
243          if ($exists && $writable) {                  $info  = getimagesize($fsPath) ;
244                  include("$html/DetailPage-undelete.html");                  $tstr = "<IMG SRC=\"$self?A=V&D=".urlpath(dirname($relPath))."&F=".urlpath(basename($relPath))."\" BORDER=0 " ;
245                  include("$html/DetailPage-rename.html");                  $tstr .= $info[3] . " ALT=\"" . $fn . " - " ;
246                    $tstr .= (int)(($fsize+1023)/1024) . "Kb\">" ;
247          }  //              echo htmlentities($tstr) . "<BR><BR>" . $tstr ;
248                            echo $tstr ;
249          if (check_perm($relDir.$fn,$gblPermNote)) {          }
250                  include("$html/DetailPage-note.html");  
251          }  
252            print '<FORM ACTION="'.$self.'" METHOD="POST">
253          print "</FORM>";                  <INPUT TYPE="HIDDEN" NAME="DIR" VALUE="'.$relDir.'">
254                    <INPUT TYPE="HIDDEN" NAME="FN" VALUE="'.$fn.'">
255          $name=basename("$fsDir/$fn");                  <INPUT TYPE="SUBMIT" NAME="POSTACTION" VALUE="CANCEL"><BR>
256          $logname=dirname("$fsDir/$fn")."/.log/$name";                  ';
257          $bakdir=dirname("$fsDir/$fn")."/.bak";  
258          if (file_exists($logname)) {          if ($file_lock && check_perm($relDir.$fn,trperm_w)) {
259                  $log=fopen($logname,"r");                  include("$html/DetailPage-unlock.html");
260                  $cl1=" class=LST"; $cl2="";          } // file_lock
261                  $logarr = array();  
262                  while($line = fgetcsv($log,512,"\t")) {          if (substr($fn,0,4) == ".del") {
263                          $cl=$cl1; $cl1=$cl2; $cl2=$cl;                  $action="UNDELETE";
264                          array_unshift($logarr,array($cl,$line[0],$line[1],$line[2],$line[3]));                  $desc="undelete previously deleted file";
265                  }          } else {
266                  fclose($log);                  $action="DELETE";
267                  if (is_dir("$fsDir/$fn")) {                  $desc="delete";
268                          $whatis="DIRECTORY";          }
269                  } else {  
270                          $whatis="FILE";          if ($exists && $writable) {
271                  }                  include("$html/DetailPage-undelete.html");
272                  print "<hr><br><b>CHANGES TO THIS $whatis</b><br><table border=0 width=100%>\n";                  include("$html/DetailPage-rename.html");
273                  $bakcount = 0;  // start from 0, skip fist backup (it's current)  
274                  while ($e = array_shift($logarr)) {          }
275                          if (strstr($e[4],"upload")) {          
276                                  if (file_exists("$bakdir/$bakcount/$name")) {          if (check_perm($relDir.$fn,$gblPermNote)) {
277                                          $e[4]="<a href=\"$webRoot".urlpath(dirname($relPath)."/.bak/$bakcount/$name")."\">$e[4]</a>";                  include("$html/DetailPage-note.html");
278                                  }          }
279                                  $bakcount++;  
280                          }          print "</FORM>";
281                          print "<tr><td$e[0]>$e[1]</td><td$e[0]>$e[2]</td><td$e[0]>$e[3]</td><td$e[0]>$e[4]</td></tr>\n";  
282                  }          $name=basename("$fsDir/$fn");
283                  print "</table>";          $logname=dirname("$fsDir/$fn")."/.log/$name";
284          }          $bakdir=dirname("$fsDir/$fn")."/.bak";
285            if (file_exists($logname)) {
286          EndHTML() ;                  $log=fopen($logname,"r");
287                    $cl1=" class=LST"; $cl2="";
288  } // end function DetailPage                  $logarr = array();
289                    while($line = fgetcsv($log,512,"\t")) {
290  //////////////////////////////////////////////////////////////////                          $cl=$cl1; $cl1=$cl2; $cl2=$cl;
291                            array_unshift($logarr,array($cl,$line[0],$line[1],$line[2],$line[3]));
292  function DisplayCode($fsRoot,$relDir,$fn) {                  }
293                    fclose($log);
294          $path = $fsRoot . $relDir . "/" . $fn ;                  if (is_dir("$fsDir/$fn")) {
295                            $whatis="DIRECTORY";
296          if (!file_exists($path)) Error("File not found",$path) ;                  } else {
297                            $whatis="FILE";
298          StartHTML("(".$relDir."/".$fn.")","");                  }
299                    print "<hr><br><b>CHANGES TO THIS $whatis</b><br><table border=0 width=100%>\n";
300          $tstr = join("",file($path)) ;                  $bakcount = 0;  // start from 0, skip fist backup (it's current)
301          $tstr = htmlentities($tstr) ;                  while ($e = array_shift($logarr)) {
302                            if (strstr($e[4],"upload")) {
303          // Tabs                                  if (file_exists("$bakdir/$bakcount/$name")) {
304          $tstr = str_replace(chr(9),"   ",$tstr) ;                                            $e[4]="<a href=\"$webRoot".urlpath(dirname($relPath)."/.bak/$bakcount/$name")."\">$e[4]</a>";
305                                    }
306          // ASP tags & XML/PHP tags                                  $bakcount++;
307          $aspbeg = "<SPAN CLASS=XML>&lt;%</SPAN><SPAN CLASS=BLK>" ;                          }
308          $aspend = "</SPAN><SPAN CLASS=XML>%&gt;</SPAN>" ;                          print "<tr><td$e[0]>$e[1]</td><td$e[0]>$e[2]</td><td$e[0]>$e[3]</td><td$e[0]>$e[4]</td></tr>\n";
309          $tstr = str_replace("&lt;%",$aspbeg,$tstr) ;                  }
310          $tstr = str_replace("%&gt;",$aspend,$tstr) ;                      print "</table>";
311            }
312          $xmlbeg = "<SPAN CLASS=XML>&lt;?</SPAN><SPAN CLASS=BLK>" ;  
313          $xmlend = "</SPAN><SPAN CLASS=XML>?&gt;</SPAN>" ;          EndHTML() ;
314          $tstr = str_replace("&lt;?",$xmlbeg,$tstr) ;  
315          $tstr = str_replace("?&gt;",$xmlend,$tstr) ;      } // end function DetailPage
316    
317          // C style comment  //////////////////////////////////////////////////////////////////
318          $tstr = str_replace("/*","<SPAN CLASS=REM>/*",$tstr) ;    
319          $tstr = str_replace("*/","*/</SPAN>",$tstr) ;                    function DisplayCode($fsRoot,$relDir,$fn) {
320    
321          // HTML comments          $path = $fsRoot . $relDir . "/" . $fn ;
322          $tstr = str_replace("&lt;!--","<I CLASS=RED>&lt;!--",$tstr) ;    
323          $tstr = str_replace("--&gt;","--&gt;</I>",$tstr) ;            if (!file_exists($path)) Error("File not found",$path) ;
324    
325          echo "<PRE>" ;            StartHTML("(".$relDir."/".$fn.")","");
326    
327          $tstr = split("\n",$tstr) ;          $tstr = join("",file($path)) ;
328          for ($i = 0 ; $i < sizeof($tstr) ; ++$i) {          $tstr = htmlentities($tstr) ;
329                  // add line numbers  
330                  echo "<BR><EM>" ;          // Tabs
331                  echo substr(("000" . ($i+1)), -4) . ":</EM> " ;          $tstr = str_replace(chr(9),"   ",$tstr) ;  
332                  $line = $tstr[$i] ;  
333                  // C++ style comments          // ASP tags & XML/PHP tags
334                  $pos = strpos($line,"//") ;          $aspbeg = "<SPAN CLASS=XML>&lt;%</SPAN><SPAN CLASS=BLK>" ;
335                  // exceptions: two slashes aren't a script comment          $aspend = "</SPAN><SPAN CLASS=XML>%&gt;</SPAN>" ;
336                  if (strstr($line,"//") &&          $tstr = str_replace("&lt;%",$aspbeg,$tstr) ;
337                      ! ($pos>0 && substr($line,$pos-1,1)==":") &&          $tstr = str_replace("%&gt;",$aspend,$tstr) ;    
338                      ! (substr($line,$pos,8) == "//--&gt;") &&  
339                      ! (substr($line,$pos,9) == "// --&gt;")) {          $xmlbeg = "<SPAN CLASS=XML>&lt;?</SPAN><SPAN CLASS=BLK>" ;
340                   $beg = substr($line,0,strpos($line,"//")) ;          $xmlend = "</SPAN><SPAN CLASS=XML>?&gt;</SPAN>" ;
341                   $end = strstr($line,"//") ;          $tstr = str_replace("&lt;?",$xmlbeg,$tstr) ;
342                   $line = $beg."<SPAN CLASS=REM>".$end."</SPAN>";          $tstr = str_replace("?&gt;",$xmlend,$tstr) ;    
343                  }  
344                  // shell & asp style comments          // C style comment
345                  $first = substr(ltrim($line),0,1) ;          $tstr = str_replace("/*","<SPAN CLASS=REM>/*",$tstr) ;  
346                  if ($first == "#" || $first == "'") {          $tstr = str_replace("*/","*/</SPAN>",$tstr) ;                  
347                   $line = "<SPAN CLASS=REM>".$line."</SPAN>";  
348                  }          // HTML comments
349                  print($line) ;          $tstr = str_replace("&lt;!--","<I CLASS=RED>&lt;!--",$tstr) ;  
350          } // next i          $tstr = str_replace("--&gt;","--&gt;</I>",$tstr) ;  
351    
352          echo "</PRE>" ;          echo "<PRE>" ;  
353                    
354          EndHTML() ;          $tstr = split("\n",$tstr) ;
355            for ($i = 0 ; $i < sizeof($tstr) ; ++$i) {
356  } // end function DisplayCode                  // add line numbers
357                    echo "<BR><EM>" ;
358  //////////////////////////////////////////////////////////////////                  echo substr(("000" . ($i+1)), -4) . ":</EM> " ;
359                    $line = $tstr[$i] ;
360  function MockIcon($txt) {                  // C++ style comments
361          $tstr = "<SPAN CLASS=MCK>" ;                  $pos = strpos($line,"//") ;
362                    // exceptions: two slashes aren't a script comment
363          switch (strtolower($txt)) {                  if (strstr($line,"//") &&
364          case ".bmp" :                      ! ($pos>0 && substr($line,$pos-1,1)==":") &&
365          case ".gif" :                      ! (substr($line,$pos,8) == "//--&gt;") &&
366          case ".jpg" :                      ! (substr($line,$pos,9) == "// --&gt;")) {
367          case ".jpeg":                   $beg = substr($line,0,strpos($line,"//")) ;
368          case ".tif" :                   $end = strstr($line,"//") ;
369          case ".tiff":                   $line = $beg."<SPAN CLASS=REM>".$end."</SPAN>";
370                  $d = 176 ;                  }
371                  break ;                  // shell & asp style comments
372          case ".doc" :                  $first = substr(ltrim($line),0,1) ;
373                  $d = 50 ;                  if ($first == "#" || $first == "'") {
374                  break ;                   $line = "<SPAN CLASS=REM>".$line."</SPAN>";
375          case ".exe" :                  }
376          case ".bat" :                  print($line) ;
377                  $d = 255 ;          } // next i
378                  break ;  
379          case ".bas" :          echo "</PRE>" ;
380          case ".c"   :                  
381          case ".cc"  :          EndHTML() ;
382          case ".src" :  
383                  $d = 255 ;  } // end function DisplayCode
384                  break ;  
385          case "file" :  //////////////////////////////////////////////////////////////////
386                  $d = 51 ;  
387                  break ;  function MockIcon($txt) {
388          case "fldr" :          $tstr = "<SPAN CLASS=MCK>" ;
389                  $d = 48 ;  
390                  break ;          switch (strtolower($txt)) {
391          case ".htm" :          case ".bmp" :
392          case ".html":          case ".gif" :
393          case ".asa" :          case ".jpg" :
394          case ".asp" :          case ".jpeg":
395          case ".cfm" :          case ".tif" :
396          case ".php3":          case ".tiff":
397          case ".php" :                  $d = 176 ;
398          case ".phtml" :                  break ;
399          case ".shtml" :          case ".doc" :
400                  $d = 182 ;                  $d = 50 ;
401                  break ;                  break ;
402          case ".pdf" :          case ".exe" :
403                  $d = 38 ;          case ".bat" :
404                  break;                  $d = 255 ;
405          case ".txt" :                  break ;
406          case ".ini" :          case ".bas" :
407                  $d = 52 ;          case ".c"   :
408                  break ;          case ".cc"  :
409          case ".xls" :          case ".src" :
410                  $d = 252 ;                  $d = 255 ;
411                  break ;                  break ;
412          case ".zip" :          case "file" :
413          case ".arc" :                  $d = 51 ;
414          case ".sit" :                  break ;
415          case ".tar" :          case "fldr" :
416          case ".gz"  :                  $d = 48 ;
417          case ".tgz" :                  break ;
418          case ".Z"   :          case ".htm" :
419                  $d = 59 ;          case ".html":
420                  break ;          case ".asa" :
421          case "view" :          case ".asp" :
422                  $d = 52 ;          case ".cfm" :
423                  break ;          case ".php3":
424          case "up" :          case ".php" :
425                  $d = 199 ;          case ".phtml" :
426                  break ;          case ".shtml" :
427          case "blank" :                  $d = 182 ;
428                  return "&nbsp;&nbsp;</SPAN>" ;                  break ;
429                  break ;          case ".pdf" :
430          default :                  $d = 38 ;
431                  $d = 51 ;                  break;
432          }          case ".txt" :
433            case ".ini" :
434          return $tstr . chr($d) . "</SPAN>" ;                  $d = 52 ;
435  } // end function MockIcon                  break ;
436            case ".xls" :
437  //////////////////////////////////////////////////////////////////                  $d = 252 ;
438                    break ;
439  function GifIcon($txt = "") {          case ".zip" :
440          global $gblIconLocation, $gblImages ;          case ".arc" :
441            case ".sit" :
442          switch (strtolower($txt)) {          case ".tar" :
443          case ".doc" :          case ".gz"  :
444                  $d = "layout.gif" ;          case ".tgz" :
445                  break ;          case ".Z"   :
446          case ".exe" :                  $d = 59 ;
447          case ".bat" :                  break ;
448                  $d = "screw2.gif" ;          case "view" :
449                  break ;                  $d = 52 ;
450          case ".bas" :                  break ;
451          case ".c"   :          case "up" :
452          case ".cc"  :                  $d = 199 ;
453          case ".src" :                  break ;
454                  $d = "c.gif" ;          case "blank" :
455                  break ;                  return "&nbsp;&nbsp;</SPAN>" ;
456          case "file" :                  break ;
457                  $d = "generic.gif" ;          default :
458                  break ;                  $d = 51 ;
459          case "fldr" :          }
460                  $d = "dir.gif" ;  
461                  break ;          return $tstr . chr($d) . "</SPAN>" ;
462          case ".phps" :  } // end function MockIcon
463                  $d = "phps.gif" ;  
464                  break ;  //////////////////////////////////////////////////////////////////
465          case ".php3" :  
466                  $d = "php3.gif" ;  function GifIcon($txt = "") {
467                  break ;          global $gblIconLocation, $gblImages ;
468          case ".htm" :  
469          case ".html":          switch (strtolower($txt)) {
470          case ".asa" :          case ".doc" :
471          case ".asp" :                  $d = "layout.gif" ;
472          case ".cfm" :                  break ;
473          case ".php3":          case ".exe" :
474          case ".php" :          case ".bat" :
475          case ".phtml" :                  $d = "screw2.gif" ;
476          case ".shtml" :                  break ;
477                  $d = "world1.gif" ;          case ".bas" :
478                  break ;          case ".c"   :
479          case ".pdf" :          case ".cc"  :
480                  $d = "pdf.gif" ;          case ".src" :
481                  break;                  $d = "c.gif" ;
482          case ".txt" :                  break ;
483          case ".ini" :          case "file" :
484                  $d = "text.gif" ;                  $d = "generic.gif" ;
485                  break ;                  break ;
486          case ".xls" :          case "fldr" :
487                  $d = "box2.gif" ;                  $d = "dir.gif" ;
488                  break ;                  break ;
489          case ".zip" :          case ".phps" :
490          case ".arc" :                  $d = "phps.gif" ;
491          case ".sit" :                  break ;
492          case ".tar" :          case ".php3" :
493          case ".gz"  :                  $d = "php3.gif" ;
494          case ".tgz" :                  break ;
495          case ".Z"   :          case ".htm" :
496                  $d = "compressed.gif" ;          case ".html":
497                  break ;          case ".asa" :
498          case "view" :          case ".asp" :
499                  $d = "index.gif" ;          case ".cfm" :
500                  break ;          case ".php3":
501          case "up" :          case ".php" :
502                  $d = "back.gif" ;          case ".phtml" :
503                  break ;          case ".shtml" :
504          case "blank" :                  $d = "world1.gif" ;
505                  $d = "blank.gif" ;                  break ;
506                  break ;          case ".pdf" :
507          case "checkout":                  $d = "pdf.gif" ;
508                  $d = "box2.gif";                  break;
509                  break;          case ".txt" :
510          case "checkin":          case ".ini" :
511                  $d = "hand.up.gif";                  $d = "text.gif" ;
512                  break;                  break ;
513          case "locked":          case ".xls" :
514                  $d = "screw2.gif";                  $d = "box2.gif" ;
515                  break;                  break ;
516          case "note":          case ".zip" :
517                  $d = "quill.gif";          case ".arc" :
518                  break;          case ".sit" :
519          default :          case ".tar" :
520                  if (in_array(strtolower($txt),$gblImages)) {          case ".gz"  :
521                          $d = "image2.gif" ;          case ".tgz" :
522                  } else {          case ".Z"   :
523                          $d = "generic.gif" ;                  $d = "compressed.gif" ;
524                  }                  break ;
525          }          case "view" :
526                    $d = "index.gif" ;
527                    break ;
528          return "<IMG SRC=\"$gblIconLocation" . $d . "\" BORDER=0>" ;          case "up" :
529  } // end function GifIcon                  $d = "back.gif" ;
530                    break ;
531  //////////////////////////////////////////////////////////////////          case "blank" :
532                    $d = "blank.gif" ;
533  function Navigate($fsRoot,$relDir) {                  break ;
534            case "checkout":
535          global $gblEditable, $gblIcon, $gblModDays, $webRoot, $gblHide,                  $d = "box2.gif";
536                  $gblIgnoreUnknownFileType, $gblRepositoryDir,                  break;
537                  $gblLogin, $gblUserName, $gblDateFmt, $gblTimeFmt,          case "checkin":
538                  $gblPermNote,                  $d = "hand.up.gif";
539                  $fsRealmDir, $realm, $realm_sep,                  break;
540                  $html, $realm_config,          case "locked":
541                  $HTTP_GET_VARS, $HTTP_SERVER_VARS;                  $d = "screw2.gif";
542                            break;
543          $self     = $HTTP_SERVER_VARS["PHP_SELF"] ;          case "note":
544                    $d = "quill.gif";
545          $relDir = chopsl($relDir)."/";                  break;
546          $fsDir = $fsRoot.$relDir;       // current directory          default :
547                    if (in_array(strtolower($txt),$gblImages)) {
548          if (!is_dir($fsDir)) Error("Dir not found","Directory <tt>$relDir<tt> not found on filesystem at <tt>$fsDir</tt>",1) ;                          $d = "image2.gif" ;
549                    } else {
550          global $debug;                          $d = "generic.gif" ;
551          $debug .= "[$gblLogin|$relDir] before >";                  }
552            }
553          if (! check_perm($relDir,trperm_b))  
554                  Error("Access denied","User <tt>$gblLogin</tt> tried to access <tt>$relDir</tt> without valid trustee.",1);  
555          $debug .= "< afeter";          return "<IMG SRC=\"$gblIconLocation" . $d . "\" BORDER=0>" ;
556    } // end function GifIcon
557          $hide_items=",$gblHide,";  
558    //////////////////////////////////////////////////////////////////
559          $dirList = array();  
560          $fileList = array();  function Navigate($fsRoot,$relDir) {
561    
562          // read directory contents          global $gblEditable, $gblIcon, $gblModDays, $webRoot, $gblHide,
563          if ( !($dir = @opendir($fsDir)) )                  $gblIgnoreUnknownFileType, $gblRepositoryDir,
564                  Error("Read Access denied",$relDir,1) ;                  $gblLogin, $gblUserName, $gblDateFmt, $gblTimeFmt,
565          while ($item = readdir($dir)) {                  $gblPermNote,
566                  if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") ) continue ;                  $fsRealmDir, $realm, $realm_sep,
567                  if (is_dir($fsDir.$item) || is_link ($fsDir.$item)) {                  $html, $realm_config,
568                          if (check_perm($relDir.$item,trperm_b)) {                  $HTTP_GET_VARS, $HTTP_SERVER_VARS;
569                                  $dirList[$item] = $item ;          
570                                  $dirNote[$item] = ReadNote($fsDir.$item);          $self     = $HTTP_SERVER_VARS["PHP_SELF"] ;
571                          }  
572                  } else if (is_file($fsDir.$item)) {          $relDir = chopsl($relDir)."/";
573                          if (check_perm($relDir.$item,trperm_r)) {          $fsDir = $fsRoot.$relDir;       // current directory
574                                  $fileList[$item] = $item ;                
575                                  $fileDate[$item] = filemtime($fsDir.$item) ;          if (!is_dir($fsDir)) Error("Dir not found","Directory <tt>$relDir<tt> not found on filesystem at <tt>$fsDir</tt>",1) ;
576                                  $fileSize[$item] = filesize($fsDir.$item) ;  
577                                  $fileNote[$item] = ReadNote($fsDir.$item);          global $debug;
578                          }          $debug .= "[$gblLogin|$relDir] before >";
579                  } else {  
580                          if (! $gblIgnoreUnknownFileType) Error("File Type Error", "Item <tt>".$fsDir.$item."</tt> is not file, directory or link. If you want to ignore errors like this, set <tt>\$gblIgnoreUnknownFileType = 1</tt> in <tt>$realm_config</tt>.",1);          if (! check_perm($relDir,trperm_b))
581                  }                  Error("Access denied","User <tt>$gblLogin</tt> tried to access <tt>$relDir</tt> without valid trustee.",1);
582          }          $debug .= "< afeter";
583          closedir($dir) ;  
584            $hide_items=",$gblHide,";
585          // scan deleted files  
586          if ( HTTP_GET_VAR("show_deleted") == 1 && ($dir = @opendir("$fsDir/.del")) ) {          $dirList = array();
587                  while ($item = readdir($dir)) {          $fileList = array();
588                          if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") || !check_perm($relDir.$item,trperm_w) ) continue ;  
589                          if (is_file($fsDir.".del/$item")) {          // read directory contents
590                                  $fileList[$item] = ".del/$item" ;                        if ( !($dir = @opendir($fsDir)) )
591                                  $fileDate[$item] = filemtime($fsDir.".del/$item") ;                  Error("Read Access denied",$relDir,1) ;
592                                  $fileSize[$item] = filesize($fsDir.".del/$item") ;          while ($item = readdir($dir)) {
593                                  $fileNote[$item] = ReadNote($fsDir.".del/$item");                  if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") ) continue ;
594                          } else {                  if (is_dir($fsDir.$item) || is_link ($fsDir.$item)) {
595                                  $dirList[$item] = ".del/$item" ;                                          if (check_perm($relDir.$item,trperm_b)) {
596                                  $dirNote[$item] = ReadNote($fsDir.".del/$item");                                  $dirList[$item] = $item ;
597                          }                                  $dirNote[$item] = ReadNote($fsDir.$item);
598                  }                          }
599                  closedir($dir) ;                  } else if (is_file($fsDir.$item)) {
600          }                          if (check_perm($relDir.$item,trperm_r)) {
601                                    $fileList[$item] = $item ;              
602          $emptyDir = ! (sizeof($dirList) || sizeof($fileList)) ;                                  $fileDate[$item] = filemtime($fsDir.$item) ;
603                                    $fileSize[$item] = filesize($fsDir.$item) ;
604          // start navigation page                                  $fileNote[$item] = ReadNote($fsDir.$item);
605          $text  = "Use this page to add, delete";                          }
606          if (! isset($HTTP_GET_VARS["show_deleted"])) {                  } else {
607                  $text .= ", <a href=$self?D=".urlencode($relDir)."&show_deleted=1>undelete</a>";                          if (! $gblIgnoreUnknownFileType) Error("File Type Error", "Item <tt>".$fsDir.$item."</tt> is not file, directory or link. If you want to ignore errors like this, set <tt>\$gblIgnoreUnknownFileType = 1</tt> in <tt>$realm_config</tt>.",1);
608          }                  }
609          $text .= " or revise files on this web site." ;          }
610          $text .= "<br>Examine list of files <a href=\"$self?A=Ch1\">changed in last day</a> or <a href=\"$self?A=Ch\">all changes</a>.";          closedir($dir) ;
611          StartHTML("(Navigate)",$text) ;  
612            // scan deleted files
613          print "<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=3 WIDTH=\"100%\">" ;          if ( HTTP_GET_VAR("show_deleted") == 1 && ($dir = @opendir("$fsDir/.del")) ) {
614                    while ($item = readdir($dir)) {
615          // updir (parent) bar                            if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") || !check_perm($relDir.$item,trperm_w) ) continue ;
616          if (chopsl($fsDir) != chopsl($fsRoot)) {                          if (is_file($fsDir.".del/$item")) {
617                  $parent = dirname($relDir) ;                                  $fileList[$item] = ".del/$item" ;              
618                  if ($parent == "") $parent = "/" ;                                  $fileDate[$item] = filemtime($fsDir.".del/$item") ;
619                                    $fileSize[$item] = filesize($fsDir.".del/$item") ;
620                  include("$html/Navigate-parent.html");                                  $fileNote[$item] = ReadNote($fsDir.".del/$item");
621          }                          } else {
622                                    $dirList[$item] = ".del/$item" ;                
623          function plural($name,$count) {                                  $dirNote[$item] = ReadNote($fsDir.".del/$item");
624                  $out="$count $name";                          }
625                  if ($count > 1) {                  }
626                          $out.="s";                  closedir($dir) ;
627                  }          }
628                  return $out;  
629          }          $emptyDir = ! (sizeof($dirList) || sizeof($fileList)) ;
630    
631          $dsort = HTTP_GET_VAR("dsort");          // start navigation page
632          if (! isset($dsort)) $dsort = "name"; // default directory sort          $text  = "Use this page to add, delete";
633            if (! isset($HTTP_GET_VARS["show_deleted"])) {
634          $dsort_arr = array(                  $text .= ", <a href=$self?D=".urlencode($relDir)."&show_deleted=1>undelete</a>";
635                  "name" => array ("rname", "note"),          }
636                  "rname" => array ("name", "note"),          $text .= " or revise files on this web site." ;
637                  "note" => array ("name", "rnote"),          $text .= "<br>Examine list of files <a href=\"$self?A=Ch1\">changed in last day</a> or <a href=\"$self?A=Ch\">all changes</a>.";
638                  "rnote" => array ("name", "note")          StartHTML("(Navigate)",$text) ;
639                  );  
640            print "<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=3 WIDTH=\"100%\">" ;
641          $fsort = HTTP_GET_VAR("fsort");  
642          if (! isset($fsort)) $fsort = "name"; // default directory sort          // updir (parent) bar  
643            if (chopsl($fsDir) != chopsl($fsRoot)) {
644          $fsort_arr = array(                  $parent = dirname($relDir) ;
645                  "name" => array ("rname", "note", "date", "size"),                  if ($parent == "") $parent = "/" ;
646                  "rname" => array ("name", "note", "date", "size"),  
647                  "note" => array ("name", "rnote", "date", "size"),                  include("$html/Navigate-parent.html");
648                  "rnote" => array ("name", "note", "date", "size"),          }
649                  "date" => array ("name", "note", "rdate", "size"),  
650                  "rdate" => array ("name", "note", "date", "size"),          function plural($name,$count) {
651                  "size" => array ("name", "note", "date", "rsize"),                  $out="$count $name";
652                  "rsize" => array ("name", "note", "date", "size")                  if ($count > 1) {
653                  );                          $out.="s";
654                    }
655          $D="D=".urlencode($relDir);                  return $out;
656            }
657          function self_args($arr = array()) {  
658                  global $self;          $dsort = HTTP_GET_VAR("dsort");
659                  $arg = implode("&",$arr);          if (! isset($dsort)) $dsort = "name"; // default directory sort
660                  if ($arg) {  
661                          return $self."?".$arg;          $dsort_arr = array(
662                  } else {                  "name" => array ("rname", "note"),
663                          return $self;                  "rname" => array ("name", "note"),
664                  }                  "note" => array ("name", "rnote"),
665          }                  "rnote" => array ("name", "note")
666          // output subdirs                  );
667          if (sizeof($dirList) > 0) {  
668                  switch ($dsort) {          $fsort = HTTP_GET_VAR("fsort");
669                          case "note":          if (! isset($fsort)) $fsort = "name"; // default directory sort
670                                  $items = $dirNote;  
671                                  asort($items);          $fsort_arr = array(
672                                  break;                  "name" => array ("rname", "note", "date", "size"),
673                          case "rnote":                  "rname" => array ("name", "note", "date", "size"),
674                                  $items = $dirNote;                  "note" => array ("name", "rnote", "date", "size"),
675                                  arsort($items);                  "rnote" => array ("name", "note", "date", "size"),
676                                  break;                  "date" => array ("name", "note", "rdate", "size"),
677                          case "rname":                  "rdate" => array ("name", "note", "date", "size"),
678                                  $items = $dirList;                  "size" => array ("name", "note", "date", "rsize"),
679                                  krsort($items);                  "rsize" => array ("name", "note", "date", "size")
680                                  break;                  );
681                          default:  
682                                  $items = $dirList;          $D="D=".urlencode($relDir);
683                                  ksort($items);  
684                                  break;          function self_args($arr = array()) {
685                  }                  global $self;
686                  $durl = self_args(array($D,"dsort=".$dsort_arr[$dsort][0]));                  $arg = implode("&",$arr);
687                  $nurl = self_args(array($D,"dsort=".$dsort_arr[$dsort][1]));                  if ($arg) {
688                            return $self."?".$arg;
689                  include("$html/Navigate-dirHeader.html");                  } else {
690                            return $self;
691                  while (list($key,$dir) = each($items)) {                  }
692            }
693                          $dir = $dirList[$key];          // output subdirs
694            if (sizeof($dirList) > 0) {
695                          $info_url=self_args(array("A"=>"A=E", "F"=>"F=".urlencode($dir), "D"=>$D));                  switch ($dsort) {
696                          if (substr($dir,0,5) == ".del/") {                          case "note":
697                                  $dir = substr($dir,5,strlen($dir)-5);                                  $items = $dirNote;
698                                  $deleted = " <a href=\"$info_url#undelete\"><SPAN CLASS=deleted TITLE=\"deleted\">deleted</span></a>";                                  asort($items);
699                          } else {                                  break;
700                                  $deleted = "";                          case "rnote":
701                          }                                  $items = $dirNote;
702                                            arsort($items);
703                          $dir_url=$self."?D=".urlencode(chopsl($relDir)."/".$dir);                                  break;
704                          if (check_perm($relDir.$dir,$gblPermNote)) {                          case "rname":
705                                  $note_html="<a href=\"$info_url#note\">".$gblIcon("note")."</a>".$dirNote[$key];                                  $items = $dirList;
706                          } else {                                  krsort($items);
707                                  $note_html=$dirNote[$key];                                  break;
708                          }                          default:
709                                    $items = $dirList;
710                          include("$html/Navigate-dirEntry.html");                                  ksort($items);
711                                    break;
712                  }  // iterate over dirs                  }
713          }  // end if no dirs                  $durl = self_args(array($D,"dsort=".$dsort_arr[$dsort][0]));
714                    $nurl = self_args(array($D,"dsort=".$dsort_arr[$dsort][1]));
715          $durl = self_args(array($D,"fsort=".$fsort_arr[$fsort][0]));  
716          $nurl = self_args(array($D,"fsort=".$fsort_arr[$fsort][1]));                  include("$html/Navigate-dirHeader.html");
717          $uurl = self_args(array($D,"fsort=".$fsort_arr[$fsort][2]));  
718          $surl = self_args(array($D,"fsort=".$fsort_arr[$fsort][3]));                  while (list($key,$dir) = each($items)) {
719    
720          include("$html/Navigate-fileHeader.html");                          $dir = $dirList[$key];
721    
722          if (sizeof($fileList) > 0) {                          $info_url=self_args(array("A"=>"A=E", "F"=>"F=".urlencode($dir), "D"=>$D));
723                  switch ($fsort) {                          if (substr($dir,0,5) == ".del/") {
724                          case "note":                                  $dir = substr($dir,5,strlen($dir)-5);
725                                  $items = $fileNote;                                  $deleted = " <a href=\"$info_url#undelete\"><SPAN CLASS=deleted TITLE=\"deleted\">deleted</span></a>";
726                                  asort($items);                          } else {
727                                  break;                                  $deleted = "";
728                          case "rnote":                          }
729                                  $items = $fileNote;          
730                                  arsort($items);                          $dir_url=$self."?D=".urlencode(chopsl($relDir)."/".$dir);
731                                  break;                          if (check_perm($relDir.$dir,$gblPermNote)) {
732                          case "date":                                  $note_html="<a href=\"$info_url#note\">".$gblIcon("note")."</a>".$dirNote[$key];
733                                  $items = $fileDate;                          } else {
734                                  asort($items);                                  $note_html=$dirNote[$key];
735                                  break;                          }
736                          case "rdate":  
737                                  $items = $fileDate;                          $dir_html = isBlank($dir,"directory");
738                                  arsort($items);  
739                                  break;                          include("$html/Navigate-dirEntry.html");
740                          case "size":  
741                                  $items = $fileSize;                  }  // iterate over dirs
742                                  asort($items);          }  // end if no dirs
743                                  break;  
744                          case "rsize":          $durl = self_args(array($D,"fsort=".$fsort_arr[$fsort][0]));
745                                  $items = $fileSize;          $nurl = self_args(array($D,"fsort=".$fsort_arr[$fsort][1]));
746                                  arsort($items);          $uurl = self_args(array($D,"fsort=".$fsort_arr[$fsort][2]));
747                                  break;          $surl = self_args(array($D,"fsort=".$fsort_arr[$fsort][3]));
748                          case "rname":  
749                                  $items = $fileList;          $html_uri = $webRoot;
750                                  krsort($items);  
751                                  break;          if (substr($relDir,0,1) == "/") {
752                          default:                  $html_uri .= substr($relDir,1,strlen($relDir)-1);
753                                  $items = $fileList;          } else {
754                                  ksort($items);                  $html_uri .= $relDir;
755                                  break;          }
756                  }  
757            include("$html/Navigate-fileHeader.html");
758            while (list($key,$file) = each($items)) {  
759                  $file = $fileList[$key];          if (sizeof($fileList) > 0) {
760                  $path = $fsDir."/".$file ;                  switch ($fsort) {
761                  $mod  = $fileDate[$key];                          case "note":
762                  $sz   = $fileSize[$key];                                  $items = $fileNote;
763                                            asort($items);
764                  if ($sz >= 10240) {                                  break;
765                          $sz = (int)(($sz+1023)/1024) . " k" ;                          case "rnote":
766                  } else {                                  $items = $fileNote;
767                          $sz .= " " ;                                  arsort($items);
768                  } // end size                                  break;
769                            case "date":
770                  $a = $b = "" ;                                  $items = $fileDate;
771                                    asort($items);
772                  $info_url=$self."?A=E&F=".urlencode($file)."&D=".urlencode($relDir);                                  break;
773                            case "rdate":
774                  if ( ($mod + $gblModDays*86400) > time() ) {                                  $items = $fileDate;
775                          $a  = "<SPAN CLASS=RED TITLE=\"Newer" ;                                  arsort($items);
776                          $a .= " than $gblModDays days\"> * </SPAN>" ;                                  break;
777                  }                          case "size":
778                                    $items = $fileSize;
779                  $file_lock=CheckLock($path);                                  asort($items);
780                                    break;
781                  $file_url_html="<A HREF=\"$self?A=V&D=".urlencode($relDir)."&F=".urlencode($file);                          case "rsize":
782                  $file_url_html.="\" TITLE=\"View file\">" ;                                  $items = $fileSize;
783                                    arsort($items);
784                  if (substr($file,0,5) != ".del/") {                                  break;
785                          $file_url_html .= $file . "</A>" . $a ;                          case "rname":
786                  } else {                                  $items = $fileList;
787                          $file_url_html .= substr($file,5,strlen($file)-5) . "</a> <a href=\"$info_url#undelete\"><SPAN CLASS=deleted TITLE=\"deleted\">deleted</span></a>";                                  krsort($items);
788                  }                                  break;
789                            default:
790                  if (check_perm($relDir.$file,$gblPermNote)) {                                  $items = $fileList;
791                          $note_html="<a href=\"$info_url#note\">".$gblIcon("note")."</a>".$fileNote[$key];                                  ksort($items);
792                  } else {                                  break;
793                          $note_html=$fileNote[$key];                  }
794                  }  
795              while (list($key,$file) = each($items)) {
796                  $ext = strtolower(strrchr($file,".")) ;                  $file = $fileList[$key];
797                    $path = $fsDir."/".$file ;
798                  if ($file_lock) {                  $mod  = $fileDate[$key];
799                          if ($file_lock == $gblUserName) {                  $sz   = $fileSize[$key];
800                                  $b.="<A HREF=\"$self?A=Ci&D=".urlencode($relDir)."&F=".urlencode($file);          
801                                  $b.="\" TITLE=\"Checkin (update) file on server\">" ;                  if ($sz >= 10240) {
802                                  $file_url_html=$b;                          $sz = (int)(($sz+1023)/1024) . " k" ;
803                                  $b.=$gblIcon("checkin")."</A>" ;                  } else {
804                                  $b.= $gblIcon("blank");                          $sz .= " " ;
805                                  $file_url_html.="$file</a> $a";                  } // end size
806                                  $note_html = $gblIcon("blank")."<b>Please check-in (update) this file</b>";  
807                          } else {                  $a = $b = "" ;
808                                  $b = $gblIcon("locked");  
809                                  $b.= $gblIcon("blank");                  $info_url=$self."?A=E&F=".urlencode($file)."&D=".urlencode($relDir);
810                                  $note_html = $gblIcon("blank")."<b>File locked by $file_lock</b>";  
811                                  $file_url_html = "$file $a";                  if ( ($mod + $gblModDays*86400) > time() ) {
812                          }                          $a  = "<SPAN CLASS=RED TITLE=\"Newer" ;
813                  } else {                          $a .= " than $gblModDays days\"> * </SPAN>" ;
814                          if (check_perm($relDir.$file,trperm_w)) {                  }
815                                  $b.="<A HREF=\"$self?A=Co&D=".urlencode($relDir)."&F=".urlencode($file);  
816                                  $b.="\" TITLE=\"Checkout file for edit\">" ;                  $file_lock=CheckLock($path);
817                                  $b.=$gblIcon("checkout")."</A>";  
818                          }                  $file_url_html="<A HREF=\"$self?A=V&D=".urlencode($relDir)."&F=".urlencode($file);
819                    $file_url_html.="\" TITLE=\"View file\">" ;
820                          if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) {    
821                                  $b.="<A HREF=\"$self?A=C&D=".urlencode($relDir)."&F=".urlencode($file);                  if (substr($file,0,5) != ".del/") {
822                                  $b.="\" TITLE=\"List contents\">" ;                          $file_url_html .= isBlank($file) . "</A>" . $a ;
823                                  $b.=$gblIcon("view")."</A>" ;                  } else {
824                          } else {                          $file_url_html .= isBlank(substr($file,5,strlen($file)-5)) . "</a> <a href=\"$info_url#undelete\"><SPAN CLASS=deleted TITLE=\"deleted\">deleted</span></a>";
825                                  $b.= $gblIcon("blank");                  }
826                          }  
827                  }                  if (check_perm($relDir.$file,$gblPermNote)) {
828                            $note_html="<a href=\"$info_url#note\">".$gblIcon("note")."</a>".$fileNote[$key];
829                  $mod = date("$gblDateFmt $gblTimeFmt",$mod);                  } else {
830                            $note_html=$fileNote[$key];
831                  include("$html/Navigate-fileEntry.html");                  }
832    
833            }  // iterate over files                  $ext = strtolower(strrchr($file,".")) ;
834          } else {  // end if no files  
835                  include("$html/Navigate-noFiles.html");                  if ($file_lock) {
836          }                          if ($file_lock == $gblUserName) {
837                                    $b.="<A HREF=\"$self?A=Ci&D=".urlencode($relDir)."&F=".urlencode($file);
838          if ($emptyDir && $relDir != "") {                                  $b.="\" TITLE=\"Checkin (update) file on server\">" ;
839                  include("$html/Navigate-emptyDir.html");                                  $file_url_html=$b;
840          } // end if emptyDir                                  $b.=$gblIcon("checkin")."</A>" ;
841                                    $b.= $gblIcon("blank");
842          include("$html/Navigate-hr.html");                                        $file_url_html.="$file</a> $a";
843                                    $note_html = $gblIcon("blank")."<b>Please check-in (update) this file</b>";
844          if (file_exists("$fsRealmDir/$realm".$realm_sep."info.inc")) {                          } else {
845                  print "<TR><TD></TD><TD COLSPAN=5>";                                  $b = $gblIcon("locked");
846                  include("$fsRealmDir/$realm".$realm_sep."info.inc");                                  $b.= $gblIcon("blank");
847                  print "</TD></TR>";                                  $note_html = $gblIcon("blank")."<b>File locked by $file_lock</b>";
848                  include("$html/Navigate-hr.html");                                        $file_url_html = "$file $a";
849          } elseif (file_exists("$gblRepositoryDir/.info.inc")) {                          }
850                  print "<TR><TD></TD><TD COLSPAN=5>";                  } else {
851                  include("$gblRepositoryDir/.info.inc");                          if (check_perm($relDir.$file,trperm_w)) {
852                  print "</TD></TR>";                                  $b.="<A HREF=\"$self?A=Co&D=".urlencode($relDir)."&F=".urlencode($file);
853                  include("$html/Navigate-hr.html");                                        $b.="\" TITLE=\"Checkout file for edit\">" ;
854          }                                  $b.=$gblIcon("checkout")."</A>";
855                            }
856          include("$html/Navigate-createNew.html");  
857                            if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) {  
858          print "</TABLE>";                                  $b.="<A HREF=\"$self?A=C&D=".urlencode($relDir)."&F=".urlencode($file);
859                                    $b.="\" TITLE=\"List contents\">" ;
860          EndHTML() ;                                  $b.=$gblIcon("view")."</A>" ;
861  } // end function Navigate                          } else {
862                                    $b.= $gblIcon("blank");
863  //////////////////////////////////////////////////////////////////                          }
864                    }
865  function UploadPage($fsRoot, $relDir, $filename="") {  
866                    $mod = date("$gblDateFmt $gblTimeFmt",$mod);
867          global $html, $HTTP_SERVER_VARS;  
868                    include("$html/Navigate-fileEntry.html");
869          $self = $HTTP_SERVER_VARS["PHP_SELF"] ;  
870          include("$html/UploadPage.html");            }  // iterate over files
871            } else {  // end if no files
872  } // end function UploadPage                  include("$html/Navigate-noFiles.html");
873            }
874  //////////////////////////////////////////////////////////////////  
875            if ($emptyDir && $relDir != "") {
876  // Error with sysadmin flag are reported to error_log or hidden from                  include("$html/Navigate-emptyDir.html");
877  // users          } // end if emptyDir
878    
879  function Error($title,$text="",$sysadmin=0,$no_404=0) {          include("$html/Navigate-hr.html");      
880          global $gblSeparateAdminMessages,  
881                  $gblMailAdminMessages,$realm,          if (file_exists("$fsRealmDir/$realm".$realm_sep."info.inc")) {
882                  $HTTP_SERVER_VARS;                  print "<TR><TD></TD><TD COLSPAN=5>";
883          if (! headers_sent() && ! $no_404) header("HTTP/1.0 404 Not Found");                  include("$fsRealmDir/$realm".$realm_sep."info.inc");
884          if ($sysadmin) {                  print "</TD></TR>";
885                  if ($gblSeparateAdminMessages) {                  include("$html/Navigate-hr.html");      
886                          $user="Your administrator ";          } elseif (file_exists("$gblRepositoryDir/.info.inc")) {
887                          if ($gblMailAdminMessages) {                  print "<TR><TD></TD><TD COLSPAN=5>";
888                                  mail($HTTP_SERVER_VARS["SERVER_ADMIN"], "docman $realm error message: $title", strip_tags($text));                  include("$gblRepositoryDir/.info.inc");
889                                  $user.="<tt>".$HTTP_SERVER_VARS["SERVER_ADMIN"]."</tt> ";                  print "</TD></TR>";
890                          }                  include("$html/Navigate-hr.html");      
891                          $user.="has been notified about error" ;          }
892                          StartHTML("($title)",$user);  
893                          echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;          include("$html/Navigate-createNew.html");
894                          EndHTML();  
895                          error_log("docman $realm: ".strip_tags($text));          print "</TABLE>";
896                  } else {  
897                          StartHTML("ADMIN: ".$title,$text) ;          EndHTML() ;
898                          echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;  } // end function Navigate
899                          EndHTML();  
900                  }  //////////////////////////////////////////////////////////////////
901          } else {  
902                  StartHTML("(".$title.")",$text) ;  function UploadPage($fsRoot, $relDir, $filename="") {
903                  echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;  
904                  EndHTML() ;          global $html, $HTTP_SERVER_VARS;
905          }  
906          exit ;          $self = $HTTP_SERVER_VARS["PHP_SELF"] ;
907  } // end function Error          include("$html/UploadPage.html");
908    
909  function LogIt($target,$msg, $changelog=0) {  } // end function UploadPage
910    
911          global $gblDateFmt, $gblTimeFmt, $gblUserName, $gblFsRoot;  //////////////////////////////////////////////////////////////////
912    
913          $dir=dirname($target);  // Error with sysadmin flag are reported to error_log or hidden from
914          if (! file_exists($dir."/.log")) {  // users
915                  if (! @mkdir($dir."/.log",0700)) Error("docman installation problem","can't create log directory <tt>$dir/.log</tt>",1);  
916          }  function Error($title,$text="",$sysadmin=0,$no_404=0) {
917          $file=basename($target);          global $gblSeparateAdminMessages,
918                    $gblMailAdminMessages,$realm,
919          $log=fopen("$dir/.log/$file","a+");                  $HTTP_SERVER_VARS;
920          fputs($log,date("$gblDateFmt\t$gblTimeFmt").          if (! headers_sent() && ! $no_404) header("HTTP/1.0 404 Not Found");
921                  "\t$gblUserName\t$msg\n");          if ($sysadmin) {
922          fclose($log);                  if ($gblSeparateAdminMessages) {
923                            $user="Your administrator ";
924          if (! $changelog) return;                          if ($gblMailAdminMessages) {
925                                    mail($HTTP_SERVER_VARS["SERVER_ADMIN"], "docman $realm error message: $title", strip_tags($text));
926          $log=fopen("$gblFsRoot/.changelog","a+");                                  $user.="<tt>".$HTTP_SERVER_VARS["SERVER_ADMIN"]."</tt> ";
927          if (substr($target,0,strlen($gblFsRoot)) == $gblFsRoot)                          }
928                  $target=substr($target,strlen($gblFsRoot),strlen($target)-strlen($gblFsRoot));                          $user.="has been notified about error" ;
929          $msg=str_replace("\t"," ",$msg);                          StartHTML("($title)",$user);
930          fputs($log,time()."\t$target\t$gblUserName\t$msg\n");                          echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;
931          fclose($log);                          EndHTML();
932                            error_log("docman $realm: ".strip_tags($text));
933          // FIX: implement e-mail notification based on $changelog                  } else {
934          // permission                          StartHTML("ADMIN: ".$title,$text) ;
935  }                          echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;
936                            EndHTML();
937                    }
938  //////////////////////////////////////////////////////////////////          } else {
939                    StartHTML("(".$title.")",$text) ;
940  function WriteNote($target,$msg) {                  echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;
941                    EndHTML() ;
942          $target=stripSlashes($target);          }
943          $dir=dirname($target);          exit ;
944          if (! file_exists($dir."/.note")) {  } // end function Error
945                  mkdir($dir."/.note",0700);  
946          }  function LogIt($target,$msg, $changelog=0) {
947          $file=basename($target);  
948            global $gblDateFmt, $gblTimeFmt, $gblUserName, $gblFsRoot;
949          $note=fopen("$dir/.note/$file","w");  
950          if (! $note) {          $dir=dirname($target);
951                  Error("Error writing note","Can't open note file <tt>$dir/.note/$file</tt> for writing",1);          if (! file_exists($dir."/.log")) {
952          }                  if (! @mkdir($dir."/.log",0700)) Error("docman installation problem","can't create log directory <tt>$dir/.log</tt>",1);
953          fputs($note,"$msg\n");          }
954          fclose($note);          $file=basename($target);
955    
956          LogIt($target,"added note $msg");          $log=fopen("$dir/.log/$file","a+");
957            fputs($log,date("$gblDateFmt\t$gblTimeFmt").
958  }                  "\t$gblUserName\t$msg\n");
959            fclose($log);
960  function ReadNote($target) {  
961            if (! $changelog) return;
962          $target=stripSlashes($target);  
963          $dir=dirname($target);          $log=fopen("$gblFsRoot/.changelog","a+");
964          $file=basename($target);          if (substr($target,0,strlen($gblFsRoot)) == $gblFsRoot)
965          $msg="";                  $target=substr($target,strlen($gblFsRoot),strlen($target)-strlen($gblFsRoot));
966          if (file_exists($dir."/.note/$file")) {          $msg=str_replace("\t"," ",$msg);
967                  $note=fopen("$dir/.note/$file","r");          fputs($log,time()."\t$target\t$gblUserName\t$msg\n");
968                  $msg=fgets($note,4096);          fclose($log);
969                  fclose($note);  
970          }          // FIX: implement e-mail notification based on $changelog
971          return HtmlSpecialChars(StripSlashes($msg));          // permission
972    }
973  }  
974    
975  //////////////////////////////////////////////////////////////////  //////////////////////////////////////////////////////////////////
976    
977  function MoveTo($source,$folder) {  function WriteNote($target,$msg) {
978    
979          $source=stripSlashes($source);          $target=stripSlashes($target);
980          $file=basename($source);          $dir=dirname($target);
981          if (! file_exists($folder)) {          if (! file_exists($dir."/.note")) {
982                  mkdir($folder,0700);                  mkdir($dir."/.note",0700);
983          }          }
984          if (file_exists($source)) {          $file=basename($target);
985                  rename($source,"$folder/$file");  
986          }          $note=fopen("$dir/.note/$file","w");
987  }          if (! $note) {
988                    Error("Error writing note","Can't open note file <tt>$dir/.note/$file</tt> for writing",1);
989  //////////////////////////////////////////////////////////////////          }
990            fputs($note,"$msg\n");
991  function Lock($target) {          fclose($note);
992    
993          global $gblUserName;          LogIt($target,"added note $msg");
994    
995          $target=stripSlashes($target);  }
996          $dir=dirname($target);  
997          if (! file_exists($dir."/.lock")) {  function ReadNote($target) {
998                  mkdir($dir."/.lock",0700);  
999          }          $target=stripSlashes($target);
1000          $file=basename($target);          $dir=dirname($target);
1001            $file=basename($target);
1002          if (file_exists("$dir/.lock/$file")) {          $msg="";
1003                  LogIt($target,"attempt to locked allready locked file!");          if (file_exists($dir."/.note/$file")) {
1004          } else {                  $note=fopen("$dir/.note/$file","r");
1005                  $lock=fopen("$dir/.lock/$file","w");                  $msg=fgets($note,4096);
1006                  fputs($lock,"$gblUserName\n");                  fclose($note);
1007                  fclose($lock);          }
1008            return HtmlSpecialChars(StripSlashes($msg));
1009                  LogIt($target,"file locked");  
1010          }  }
1011    
1012  }  //////////////////////////////////////////////////////////////////
1013    
1014  function CheckLock($target) {  function MoveTo($source,$folder) {
1015    
1016          $target=stripSlashes($target);          $source=stripSlashes($source);
1017          $dir=dirname($target);          $file=basename($source);
1018          $file=basename($target);          if (! file_exists($folder)) {
1019          $msg=0;                  mkdir($folder,0700);
1020          if (file_exists($dir."/.lock/$file")) {          }
1021                  $lock=fopen("$dir/.lock/$file","r");          if (file_exists($source)) {
1022                  $msg=fgets($lock,4096);                  rename($source,"$folder/$file");
1023                  fclose($lock);          }
1024          }  }
1025          return chop($msg);  
1026    //////////////////////////////////////////////////////////////////
1027  }  
1028    function Lock($target) {
1029  function Unlock($target) {  
1030            global $gblUserName;
1031          $target=stripSlashes($target);  
1032          $dir=dirname($target);          $target=stripSlashes($target);
1033          $file=basename($target);          $dir=dirname($target);
1034          if (file_exists($dir."/.lock/$file")) {          if (! file_exists($dir."/.lock")) {
1035                  unlink("$dir/.lock/$file");                  mkdir($dir."/.lock",0700);
1036                  LogIt($target,"file unlocked");          }
1037          } else {          $file=basename($target);
1038                  LogIt($target,"attempt to unlocked non-locked file!");  
1039          }          if (file_exists("$dir/.lock/$file")) {
1040                    LogIt($target,"attempt to locked allready locked file!");
1041  }          } else {
1042                    $lock=fopen("$dir/.lock/$file","w");
1043  //////////////////////////////////////////////////////////////////                  fputs($lock,"$gblUserName\n");
1044                    fclose($lock);
1045  function urlpath($url) {  
1046          $url=urlencode(StripSlashes("$url"));                  LogIt($target,"file locked");
1047          $url=str_replace("%2F","/",$url);          }
1048          $url=str_replace("+","%20",$url);  
1049          return($url);  }
1050  }  
1051    function CheckLock($target) {
1052  //////////////////////////////////////////////////////////////////  
1053            $target=stripSlashes($target);
1054  function safe_rename($fromdir,$fromfile,$tofile) {          $dir=dirname($target);
1055            $file=basename($target);
1056          global $gblNumBackups;          $msg=0;
1057            if (file_exists($dir."/.lock/$file")) {
1058          function try_rename($from,$to) {                  $lock=fopen("$dir/.lock/$file","r");
1059  #               print "$from -> $to\n";                  $msg=fgets($lock,4096);
1060                  if (file_exists($from) && is_writeable(dirname($to))) {                  fclose($lock);
1061                          return rename($from,$to);          }
1062                  } else {          return chop($msg);
1063                          return 0;  
1064                  }  }
1065          }  
1066    function Unlock($target) {
1067          function try_dir($todir) {  
1068                  if (! file_exists($todir)) {          $target=stripSlashes($target);
1069                          @mkdir($todir,0700);          $dir=dirname($target);
1070                  }          $file=basename($target);
1071          }          if (file_exists($dir."/.lock/$file")) {
1072                    unlink("$dir/.lock/$file");
1073          $to="$fromdir/$tofile";                  LogIt($target,"file unlocked");
1074          $todir=dirname($to);          } else {
1075          $tofile=basename($to);                  LogIt($target,"attempt to unlocked non-locked file!");
1076            }
1077  #       print "<pre>$fromdir / $fromfile -> $todir / $tofile\n\n";  
1078    }
1079          if (! try_rename("$fromdir/$fromfile","$todir/$tofile")) Error("Rename error","Can't rename file <tt>$fromfile</tt> to <tt>$tofile</tt>",1);  
1080          try_dir("$todir/.log");  //////////////////////////////////////////////////////////////////
1081          try_rename("$fromdir/.log/$fromfile","$todir/.log/$tofile");  
1082          try_dir("$todir/.note");  function urlpath($url) {
1083          try_rename("$fromdir/.note/$fromfile","$todir/.note/$tofile");          $url=urlencode(StripSlashes("$url"));
1084          try_dir("$todir/.lock");          $url=str_replace("%2F","/",$url);
1085          try_rename("$fromdir/.lock/$fromfile","$todir/.lock/$tofile");          $url=str_replace("+","%20",$url);
1086          try_dir("$todir/.bak");          return($url);
1087          for($i=0;$i<=$gblNumBackups;$i++) {  }
1088                  try_rename("$fromdir/.bak/$i/$fromfile","$todir/.bak/$i/$tofile");  
1089          }  //////////////////////////////////////////////////////////////////
1090  }  
1091    function safe_rename($fromdir,$fromfile,$tofile) {
1092    
1093  //////////////////////////////////////////////////////////////////          global $gblNumBackups;
1094    
1095  // recursivly delete directory          function try_rename($from,$to) {
1096    #               print "$from -> $to\n";
1097  function rrmdir($dir) {                  if (file_exists($from) && is_writeable(dirname($to))) {
1098          $handle=opendir($dir);                          return rename($from,$to);
1099          while ($file = readdir($handle)) {                  } else {
1100                  if ($file != "." && $file != "..") {                          return 0;
1101                          if (is_dir("$dir/$file"))                  }
1102                                  rrmdir("$dir/$file");          }
1103                          else  
1104                                  if (! @unlink("$dir/$file")) return(0);          function try_dir($todir) {
1105                  }                  if (! file_exists($todir)) {
1106          }                          @mkdir($todir,0700);
1107          closedir($handle);                  }
1108          return @rmdir($dir);          }
1109  }  
1110            $to="$fromdir/$tofile";
1111  //////////////////////////////////////////////////////////////////          $todir=dirname($to);
1112            $tofile=basename($to);
1113  function DisplayChangeLog($day) {  
1114    #       print "<pre>$fromdir / $fromfile -> $todir / $tofile\n\n";
1115          global $gblFsRoot, $gblDateFmt, $gblTimeFmt,  
1116                  $HTTP_SERVER_VARS;          if (! try_rename("$fromdir/$fromfile","$todir/$tofile")) Error("Rename error","Can't rename file <tt>$fromfile</tt> to <tt>$tofile</tt>",1);
1117            try_dir("$todir/.log");
1118          $self  = $HTTP_SERVER_VARS["PHP_SELF"];          try_rename("$fromdir/.log/$fromfile","$todir/.log/$tofile");
1119            try_dir("$todir/.note");
1120          if (!file_exists("$gblFsRoot/.changelog")) return;          try_rename("$fromdir/.note/$fromfile","$todir/.note/$tofile");
1121          $log=fopen("$gblFsRoot/.changelog","r");          try_dir("$todir/.lock");
1122          $logarr = array();          try_rename("$fromdir/.lock/$fromfile","$todir/.lock/$tofile");
1123          while($line = fgetcsv($log,512,"\t")) {          try_dir("$todir/.bak");
1124                  while (sizeof($line) > 4) {          for($i=0;$i<=$gblNumBackups;$i++) {
1125                          $tmp = array_pop($line);                  try_rename("$fromdir/.bak/$i/$fromfile","$todir/.bak/$i/$tofile");
1126                          $line.=" $tmp";          }
1127                  }  }
1128                  if ($day!=1 || ($day==1 && (time()-$line[0] < 24*60*60))) {  
1129                          array_unshift($logarr,array($line[0],$line[1],$line[2],$line[3]));  
1130                  }  //////////////////////////////////////////////////////////////////
1131          }  
1132          fclose($log);  // recursivly delete directory
1133          $cl1=" class=LST"; $cl2="";  
1134          print "<table border=0 width=100%>\n";  function rrmdir($dir) {
1135          while ($e = array_shift($logarr)) {          $handle=opendir($dir);
1136                  $cl=$cl1; $cl1=$cl2; $cl2=$cl;          while ($file = readdir($handle)) {
1137                  $date = date($gblDateFmt, $e[0]);                  if ($file != "." && $file != "..") {
1138                  $time = date($gblTimeFmt, $e[0]);                          if (is_dir("$dir/$file"))
1139                  $dir = dirname($e[1]);                                  rrmdir("$dir/$file");
1140                  $file = basename($e[1]);                          else
1141                  print "<tr><td$cl>$date</td><td$cl>$time</td><td$cl><a href=\"$HTTP_SERVER_VARS[PHP_SELF]?D=".urlencode($dir)."\">$dir</a>/$file</td><td$cl>$e[2]</td><td$cl>$e[3]</td></tr>\n";                                  if (! @unlink("$dir/$file")) return(0);
1142          }                  }
1143          print "</table>";          }
1144          print "<p>".GifIcon("up")." Back to <a href=\"$self\">front page</a>.</p>";          closedir($handle);
1145  }          return @rmdir($dir);
1146    }
1147  //////////////////////////////////////////////////////////////////  
1148    //////////////////////////////////////////////////////////////////
1149  function Download($path,$force=0) {  
1150          global $HTTP_SERVER_VARS,$mime_type;  function DisplayChangeLog($day) {
1151    
1152          // default transfer-encoding          global $gblFsRoot, $gblDateFmt, $gblTimeFmt,
1153          $encoding = "binary";                  $HTTP_SERVER_VARS;
1154    
1155          // known transfer encodings          $self  = $HTTP_SERVER_VARS["PHP_SELF"];
1156          $encoding_ext = array(  
1157                  "gz" => "x-gzip",          if (!file_exists("$gblFsRoot/.changelog")) return;
1158                  "Z" => "x-compress",          $log=fopen("$gblFsRoot/.changelog","r");
1159          );          $logarr = array();
1160            while($line = fgetcsv($log,512,"\t")) {
1161          $file = basename($path);                  while (sizeof($line) > 4) {
1162          $size = filesize($path);                          $tmp = array_pop($line);
1163                            $line.=" $tmp";
1164          $ext_arr = explode(".",$file);                  }
1165          $ext = array_pop($ext_arr);                  if ($day!=1 || ($day==1 && (time()-$line[0] < 24*60*60))) {
1166          if (isset($encoding_ext[$ext])) {                          array_unshift($logarr,array($line[0],$line[1],$line[2],$line[3]));
1167                  $encoding = $encoding_ext[$ext];                  }
1168                  $ext = array_pop($ext_arr);          }
1169          }          fclose($log);
1170            $cl1=" class=LST"; $cl2="";
1171          if ($force || !isset($mime_type[$ext])) {          print "<table border=0 width=100%>\n";
1172                  header("Content-Type: application/force-download");          while ($e = array_shift($logarr)) {
1173          } else {                  $cl=$cl1; $cl1=$cl2; $cl2=$cl;
1174                  header("Content-Type: $mime_type[$ext]");                  $date = date($gblDateFmt, $e[0]);
1175          }                  $time = date($gblTimeFmt, $e[0]);
1176                    $dir = dirname($e[1]);
1177          // IE5.5 just downloads index.php if we don't do this                  $file = basename($e[1]);
1178          if(preg_match("/MSIE 5.5/", $HTTP_SERVER_VARS["HTTP_USER_AGENT"])) {                  print "<tr><td$cl>$date</td><td$cl>$time</td><td$cl><a href=\"$HTTP_SERVER_VARS[PHP_SELF]?D=".urlencode($dir)."\">$dir</a>/$file</td><td$cl>$e[2]</td><td$cl>$e[3]</td></tr>\n";
1179                  header("Content-Disposition: filename=$file");          }
1180          } else {          print "</table>";
1181                  header("Content-Disposition: attachment; filename=$file");          print "<p>".GifIcon("up")." Back to <a href=\"$self\">front page</a>.</p>";
1182          }  }
1183    
1184          header("Content-Transfer-Encoding: $encoding");  //////////////////////////////////////////////////////////////////
1185          $fh = fopen($path, "r");  
1186          fpassthru($fh);  function Download($path,$force=0) {
1187  }          global $HTTP_SERVER_VARS,$mime_type;
1188    
1189            // default transfer-encoding
1190  //////////////////////////////////////////////////////////////////          $encoding = "binary";
1191    
1192  function chopsl($path) {          // known transfer encodings
1193          $path=str_replace("//","/",$path);          $encoding_ext = array(
1194          if (substr($path,strlen($path)-1,1) == "/") $path=substr($path,0,strlen($path)-1);                  "gz" => "x-gzip",
1195          return $path;                  "Z" => "x-compress",
1196  }          );
1197    
1198  //////////////////////////////////////////////////////////////////          $file = basename($path);
1199  /*          $size = filesize($path);
1200          Document manager ACL implementation  
1201            $ext_arr = explode(".",$file);
1202          Written by Dobrica Pavlinusic <dpavlin@rot13.org>          $ext = array_pop($ext_arr);
1203            if (isset($encoding_ext[$ext])) {
1204          Based on ideas from Linux trustees code                  $encoding = $encoding_ext[$ext];
1205          by Vyacheslav Zavadsky <zavadsky@braysystems.com>                  $ext = array_pop($ext_arr);
1206  */          }
1207    
1208  define('trmask_not',1 << 0);          if ($force || !isset($mime_type[$ext])) {
1209  define('trmask_clear',1 << 1);                  header("Content-Type: application/force-download");
1210  define('trmask_deny',1 << 2);          } else {
1211  define('trmask_one_level',1 << 3);                  header("Content-Type: $mime_type[$ext]");
1212  define('trmask_group',1 << 4);          }
1213    
1214  define('trperm_r',1 << 5);          // IE5.5 just downloads index.php if we don't do this
1215  define('trperm_w',1 << 6);          if(preg_match("/MSIE 5.5/", $HTTP_SERVER_VARS["HTTP_USER_AGENT"])) {
1216  define('trperm_b',1 << 7);                  header("Content-Disposition: filename=$file");
1217  define('trperm_n',1 << 8);          } else {
1218                    header("Content-Disposition: attachment; filename=$file");
1219  $trustee_a2n = array(          }
1220          '!' => trmask_not,  
1221          'C' => trmask_clear,          header("Content-Transfer-Encoding: $encoding");
1222          'D' => trmask_deny,          $fh = fopen($path, "r");
1223          'O' => trmask_one_level,          fpassthru($fh);
1224          '+' => trmask_group,  }
1225          'R' => trperm_r,  
1226          'W' => trperm_w,  
1227          'B' => trperm_b,  //////////////////////////////////////////////////////////////////
1228          'N' => trperm_n,  
1229  );  function chopsl($path) {
1230            $path=str_replace("//","/",$path);
1231  // debugging function          if (substr($path,strlen($path)-1,1) == "/") $path=substr($path,0,strlen($path)-1);
1232  function display_trustee($t) {          return $path;
1233          global $trustee_a2n;  }
1234          $out="";  
1235          foreach ($trustee_a2n as $c=>$v) {  //////////////////////////////////////////////////////////////////
1236                  if ($t & $v) $out.=$c;  /*
1237          }          Document manager ACL implementation
1238          return $out;  
1239  }          Written by Dobrica Pavlinusic <dpavlin@rot13.org>
1240  function display_all_trustee() {  
1241          global $trustees;          Based on ideas from Linux trustees code
1242          print "trustee dump:<br>\n";          by Vyacheslav Zavadsky <zavadsky@braysystems.com>
1243          foreach ($trustees as $path => $tr) {  */
1244                  print "<br><tt>$path</tt>\n";  
1245                  foreach ($tr as $user=>$perm) {  define('trmask_not',1 << 0);
1246                          print "$user == $perm (".display_trustee($perm).")<br>\n";  define('trmask_clear',1 << 1);
1247                  }  define('trmask_deny',1 << 2);
1248          }  define('trmask_one_level',1 << 3);
1249  }  define('trmask_group',1 << 4);
1250    
1251  function init_trustee() {  define('trperm_r',1 << 5);
1252    define('trperm_w',1 << 6);
1253  global $trustee_conf,$trustee_php,$trustee_a2n,$groups,$trustees;  define('trperm_b',1 << 7);
1254    define('trperm_n',1 << 8);
1255  // do we need to re-create compiled trustees?  
1256  if (! file_exists($trustee_conf)) {  $trustee_a2n = array(
1257          # $error="$trustee_conf doesn't exits";          '!' => trmask_not,
1258          return 0;       # don't use trustees          'C' => trmask_clear,
1259  } elseif (file_exists($trustee_conf) && !is_readable($trustee_conf)) {          'D' => trmask_deny,
1260          $error="<tt>$trustee_conf</tt> exits, but is not readable";          'O' => trmask_one_level,
1261  } elseif (!is_writable(dirname($trustee_php))) {          '+' => trmask_group,
1262          $error="<tt>".dirname($trustee_php)."</tt> must be writable by web server user";          'R' => trperm_r,
1263  } elseif (file_exists($trustee_php) && !is_writable($trustee_php)) {          'W' => trperm_w,
1264          $error="trustees cache file <tt>$trustee_php</tt> exists, but is not writable by web server";          'B' => trperm_b,
1265  } elseif (@filemtime($trustee_conf) >= @filemtime($trustee_php)) {          'N' => trperm_n,
1266          $fp_php=@fopen($trustee_php,"w");  );
1267          fputs($fp_php,"<?php // don't edit by hand!\n");  
1268    // debugging function
1269          $fp_conf=fopen($trustee_conf,"r");  function display_trustee($t) {
1270            global $trustee_a2n;
1271          $groups_arr = array();          $out="";
1272          $perm_arr = array();          foreach ($trustee_a2n as $c=>$v) {
1273                    if ($t & $v) $out.=$c;
1274          $tr_arr = array();          }
1275            return $out;
1276          while (! feof($fp_conf)) {  }
1277                  $l = trim(fgets($fp_conf,4096));  function display_all_trustee() {
1278                  if (substr($l,0,1) == "+") {    // no comment          global $trustees;
1279                          $arr=explode(":",$l);          print "trustee dump:<br>\n";
1280                          $groups_arr[$arr[0]] = $arr[1] ;          foreach ($trustees as $path => $tr) {
1281                  } elseif (substr($l,0,1) != "#") {                  print "<br><tt>$path</tt>\n";
1282                          $arr=explode(":",$l);                  foreach ($tr as $user=>$perm) {
1283                          $path=array_shift($arr);                          print "$user == $perm (".display_trustee($perm).")<br>\n";
1284                          if ($path == "") continue;                  }
1285                          $sep2="";          }
1286                          while ($user=array_shift($arr)) {  }
1287                                  $perm=0;  
1288                                  if (substr($user,0,1) == "+") {  function init_trustee() {
1289                                          $perm|=trmask_group;  
1290                                          $user=substr($user,1,strlen($user)-1);  global $trustee_conf,$trustee_php,$trustee_a2n,$groups,$trustees;
1291                                  }  
1292                                  $perm_ascii=array_shift($arr);  // do we need to re-create compiled trustees?
1293                                  for ($i=0;$i<strlen($perm_ascii);$i++) {  if (! file_exists($trustee_conf)) {
1294                                          $ch=strtoupper($perm_ascii[$i]);          # $error="$trustee_conf doesn't exits";
1295                                          if (isset($trustee_a2n[$ch])) {          return 0;       # don't use trustees
1296                                                  $perm|=$trustee_a2n[$ch];  } elseif (file_exists($trustee_conf) && !is_readable($trustee_conf)) {
1297                                          } else {          $error="<tt>$trustee_conf</tt> exits, but is not readable";
1298                                                  $error.="trustee error in line '$l' [Unknown modifier '$ch']<br>\n";  } elseif (!is_writable(dirname($trustee_php))) {
1299                                          }          $error="<tt>".dirname($trustee_php)."</tt> must be writable by web server user";
1300                                  }  } elseif (file_exists($trustee_php) && !is_writable($trustee_php)) {
1301                                  if (isset($tr_arr[$path][$user])) {          $error="trustees cache file <tt>$trustee_php</tt> exists, but is not writable by web server";
1302                                          $tr_arr[$path][$user] |= $perm;  } elseif (@filemtime($trustee_conf) >= @filemtime($trustee_php)) {
1303                                  } else {          $fp_php=@fopen($trustee_php,"w");
1304                                          $tr_arr[$path][$user] = $perm;          fputs($fp_php,"<?php // don't edit by hand!\n");
1305                                  }  
1306                          }          $fp_conf=fopen($trustee_conf,"r");
1307                  }  
1308          }          $groups_arr = array();
1309            $perm_arr = array();
1310          fclose($fp_conf);  
1311            $tr_arr = array();
1312          // save trustees  
1313          $tr_out='$trustees = array (';          while (! feof($fp_conf)) {
1314          $sep1="";                  $l = trim(fgets($fp_conf,4096));
1315          while (list ($path, $tr) = each ($tr_arr)) {                  if (substr($l,0,1) == "+") {    // no comment
1316                  $tr_out.="$sep1\n\t'$path'=>array(";                          $arr=explode(":",$l);
1317                  $sep2="";                          $groups_arr[$arr[0]] = str_replace(" ","",$arr[1]) ;
1318                  while (list($user,$perm)=each($tr)) {                  } elseif (substr($l,0,1) != "#") {
1319                          $tr_out.="$sep2\n\t\t'$user'=>$perm";                          $arr=explode(":",$l);
1320                          $sep2=",";                          $path=array_shift($arr);
1321                  }                          if ($path == "") continue;
1322                  $tr_out.="\n\t)";                          $sep2="";
1323                  $sep1=",";                          while ($user=array_shift($arr)) {
1324          }                                  $perm=0;
1325          $tr_out.="\n);";                                  if (substr($user,0,1) == "+") {
1326                                            $perm|=trmask_group;
1327          // save groups                                          $user=substr($user,1,strlen($user)-1);
1328          $gr_out='$groups = array (';                                  }
1329          $sep="";                                  $perm_ascii=array_shift($arr);
1330          while (list ($group, $members) = each ($groups_arr)) {                                  for ($i=0;$i<strlen($perm_ascii);$i++) {
1331                  $gr_out.="$sep\n\t'";                                          $ch=strtoupper($perm_ascii[$i]);
1332                  $gr_out.=substr($group,1,strlen($group)-1);                                          if (isset($trustee_a2n[$ch])) {
1333                  $gr_out.="'=>array('".join("','",explode(",",$members))."')";                                                  $perm|=$trustee_a2n[$ch];
1334                  $sep=",";                                          } else {
1335          }                                                  $error.="trustee error in line '$l' [Unknown modifier '$ch']<br>\n";
1336          $gr_out.="\n);\n";                                          }
1337                                    }
1338          fputs($fp_php,$gr_out);                                  if (isset($tr_arr[$path][$user])) {
1339          fputs($fp_php,$tr_out);                                          $tr_arr[$path][$user] |= $perm;
1340          fputs($fp_php,"?>\n");                                  } else {
1341          fclose($fp_php);                                          $tr_arr[$path][$user] = $perm;
1342  }                                  }
1343                            }
1344  if (isset($error)) {                  }
1345          Error("Trustee error",$error,1);          }
1346  } else {  
1347          include_once("$trustee_php");          fclose($fp_conf);
1348  }  
1349            // save trustees
1350  return 1;          $tr_out='$trustees = array (';
1351            $sep1="";
1352  }//init_trustee          while (list ($path, $tr) = each ($tr_arr)) {
1353                    $tr_out.="$sep1\n\t'$path'=>array(";
1354  function in_group($user,$group) {                  $sep2="";
1355          global $groups;                  while (list($user,$perm)=each($tr)) {
1356          return in_array($user,$groups[$group]);                          $tr_out.="$sep2\n\t\t'$user'=>$perm";
1357  }                          $sep2=",";
1358                    }
1359  // helper function                  $tr_out.="\n\t)";
1360  function unroll_perm($u,$t,$perm,$one_level) {                  $sep1=",";
1361            }
1362          if ($t & trmask_one_level && !$one_level) return $perm;          $tr_out.="\n);";
1363    
1364          if ($t & trmask_deny) {          // save groups
1365                  if ($t & trmask_clear) {          $gr_out='$groups = array (';
1366                          $perm['deny'] &= ~$t;          $sep="";
1367                  } else {          while (list ($group, $members) = each ($groups_arr)) {
1368                          $perm['deny'] |= $t;                  $gr_out.="$sep\n\t'";
1369                  }                  $gr_out.=substr($group,1,strlen($group)-1);
1370          } elseif ($t & trmask_clear) {                  $gr_out.="'=>array('".join("','",explode(",",$members))."')";
1371                  $perm['allow'] &= ~$t;                  $sep=",";
1372          } else {          }
1373                  $perm['allow'] |= $t;          $gr_out.="\n);\n";
1374          }  
1375          return $perm;          fputs($fp_php,$gr_out);
1376  }// end of helper function          fputs($fp_php,$tr_out);
1377            fputs($fp_php,"?>\n");
1378  function check_trustee($user,$path) {          fclose($fp_php);
1379          global $trustees,$HAVE_TRUSTEE;  }
1380          $perm['allow'] = 0;  
1381          $perm['deny'] = 0;  if (isset($error)) {
1382            Error("Trustee error",$error,1);
1383          // do we use trustees?  } else {
1384          if (! $HAVE_TRUSTEE) return $perm;          include_once("$trustee_php");
1385    }
1386          if (! isset($trustees)) Error("Trustees not found","Can't find in-memory trustee structure <tt>\$trustees</tt>. Probably bug in code. Contact <tt>dpavlin@rot13.org</tt>",1);  
1387    return 1;
1388  global $debug;  
1389  $debug .= "<br>check_trustee $path ... ";  }//init_trustee
1390    
1391          $path_arr=explode("/",$path);  function in_group($user,$group) {
1392          $tmppath = "/";          global $groups;
1393          while (count($path_arr)) {          return in_array($user,$groups[$group]);
1394                  $tmppath.=array_shift($path_arr);  }
1395  $debug.= ">> $tmppath ";  
1396    // helper function
1397                  if (! isset($trustees[$tmppath])) continue;  function unroll_perm($u,$t,$perm,$one_level) {
1398                  $tr = $trustees[$tmppath];  
1399            if ($t & trmask_one_level && !$one_level) return $perm;
1400                  $one_level = (!count($path_arr));  
1401  $debug.=" O($one_level) ";          if ($t & trmask_deny) {
1402                    if ($t & trmask_clear) {
1403                  if (isset($tr)) {                          $perm['deny'] &= ~$t;
1404                          // first apply trustee for all                  } else {
1405                          if (isset($tr['*']) && $user!="anonymous") {                          $perm['deny'] |= $t;
1406                                  $perm = unroll_perm($user,$tr['*'],$perm, $one_level);                  }
1407                                  unset($tr['*']);          } elseif ($t & trmask_clear) {
1408                          }                  $perm['allow'] &= ~$t;
1409                          // then apply not and group policies          } else {
1410                          foreach ($tr as $g=>$t) {                  $perm['allow'] |= $t;
1411                                  if ($t & trmask_not && $g != $user) {          }
1412                                          $t = $t & ~trmask_not;          return $perm;
1413                                          $perm = unroll_perm($user,$t,$perm, $one_level);  }// end of helper function
1414                                          unset($tr[$g]);  
1415    function check_trustee($user,$path) {
1416                                  } elseif ($t & trmask_group && in_group($user,$g)) {          global $trustees,$HAVE_TRUSTEE;
1417                                          // resolv user          $perm['allow'] = 0;
1418                                          $t = $t & ~trmask_group;          $perm['deny'] = 0;
1419                                          $perm = unroll_perm($user,$t,$perm, $one_level);  
1420                                          unset($tr[$g]);          // do we use trustees?
1421                                  }          if (! $HAVE_TRUSTEE) return $perm;
1422                          }  
1423                          // then apply user policy          if (! isset($trustees)) Error("Trustees not found","Can't find in-memory trustee structure <tt>\$trustees</tt>. Probably bug in code. Contact <tt>dpavlin@rot13.org</tt>",1);
1424                          if (isset($tr[$user])) {  
1425                                  $perm = unroll_perm($user,$tr[$user],$perm,$one_level);  global $debug;
1426                                  unset($tr[$user]);  $debug .= "<br>check_trustee $path ... ";
1427                          }  
1428                  }          $path_arr=explode("/",$path);
1429  $debug.="d(".display_trustee($perm['deny']).") a(".display_trustee($perm['allow']).") ";          $tmppath="";
1430            while (count($path_arr)) {
1431          }                  $p = array_shift($path_arr);
1432  $debug.="<br>check_trustee: user: $user path: $path==$tmppath perm: ";  $debug.= "[$p] ";
1433  $debug.="d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).")<Br>\n";                  # add trailing slash
1434          return $perm;                  if (substr($tmppath,strlen($tmppath)-1,1) != "/") {
1435  }                          $tmppath.="/";
1436                    }
1437  // handy functions                  # append currnet dir to tmppath
1438                    if (isset($p)) {
1439  function check_perm($path,$trperm) {                          $tmppath.=$p;
1440          global $gblLogin,$HAVE_TRUSTEE;                  }
1441    $debug.= ">> $tmppath ";
1442          $path = str_replace("//","/",$path);  
1443                    if (! isset($trustees[$tmppath])) continue;
1444          global $debug;                  $tr = $trustees[$tmppath];
1445  $debug.="<br>check_perm: on <tt>$path</tt> for perm ".display_trustee($trperm)."<br>\n";  
1446                    $one_level = (!count($path_arr));
1447          $return = ! $HAVE_TRUSTEE;  $debug.=" O($one_level) ";
1448          if ($HAVE_TRUSTEE) {  
1449                  $perm = check_trustee($gblLogin,$path);                  if (isset($tr)) {
1450  $debug.=" d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).") perm to have: $trperm (".display_trustee($trperm).")";                          // first apply trustee for all
1451                  if ($perm['deny'] & $trperm) $return=0;                          if (isset($tr['*']) && $user!="anonymous") {
1452                  elseif (($perm['allow'] & $trperm) == $trperm) $return=1;                                  $perm = unroll_perm($user,$tr['*'],$perm, $one_level);
1453          }                                  unset($tr['*']);
1454  $debug.=" return: $return<br>\n";                          }
1455          return($return);                          // then apply not and group policies
1456  }                          foreach ($tr as $g=>$t) {
1457                                    if ($t & trmask_not && $g != $user) {
1458  //////////////////////////////////////////////////////////////////                                          $t = $t & ~trmask_not;
1459                                            $perm = unroll_perm($user,$t,$perm, $one_level);
1460  function readMime() {                                          unset($tr[$g]);
1461          global $mime_type;  
1462                                    } elseif ($t & trmask_group && in_group($user,$g)) {
1463          if (! isset($gblMimeTypes)) {                                          // resolv user
1464                  $gblMimeTypes = "/etc/mime.types";                                          $t = $t & ~trmask_group;
1465          }                                          $perm = unroll_perm($user,$t,$perm, $one_level);
1466                                            unset($tr[$g]);
1467          $mime = @fopen($gblMimeTypes,"r");                                  }
1468                            }
1469          if (! $mime) Error("Can't read MIME types","<tt>$gblMimeTypes</tt> file not found. You can setup other <tt>mime.types</tt> file using <tt>\$gblMimeTypes</tt> in <tt>$realm_config</tt>");                          // then apply user policy
1470                            if (isset($tr[$user])) {
1471          while($line = fgets($mime,80)) {                                  $perm = unroll_perm($user,$tr[$user],$perm,$one_level);
1472                  if (substr($line,0,1) == "#") continue; // skip comment                                  unset($tr[$user]);
1473                  $arr = preg_split("/[\s\t]+/",$line);                          }
1474                  $type = array_shift($arr);                  }
1475                  while ($ext = array_shift($arr)) {  $debug.="d(".display_trustee($perm['deny']).") a(".display_trustee($perm['allow']).") ";
1476                          $mime_type[$ext] = $type;  
1477                  }          }
1478          }  $debug.="<br>check_trustee: user: $user path: $path==$tmppath perm: ";
1479    $debug.="d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).")<Br>\n";
1480          fclose($mime);          return $perm;
1481  }  }
1482    
1483  //////////////////////////////////////////////////////////////////  // handy functions
1484    
1485  // check for invalid characters in filename and dirname (.. and /)  function check_perm($path,$trperm) {
1486            global $gblLogin,$HAVE_TRUSTEE;
1487  function check_dirname($file) {  
1488          if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in directory name <tt>$file</tt>",1);          $path = str_replace("//","/",$path);
1489  }  
1490            global $debug;
1491  function check_filename($file) {  $debug.="<br>check_perm: on <tt>$path</tt> for perm ".display_trustee($trperm)."<br>\n";
1492          if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in file name <tt>$file</tt>",1);  
1493          // remove deleted directory (for undelete to work)          $return = ! $HAVE_TRUSTEE;
1494          $file = str_replace(".del/","",$file);          if ($HAVE_TRUSTEE) {
1495          if (strstr($file,"/")) Error("Security violation","No slashes <tt>/</tt> allowed in file name <tt>$file</tt>",1);                  $perm = check_trustee($gblLogin,$path);
1496  }  $debug.=" d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).") perm to have: $trperm (".display_trustee($trperm).")";
1497                    if ($perm['deny'] & $trperm) $return=0;
1498  // bla/blo/../foo will return bla/foo                  elseif (($perm['allow'] & $trperm) == $trperm) $return=1;
1499  function remove_parent($path) {          }
1500          while (preg_match(",/[^/]+/\.\./,",$path)) {  $debug.=" return: $return<br>\n";
1501                  $path = preg_replace(",/[^/]+/\.\./,","",$path);          return($return);
1502          }  }
1503          if (substr($path,0,1) != "/") $path = "/".$path;  
1504          return $path;  //////////////////////////////////////////////////////////////////
1505  }  
1506    function readMime() {
1507  //////////////////////////////////////////////////////////////////          global $mime_type, $gblMimeTypes;
1508    
1509  // functions to move HTTP server variables to global namespace          if (! isset($gblMimeTypes)) {
1510  // [replacement for register_globals in php.ini]                  $gblMimeTypes = "/etc/mime.types";
1511            }
1512  function HTTP_GET_VAR($var) {  
1513          global $HTTP_GET_VARS, ${$var};          $mime = @fopen($gblMimeTypes,"r");
1514          if (isset($HTTP_GET_VARS[$var])) {  
1515                  $$var = stripSlashes($HTTP_GET_VARS[$var]);          if (! $mime) Error("Can't read MIME types","<tt>$gblMimeTypes</tt> file not found. You can setup other <tt>mime.types</tt> file using <tt>\$gblMimeTypes</tt> in <tt>$realm_config</tt>");
1516                  return $$var;  
1517          }          while($line = fgets($mime,80)) {
1518  }                  if (substr($line,0,1) == "#") continue; // skip comment
1519                    $arr = preg_split("/[\s\t]+/",$line);
1520  function HTTP_POST_VAR($var) {                  $type = array_shift($arr);
1521          global $HTTP_POST_VARS, ${$var};                  while ($ext = array_shift($arr)) {
1522          if (isset($HTTP_POST_VARS[$var])) {                          $mime_type[$ext] = $type;
1523                  $$var = $HTTP_POST_VARS[$var];                  }
1524                  return $$var;          }
1525          }  
1526  }          fclose($mime);
1527    }
1528  function HTTP_SERVER_VAR($var) {  
1529          global $HTTP_SERVER_VARS, ${$var};  //////////////////////////////////////////////////////////////////
1530          if (isset($HTTP_SERVER_VARS[$var])) {  
1531                  $$var = $HTTP_SERVER_VARS[$var];  // check for invalid characters in filename and dirname (.. and /)
1532                  return $$var;  
1533          }  function check_dirname($file) {
1534  }          if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in directory name <tt>$file</tt>",1);
1535    }
1536  //////////////////////////////////////////////////////////////////  
1537    function check_filename($file) {
1538  function Warn($text) {          if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in file name <tt>$file</tt>",1);
1539  }          // remove deleted directory (for undelete to work)
1540            $file = str_replace(".del/","",$file);
1541  //////////////////////////////////////////////////////////////////          if (strstr($file,"/")) Error("Security violation","No slashes <tt>/</tt> allowed in file name <tt>$file</tt>",1);
1542  // MAIN PROGRAM  }
1543    
1544          $gblFilePerms = 0640 ;         // default for new files  // bla/blo/../foo will return bla/foo
1545          $gblDirPerms  = 0750 ;          // default for new dirs  function remove_parent($path) {
1546            while (preg_match(",/[^/]+/\.\./,",$path)) {
1547          if (isset($HTTP_GET_VARS["STYLE"]) && $HTTP_GET_VARS["STYLE"] == "get") {                  $path = preg_replace(",/[^/]+/\.\./,","",$path);
1548                  include("$html/docman.css");          }
1549                  exit;          if (substr($path,0,1) != "/") $path = "/".$path;
1550          }          return $path;
1551    }
1552          // location of master docman configuration file  
1553          $docman_conf = "/etc/docman.conf";  //////////////////////////////////////////////////////////////////
1554          if (! file_exists($docman_conf)) {  
1555                  $error = "Can't find master configuration file <tt>$docman_conf</tt>. See <tt>docman2/doc/upgrade.html#docman_conf</tt> for more informations";  // functions to move HTTP server variables to global namespace
1556                    // [replacement for register_globals in php.ini]
1557                  error_log("docman: $error");  
1558                  Error("docman not installed completly",$error);  function HTTP_GET_VAR($var) {
1559          }          global $HTTP_GET_VARS, ${$var};
1560          include($docman_conf);          if (isset($HTTP_GET_VARS[$var])) {
1561                    $$var = stripSlashes($HTTP_GET_VARS[$var]);
1562          if (! isset($fsRealmDir)) {                  return $$var;
1563                  $fsRealmDir = "$gblIncDir/realm";          }
1564          }  }
1565    
1566          // try to add dir to script name to realm var  function HTTP_POST_VAR($var) {
1567          if (is_dir("$fsRealmDir/$realm/".dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]))) {          global $HTTP_POST_VARS, ${$var};
1568                  $realm .= dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]);          if (isset($HTTP_POST_VARS[$var])) {
1569                  $realm_sep = "/";                  $$var = $HTTP_POST_VARS[$var];
1570          } else {                  return $$var;
1571                  $realm_sep = ".";          }
1572          }  }
1573    
1574          $realm_config = $fsRealmDir."/".$realm.$realm_sep."conf";  function HTTP_SERVER_VAR($var) {
1575            global $HTTP_SERVER_VARS, ${$var};
1576          // read user-defined configuration          if (isset($HTTP_SERVER_VARS[$var])) {
1577          if (file_exists($realm_config)) {                  $$var = $HTTP_SERVER_VARS[$var];
1578                  include($realm_config);                  return $$var;
1579          } else {          }
1580                  Error("Configuration error","Can't find configuration file at <tt>$realm_config</tt> !");  }
1581          }  
1582    //////////////////////////////////////////////////////////////////
1583          if (! isset($gblRepositoryDir)) Error("Configuration error","<tt>\$gblRepositoryDir</tt> is not setuped in realm configuration file <tt>$realm_config</tt>");  
1584    function Warn($text) {
1585          // where do we get users from?  }
1586          if (file_exists("$gblIncDir/htusers/$gblUsers.php")) {  
1587                  include("$gblIncDir/htusers/$gblUsers.php");  //////////////////////////////////////////////////////////////////
1588          } else {  
1589                  Error("Configuration error","Can't find user handling module at <tt>$gblIncDir/htusers/$gblUsers.php</tt> ! Please fix <tt>$realm_config</tt>");  function isBlank($file,$what = "filename") {
1590          }          if (trim($file) == "") return "<i>whitespace $what</i>";
1591            if ($file == "") return "<i>no $what</i>";
1592          // take additional login vars          return $file;
1593          HTTP_GET_VAR("relogin");  }
1594          HTTP_GET_VAR("force_login");  
1595    //////////////////////////////////////////////////////////////////
1596          // if no password, or empty password logout  // MAIN PROGRAM
1597          if (  
1598                  isset($gblLogin) && (          $gblFilePerms = 0640 ;         // default for new files
1599                          !isset($relogin) || (          $gblDirPerms  = 0750 ;          // default for new dirs
1600                                  isset($relogin) && $relogin != md5($gblLogin.$gblPasswd)  
1601                          )          if (isset($HTTP_GET_VARS["STYLE"]) && $HTTP_GET_VARS["STYLE"] == "get") {
1602                  ) && (                  include("$html/docman.css");
1603                          $gblPasswd == "" || !isset($gblPasswd)                  exit;
1604                  ) && !isset($force_login) && $gblLogin != "anonymous"          }
1605             ) {  
1606                  StartHTML("Logout completed","Your login credentials has been erased") ;          // set fsRealmDir
1607                  EndHTML() ;          if (! isset($fsRealmDir)) {
1608                  exit ;                  $fsRealmDir = "$gblIncDir/realm";
1609          }          }
1610    
1611          if (!is_dir($gblRepositoryDir)) Error("Repository dir not found","Can't find repository directory <tt>$gblRepositoryDir</tt>. Please fix that in <tt>$realm_config</tt> variable <tt>\$gblRepositoryDir</tt>.",1);          // try to add dir to script name to realm var
1612            if (is_dir("$fsRealmDir/$realm/".dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]))) {
1613          // trustee (ACL) file configuration                  $realm .= dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]);
1614          $trustee_conf="$fsRealmDir/$realm".$realm_sep."trustee";                  $realm_sep = "/";
1615          // compiled version of trustee file          } else {
1616          $trustee_php="$gblRepositoryDir/.trustee.php";                  $realm_sep = ".";
1617          // get ACL informations          }
1618          $HAVE_TRUSTEE = init_trustee();  
1619            $realm_config = $fsRealmDir."/".$realm.$realm_sep."conf";
1620          if (strtolower($gblLogin) == "anonymous" || !isset($gblLogin)) {  
1621                  $perm = check_trustee("anonymous","/");          // read user-defined configuration
1622                  // browsing must be explicitly allowed for root directory          if (file_exists($realm_config)) {
1623                  // of repository for anonymous user to work!                  include($realm_config);
1624                  if ($perm['allow'] & trperm_b) {          } else {
1625                          $gblLogin = $gblPasswd = "anonymous";                  Error("Configuration error","Can't find configuration file at <tt>$realm_config</tt> !");
1626                          $secHash = md5($gblLogin.$gblPasswd);          }
1627                          $gblUserName = "Anonymous user";  
1628                  }          if (! isset($gblRepositoryDir)) Error("Configuration error","<tt>\$gblRepositoryDir</tt> is not setuped in realm configuration file <tt>$realm_config</tt>");
1629          }  
1630            // where do we get users from?
1631          // authentication failure          if (file_exists("$gblIncDir/htusers/$gblUsers.php")) {
1632          if ( md5($gblLogin.$gblPasswd) != $secHash ||                  include("$gblIncDir/htusers/$gblUsers.php");
1633                  isset($relogin) && $secHash == $relogin) {          } else {
1634                  header("WWW-authenticate: basic realm=\"$realm\"") ;                  Error("Configuration error","Can't find user handling module at <tt>$gblIncDir/htusers/$gblUsers.php</tt> ! Please fix <tt>$realm_config</tt>");
1635                  header("HTTP/1.0 401 Unauthorized") ;          }
1636                  Error("401 Unauthorized","No trespassing !",0,1);  
1637          }          // take additional login vars
1638            HTTP_GET_VAR("relogin");
1639            HTTP_GET_VAR("force_login");
1640          // read mime.types  
1641          readMime();          // if no password, or empty password logout
1642            if (
1643          if (! isset($gblPermNote)) {                  isset($gblLogin) && (
1644                  $gblPermNote = trperm_r;                          !isset($relogin) || (
1645          }                                  isset($relogin) && $relogin != md5($gblLogin.$gblPasswd)
1646                            )
1647          HTTP_POST_VAR("FN");                  ) && (
1648                            $gblPasswd == "" || !isset($gblPasswd)
1649          if ($HTTP_SERVER_VARS["REQUEST_METHOD"] == "POST") {                  ) && !isset($force_login) && $gblLogin != "anonymous"
1650                  // take variables from server             ) {
1651                  if (HTTP_POST_VAR("FN"))                  StartHTML("Logout completed","Your login credentials has been erased") ;
1652                          check_filename($FN);                  EndHTML() ;
1653                  if (HTTP_POST_VAR("DIR")) {                  exit ;
1654                          check_dirname($DIR);          }
1655                          $relDir = $DIR;  
1656                  } else {          if (!is_dir($gblRepositoryDir)) Error("Repository dir not found","Can't find repository directory <tt>$gblRepositoryDir</tt>. Please fix that in <tt>$realm_config</tt> variable <tt>\$gblRepositoryDir</tt>.",1);
1657                          trigger_error("Can't get DIR",E_USER_WARNING);  
1658                          $relDir = "/";          // trustee (ACL) file configuration
1659                  }          $trustee_conf="$fsRealmDir/$realm".$realm_sep."trustee";
1660                  if (HTTP_POST_VAR("RELPATH")) check_dirname($RELPATH);          // compiled version of trustee file
1661                  HTTP_POST_VAR("T");          $trustee_php="$gblRepositoryDir/.trustee.php";
1662                  HTTP_POST_VAR("CONFIRM");          // get ACL informations
1663          } else {          $HAVE_TRUSTEE = init_trustee();
1664                  // get  
1665                  HTTP_GET_VAR("A");          if (strtolower($gblLogin) == "anonymous" || !isset($gblLogin)) {
1666                  if (HTTP_GET_VAR("D")) {                  $perm = check_trustee("anonymous","/");
1667                          check_dirname($D);                  // browsing must be explicitly allowed for root directory
1668                          $D=urldecode($D);                  // of repository for anonymous user to work!
1669                          $relDir = $D;                  if ($perm['allow'] & trperm_b) {
1670                  } else {                          $gblLogin = $gblPasswd = "anonymous";
1671                          //trigger_error("Can't get D",E_USER_WARNING);                          $secHash = md5($gblLogin.$gblPasswd);
1672                          $relDir = "/";                          $gblUserName = "Anonymous user";
1673                  }                  }
1674                  if (HTTP_GET_VAR("F")) check_filename($F);          }
1675          }  
1676            // authentication failure
1677          $relScriptDir = dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]) ;              if ( md5($gblLogin.$gblPasswd) != $secHash ||
1678          // i.e. /docman                  isset($relogin) && $secHash == $relogin) {
1679                    header("WWW-authenticate: basic realm=\"$realm\"") ;
1680          // start on server root                  header("HTTP/1.0 401 Unauthorized") ;
1681          $gblFsRoot = $gblRepositoryDir;                  Error("401 Unauthorized","No trespassing !",0,1);
1682          // i.e. /home/httpd/repository          }
1683    
1684          $fsDir = $gblFsRoot . $relDir ; // current directory  
1685          if ( !is_dir($fsDir) ) Error("Dir not found","Can't find <tt>$relDir</tt> which points to <tt>$fsDir</tt>",1) ;          // read mime.types
1686            readMime();
1687          if ($relDir == "") $relDir="/";  
1688            if (! isset($gblPermNote)) {
1689          if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") {                  $gblPermNote = trperm_r;
1690                  $webRoot  = "https://";          }
1691          } else {  
1692                  $webRoot  = "http://";          HTTP_POST_VAR("FN");
1693          }  
1694          $webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir;          if ($HTTP_SERVER_VARS["REQUEST_METHOD"] == "POST") {
1695                    // take variables from server
1696          if (HTTP_POST_VAR("POSTACTION")) switch ($POSTACTION) {                  if (HTTP_POST_VAR("FN"))
1697          case "UPLOAD" :                          check_filename($FN);
1698                  $FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]);                  if (HTTP_POST_VAR("DIR")) {
1699                  $FN=stripSlashes($HTTP_POST_FILES["FN"]["name"]);                          check_dirname($DIR);
1700                  if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;                          $relDir = $DIR;
1701                    } else {
1702                  $source = $FN_name ;                          trigger_error("Can't get DIR",E_USER_WARNING);
1703                  if (! file_exists($source)) {                          $relDir = "/";
1704                          Error("You must select file with browse to upload it!");                  }
1705                  }                  if (HTTP_POST_VAR("RELPATH")) check_dirname($RELPATH);
1706                    HTTP_POST_VAR("T");
1707                  if (HTTP_POST_VAR("FILENAME")) check_filename($FILENAME);                  HTTP_POST_VAR("CONFIRM");
1708            } else {
1709                  if (! isset($FILENAME)) {       // from update file                  // get
1710                          $target = "$fsDir/".basename($FN);                  HTTP_GET_VAR("A");
1711                  } else {                  if (HTTP_GET_VAR("D")) {
1712                          $target = "$fsDir/$FILENAME";                          check_dirname($D);
1713                  }                          $D=urldecode($D);
1714                            $relDir = $D;
1715                  if (! check_perm("$relDir/".basename($target), trperm_w))                  } else {
1716                          Error("Access denied","User <tt>$gblLogin</tt> tried to upload <tt>$relDir/".basename($target)."</tt> without valid trustee.",1);                          //trigger_error("Can't get D",E_USER_WARNING);
1717                            $relDir = "/";
1718                  // backup old files first                  }
1719                  $dir=dirname($target);                  if (HTTP_GET_VAR("F")) check_filename($F);
1720                  if (! file_exists($dir."/.bak")) {          }
1721                          mkdir($dir."/.bak",0700);  
1722                  }          $relScriptDir = dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]) ;    
1723                  if (! file_exists($dir."/.bak/$gblNumBackups")) {          // i.e. /docman
1724                          mkdir($dir."/.bak/$gblNumBackups",0700);  
1725                  }          // start on server root
1726                  $file=basename($target);          $gblFsRoot = $gblRepositoryDir;
1727                  for($i=$gblNumBackups-1;$i>0;$i--) {          // i.e. /home/httpd/repository
1728                          MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/");  
1729                  }          $fsDir = $gblFsRoot . $relDir ; // current directory
1730                  MoveTo($target,$dir."/.bak/1/");          if ( !is_dir($fsDir) ) Error("Dir not found","Can't find <tt>$relDir</tt> which points to <tt>$fsDir</tt>",1) ;
1731    
1732                  copy($source,$target) ;          if ($relDir == "") $relDir="/";
1733                  chmod($target,$gblFilePerms) ;  
1734                  clearstatcache() ;          if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") {
1735                  if (isset($FILENAME)) {                  $webRoot  = "https://";
1736                          LogIt($target,"check-in",trperm_r | trperm_w);          } else {
1737                          Unlock($target);                  $webRoot  = "http://";
1738                  } else {          }
1739                          LogIt($target,"uploaded",trperm_r | trperm_w);          $webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir;
1740                  }  
1741                  break ;          if (HTTP_POST_VAR("POSTACTION")) switch ($POSTACTION) {
1742            case "UPLOAD" :
1743          case "SAVE" :                  $FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]);
1744                  $path = $gblFsRoot . $RELPATH ;                  $FN=stripSlashes($HTTP_POST_FILES["FN"]["name"]);
1745                  $path=stripSlashes($path);                  if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;
1746    
1747                  if (! check_perm("$RELPATH", trperm_w))                  $source = $FN_name ;
1748                          Error("Access denied","User <tt>$gblLogin</tt> tried to save <tt>$RELPATH</tt> without valid trustee.",1);                  if (! file_exists($source)) {
1749                            Error("You must select file with browse to upload it!","If file is too big, you might need to modify php configuration options <tt>post_max_size</tt> and <tt>upload_max_filesize</tt>",1);
1750                  $writable = is_writeable($path) ;                  }
1751                  $legaldir = is_writeable(dirname($path)) ;  
1752                  $exists   = (file_exists($path)) ? 1 : 0 ;                  if (HTTP_POST_VAR("FILENAME")) check_filename($FILENAME);
1753                  // FIX: more verbose error message  
1754                  if (!($writable || (!$exists && $legaldir)))                  if (! isset($FILENAME)) {       // from update file
1755                          Error("Write denied",$RELPATH) ;                          $target = "$fsDir/".basename($FN);
1756                  $fh = fopen($path, "w") ;                  } else {
1757                  HTTP_POST_VAR("FILEDATA");                          $target = "$fsDir/$FILENAME";
1758                  fwrite($fh,$FILEDATA) ;                  }
1759                  fclose($fh) ;  
1760                  clearstatcache() ;                  if (! check_perm("$relDir/".basename($target), trperm_w))
1761                  LogIt($path,"saved changes",trperm_r);                          Error("Access denied","User <tt>$gblLogin</tt> tried to upload <tt>$relDir/".basename($target)."</tt> without valid trustee.",1);
1762                  break ;  
1763                    // backup old files first
1764          case "CREATE" :                  $dir=dirname($target);
1765                  // we know $fsDir exists                  if (! file_exists($dir."/.bak")) {
1766                  if (! check_perm($relDir, trperm_w))                          mkdir($dir."/.bak",0700);
1767                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");                  }
1768                  if ($T == "D") $type = "directory";                  if (! file_exists($dir."/.bak/$gblNumBackups")) {
1769                  else $type ="file";                          mkdir($dir."/.bak/$gblNumBackups",0700);
1770                  if ($FN == "") Error("Can't create $type","You must enter name of $type to create it.");                  }
1771                  if (!is_writeable($fsDir)) Error("Write denied","User <tt>$gblLogin</tt> has trustee to write in <tt>$relDir</tt> but permissions on <tt>$fsDir</tt> are wrong!", 1) ;                  $file=basename($target);
1772                  $path = "$fsDir/$FN";           // file or dir to create                  for($i=$gblNumBackups-1;$i>0;$i--) {
1773                  $relPath = "$relDir/$FN";                          MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/");
1774                    }
1775                  if (file_exists($path))                  MoveTo($target,$dir."/.bak/1/");
1776                          Error("Can't create $type","Object <tt>$relPath</tt> allready exists");  
1777                    copy($source,$target) ;
1778                  switch ( $T ) {                  chmod($target,$gblFilePerms) ;
1779                  case "D" :  // create a directory                  clearstatcache() ;
1780                          if ( ! @mkdir($path,$gblDirPerms) )                  if (isset($FILENAME)) {
1781                                  Error("Mkdir failed",$relPath) ; // eg. if it exists                          LogIt($target,"check-in",trperm_r | trperm_w);
1782                          else                          Unlock($target);
1783                                  LogIt($path."/","dir created",trperm_w);                  } else {
1784                          clearstatcache() ;                          LogIt($target,"uploaded",trperm_r | trperm_w);
1785                          break ;                  }
1786                  case "F" :  // create a new file                  break ;
1787  // this functionality is doubled in DetailView().  
1788  // better keep it here altogether          case "SAVE" :
1789  // chmod perms to $gblFilePerms                  $path = $gblFsRoot . $RELPATH ;
1790                          if ( file_exists($path) && !is_writeable($path) )                  $path=stripSlashes($path);
1791                                  Error("File not writable", "User <tt>$gblLogin</tt> has trustee to write in <tt>$relPath</tt> but permissions on <tt>$path</tt> are wrong!", 1) ;  
1792                          $fh = fopen($path, "w+") ;                  if (! check_perm("$RELPATH", trperm_w))
1793                          if ($fh) {                          Error("Access denied","User <tt>$gblLogin</tt> tried to save <tt>$RELPATH</tt> without valid trustee.",1);
1794                                  fputs($fh,"\n");  
1795                                  fclose($fh) ;                  $writable = is_writeable($path) ;
1796                                  LogIt($path,"file created",trperm_r | trperm_w);                  $legaldir = is_writeable(dirname($path)) ;
1797                          } else {                  $exists   = (file_exists($path)) ? 1 : 0 ;
1798                                  Error("Creation of file $relPath failed", "User <tt>$gblLogin</tt> has trustee to write in <tt>$relPath</tt> but creation of <tt>$path</tt> failed!", 1) ;                  // FIX: more verbose error message
1799                          }                  if (!($writable || (!$exists && $legaldir)))
1800                          $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;                          Error("Write denied",$RELPATH) ;
1801                          header("Location: " . $tstr) ;                  $fh = fopen($path, "w") ;
1802                          exit ;                  HTTP_POST_VAR("FILEDATA");
1803                  }                  fwrite($fh,$FILEDATA) ;
1804                  break ;                  fclose($fh) ;
1805                    clearstatcache() ;
1806          case "DELETE" :                    LogIt($path,"saved changes",trperm_r);
1807                  if ( $CONFIRM != "on" ) break;                  break ;
1808    
1809                  if ( isset($FN) && $FN != "") {          case "CREATE" :
1810                          $path=$fsDir."/".$FN;                  // we know $fsDir exists
1811                          $what = "file";                  if (! check_perm($relDir, trperm_w))
1812                  } elseif (isset($DIR)) {                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");
1813                          $path=$gblFsRoot."/".$DIR;                  if ($T == "D") $type = "directory";
1814                          $what = "directory";                  else $type ="file";
1815                  } else  {                  if ($FN == "") Error("Can't create $type","You must enter name of $type to create it.");
1816                          Error("Can't delete object","Can't find filename <tt>\$FN</tt> or dirname in <tt>\$DIR</tt>",1);                  if (!is_writeable($fsDir)) Error("Write denied","User <tt>$gblLogin</tt> has trustee to write in <tt>$relDir</tt> but permissions on <tt>$fsDir</tt> are wrong!", 1) ;
1817                  }                  $path = "$fsDir/$FN";           // file or dir to create
1818                    $relPath = "$relDir/$FN";
1819                  if (! check_perm("$relDir/$FN", trperm_w))  
1820                          Error("Access denied","User <tt>$gblLogin</tt> tried to erase $what <tt>$relDir/$FN</tt> without valid trustee.",1);                  if (file_exists($path))
1821                            Error("Can't create $type","Object <tt>$relPath</tt> allready exists");
1822                  $tstr  = "Attempt to delete non-existing object or insufficient privileges: " ;  
1823                    switch ( $T ) {
1824                  $dir=dirname($path);                  case "D" :  // create a directory
1825                  $file=basename($path);                          if ( ! @mkdir($path,$gblDirPerms) )
1826                                    Error("Mkdir failed",$relPath) ; // eg. if it exists
1827                  if (! file_exists("$dir/.del")) {                          else
1828                          mkdir("$dir/.del",0700);                                  LogIt($path."/","dir created",trperm_w);
1829                  }                          clearstatcache() ;
1830                            break ;
1831                  if ( ! @rename($path,"$dir/.del/$file") ) {                  case "F" :  // create a new file
1832                          LogIt($path,"$what delete failed");  // this functionality is doubled in DetailView().
1833                          Error("Can't delete $what",$tstr."<tt>".$relDir."/".$FN."</tt>") ;  // better keep it here altogether
1834                  } else {  // chmod perms to $gblFilePerms
1835                          LogIt($path,"$what deleted",trperm_w);                          if ( file_exists($path) && !is_writeable($path) )
1836                          MoveTo("$dir/.log/$file","$dir/.del/.log/");                                  Error("File not writable", "User <tt>$gblLogin</tt> has trustee to write in <tt>$relPath</tt> but permissions on <tt>$path</tt> are wrong!", 1) ;
1837                          MoveTo("$dir/.note/$file","$dir/.del/.note/");                          $fh = fopen($path, "w+") ;
1838                          MoveTo("$dir/.lock/$file","$dir/.del/.lock/");                          if ($fh) {
1839                  }                                  fputs($fh,"\n");
1840                  break ;                                  fclose($fh) ;
1841                                    LogIt($path,"file created",trperm_r | trperm_w);
1842          case "UNDELETE" :                            } else {
1843                  if ( $CONFIRM != "on" ) break ;                                  Error("Creation of file $relPath failed", "User <tt>$gblLogin</tt> has trustee to write in <tt>$relPath</tt> but creation of <tt>$path</tt> failed!", 1) ;
1844                            }
1845                  if (substr($FN,0,4) != ".del") break ;                          $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;
1846                  $file=substr($FN,4,strlen($FN)-4);                          header("Location: " . $tstr) ;
1847                            exit ;
1848                  if (! check_perm("$relDir/$file", trperm_w))                  }
1849                          Error("Access denied","User <tt>$gblLogin</tt> tried to undelete <tt>$relDir/$file</tt> without valid trustee.",1);                  break ;
1850    
1851                  LogIt("$fsDir/.del/$file","undeleted",trperm_w);          case "DELETE" :  
1852                  MoveTo("$fsDir/.del/$file","$fsDir/");                  if ( $CONFIRM != "on" ) break;
1853                  MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/");  
1854                  MoveTo("$fsDir/.del/.note/$file","$fsDir/.note/");                  if ( isset($FN) && $FN != "") {
1855                  MoveTo("$fsDir/.del/.lock/$file","$fsDir/.lock/");                          $path=$fsDir."/".$FN;
1856                            $what = "file";
1857                  break ;                  } elseif (isset($DIR)) {
1858                            $path=$gblFsRoot."/".$DIR;
1859          case "RENAME" :                            $what = "directory";
1860                  if ( $CONFIRM != "on" ) break ;                  } else  {
1861                            Error("Can't delete object","Can't find filename <tt>\$FN</tt> or dirname in <tt>\$DIR</tt>",1);
1862                  if (HTTP_POST_VAR("NEWNAME")) {                  }
1863                          $dest = remove_parent($relDir.$NEWNAME);  
1864                          if (! check_perm($relDir.$FN, trperm_w) ||                  if (! check_perm("$relDir/$FN", trperm_w))
1865                              ! check_perm($dest, trperm_w) )                          Error("Access denied","User <tt>$gblLogin</tt> tried to erase $what <tt>$relDir/$FN</tt> without valid trustee.",1);
1866                                  Error("Access denied","User <tt>$gblLogin</tt> tried to rename <tt>$relDir$FN</tt> to <tt>$dest</tt> without valid trustee.",1);  
1867                  } else {                  $tstr  = "Attempt to delete non-existing object or insufficient privileges: " ;
1868                          Error("Rename error","Can't find new name in var <tt>\$NEWNAME</tt>",1);  
1869                  }                  $dir=dirname($path);
1870                  LogIt("$fsDir/$FN","renamed $FN to $NEWNAME",trperm_r);                  $file=basename($path);
1871                  safe_rename($fsDir,$FN,$NEWNAME);  
1872                  break ;                  if (! file_exists("$dir/.del")) {
1873                            mkdir("$dir/.del",0700);
1874          case "NOTE" :                  }
1875                  if (! HTTP_POST_VAR("NOTE"))  
1876                          Error("Can't add note to object","Can't find var <tt>\$NOTE</tt>",1);                  if ( ! @rename($path,"$dir/.del/$file") ) {
1877                  if (! check_perm("$relDir/$FN", trperm_w))                          LogIt($path,"$what delete failed");
1878                          Error("Access denied","User <tt>$gblLogin</tt> tried to add note to <tt>$relDir/$FN</tt> without valid trustee.",1);                          Error("Can't delete $what",$tstr."<tt>".$relDir."/".$FN."</tt>") ;
1879                    } else {
1880                  WriteNote("$fsDir/$FN",$NOTE);                          LogIt($path,"$what deleted",trperm_w);
1881                  break ;                          MoveTo("$dir/.log/$file","$dir/.del/.log/");
1882                            MoveTo("$dir/.note/$file","$dir/.del/.note/");
1883          case "UNLOCK" :                            MoveTo("$dir/.lock/$file","$dir/.del/.lock/");
1884                  if ( $CONFIRM != "on" ) break ;                  }
1885                  if (! check_perm("$relDir/$FN", trperm_w))                  break ;
1886                          Error("Access denied","User <tt>$gblLogin</tt> tried to unlock <tt>$relDir/$FN</tt> without valid trustee.",1);  
1887                  Unlock("$fsDir/$FN");          case "UNDELETE" :  
1888                  break ;                  if ( $CONFIRM != "on" ) break ;
1889    
1890          default :                  if (substr($FN,0,4) != ".del") break ;
1891                  // user hit "CANCEL" or undefined action                  $file=substr($FN,4,strlen($FN)-4);
1892          }  
1893                    if (! check_perm("$relDir/$file", trperm_w))
1894          // common to all POSTs : redirect to directory view ($relDir)                          Error("Access denied","User <tt>$gblLogin</tt> tried to undelete <tt>$relDir/$file</tt> without valid trustee.",1);
1895          if (isset($POSTACTION)) {  
1896                  $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?D=".urlencode($relDir);                  LogIt("$fsDir/.del/$file","undeleted",trperm_w);
1897                  header("Location: ".$tstr) ;                    MoveTo("$fsDir/.del/$file","$fsDir/");
1898                  exit ;                  MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/");
1899          }                  MoveTo("$fsDir/.del/.note/$file","$fsDir/.note/");
1900                    MoveTo("$fsDir/.del/.lock/$file","$fsDir/.lock/");
1901          // check for mode.. navigate, code display, upload, or detail?  
1902          // $A=U : upload to path given in $D                  break ;
1903          // $A=E : display detail of file $D/$F and edit  
1904          // $A=C : display code in file $D/$F          case "RENAME" :  
1905          // $A=Co : checkout file $D/$F                  if ( $CONFIRM != "on" ) break ;
1906          // $A=Ci : checkin file $D/$F  
1907          // $A=V : view file (do nothing except log)                  if (HTTP_POST_VAR("NEWNAME")) {
1908          // $A=I : include file .$F.php from [$gblIncDir|realm]/include_php                          $dest = remove_parent($relDir.$NEWNAME);
1909          // default : display directory $D                          if (! check_perm($relDir.$FN, trperm_w) ||
1910                                ! check_perm($dest, trperm_w) )
1911          if (isset($A)) switch ($A) {                                  Error("Access denied","User <tt>$gblLogin</tt> tried to rename <tt>$relDir$FN</tt> to <tt>$dest</tt> without valid trustee.",1);
1912          case "U" :                  } else {
1913                  // upload to $relDir                          Error("Rename error","Can't find new name in var <tt>\$NEWNAME</tt>",1);
1914                  if (! check_perm($relDir, trperm_w))                  }
1915                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");                  LogIt("$fsDir/$FN","renamed $FN to $NEWNAME",trperm_r);
1916                  if (!is_writeable($gblFsRoot . $relDir))                  safe_rename($fsDir,$FN,$NEWNAME);
1917                          Error("Write access denied","User <tt>$gblLogin</tt> has permission on <tt>$relDir</tt>, but directory is not writable",1);                  break ;
1918                  $text  = "Use this page to upload a single " ;  
1919                  $text .= "file to <B>$realm</B>." ;          case "NOTE" :
1920                  StartHTML("(Upload Page)", $text) ;                  if (! HTTP_POST_VAR("NOTE"))
1921                  UploadPage($gblFsRoot, $relDir) ;                          Error("Can't add note to object","Can't find var <tt>\$NOTE</tt>",1);
1922                  EndHTML() ;                  if (! check_perm("$relDir/$FN", trperm_w))
1923                  exit ;                          Error("Access denied","User <tt>$gblLogin</tt> tried to add note to <tt>$relDir/$FN</tt> without valid trustee.",1);
1924          case "E" :  
1925                  // detail of $relDir/$F                  WriteNote("$fsDir/$FN",$NOTE);
1926                  if (is_file("$gblFsRoot/$relDir/$F") || is_dir("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ;                  break ;
1927                  exit ;  
1928          case "C" :          case "UNLOCK" :  
1929                  $F=stripSlashes($F);                  if ( $CONFIRM != "on" ) break ;
1930                  // listing of $relDir/$F                  if (! check_perm("$relDir/$FN", trperm_w))
1931                  DisplayCode($gblFsRoot, $relDir, $F) ;                          Error("Access denied","User <tt>$gblLogin</tt> tried to unlock <tt>$relDir/$FN</tt> without valid trustee.",1);
1932                  exit ;                  Unlock("$fsDir/$FN");
1933          case "Co" :                  break ;
1934                  // checkout  
1935                  Lock("$gblFsRoot/$relDir/$F");          default :
1936                  Download("$gblFsRoot/$relDir/$F",1);                  // user hit "CANCEL" or undefined action
1937                  exit;          }
1938          case "Ci" :  
1939                  $F=stripSlashes($F);          // common to all POSTs : redirect to directory view ($relDir)
1940                  // upload && update to $relDir          if (isset($POSTACTION)) {
1941                  if (!is_writeable($gblFsRoot . $relDir))                  $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?D=".urlencode($relDir);
1942                          Error("Write access denied",$relDir) ;                  header("Location: ".$tstr) ;  
1943                  $text  = "Use this page to update a single " ;                  exit ;
1944                  $text .= "file to <B>$realm</B>." ;          }
1945                  StartHTML("(Update file Page)", $text) ;  
1946                  UploadPage($gblFsRoot, $relDir, $F) ;          // check for mode.. navigate, code display, upload, or detail?
1947                  EndHTML() ;          // $A=U : upload to path given in $D
1948                  exit ;          // $A=E : display detail of file $D/$F and edit
1949          case "V" :          // $A=C : display code in file $D/$F
1950                  // view          // $A=Co : checkout file $D/$F
1951                  LogIt("$gblFsRoot/$relDir/$F","viewed");          // $A=Ci : checkin file $D/$F
1952                  Download("$gblFsRoot/$relDir/$F",$gblForceDownload);          // $A=V : view file (do nothing except log)
1953                  exit;          // $A=I : include file .$F.php from [$gblIncDir|realm]/include_php
1954          case "Ch" :          // default : display directory $D
1955                  StartHTML("(File changes)","All changes chronologicaly...");  
1956                  DisplayChangeLog(0);    // all          if (isset($A)) switch ($A) {
1957                  EndHTML() ;          case "U" :
1958                  exit;                  // upload to $relDir
1959          case "Ch1" :                  if (! check_perm($relDir, trperm_w))
1960                  StartHTML("(File changes)","Changes to files in last day...");                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");
1961                  DisplayChangeLog(1);                  if (!is_writeable($gblFsRoot . $relDir))
1962                  EndHTML() ;                          Error("Write access denied","User <tt>$gblLogin</tt> has permission on <tt>$relDir</tt>, but directory is not writable",1);
1963                  exit;                  $text  = "Use this page to upload a single " ;
1964          case "I" :                  $text .= "file to <B>$realm</B>." ;
1965                  if (! isset($F) || $F == "")                  StartHTML("(Upload Page)", $text) ;
1966                          Error("Can't find file to include","Your request didn't specify file to include which should be in variable <tt>F</tt> like <tt>$HTTP_SERVER_VARS[REQUEST_URI]<b>&F=include_php_file</b></tt>",1);                  UploadPage($gblFsRoot, $relDir) ;
1967                  $inc_file="$fsRealmDir/$realm".$realm_sep.$F.".php";                  EndHTML() ;
1968                  if (! file_exists($inc_file)) {                  exit ;
1969                          Error("Can't find file to include","Can't find include file <tt>$F.php</tt> in <tt>$fsRealmDir/$realm/</tt>. Meybe you should copy <tt>$gblIncDir/include_php/$F.php</tt> to <tt>$inc_file<tt> ?",1);          case "E" :
1970                  }                  // detail of $relDir/$F
1971                  if (!is_readable($inc_file))                  if (is_file("$gblFsRoot/$relDir/$F") || is_dir("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ;
1972                          Error("Read access to include file denied","Can't read PHP include file <tt>$inc_file</tt>. Fix permissions on it.",1);                  exit ;
1973                  $text  = "Your include file should define \$text variable which holds this text and \$title variable which is page title";          case "C" :
1974                  $title = "You should define \$title variable with page title";                  $F=stripSlashes($F);
1975                  include($inc_file);                  // listing of $relDir/$F
1976                  StartHTML($title, $text) ;                  DisplayCode($gblFsRoot, $relDir, $F) ;
1977                  print "<p>".GifIcon(up)." Back to <a href=$HTTP_SERVER_VARS[PHP_SELF]>front page</a>.</p>";                  exit ;
1978                  EndHTML() ;          case "Co" :
1979                  exit ;                  // checkout
1980          }                  Lock("$gblFsRoot/$relDir/$F");
1981                    Download("$gblFsRoot/$relDir/$F",1);
1982          // default: display directory $relDir                  exit;
1983          Navigate($gblFsRoot,$relDir) ;            case "Ci" :
1984          exit ;                  $F=stripSlashes($F);
1985                    // upload && update to $relDir
1986          Error("Whooah!","By cartesian logic, this never happens",1) ;                  if (!is_writeable($gblFsRoot . $relDir))
1987  ?>                          Error("Write access denied",$relDir) ;
1988                    $text  = "Use this page to update a single " ;
1989                    $text .= "file to <B>$realm</B>." ;
1990                    StartHTML("(Update file Page)", $text) ;
1991                    UploadPage($gblFsRoot, $relDir, $F) ;
1992                    EndHTML() ;
1993                    exit ;
1994            case "V" :
1995                    // view
1996                    LogIt("$gblFsRoot/$relDir/$F","viewed");
1997                    Download("$gblFsRoot/$relDir/$F",$gblForceDownload);
1998                    exit;
1999            case "Ch" :
2000                    StartHTML("(File changes)","All changes chronologicaly...");
2001                    DisplayChangeLog(0);    // all
2002                    EndHTML() ;
2003                    exit;
2004            case "Ch1" :
2005                    StartHTML("(File changes)","Changes to files in last day...");
2006                    DisplayChangeLog(1);
2007                    EndHTML() ;
2008                    exit;
2009            case "I" :
2010                    if (! isset($F) || $F == "")
2011                            Error("Can't find file to include","Your request didn't specify file to include which should be in variable <tt>F</tt> like <tt>$HTTP_SERVER_VARS[REQUEST_URI]<b>&F=include_php_file</b></tt>",1);
2012                    $inc_file="$fsRealmDir/$realm".$realm_sep.$F.".php";
2013                    if (! file_exists($inc_file)) {
2014                            Error("Can't find file to include","Can't find include file <tt>$F.php</tt> in <tt>$fsRealmDir/$realm/</tt>. Meybe you should copy <tt>$gblIncDir/include_php/$F.php</tt> to <tt>$inc_file<tt> ?",1);
2015                    }
2016                    if (!is_readable($inc_file))
2017                            Error("Read access to include file denied","Can't read PHP include file <tt>$inc_file</tt>. Fix permissions on it.",1);
2018                    $text  = "Your include file should define \$text variable which holds this text and \$title variable which is page title";
2019                    $title = "You should define \$title variable with page title";
2020                    include($inc_file);
2021                    StartHTML($title, $text) ;
2022                    print "<p>".GifIcon(up)." Back to <a href=$HTTP_SERVER_VARS[PHP_SELF]>front page</a>.</p>";
2023                    EndHTML() ;
2024                    exit ;
2025            }
2026    
2027            // default: display directory $relDir
2028            Navigate($gblFsRoot,$relDir) ;  
2029            exit ;
2030    
2031            Error("Whooah!","By cartesian logic, this never happens",1) ;
2032    ?>
2033    

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.26