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

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

  ViewVC Help
Powered by ViewVC 1.1.26