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

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

  ViewVC Help
Powered by ViewVC 1.1.26