/[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.20 by dpavlin, Sun Jul 28 17:51:20 2002 UTC revision 1.39 by dpavlin, Wed Jun 11 02:32:50 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";          // this is defined in global configuration file /etc/docman.conf
27            //$gblIncDir = "/data/docman2";
28          // force download on view (so it won't open in associated application)  
29          $gblForceDownload = 0;          // force download on view (so it won't open in associated application)
30            $gblForceDownload = 0;
31          // date format  
32          $gblDateFmt="Y-m-d";          // date format
33  //      $gblDateFmt="D, F d, Y";          $gblDateFmt="Y-m-d";
34    //      $gblDateFmt="D, F d, Y";
35          // time format  
36          $gblTimeFmt="H:i:s";          // time format
37  //      $gblTimeFmt="g:i:sA";          $gblTimeFmt="H:i:s";
38    //      $gblTimeFmt="g:i:sA";
39          // Number of backup files to keep  
40          $gblNumBackups=3;          // Number of backup files to keep
41            $gblNumBackups=3;
42          // show red star if newer than ... days  
43          $gblModDays=1;          // show red star if newer than ... days
44            $gblModDays=1;
45          // choose GifIcon below unless you have the M$  
46          // WingDings font installed on your system          // choose GifIcon below unless you have the M$
47            // WingDings font installed on your system
48          $gblIcon="GifIcon";             // MockIcon or GifIcon  
49            $gblIcon="GifIcon";             // MockIcon or GifIcon
50          // the directory below should be /icons/ or /icons/small/  
51          // on Apache; a set of icons is included in the distribution          // the directory below should be /icons/ or /icons/small/
52            // on Apache; a set of icons is included in the distribution
53          $gblIconLocation="/icons/";  
54            $gblIconLocation="/icons/";
55          // files you want to be able to edit in text mode  
56          // and view with (primitive) syntax highlighting          // files you want to be able to edit in text mode
57            // and view with (primitive) syntax highlighting
58          $gblEditable = array( ".txt",".asa",".asp",".htm",".html",  
59                                ".cfm",".php3",".php",".phtml",          $gblEditable = array( ".txt",".asa",".asp",".htm",".html",
60                                ".shtml",".css" ) ;                                ".cfm",".php3",".php",".phtml",
61                                  ".shtml",".css" ) ;
62          // files that will display as images on the detail page  
63          // (useless if your browser doesn't support them)          // files that will display as images on the detail page
64            // (useless if your browser doesn't support them)
65          $gblImages   = array( ".jpg",".jpeg",".gif",".png",".ico",  
66                                ".bmp",".xbm") ;          $gblImages   = array( ".jpg",".jpeg",".gif",".png",".ico",
67                                  ".bmp",".xbm") ;
68          // which files to hide (separated by ,)  
69          $gblHide = "";          // which files to hide (separated by ,)
70            $gblHide = "";
71          // Where are users? (by default in .htusers file)  
72          $gblUsers = "file";          // Where are users? (by default in .htusers file)
73            $gblUsers = "file";
74  //////////////////////////////////////////////////////////////////  
75    //////////////////////////////////////////////////////////////////
76          $gblTitle = "Document Manager";  
77          $gblVersion = "2.0-pre1";          $gblTitle = "Document Manager";
78            $gblVersion = "2.0-pre4";
79          $secHash    = "";  
80            $secHash    = "";
81          // location of html files  
82          $html = $gblIncDir."/html";          // load language
83            if (isset($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"])) {
84          LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]);                  LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]);
85            }
86          // for security and configuration  
87          $realm=$HTTP_SERVER_VARS["HTTP_HOST"];          // does gettext locale function exits?
88            if (! function_exists("_")) {
89          $fsDocumentRoot = dirname($HTTP_SERVER_VARS["SCRIPT_FILENAME"]);                  function _($str) { return $str; }
90          if ($fsDocumentRoot == "") Error("Configuration error","Can't get SCRIPT_FILENAME from your web server. Please set <tt>\$fsDocumentRoot</tt> in <tt>\$</tt>",1);          }
91    
92          // globals for later          // for security and configuration
93          $gblLogin = $HTTP_SERVER_VARS["PHP_AUTH_USER"];          $realm=$HTTP_SERVER_VARS["HTTP_HOST"];
94          $gblPasswd = $HTTP_SERVER_VARS["PHP_AUTH_PW"];  
95            $fsDocumentRoot = dirname($HTTP_SERVER_VARS["SCRIPT_FILENAME"]);
96  //////////////////////////////////////////////////////////////////          if ($fsDocumentRoot == "") Error("Configuration error","Can't get SCRIPT_FILENAME from your web server. Please set <tt>\$fsDocumentRoot</tt> in <tt>\$</tt>",1);
97    
98  function LoadLanguage($lang) {          // globals for later
99            $gblLogin = HTTP_SERVER_VAR("PHP_AUTH_USER");
100          global $gblIncDir,$html;          $gblPasswd = HTTP_SERVER_VAR("PHP_AUTH_PW");
101    
102          if (file_exists($gblIncDir."/lang/$lang.php")) {  //////////////////////////////////////////////////////////////////
103                  include($gblIncDir."/lang/$lang.php");  
104                  $html .= "-$lang";  function LoadLanguage($lang) {
105          } else {  
106                  include($gblIncDir."/lang/default.php");          global $gblIncDir,$html;
107          }  
108  }          if (file_exists($gblIncDir."/lang/$lang.php")) {
109                    include($gblIncDir."/lang/$lang.php");
110  function StartHTML($title,$text="") {                  $html .= "-$lang";
111            } else {
112          global $html,$fsDocumentRoot,$gblTitle,$HTTP_SERVER_VARS;                  include($gblIncDir."/lang/default.php");
113            }
114          $host  = $HTTP_SERVER_VARS["HTTP_HOST"] ;  }
115          $self  = $HTTP_SERVER_VARS["PHP_SELF"] ;  
116    function StartHTML($title,$text="") {
117          if (file_exists("$fsDocumentRoot/docman.css")) {  
118                  $css=dirname($self)."/docman.css";          global $html,$fsDocumentRoot,$gblTitle,$HTTP_SERVER_VARS;
119          } else {  
120                  $css=$self."?STYLE=get";          $host  = $HTTP_SERVER_VARS["HTTP_HOST"] ;
121          }          $self  = $HTTP_SERVER_VARS["PHP_SELF"] ;
122    
123          include("$html/head.html");          if (file_exists("$fsDocumentRoot/docman.css")) {
124  }                  $css = "";
125                    $d = dirname($self);
126  //////////////////////////////////////////////////////////////////                  if ($d != "/") $css = $d;
127                    $css .= "/docman.css";
128  function EndHTML() {          } else {
129                    $css=$self."?STYLE=get";
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) {  function check_trustee($user,$path) {
1397            global $trustees,$HAVE_TRUSTEE;
1398          // check user FIX          $perm['allow'] = 0;
1399          if ($t & trmask_not && ($u==$user)) continue;          $perm['deny'] = 0;
1400          if (!($t & trmask_not) && ($u!=$user)) continue;  
1401            // do we use trustees?
1402          if ($t & trmask_deny) {          if (! $HAVE_TRUSTEE) return $perm;
1403                  if ($t & trmask_clear) {  
1404                          $perm['deny'] &= ~$t;          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                  } else {  
1406                          $perm['deny'] |= $t;  global $debug;
1407                  }  $debug .= "<br>check_trustee $path ... ";
1408          } elseif ($t & trmask_clear) {  
1409                  $perm['allow'] &= ~$t;          $path_arr=explode("/",$path);
1410          } else {          $tmppath="";
1411                  $perm['allow'] |= $t;          while (count($path_arr)) {
1412          }                  $p = array_shift($path_arr);
1413          return $perm;  $debug.= "[$p] ";
1414  }// end of helper function                  # add trailing slash
1415                    if (substr($tmppath,strlen($tmppath)-1,1) != "/") {
1416  function check_trustee($user,$path) {                          $tmppath.="/";
1417          global $trustees;                  }
1418          $perm['allow'] = 0;                  # append currnet dir to tmppath
1419          $perm['deny'] = 0;                  if (isset($p)) {
1420                            $tmppath.=$p;
1421          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);                  }
1422    $debug.= ">> $tmppath ";
1423  global $debug;  
1424  $debug .= "<br>check_trustee $path ... ";                  if (! isset($trustees[$tmppath])) continue;
1425                    $tr = $trustees[$tmppath];
1426          $path_arr=explode("/",$path);  
1427          $tmppath = "/";                  $one_level = (!count($path_arr));
1428          while (count($path_arr)) {  $debug.=" O($one_level) ";
1429                  $tmppath.=array_shift($path_arr);  
1430  $debug.= ">> $tmppath ";                  if (isset($tr)) {
1431                            // first apply trustee for all
1432                  # clear one level flag                          if (isset($tr['*']) && $user!="anonymous") {
1433                  $perm['allow'] &= ~trmask_one_level;                                  $perm = unroll_perm($user,$tr['*'],$perm, $one_level);
1434                  $perm['deny'] &= ~trmask_one_level;                                  unset($tr['*']);
1435                            }
1436                  if (! isset($trustees[$tmppath])) continue;                          // then apply not and group policies
1437                  $tr = $trustees[$tmppath];                          foreach ($tr as $g=>$t) {
1438                                    if ($t & trmask_not && $g != $user) {
1439                  if (isset($tr)) {                                          $t = $t & ~trmask_not;
1440                          // first apply trustee for all                                          $perm = unroll_perm($user,$t,$perm, $one_level);
1441                          if (isset($tr['*'])) {                                          unset($tr[$g]);
1442                                  $perm = unroll_perm($user,$tr['*'],$user, $perm);  
1443                                  unset($tr['*']);                                  } elseif ($t & trmask_group && in_group($user,$g)) {
1444                          }                                          // resolv user
1445                          // then apply group policies                                          $t = $t & ~trmask_group;
1446                          foreach ($tr as $g=>$t) {                                          $perm = unroll_perm($user,$t,$perm, $one_level);
1447                                  if ($t & trmask_group && in_group($user,$g)) {                                          unset($tr[$g]);
1448                                          // resolv user                                  }
1449                                          $t = $t & ~trmask_group;                          }
1450                                          $perm = unroll_perm($user,$t,$user, $perm);                          // then apply user policy
1451                                          unset($tr[$g]);                          if (isset($tr[$user])) {
1452                                  }                                  $perm = unroll_perm($user,$tr[$user],$perm,$one_level);
1453                          }                                  unset($tr[$user]);
1454                          // then apply user policy                          }
1455                          if (isset($tr[$user])) {                  }
1456                                  $perm = unroll_perm($user,$tr[$user],$user, $perm);  $debug.="d(".display_trustee($perm['deny']).") a(".display_trustee($perm['allow']).") ";
1457                                  unset($tr[$user]);  
1458                          }          }
1459                  }  $debug.="<br>check_trustee: user: $user path: $path==$tmppath perm: ";
1460  $debug.="d(".display_trustee($perm['deny']).") a(".display_trustee($perm['allow']).") ";  $debug.="d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).")<Br>\n";
1461            return $perm;
1462          }  }
1463  $debug.="<br>check_trustee: user: $user path: $path==$tmppath perm: ";  
1464  $debug.="d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).")<Br>\n";  // handy functions
1465          return $perm;  
1466  }  function check_perm($path,$trperm) {
1467            global $gblLogin,$HAVE_TRUSTEE;
1468  // handy functions  
1469            $path = str_replace("//","/",$path);
1470  function check_perm($path,$trperm) {  
1471          global $gblLogin,$HAVE_TRUSTEE;          global $debug;
1472    $debug.="<br>check_perm: on <tt>$path</tt> for perm ".display_trustee($trperm)."<br>\n";
1473          global $debug;  
1474  $debug.="<br>check_perm: on <tt>$path</tt> for perm ".display_trustee($trperm)."<br>\n";          $return = ! $HAVE_TRUSTEE;
1475            if ($HAVE_TRUSTEE) {
1476          $return = ! $HAVE_TRUSTEE;                  $perm = check_trustee($gblLogin,$path);
1477          if ($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                  $perm = check_trustee($gblLogin,$path);                  if ($perm['deny'] & $trperm) $return=0;
1479  $debug.=" d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).") perm to have: $trperm (".display_trustee($trperm).")";                  elseif (($perm['allow'] & $trperm) == $trperm) $return=1;
1480                  if ($perm['deny'] & $trperm) $return=0;          }
1481                  elseif (($perm['allow'] & $trperm) == $trperm) $return=1;  $debug.=" return: $return<br>\n";
1482          }          return($return);
1483  $debug.=" return: $return<br>\n";  }
1484          return($return);  
1485  }  //////////////////////////////////////////////////////////////////
1486    
1487  //////////////////////////////////////////////////////////////////  function readMime() {
1488            global $mime_type, $gblMimeTypes;
1489  function readMime() {  
1490          global $mime_type;          if (! isset($gblMimeTypes)) {
1491                    $gblMimeTypes = "/etc/mime.types";
1492          if (! isset($gblMimeTypes)) {          }
1493                  $gblMimeTypes = "/etc/mime.types";  
1494          }          $mime = @fopen($gblMimeTypes,"r");
1495    
1496          $mime = @fopen($gblMimeTypes,"r");          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    
1498          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>");          while($line = fgets($mime,80)) {
1499                    if (substr($line,0,1) == "#") continue; // skip comment
1500          while($line = fgets($mime,80)) {                  $arr = preg_split("/[\s\t]+/",$line);
1501                  if (substr($line,0,1) == "#") continue; // skip comment                  $type = array_shift($arr);
1502                  $arr = preg_split("/[\s\t]+/",$line);                  while ($ext = array_shift($arr)) {
1503                  $type = array_shift($arr);                          $mime_type[$ext] = $type;
1504                  while ($ext = array_shift($arr)) {                  }
1505                          $mime_type[$ext] = $type;          }
1506                  }  
1507          }          fclose($mime);
1508    }
1509          fclose($mime);  
1510  }  //////////////////////////////////////////////////////////////////
1511    
1512  //////////////////////////////////////////////////////////////////  // check for invalid characters in filename and dirname (.. and /)
1513    
1514  // check for invalid characters in filename and dirname (.. and /)  function check_dirname($file) {
1515            if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in directory name <tt>$file</tt>",1);
1516  function check_dirname($file) {  }
1517          if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in directory name <tt>$file</tt>",1);  
1518  }  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  function check_filename($file) {          // remove deleted directory (for undelete to work)
1521          if (strstr($file,"..")) Error("Security violation","No parent dir <tt>..</tt> allowed in file name <tt>$file</tt>",1);          $file = str_replace(".del/","",$file);
1522          if (strstr($file,"/")) Error("Security violation","No slashes <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  }  }
1524    
1525  //////////////////////////////////////////////////////////////////  // bla/blo/../foo will return bla/foo
1526    function remove_parent($path) {
1527  // functions to move HTTP server variables to global namespace          while (preg_match(",/[^/]+/\.\./,",$path)) {
1528  // [replacement for register_globals in php.ini]                  $path = preg_replace(",/[^/]+/\.\./,","",$path);
1529            }
1530  function HTTP_GET_VAR($var) {          if (substr($path,0,1) != "/") $path = "/".$path;
1531          global $HTTP_GET_VARS, $GLOBALS;          return $path;
1532          if (isset($HTTP_GET_VARS[$var])) {  }
1533                  $GLOBALS[$var] = stripSlashes($HTTP_GET_VARS[$var]);  
1534                  return $GLOBALS[$var];  //////////////////////////////////////////////////////////////////
1535          }  
1536  }  // functions to move HTTP server variables to global namespace
1537    // [replacement for register_globals in php.ini]
1538  function HTTP_POST_VAR($var) {  
1539          global $HTTP_GET_VARS, $GLOBALS;  function HTTP_GET_VAR($var) {
1540          if (isset($HTTP_GET_VARS[$var])) {          global $HTTP_GET_VARS, ${$var};
1541                  $GLOBALS[$var] = stripSlashes($HTTP_GET_VARS[$var]);          if (isset($HTTP_GET_VARS[$var])) {
1542                  return $GLOBALS[$var];                  $$var = stripSlashes($HTTP_GET_VARS[$var]);
1543          }                  return $$var;
1544  }          }
1545    }
1546  //////////////////////////////////////////////////////////////////  
1547    function HTTP_POST_VAR($var) {
1548  function Warn($text) {          global $HTTP_POST_VARS, ${$var};
1549  }          if (isset($HTTP_POST_VARS[$var])) {
1550                    $$var = $HTTP_POST_VARS[$var];
1551  //////////////////////////////////////////////////////////////////                  return $$var;
1552  // MAIN PROGRAM          }
1553    }
1554          $gblFilePerms = 0640 ;         // default for new files  
1555          $gblDirPerms  = 0750 ;          // default for new dirs  function HTTP_SERVER_VAR($var) {
1556            global $HTTP_SERVER_VARS, ${$var};
1557          if (isset($HTTP_GET_VARS["STYLE"]) && $HTTP_GET_VARS["STYLE"] == "get") {          if (isset($HTTP_SERVER_VARS[$var])) {
1558                  include("$html/docman.css");                  $$var = $HTTP_SERVER_VARS[$var];
1559                  exit;                  return $$var;
1560          }          }
1561    }
1562          // location of master docman configuration file  
1563          $docman_conf = "/etc/docman.conf";  //////////////////////////////////////////////////////////////////
1564          if (! file_exists($docman_conf)) {  
1565                  $error = "Can't find master configuration file $docman_conf. See docman2/doc/upgrade.html#docman_conf for more informations";  function Warn($text) {
1566                    }
1567                  error_log("docman: $error");  
1568                  Error("docman not installed completly",$error);  //////////////////////////////////////////////////////////////////
1569          }  
1570          include($docman_conf);  function isBlank($file,$what = "filename") {
1571            if (trim($file) == "") return "<i>whitespace $what</i>";
1572          if (! isset($fsRealmDir)) {          if ($file == "") return "<i>no $what</i>";
1573                  $fsRealmDir = "$gblIncDir/realm";          return $file;
1574          }  }
1575    
1576          // try to add dir to script name to realm var  //////////////////////////////////////////////////////////////////
1577          if (is_dir("$fsRealmDir/$realm/".dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]))) {  // MAIN PROGRAM
1578                  $realm .= dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]);  
1579                  $realm_sep = "/";          $gblFilePerms = 0640 ;         // default for new files
1580          } else {          $gblDirPerms  = 0750 ;          // default for new dirs
1581                  $realm_sep = ".";  
1582          }          if (isset($HTTP_GET_VARS["STYLE"]) && $HTTP_GET_VARS["STYLE"] == "get") {
1583                    include("$html/docman.css");
1584          $realm_config = $fsRealmDir."/".$realm.$realm_sep."conf";                  exit;
1585            }
1586          // read user-defined configuration  
1587          if (file_exists($realm_config)) {          // location of master docman configuration file
1588                  include($realm_config);          $docman_conf = "/etc/docman.conf";
1589          } else {          if (! file_exists($docman_conf)) {
1590                  Error("Configuration error","Can't find configuration file at <tt>$realm_config</tt> !");                  $error = "Can't find master configuration file <tt>$docman_conf</tt>. See <tt>docman2/doc/upgrade.html#docman_conf</tt> for more informations";
1591          }  
1592                    error_log("docman: $error");
1593          if (! isset($gblRepositoryDir)) Error("Configuration error","<tt>\$gblRepositoryDir</tt> is not setuped in realm configuration file <tt>$realm_config</tt>");                  Error("docman not installed completly",$error);
1594            }
1595          // where do we get users from?          include($docman_conf);
1596          if (file_exists("$gblIncDir/htusers/$gblUsers.php")) {  
1597                  include("$gblIncDir/htusers/$gblUsers.php");          // location of html files
1598          } else {          $html = $gblIncDir."/html";
1599                  Error("Configuration error","Can't find user handling module at <tt>$gblIncDir/htusers/$gblUsers.php</tt> ! Please fix <tt>$realm_config</tt>");  
1600          }          // set fsRealmDir
1601            if (! isset($fsRealmDir)) {
1602          // take additional login vars                  $fsRealmDir = "$gblIncDir/realm";
1603          HTTP_GET_VAR("relogin");          }
1604          HTTP_GET_VAR("force_login");  
1605            // try to add dir to script name to realm var
1606          // if no password, or empty password logout          if (is_dir("$fsRealmDir/$realm/".dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]))) {
1607          if (                  $realm .= dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]);
1608                  isset($gblLogin) && (                  $realm_sep = "/";
1609                          !isset($relogin) || (          } else {
1610                                  isset($relogin) && $relogin != md5($gblLogin.$gblPasswd)                  $realm_sep = ".";
1611                          )          }
1612                  ) && (  
1613                          $gblPasswd == "" || !isset($gblPasswd)          $realm_config = $fsRealmDir."/".$realm.$realm_sep."conf";
1614                  ) && !isset($force_login) && $gblLogin != "anonymous"  
1615             ) {          // read user-defined configuration
1616                  StartHTML("Logout completed","Your login credentials has been erased") ;          if (file_exists($realm_config)) {
1617                  EndHTML() ;                  include($realm_config);
1618                  exit ;          } else {
1619          }                  Error("Configuration error","Can't find configuration file at <tt>$realm_config</tt> !");
1620            }
1621          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);  
1622            if (! isset($gblRepositoryDir)) Error("Configuration error","<tt>\$gblRepositoryDir</tt> is not setuped in realm configuration file <tt>$realm_config</tt>");
1623          // trustee (ACL) file configuration  
1624          $trustee_conf="$fsRealmDir/$realm".$realm_sep."trustee";          // where do we get users from?
1625          // compiled version of trustee file          if (file_exists("$gblIncDir/htusers/$gblUsers.php")) {
1626          $trustee_php="$gblRepositoryDir/.trustee.php";                  include("$gblIncDir/htusers/$gblUsers.php");
1627          // get ACL informations          } else {
1628          $HAVE_TRUSTEE = init_trustee();                  Error("Configuration error","Can't find user handling module at <tt>$gblIncDir/htusers/$gblUsers.php</tt> ! Please fix <tt>$realm_config</tt>");
1629            }
1630          if (strtolower($gblLogin) == "anonymous" || !isset($gblLogin)) {  
1631                  $perm = check_trustee("anonymous","/");          // take additional login vars
1632                  // browsing must be explicitly allowed for root directory          HTTP_GET_VAR("relogin");
1633                  // of repository for anonymous user to work!          HTTP_GET_VAR("force_login");
1634                  if ($perm['allow'] & trperm_b) {  
1635                          $gblLogin = $gblPasswd = "anonymous";          // if no password, or empty password logout
1636                          $secHash = md5($gblLogin.$gblPasswd);          if (
1637                          $gblUserName = "Anonymous user";                  isset($gblLogin) && (
1638                  }                          !isset($relogin) || (
1639          }                                  isset($relogin) && $relogin != md5($gblLogin.$gblPasswd)
1640                            )
1641          // authentication failure                  ) && (
1642          if ( md5($gblLogin.$gblPasswd) != $secHash ||                          $gblPasswd == "" || !isset($gblPasswd)
1643                  isset($relogin) && $secHash == $relogin) {                  ) && !isset($force_login) && $gblLogin != "anonymous"
1644                  header("WWW-authenticate: basic realm=\"$realm\"") ;             ) {
1645                  header("HTTP/1.0 401 Unauthorized") ;                  StartHTML("Logout completed","Your login credentials has been erased") ;
1646                  Error("401 Unauthorized","No trespassing !",0,1);                  EndHTML() ;
1647          }                  exit ;
1648            }
1649    
1650          // read mime.types          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);
1651          readMime();  
1652            // trustee (ACL) file configuration
1653          if ($HTTP_SERVER_VARS["REQUEST_METHOD"] == "POST") {          $trustee_conf="$fsRealmDir/$realm".$realm_sep."trustee";
1654                  // take variables from server          // compiled version of trustee file
1655                  if (HTTP_POST_VAR("FN")) check_filename($FN);          $trustee_php="$gblRepositoryDir/.trustee.php";
1656                  if (HTTP_POST_VAR("DIR")) {          // get ACL informations
1657                          check_dirname($DIR);          $HAVE_TRUSTEE = init_trustee();
1658                          $relDir = $DIR;  
1659                  } else {          if (strtolower($gblLogin) == "anonymous" || !isset($gblLogin)) {
1660                          trigger_error("Can't get DIR",E_USER_WARNING);                  $perm = check_trustee("anonymous","/");
1661                  }                  // browsing must be explicitly allowed for root directory
1662                  if (HTTP_POST_VAR("RELPATH")) check_dirname($RELPATH);                  // of repository for anonymous user to work!
1663                  HTTP_POST_VAR("T");                  if ($perm['allow'] & trperm_b) {
1664                  HTTP_POST_VAR("CONFIRM");                          $gblLogin = $gblPasswd = "anonymous";
1665                            $secHash = md5($gblLogin.$gblPasswd);
1666          } else {                          $gblUserName = "Anonymous user";
1667                  // get                  }
1668                  HTTP_GET_VAR("A");          }
1669                  if (HTTP_GET_VAR("D")) {  
1670                          check_dirname($D);          // authentication failure
1671                          $D=urldecode($D);          if ( md5($gblLogin.$gblPasswd) != $secHash ||
1672                          $relDir = $D;                  isset($relogin) && $secHash == $relogin) {
1673                  } else {                  header("WWW-authenticate: basic realm=\"$realm\"") ;
1674                          //trigger_error("Can't get D",E_USER_WARNING);                  header("HTTP/1.0 401 Unauthorized") ;
1675                          $relDir = "/";                  Error("401 Unauthorized","No trespassing !",0,1);
1676                  }          }
1677                  if (HTTP_GET_VAR("F")) check_filename($F);  
1678          }  
1679            // read mime.types
1680          $relScriptDir = dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]) ;              readMime();
1681          // i.e. /docman  
1682            if (! isset($gblPermNote)) {
1683          // start on server root                  $gblPermNote = trperm_r;
1684          $gblFsRoot = $gblRepositoryDir;          }
1685          // i.e. /home/httpd/repository  
1686            HTTP_POST_VAR("FN");
1687          $fsDir = $gblFsRoot . $relDir ; // current directory  
1688          if ( !is_dir($fsDir) ) Error("Dir not found","Can't find <tt>$relDir</tt> which points to <tt>$fsDir</tt>",1) ;          if ($HTTP_SERVER_VARS["REQUEST_METHOD"] == "POST") {
1689                    // take variables from server
1690          if ($relDir == "") $relDir="/";                  if (HTTP_POST_VAR("FN"))
1691                            check_filename($FN);
1692          if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") {                  if (HTTP_POST_VAR("DIR")) {
1693                  $webRoot  = "https://";                          check_dirname($DIR);
1694          } else {                          $relDir = $DIR;
1695                  $webRoot  = "http://";                  } else {
1696          }                          trigger_error("Can't get DIR",E_USER_WARNING);
1697          $webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir;                          $relDir = "/";
1698                    }
1699          if (HTTP_POST_VAR("POSTACTION")) switch ($POSTACTION) {                  if (HTTP_POST_VAR("RELPATH")) check_dirname($RELPATH);
1700          case "UPLOAD" :                  HTTP_POST_VAR("T");
1701                  $FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]);                  HTTP_POST_VAR("CONFIRM");
1702                  $FN=stripSlashes($HTTP_POST_FILES["FN"]["name"]);          } else {
1703                  if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;                  // get
1704                    HTTP_GET_VAR("A");
1705                  $source = $FN_name ;                  if (HTTP_GET_VAR("D")) {
1706                  if (! file_exists($source)) {                          check_dirname($D);
1707                          Error("You must select file with browse to upload it!");                          $D=urldecode($D);
1708                  }                          $relDir = $D;
1709                    } else {
1710                  $FILENAME = $HTTP_POST_VARS["FILENAME"];                          //trigger_error("Can't get D",E_USER_WARNING);
1711                  check_filename($FILENAME);                          $relDir = "/";
1712                    }
1713                  if (! isset($FILENAME)) {       // from update file                  if (HTTP_GET_VAR("F")) check_filename($F);
1714                          $target = "$fsDir/".basename($FN);          }
1715                  } else {  
1716                          $target = "$fsDir/$FILENAME";          $relScriptDir = dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]) ;    
1717                  }          // i.e. /docman
1718    
1719                  // backup old files first          // start on server root
1720                  $dir=dirname($target);          $gblFsRoot = $gblRepositoryDir;
1721                  if (! file_exists($dir."/.bak")) {          // i.e. /home/httpd/repository
1722                          mkdir($dir."/.bak",0700);  
1723                  }          $fsDir = $gblFsRoot . $relDir ; // current directory
1724                  if (! file_exists($dir."/.bak/$GLOBALS[gblNumBackups]")) {          if ( !is_dir($fsDir) ) Error("Dir not found","Can't find <tt>$relDir</tt> which points to <tt>$fsDir</tt>",1) ;
1725                          mkdir($dir."/.bak/$GLOBALS[gblNumBackups]",0700);  
1726                  }          if ($relDir == "") $relDir="/";
1727                  $file=basename($target);  
1728                  for($i=$GLOBALS[gblNumBackups]-1;$i>0;$i--) {          if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") {
1729                          MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/");                  $webRoot  = "https://";
1730                  }          } else {
1731                  MoveTo($target,$dir."/.bak/1/");                  $webRoot  = "http://";
1732            }
1733                  copy($source,$target) ;          $webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir;
1734                  chmod($target,$gblFilePerms) ;  
1735                  clearstatcache() ;          if (HTTP_POST_VAR("POSTACTION")) switch ($POSTACTION) {
1736                  if (isset($FILENAME)) {          case "UPLOAD" :
1737                          LogIt($target,"check-in",trperm_r | trperm_w);                  $FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]);
1738                          Unlock($target);                  $FN=stripSlashes($HTTP_POST_FILES["FN"]["name"]);
1739                  } else {                  if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;
1740                          LogIt($target,"uploaded",trperm_r | trperm_w);  
1741                  }                  $source = $FN_name ;
1742                  break ;                  if (! file_exists($source)) {
1743                            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);
1744          case "SAVE" :                  }
1745                  $path = $gblFsRoot . $RELPATH ;  
1746                  $path=stripSlashes($path);                  if (HTTP_POST_VAR("FILENAME")) check_filename($FILENAME);
1747                  $writable = is_writeable($path) ;  
1748                  $legaldir = is_writeable(dirname($path)) ;                  if (! isset($FILENAME)) {       // from update file
1749                  $exists   = (file_exists($path)) ? 1 : 0 ;                          $target = "$fsDir/".basename($FN);
1750  // check for legal extension here as well                  } else {
1751                  if (!($writable || (!$exists && $legaldir)))                          $target = "$fsDir/$FILENAME";
1752                          Error("Write denied",$RELPATH) ;                  }
1753                  $fh = fopen($path, "w") ;  
1754                  $FILEDATA=stripSlashes($FILEDATA);                  if (! check_perm("$relDir/".basename($target), trperm_w))
1755                  fwrite($fh,$FILEDATA) ;                          Error("Access denied","User <tt>$gblLogin</tt> tried to upload <tt>$relDir/".basename($target)."</tt> without valid trustee.",1);
1756                  fclose($fh) ;  
1757                  clearstatcache() ;                  // backup old files first
1758                  LogIt($path,"saved changes",trperm_r);                  $dir=dirname($target);
1759                  break ;                  if (! file_exists($dir."/.bak")) {
1760                            mkdir($dir."/.bak",0700);
1761          case "CREATE" :                  }
1762                  // we know $fsDir exists                  if (! file_exists($dir."/.bak/$gblNumBackups")) {
1763                  if ($FN == "") break;           // no filename!                          mkdir($dir."/.bak/$gblNumBackups",0700);
1764                  if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;                  }
1765                  $path = $fsDir . "/" . $FN ;  // file or dir to create                  $file=basename($target);
1766                  $relPath = $relDir . "/" . $FN ;                  for($i=$gblNumBackups-1;$i>0;$i--) {
1767                  switch ( $T ) {                          MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/");
1768                  case "D" :  // create a directory                  }
1769                          if ( ! @mkdir($path,$gblDirPerms) )                  MoveTo($target,$dir."/.bak/1/");
1770                                  Error("Mkdir failed",$relPath) ; // eg. if it exists  
1771                          else                  copy($source,$target) ;
1772                                  LogIt($path."/","dir created",trperm_w);                  chmod($target,$gblFilePerms) ;
1773                          clearstatcache() ;                  clearstatcache() ;
1774                          break ;                  if (isset($FILENAME)) {
1775                  case "F" :  // create a new file                          LogIt($target,"check-in",trperm_r | trperm_w);
1776  // this functionality is doubled in DetailView().                          Unlock($target);
1777  // better keep it here altogether                  } else {
1778  // chmod perms to $gblFilePerms                          LogIt($target,"uploaded",trperm_r | trperm_w);
1779                          if ( file_exists($path) && !is_writeable($path) )                  }
1780                                  Error("File not writable", $relPath) ;                  break ;
1781                          $fh = fopen($path, "w+") ;  
1782                          if ($fh) {          case "SAVE" :
1783                                  fputs($fh,"\n");                  $path = $gblFsRoot . $RELPATH ;
1784                                  fclose($fh) ;                  $path=stripSlashes($path);
1785                                  LogIt($path,"file created",trperm_r | trperm_w);  
1786                          } else {                  if (! check_perm("$RELPATH", trperm_w))
1787                                  Error("Creation of file $relPath failed -- $path");                          Error("Access denied","User <tt>$gblLogin</tt> tried to save <tt>$RELPATH</tt> without valid trustee.",1);
1788                          }  
1789                          $tstr = "$PHP_SELF?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;                  $writable = is_writeable($path) ;
1790                          header("Location: " . $tstr) ;                  $legaldir = is_writeable(dirname($path)) ;
1791                          exit ;                  $exists   = (file_exists($path)) ? 1 : 0 ;
1792                  }                  // FIX: more verbose error message
1793                  break ;                  if (!($writable || (!$exists && $legaldir)))
1794                            Error("Write denied",$RELPATH) ;
1795          case "DELETE" :                    $fh = fopen($path, "w") ;
1796                  if ( $CONFIRM != "on" ) break ;                  HTTP_POST_VAR("FILEDATA");
1797                    fwrite($fh,$FILEDATA) ;
1798                  $tstr  = "Attempt to delete non-existing object or " ;                  fclose($fh) ;
1799                  $tstr .= "insufficient privileges: " ;                  clearstatcache() ;
1800                    LogIt($path,"saved changes",trperm_r);
1801                  if ( $FN != "") {  // delete file                  break ;
1802                          $path =  $fsDir . "/" . $FN ;  
1803                            case "CREATE" :
1804                          $dir=dirname($path);                  // we know $fsDir exists
1805                          $file=basename($path);                  if (! check_perm($relDir, trperm_w))
1806                          if (! file_exists("$dir/.del")) {                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");
1807                                  mkdir("$dir/.del",0700);                  if ($T == "D") $type = "directory";
1808                          }                  else $type ="file";
1809                    if ($FN == "") Error("Can't create $type","You must enter name of $type to create it.");
1810  //                      if ( ! @unlink($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) ;
1811                          if ( ! rename($path,"$dir/.del/$file") ) {                  $path = "$fsDir/$FN";           // file or dir to create
1812                                  LogIt($path,"file delete failed");                  $relPath = "$relDir/$FN";
1813                                  Error("File delete failed", $tstr . $path) ;  
1814                          } else {                  if (file_exists($path))
1815                                  LogIt($path,"file deleted",trperm_w);                          Error("Can't create $type","Object <tt>$relPath</tt> allready exists");
1816                                  MoveTo("$dir/.log/$file","$dir/.del/.log/");  
1817                                  MoveTo("$dir/.note/$file","$dir/.del/.note/");                  switch ( $T ) {
1818                                  MoveTo("$dir/.lock/$file","$dir/.del/.lock/");                  case "D" :  // create a directory
1819                          }                          if ( ! @mkdir($path,$gblDirPerms) )
1820                  } else {  // delete directory                                  Error("Mkdir failed",$relPath) ; // eg. if it exists
1821                          if ( ! @rrmdir($fsDir) ) {                          else
1822                                  Error("Rmdir failed", $tstr . $fsDir) ;                                  LogIt($path."/","dir created",trperm_w);
1823                          } else {                          clearstatcache() ;
1824                                  LogIt($path,"dir deleted",trperm_w);                          break ;
1825                                  $relDir = dirname($relDir) ;  // move up                  case "F" :  // create a new file
1826                          }  // this functionality is doubled in DetailView().
1827                  }  // better keep it here altogether
1828                  break ;  // chmod perms to $gblFilePerms
1829                            if ( file_exists($path) && !is_writeable($path) )
1830          case "UNDELETE" :                                    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) ;
1831                  if ( $CONFIRM != "on" ) break ;                          $fh = fopen($path, "w+") ;
1832                            if ($fh) {
1833                  if (substr($FN,0,4) != ".del") break ;                                  fputs($fh,"\n");
1834                  $file=substr($FN,4,strlen($FN)-4);                                  fclose($fh) ;
1835                                    LogIt($path,"file created",trperm_r | trperm_w);
1836                  LogIt("$fsDir/.del/$file","undeleted",trperm_w);                          } else {
1837                  MoveTo("$fsDir/.del/$file","$fsDir/");                                  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) ;
1838                  MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/");                          }
1839                  MoveTo("$fsDir/.del/.note/$file","$fsDir/.note/");                          $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;
1840                  MoveTo("$fsDir/.del/.lock/$file","$fsDir/.lock/");                          header("Location: " . $tstr) ;
1841                            exit ;
1842                  break ;                  }
1843                    break ;
1844          case "RENAME" :    
1845                  if ( $CONFIRM != "on" ) break ;          case "DELETE" :  
1846                    if ( $CONFIRM != "on" ) break;
1847                  $NEWNAME=stripSlashes($HTTP_POST_VARS["NEWNAME"]);  
1848                  LogIt("$fsDir/$FN","renamed $FN to $NEWNAME",trperm_r);                  if ( isset($FN) && $FN != "") {
1849                  safe_rename($fsDir,$FN,$NEWNAME);                          $path=$fsDir."/".$FN;
1850                  break ;                          $what = "file";
1851                    } elseif (isset($DIR)) {
1852          case "NOTE" :                            $path=$gblFsRoot."/".$DIR;
1853                  $NOTE=stripSlashes($HTTP_POST_VARS["NOTE"]);                          $what = "directory";
1854                  WriteNote("$fsDir/$FN","$NOTE");                  } else  {
1855                  break ;                          Error("Can't delete object","Can't find filename <tt>\$FN</tt> or dirname in <tt>\$DIR</tt>",1);
1856                    }
1857          case "UNLOCK" :    
1858                  if ( $CONFIRM != "on" ) break ;                  if (! check_perm("$relDir/$FN", trperm_w))
1859                  Unlock("$fsDir/$FN");                          Error("Access denied","User <tt>$gblLogin</tt> tried to erase $what <tt>$relDir/$FN</tt> without valid trustee.",1);
1860                  break ;  
1861                    $tstr  = "Attempt to delete non-existing object or insufficient privileges: " ;
1862          default :  
1863                  // user hit "CANCEL" or undefined action                  $dir=dirname($path);
1864          }                  $file=basename($path);
1865    
1866          // common to all POSTs : redirect to directory view ($relDir)                  if (! file_exists("$dir/.del")) {
1867          if (isset($POSTACTION)) {                          mkdir("$dir/.del",0700);
1868                  $tstr = $PHP_SELF . "?D=" . urlencode($relDir) ;                  }
1869                  header("Location: " . $tstr) ;    
1870                  exit ;                  if ( ! @rename($path,"$dir/.del/$file") ) {
1871          }                          LogIt($path,"$what delete failed");
1872                                    Error("Can't delete $what",$tstr."<tt>".$relDir."/".$FN."</tt>") ;
1873          // check for mode.. navigate, code display, upload, or detail?                  } else {
1874          // $A=U : upload to path given in $D                          LogIt($path,"$what deleted",trperm_w);
1875          // $A=E : display detail of file $D/$F and edit                          MoveTo("$dir/.log/$file","$dir/.del/.log/");
1876          // $A=C : display code in file $D/$F                          MoveTo("$dir/.note/$file","$dir/.del/.note/");
1877          // $A=Co : checkout file $D/$F                          MoveTo("$dir/.lock/$file","$dir/.del/.lock/");
1878          // $A=Ci : checkin file $D/$F                  }
1879          // $A=V : view file (do nothing except log)                  break ;
1880          // $A=I : include file .$F.php from [$gblIncDir|realm]/include_php  
1881          // default : display directory $D          case "UNDELETE" :  
1882                    if ( $CONFIRM != "on" ) break ;
1883          if (isset($A)) switch ($A) {  
1884          case "U" :                  if (substr($FN,0,4) != ".del") break ;
1885                  // upload to $relDir                  $file=substr($FN,4,strlen($FN)-4);
1886                  if (! check_perm($relDir, trperm_w))  
1887                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");                  if (! check_perm("$relDir/$file", trperm_w))
1888                  if (!is_writeable($gblFsRoot . $relDir))                          Error("Access denied","User <tt>$gblLogin</tt> tried to undelete <tt>$relDir/$file</tt> without valid trustee.",1);
1889                          Error("Write access denied","User <tt>$gblLogin</tt> has permission on <tt>$relDir</tt>, but directory is not writable",1);  
1890                  $text  = "Use this page to upload a single " ;                  LogIt("$fsDir/.del/$file","undeleted",trperm_w);
1891                  $text .= "file to <B>$realm</B>." ;                  MoveTo("$fsDir/.del/$file","$fsDir/");
1892                  StartHTML("(Upload Page)", $text) ;                  MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/");
1893                  UploadPage($gblFsRoot, $relDir) ;                  MoveTo("$fsDir/.del/.note/$file","$fsDir/.note/");
1894                  EndHTML() ;                  MoveTo("$fsDir/.del/.lock/$file","$fsDir/.lock/");
1895                  exit ;  
1896          case "E" :                  break ;
1897                  // detail of $relDir/$F  
1898                  if (is_file("$gblFsRoot/$relDir/$F") || is_dir("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ;          case "RENAME" :  
1899                  exit ;                  if ( $CONFIRM != "on" ) break ;
1900          case "C" :  
1901                  $F=stripSlashes($F);                  if (HTTP_POST_VAR("NEWNAME")) {
1902                  // listing of $relDir/$F                          $dest = remove_parent($relDir.$NEWNAME);
1903                  DisplayCode($gblFsRoot, $relDir, $F) ;                          if (! check_perm($relDir.$FN, trperm_w) ||
1904                  exit ;                              ! check_perm($dest, trperm_w) )
1905          case "Co" :                                  Error("Access denied","User <tt>$gblLogin</tt> tried to rename <tt>$relDir$FN</tt> to <tt>$dest</tt> without valid trustee.",1);
1906                  // checkout                  } else {
1907                  Lock("$gblFsRoot/$relDir/$F");                          Error("Rename error","Can't find new name in var <tt>\$NEWNAME</tt>",1);
1908                  Download("$gblFsRoot/$relDir/$F",1);                  }
1909                  exit;                  LogIt("$fsDir/$FN","renamed $FN to $NEWNAME",trperm_r);
1910          case "Ci" :                  safe_rename($fsDir,$FN,$NEWNAME);
1911                  $F=stripSlashes($F);                  break ;
1912                  // upload && update to $relDir  
1913                  if (!is_writeable($gblFsRoot . $relDir))          case "NOTE" :
1914                          Error("Write access denied",$relDir) ;                  if (! HTTP_POST_VAR("NOTE"))
1915                  $text  = "Use this page to update a single " ;                          Error("Can't add note to object","Can't find var <tt>\$NOTE</tt>",1);
1916                  $text .= "file to <B>$realm</B>." ;                  if (! check_perm("$relDir/$FN", trperm_w))
1917                  StartHTML("(Update file Page)", $text) ;                          Error("Access denied","User <tt>$gblLogin</tt> tried to add note to <tt>$relDir/$FN</tt> without valid trustee.",1);
1918                  UploadPage($gblFsRoot, $relDir, $F) ;  
1919                  EndHTML() ;                  WriteNote("$fsDir/$FN",$NOTE);
1920                  exit ;                  break ;
1921          case "V" :  
1922                  // view          case "UNLOCK" :  
1923                  LogIt("$gblFsRoot/$relDir/$F","viewed");                  if ( $CONFIRM != "on" ) break ;
1924                  Download("$gblFsRoot/$relDir/$F",$gblForceDownload);                  if (! check_perm("$relDir/$FN", trperm_w))
1925                  exit;                          Error("Access denied","User <tt>$gblLogin</tt> tried to unlock <tt>$relDir/$FN</tt> without valid trustee.",1);
1926          case "Ch" :                  Unlock("$fsDir/$FN");
1927                  StartHTML("(File changes)","All changes chronologicaly...");                  break ;
1928                  DisplayChangeLog(0);    // all  
1929                  EndHTML() ;          default :
1930                  exit;                  // user hit "CANCEL" or undefined action
1931          case "Ch1" :          }
1932                  StartHTML("(File changes)","Changes to files in last day...");  
1933                  DisplayChangeLog(1);          // common to all POSTs : redirect to directory view ($relDir)
1934                  EndHTML() ;          if (isset($POSTACTION)) {
1935                  exit;                  $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?D=".urlencode($relDir);
1936          case "I" :                  header("Location: ".$tstr) ;  
1937                  if (! isset($F) || $F == "")                  exit ;
1938                          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);          }
1939                  $inc_file="$fsRealmDir/$realm".$realm_sep.$F.".php";  
1940                  if (! file_exists($inc_file)) {          // check for mode.. navigate, code display, upload, or detail?
1941                          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=U : upload to path given in $D
1942                  }          // $A=E : display detail of file $D/$F and edit
1943                  if (!is_readable($inc_file))          // $A=C : display code in file $D/$F
1944                          Error("Read access to include file denied","Can't read PHP include file <tt>$inc_file</tt>. Fix permissions on it.",1);          // $A=Co : checkout file $D/$F
1945                  $text  = "Your include file should define \$text variable which holds this text and \$title variable which is page title";          // $A=Ci : checkin file $D/$F
1946                  $title = "You should define \$title variable with page title";          // $A=V : view file (do nothing except log)
1947                  include($inc_file);          // $A=I : include file .$F.php from [$gblIncDir|realm]/include_php
1948                  StartHTML($title, $text) ;          // default : display directory $D
1949                  print "<p>".GifIcon(up)." Back to <a href=$HTTP_SERVER_VARS[PHP_SELF]>front page</a>.</p>";  
1950                  EndHTML() ;          if (isset($A)) switch ($A) {
1951                  exit ;          case "U" :
1952          }                  // upload to $relDir
1953                    if (! check_perm($relDir, trperm_w))
1954          // default: display directory $relDir                          Error("Write access denied","You don't have permission to write in <tt>$relDir</tt>");
1955          Navigate($gblFsRoot,$relDir) ;                    if (!is_writeable($gblFsRoot . $relDir))
1956          exit ;                          Error("Write access denied","User <tt>$gblLogin</tt> has permission on <tt>$relDir</tt>, but directory is not writable",1);
1957                    $text  = "Use this page to upload a single " ;
1958          Error("Whooah!","By cartesian logic, this never happens",1) ;                  $text .= "file to <B>$realm</B>." ;
1959  ?>                  StartHTML("(Upload Page)", $text) ;
1960                    UploadPage($gblFsRoot, $relDir) ;
1961                    EndHTML() ;
1962                    exit ;
1963            case "E" :
1964                    // detail of $relDir/$F
1965                    if (is_file("$gblFsRoot/$relDir/$F") || is_dir("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ;
1966                    exit ;
1967            case "C" :
1968                    $F=stripSlashes($F);
1969                    // listing of $relDir/$F
1970                    DisplayCode($gblFsRoot, $relDir, $F) ;
1971                    exit ;
1972            case "Co" :
1973                    // checkout
1974                    Lock("$gblFsRoot/$relDir/$F");
1975                    Download("$gblFsRoot/$relDir/$F",1);
1976                    exit;
1977            case "Ci" :
1978                    $F=stripSlashes($F);
1979                    // upload && update to $relDir
1980                    if (!is_writeable($gblFsRoot . $relDir))
1981                            Error("Write access denied",$relDir) ;
1982                    $text  = "Use this page to update a single " ;
1983                    $text .= "file to <B>$realm</B>." ;
1984                    StartHTML("(Update file Page)", $text) ;
1985                    UploadPage($gblFsRoot, $relDir, $F) ;
1986                    EndHTML() ;
1987                    exit ;
1988            case "V" :
1989                    // view
1990                    LogIt("$gblFsRoot/$relDir/$F","viewed");
1991                    Download("$gblFsRoot/$relDir/$F",$gblForceDownload);
1992                    exit;
1993            case "Ch" :
1994                    StartHTML("(File changes)","All changes chronologicaly...");
1995                    DisplayChangeLog(0);    // all
1996                    EndHTML() ;
1997                    exit;
1998            case "Ch1" :
1999                    StartHTML("(File changes)","Changes to files in last day...");
2000                    DisplayChangeLog(1);
2001                    EndHTML() ;
2002                    exit;
2003            case "I" :
2004                    if (! isset($F) || $F == "")
2005                            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);
2006                    $inc_file="$fsRealmDir/$realm".$realm_sep.$F.".php";
2007                    if (! file_exists($inc_file)) {
2008                            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);
2009                    }
2010                    if (!is_readable($inc_file))
2011                            Error("Read access to include file denied","Can't read PHP include file <tt>$inc_file</tt>. Fix permissions on it.",1);
2012                    $text  = "Your include file should define \$text variable which holds this text and \$title variable which is page title";
2013                    $title = "You should define \$title variable with page title";
2014                    include($inc_file);
2015                    StartHTML($title, $text) ;
2016                    print "<p>".GifIcon(up)." Back to <a href=$HTTP_SERVER_VARS[PHP_SELF]>front page</a>.</p>";
2017                    EndHTML() ;
2018                    exit ;
2019            }
2020    
2021            // default: display directory $relDir
2022            Navigate($gblFsRoot,$relDir) ;  
2023            exit ;
2024    
2025            Error("Whooah!","By cartesian logic, this never happens",1) ;
2026    ?>
2027    

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.39

  ViewVC Help
Powered by ViewVC 1.1.26