/[docman2]/docman.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /docman.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

  ViewVC Help
Powered by ViewVC 1.1.26