/[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

Contents of /docman.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Sat Jul 27 14:45:30 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
Changes since 1.2: +37 -14 lines
Separate administrative messages from user errors, ability to log them
in Apache ErrorLog or e-mail them to ServerAdmin

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

  ViewVC Help
Powered by ViewVC 1.1.26