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

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.37

  ViewVC Help
Powered by ViewVC 1.1.26