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

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.41

  ViewVC Help
Powered by ViewVC 1.1.26