/[docman2]/docman.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /docman.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

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

  ViewVC Help
Powered by ViewVC 1.1.26