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

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

  ViewVC Help
Powered by ViewVC 1.1.26