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

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.26