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

Diff of /docman.php

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.26