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

Legend:
Removed from v.1.21  
changed lines
  Added in v.1.38

  ViewVC Help
Powered by ViewVC 1.1.26