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

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

  ViewVC Help
Powered by ViewVC 1.1.26