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

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

  ViewVC Help
Powered by ViewVC 1.1.26