/[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.4 - (show annotations)
Sat Jul 27 19:12:42 2002 UTC (21 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.3: +124 -88 lines
changes to run without register_globals in php.ini,
support for downloading of file (with correct contect-type, so that
Word documents open in Word), new $gblIgnoreUnknownFileType option.

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_SERVER_VARS[HTTP_HOST];
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 = $HTTP_SERVER_VARS[PHP_AUTH_USER];
94 $gblPasswd = $HTTP_SERVER_VARS[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,$HTTP_SERVER_VARS;
113
114 $title = $gblTitle." ".$title ;
115 $host = $HTTP_SERVER_VARS["HTTP_HOST"] ;
116 $self = $HTTP_SERVER_VARS["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, $HTTP_SERVER_VARS ;
156 $self = $HTTP_SERVER_VARS["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 $gblIgnoreUnknownFileType,
576 $HTTP_GET_VARS, $html, $realm_config;
577
578 $self = $HTTP_SERVER_VARS["PHP_SELF"] ;
579
580 if ($relDir == "") $relDir = "/";
581
582 $fsDir = $fsRoot.$relDir."/"; // current directory
583
584 if (!is_dir($fsDir)) Error("Dir not found",$relDir) ;
585
586 $hide_items=",$gblHide,";
587
588 // read directory contents
589 if ( !($dir = @opendir($fsDir)) )
590 Error("Read Access denied",$relDir,1) ;
591 while ($item = readdir($dir)) {
592 if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") ) continue ;
593 if (is_dir($fsDir.$item) || is_link ($fsDir.$item)) {
594 if (check_perm($relDir.$item,trperm_b)) {
595 $dirList[$item] = $item ;
596 $dirNote[$item] = ReadNote($fsDir.$item);
597 }
598 } else if (is_file($fsDir.$item)) {
599 if (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 }
605 } else {
606 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);
607 }
608 }
609 closedir($dir) ;
610
611 // scan deleted files
612 if ( $HTTP_GET_VARS["show_deleted"] == 1 && ($dir = @opendir("$fsDir/.del")) ) {
613 while ($item = readdir($dir)) {
614 if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") || !check_perm($relDir.$item,trperm_w) ) continue ;
615 $fileList[$item] = ".del/$item" ;
616 $fileDate[$item] = filemtime($fsDir.".del/$path") ;
617 $fileSize[$item] = filesize($fsDir.".del/$path") ;
618 $fileNote[$item] = ReadNote($fsDir.".del/$item");
619 }
620 closedir($dir) ;
621 }
622
623 $emptyDir = ! (sizeof($dirList) || sizeof($fileList)) ;
624
625 // start navigation page
626 $text = "Use this page to add, delete";
627 if (! isset($HTTP_GET_VARS[show_deleted])) {
628 $text .= ", <a href=$self?D=".urlencode($relDir)."&show_deleted=1>undelete</a>";
629 }
630 $text .= " or revise files on this web site." ;
631 $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>.";
632 StartHTML("(Navigate)",$text) ;
633
634 echo "<TABLE BORDER=0 CELLPADDING=2
635 CELLSPACING=3 WIDTH=\"100%\">" ;
636
637 // updir bar
638 if (chopsl($fsDir) != chopsl($fsRoot)) {
639 $parent = dirname($relDir) ;
640 if ($parent == "") $parent = "/" ;
641
642 include("$html/Navigate-parent.html");
643 }
644
645 function plural($name,$count) {
646 $out="$count $name";
647 if ($count > 1) {
648 $out.="s";
649 }
650 return $out;
651 }
652
653 $dsort = $HTTP_GET_VARS[dsort];
654 if (! isset($dsort)) $dsort = "name"; // default directory sort
655
656 $dsort_arr = array(
657 "name" => array ("rname", "note"),
658 "rname" => array ("name", "note"),
659 "note" => array ("name", "rnote"),
660 "rnote" => array ("name", "note")
661 );
662
663 $fsort = $HTTP_GET_VARS[fsort];
664 if (! isset($fsort)) $fsort = "name"; // default directory sort
665
666 $fsort_arr = array(
667 "name" => array ("rname", "note", "date", "size"),
668 "rname" => array ("name", "note", "date", "size"),
669 "note" => array ("name", "rnote", "date", "size"),
670 "rnote" => array ("name", "note", "date", "size"),
671 "date" => array ("name", "note", "rdate", "size"),
672 "rdate" => array ("name", "note", "date", "size"),
673 "size" => array ("name", "note", "date", "rsize"),
674 "rsize" => array ("name", "note", "date", "size")
675 );
676
677 $D="D=".urlencode($relDir);
678
679 function self_args($arr = array()) {
680 $arg = implode("&",$arr);
681 if ($arg) {
682 return $self."?".$arg;
683 } else {
684 return $self;
685 }
686 }
687 // output subdirs
688 if (sizeof($dirList) > 0) {
689 switch ($dsort) {
690 case "note":
691 $items = $dirNote;
692 asort($items);
693 break;
694 case "rnote":
695 $items = $dirNote;
696 arsort($items);
697 break;
698 case "rname":
699 $items = $dirList;
700 krsort($items);
701 break;
702 default:
703 $items = $dirList;
704 ksort($items);
705 break;
706 }
707 $durl = self_args(array($D,"dsort=".$dsort_arr[$dsort][0]));
708 $nurl = self_args(array($D,"dsort=".$dsort_arr[$dsort][1]));
709
710 include("$html/Navigate-dirHeader.html");
711
712 while (list($key,$dir) = each($items)) {
713
714 $dir = $dirList[$key];
715
716 $info_url=self_args(array("A"=>"A=E", "F"=>"F=".urlencode($dir), "D"=>$D));
717 $dir_url=$self."?D=".urlencode($relDir."/".$dir);
718 include("$html/Navigate-dirEntry.html");
719
720 } // iterate over dirs
721 } // end if no dirs
722
723 $durl = self_args(array($D,"fsort=".$fsort_arr[$fsort][0]));
724 $nurl = self_args(array($D,"fsort=".$fsort_arr[$fsort][1]));
725 $uurl = self_args(array($D,"fsort=".$fsort_arr[$fsort][2]));
726 $surl = self_args(array($D,"fsort=".$fsort_arr[$fsort][3]));
727
728 include("$html/Navigate-fileHeader.html");
729
730 if (sizeof($fileList) > 0) {
731 switch ($fsort) {
732 case "note":
733 $items = $fileNote;
734 asort($items);
735 break;
736 case "rnote":
737 $items = $fileNote;
738 arsort($items);
739 break;
740 case "date":
741 $items = $fileDate;
742 asort($items);
743 break;
744 case "rdate":
745 $items = $fileDate;
746 arsort($items);
747 break;
748 case "size":
749 $items = $fileSize;
750 asort($items);
751 break;
752 case "rsize":
753 $items = $fileSize;
754 arsort($items);
755 break;
756 case "rname":
757 $items = $fileList;
758 krsort($items);
759 break;
760 default:
761 $items = $fileList;
762 ksort($items);
763 break;
764 }
765
766 while (list($key,$file) = each($items)) {
767 $file = $fileList[$key];
768 $path = $fsDir."/".$file ;
769 $mod = $fileDate[$key];
770 $sz = $fileSize[$key];
771
772 if ($sz >= 10240) {
773 $sz = (int)(($sz+1023)/1024) . " k" ;
774 } else {
775 $sz .= " " ;
776 } // end size
777
778 $a = $b = "" ;
779
780 $info_url=$self."?A=E&F=".urlencode($file)."&D=".urlencode($relDir);
781
782 if ( ($mod + $gblModDays*86400) > time() ) {
783 $a = "<SPAN CLASS=RED TITLE=\"Newer" ;
784 $a .= " than $gblModDays days\"> * </SPAN>" ;
785 }
786
787 $file_lock=CheckLock($path);
788
789 $file_url_html="<A HREF=\"$self?A=V&D=".urlencode($relDir)."&F=".urlencode($file);
790 $file_url_html.="\" TITLE=\"View file\">" ;
791
792 if (substr($file,0,5) != ".del/") {
793 $file_url_html .= $file . "</A>" . $a ;
794 } else {
795 $file_url_html .= substr($file,5,strlen($file)-5) . "</a> <SPAN CLASS=RED TITLE=\"deleted\"> <a href=\"$info_url#undelete\">deleted</a> </span>";
796 }
797
798 $note_html="<a href=\"$info_url#note\">".$gblIcon("note")."</a>".ReadNote($path);
799
800 $ext = strtolower(strrchr($file,".")) ;
801
802 if ($file_lock) {
803 if ($file_lock == $GLOBALS[gblUserName]) {
804 $b.="<A HREF=\"$self?A=Ci&D=".urlencode($relDir)."&F=".urlencode($file);
805 $b.="\" TITLE=\"Checkin (update) file on server\">" ;
806 $file_url_html=$b;
807 $b.=$gblIcon("checkin")."</A>" ;
808 $b.= $gblIcon("blank");
809 $file_url_html.="$file</a> $a";
810 $note_html = $gblIcon("blank")."<b>Please check-in (update) this file</b>";
811 } else {
812 $b = $gblIcon("locked");
813 $b.= $gblIcon("blank");
814 $note_html = $gblIcon("blank")."<b>File locked by $file_lock</b>";
815 $file_url_html = "$file $a";
816 }
817 } else {
818 $b.="<A HREF=\"$self?A=Co&D=".urlencode($relDir)."&F=".urlencode($file);
819 $b.="\" TITLE=\"Checkout file for edit\">" ;
820 $b.=$gblIcon("checkout")."</A>";
821
822 if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) {
823 $b.="<A HREF=\"$self?A=C&D=".urlencode($relDir)."&F=".urlencode($file);
824 $b.="\" TITLE=\"List contents\">" ;
825 $b.=$gblIcon("view")."</A>" ;
826 } else {
827 $b.= $gblIcon("blank");
828 }
829 }
830
831 $mod = date("$GLOBALS[gblDateFmt] $GLOBALS[gblTimeFmt]",$mod);
832
833 include("$html/Navigate-fileEntry.html");
834
835 } // iterate over files
836 } else { // end if no files
837 ?>
838 <TR><TD></TD><TD COLSPAN=5 CLASS=LST>
839 No files in this directory
840 </TD></TR>
841 <?
842 }
843
844 if ($emptyDir && $relDir != "") {
845 ?>
846
847 <FORM METHOD="POST" ACTION="<?= $self ?>">
848 <TR><TD></TD><TD COLSPAN=5 CLASS=BAR>
849 <INPUT TYPE="HIDDEN" NAME="DIR" VALUE="<?= $relDir ?>">
850 OK TO DELETE THIS EMPTY FOLDER?
851 <INPUT TYPE="CHECKBOX" NAME="CONFIRM">
852 <INPUT TYPE="SUBMIT" NAME="POSTACTION" VALUE="DELETE">
853 </TD></TR>
854 </FORM>
855
856 <?php
857 } // end if emptyDir
858 ?>
859
860 <TR><TD></TD><TD COLSPAN=5><HR></TD></TR>
861
862 <?
863 if (file_exists(".info.inc")) {
864 print "<TR><TD></TD><TD COLSPAN=5>";
865 include(".info.inc");
866 print "</TD></TR>
867 <TR><TD></TD><TD COLSPAN=5><HR></TD></TR>";
868 }
869 ?>
870
871 <FORM METHOD="POST" ACTION="<?= $self ?>">
872 <TR><TD></TD><TD COLSPAN=5 CLASS=BAR>CREATE NEW
873 <INPUT TYPE="RADIO" NAME="T" VALUE="D" CHECKED>DIRECTORY -OR-
874 <INPUT TYPE="RADIO" NAME="T" VALUE="F">FILE : &nbsp;&nbsp;
875 <NOBR>NAME <INPUT TYPE="TEXT" NAME="FN" SIZE=14>
876 <INPUT TYPE="HIDDEN" NAME="POSTACTION" VALUE="CREATE">
877 <INPUT TYPE="HIDDEN" NAME="DIR" VALUE="<?= $relDir ?>">
878 <INPUT TYPE="SUBMIT" VALUE="CREATE" NAME="CREATE">
879 </NOBR>
880 <NOBR>OR <A HREF="<?= $self ?>?A=U&D=<?= urlencode($relDir) ?>">UPLOAD</A> A FILE</NOBR>
881 </TD></TR>
882 </FORM>
883 </TABLE>
884
885 <?php
886 EndHTML() ;
887 } // end function Navigate
888
889 //////////////////////////////////////////////////////////////////
890
891 function UploadPage($fsRoot, $relDir, $filename="") {
892
893 global $html, $HTTP_SERVER_VARS;
894
895 $self = $HTTP_SERVER_VARS["PHP_SELF"] ;
896 if ($relDir == "") $relDir = "/" ;
897
898 include("$html/UploadPage.html");
899
900 } // end function UploadPage
901
902 //////////////////////////////////////////////////////////////////
903
904 // Error with sysadmin flag are reported to error_log or hidden from
905 // users
906
907 function Error($title,$text="",$sysadmin=0) {
908 global $gblSeparateAdminMessages,
909 $gblMailAdminMessages,$realm,
910 $HTTP_SERVER_VARS;
911 if (! headers_sent()) header("HTTP/1.0 404 Not Found");
912 if ($sysadmin) {
913 if ($gblSeparateAdminMessages) {
914 $user="Your administrator ";
915 if ($gblMailAdminMessages) {
916 mail($HTTP_SERVER_VARS["SERVER_ADMIN"], "docman $realm error message: $title", strip_tags($text));
917 $user.="<tt>".$HTTP_SERVER_VARS["SERVER_ADMIN"]."</tt> ";
918 }
919 $user.="has been notified about error" ;
920 StartHTML("($title)",$user);
921 echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;
922 EndHTML();
923 error_log("docman $realm: ".strip_tags($text));
924 } else {
925 StartHTML("ADMIN: ".$title,$text) ;
926 echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;
927 EndHTML();
928 }
929 } else {
930 StartHTML("(".$title.")",$text) ;
931 echo "<P ALIGN=center>Hit your Browser's Back Button.</P>" ;
932 EndHTML() ;
933 }
934 exit ;
935 } // end function Error
936
937 function LogIt($target,$msg, $changelog=0) {
938
939 $dir=dirname($target);
940 if (! file_exists($dir."/.log")) {
941 if (! @mkdir($dir."/.log",0700)) Error("docman installation problem","can't create log directory <tt>$dir/.log</tt>",1);
942 }
943 $file=basename($target);
944
945 $log=fopen("$dir/.log/$file","a+");
946 fputs($log,date("$GLOBALS[gblDateFmt]\t$GLOBALS[gblTimeFmt]").
947 "\t$GLOBALS[gblUserName]\t$msg\n");
948 fclose($log);
949
950 if (! $changelog) return;
951
952 global $gblFsRoot;
953 $log=fopen("$gblFsRoot/.changelog","a+");
954 if (substr($target,0,strlen($gblFsRoot)) == $gblFsRoot)
955 $target=substr($target,strlen($gblFsRoot),strlen($target)-strlen($gblFsRoot));
956 $msg=str_replace("\t"," ",$msg);
957 fputs($log,time()."\t$target\t$GLOBALS[gblUserName]\t$msg\n");
958 fclose($log);
959
960 // FIX: implement e-mail notification based on $changelog
961 // permission
962 }
963
964
965 //////////////////////////////////////////////////////////////////
966
967 function WriteNote($target,$msg) {
968
969 $target=stripSlashes($target);
970 $dir=dirname($target);
971 if (! file_exists($dir."/.note")) {
972 mkdir($dir."/.note",0700);
973 }
974 $file=basename($target);
975
976 $note=fopen("$dir/.note/$file","w");
977 fputs($note,"$msg\n");
978 fclose($note);
979
980 LogIt($target,"added note $msg");
981
982 }
983
984 function ReadNote($target) {
985
986 $target=stripSlashes($target);
987 $dir=dirname($target);
988 $file=basename($target);
989 $msg="";
990 if (file_exists($dir."/.note/$file")) {
991 $note=fopen("$dir/.note/$file","r");
992 $msg=fgets($note,4096);
993 fclose($note);
994 }
995 return HtmlSpecialChars(StripSlashes($msg));
996
997 }
998
999 //////////////////////////////////////////////////////////////////
1000
1001 function MoveTo($source,$folder) {
1002
1003 $source=stripSlashes($source);
1004 $file=basename($source);
1005 if (! file_exists($folder)) {
1006 mkdir($folder,0700);
1007 }
1008 if (file_exists($source)) {
1009 rename($source,"$folder/$file");
1010 }
1011 }
1012
1013 //////////////////////////////////////////////////////////////////
1014
1015 function Lock($target) {
1016
1017 $target=stripSlashes($target);
1018 $dir=dirname($target);
1019 if (! file_exists($dir."/.lock")) {
1020 mkdir($dir."/.lock",0700);
1021 }
1022 $file=basename($target);
1023
1024 if (file_exists("$dir/.lock/$file")) {
1025 LogIt($target,"attempt to locked allready locked file!");
1026 } else {
1027 $lock=fopen("$dir/.lock/$file","w");
1028 fputs($lock,"$GLOBALS[gblUserName]\n");
1029 fclose($lock);
1030
1031 LogIt($target,"file locked");
1032 }
1033
1034 }
1035
1036 function CheckLock($target) {
1037
1038 $target=stripSlashes($target);
1039 $dir=dirname($target);
1040 $file=basename($target);
1041 $msg=0;
1042 if (file_exists($dir."/.lock/$file")) {
1043 $lock=fopen("$dir/.lock/$file","r");
1044 $msg=fgets($lock,4096);
1045 fclose($lock);
1046 }
1047 return chop($msg);
1048
1049 }
1050
1051 function Unlock($target) {
1052
1053 $target=stripSlashes($target);
1054 $dir=dirname($target);
1055 $file=basename($target);
1056 if (file_exists($dir."/.lock/$file")) {
1057 unlink("$dir/.lock/$file");
1058 LogIt($target,"file unlocked");
1059 } else {
1060 LogIt($target,"attempt to unlocked non-locked file!");
1061 }
1062
1063 }
1064
1065 //////////////////////////////////////////////////////////////////
1066
1067 function urlpath($url) {
1068 $url=urlencode(StripSlashes("$url"));
1069 $url=str_replace("%2F","/",$url);
1070 $url=str_replace("+","%20",$url);
1071 return($url);
1072 }
1073
1074 //////////////////////////////////////////////////////////////////
1075
1076 function safe_rename($fromdir,$fromfile,$tofile) {
1077 function try_rename($from,$to) {
1078 # print "$from -> $to\n";
1079 if (file_exists($from) && is_writeable(dirname($to))) {
1080 rename($from,$to);
1081 }
1082 }
1083
1084 function try_dir($todir) {
1085 if (! file_exists($todir)) {
1086 mkdir($todir,0700);
1087 }
1088 }
1089
1090 $to="$fromdir/$tofile";
1091 $todir=dirname($to);
1092 $tofile=basename($to);
1093
1094 # print "<pre>$fromdir / $fromfile -> $todir / $tofile\n\n";
1095
1096 try_rename("$fromdir/$fromfile","$todir/$tofile");
1097 try_dir("$todir/.log");
1098 try_rename("$fromdir/.log/$fromfile","$todir/.log/$tofile");
1099 try_dir("$todir/.note");
1100 try_rename("$fromdir/.note/$fromfile","$todir/.note/$tofile");
1101 try_dir("$todir/.lock");
1102 try_rename("$fromdir/.lock/$fromfile","$todir/.lock/$tofile");
1103 try_dir("$todir/.bak");
1104 for($i=0;$i<=$GLOBALS[gblNumBackups];$i++) {
1105 try_rename("$fromdir/.bak/$i/$fromfile","$todir/.bak/$i/$tofile");
1106 }
1107 }
1108
1109
1110 //////////////////////////////////////////////////////////////////
1111
1112 // recursivly delete directory
1113
1114 function rrmdir($dir) {
1115 $handle=opendir($dir);
1116 while ($file = readdir($handle)) {
1117 if ($file != "." && $file != "..") {
1118 if (is_dir("$dir/$file"))
1119 rrmdir("$dir/$file");
1120 else
1121 if (! @unlink("$dir/$file")) return(0);
1122 }
1123 }
1124 closedir($handle);
1125 return @rmdir($dir);
1126 }
1127
1128 //////////////////////////////////////////////////////////////////
1129
1130 function DisplayChangeLog($day) {
1131
1132 global $gblFsRoot,$HTTP_SERVER_VARS;
1133
1134 if (!file_exists("$gblFsRoot/.changelog")) return;
1135 $log=fopen("$gblFsRoot/.changelog","r");
1136 $logarr = array();
1137 while($line = fgetcsv($log,512,"\t")) {
1138 while (sizeof($line) > 4) {
1139 $tmp = array_pop($line);
1140 $line.=" $tmp";
1141 }
1142 if ($day!=1 || ($day==1 && (time()-$line[0] < 24*60*60))) {
1143 array_unshift($logarr,array($line[0],$line[1],$line[2],$line[3]));
1144 }
1145 }
1146 fclose($log);
1147 $cl1=" class=LST"; $cl2="";
1148 print "<table border=0 width=100%>\n";
1149 while ($e = array_shift($logarr)) {
1150 $cl=$cl1; $cl1=$cl2; $cl2=$cl;
1151 $date = date("$GLOBALS[gblDateFmt]", $e[0]);
1152 $time = date("$GLOBALS[gblTimeFmt]", $e[0]);
1153 $dir = dirname($e[1]);
1154 $file = basename($e[1]);
1155 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";
1156 }
1157 print "</table>";
1158 print "<p>".GifIcon(up)." Back to <a href=$HTTP_SERVER_VARS[PHP_SELF]>front page</a>.</p>";
1159 }
1160
1161 //////////////////////////////////////////////////////////////////
1162
1163 function Download($path,$force=0) {
1164 global $HTTP_SERVER_VARS,$mime_type;
1165
1166 // default transfer-encoding
1167 $encoding = "binary";
1168
1169 // known transfer encodings
1170 $encoding_ext = array(
1171 "gz" => "x-gzip",
1172 "Z" => "x-compress",
1173 );
1174
1175 $file = basename($path);
1176 $size = filesize($path);
1177
1178 $ext_arr = explode(".",$file);
1179 $ext = array_pop($ext_arr);
1180 if ($encoding_ext[$ext]) {
1181 $encoding = $encoding_ext[$ext];
1182 $ext = array_pop($ext_arr);
1183 }
1184
1185 if ($force || !isset($mime_type[$ext])) {
1186 header("Content-Type: application/force-download");
1187 } else {
1188 header("Content-Type: $mime_type[$ext]");
1189 }
1190
1191 // IE5.5 just downloads index.php if we don't do this
1192 if(preg_match("/MSIE 5.5/", $HTTP_SERVER_VARS[HTTP_USER_AGENT])) {
1193 header("Content-Disposition: filename=$file");
1194 } else {
1195 header("Content-Disposition: attachment; filename=$file");
1196 }
1197
1198 header("Content-Transfer-Encoding: $encoding");
1199 $fh = fopen($path, "r");
1200 fpassthru($fh);
1201 }
1202
1203
1204 //////////////////////////////////////////////////////////////////
1205
1206 function chopsl($path) {
1207 if (substr($path,strlen($path)-1,1) == "/") $path=substr($path,0,strlen($path)-1);
1208 $path=str_replace("//","/",$path);
1209 return $path;
1210 }
1211
1212 //////////////////////////////////////////////////////////////////
1213 /*
1214 Document manager ACL implementation
1215
1216 Written by Dobrica Pavlinusic <dpavlin@rot13.org>
1217
1218 Based on ideas from Linux trustees code
1219 by Vyacheslav Zavadsky <zavadsky@braysystems.com>
1220 */
1221
1222 define(trmask_not,1 << 0);
1223 define(trmask_clear,1 << 1);
1224 define(trmask_deny,1 << 2);
1225 define(trmask_one_level,1 << 3);
1226 define(trmask_group,1 << 4);
1227
1228 define(trperm_r,1 << 5);
1229 define(trperm_w,1 << 6);
1230 define(trperm_b,1 << 7);
1231 define(trperm_n,1 << 8);
1232
1233 $trustee_a2n = array(
1234 '!' => trmask_not,
1235 'C' => trmask_clear,
1236 'D' => trmask_deny,
1237 'O' => trmask_one_level,
1238 '+' => trmask_group,
1239 'R' => trperm_r,
1240 'W' => trperm_w,
1241 'B' => trperm_b,
1242 'N' => trperm_n,
1243 );
1244
1245 // debugging function
1246 function display_trustee($t) {
1247 global $trustee_a2n;
1248 $out="";
1249 foreach ($trustee_a2n as $c=>$v) {
1250 if ($t & $v) $out.=$c;
1251 }
1252 return $out;
1253 }
1254 function display_all_trustee() {
1255 global $trustees;
1256 print "trustee dump:<br>\n";
1257 foreach ($trustees as $path => $tr) {
1258 print "<br><tt>$path</tt>\n";
1259 foreach ($tr as $user=>$perm) {
1260 print "$user == $perm (".display_trustee($perm).")<br>\n";
1261 }
1262 }
1263 }
1264
1265 function init_trustee() {
1266
1267 global $trustee_conf,$trustee_php,$trustee_a2n,$groups,$trustees;
1268
1269 // do we need to re-create compiled trustees?
1270 if (! file_exists($trustee_conf)) {
1271 # $error="$trustee_conf doesn't exits";
1272 return 0; # don't use trustees
1273 } elseif (file_exists($trustee_conf) && !is_readable($trustee_conf)) {
1274 $error="<tt>$trustee_conf</tt> exits, but is not readable";
1275 } elseif (!is_writable(dirname($trustee_php))) {
1276 $error="<tt>".dirname($trustee_php)."</tt> must be writable by web server user";
1277 } elseif (file_exists($trustee_php) && !is_writable($trustee_php)) {
1278 $error="trustees cache file <tt>$trustee_php</tt> exists, but is not writable by web server";
1279 } elseif (1 || filemtime($trustee_conf) >= filemtime($trustee_php)) {
1280 $fp_php=@fopen($trustee_php,"w");
1281 fputs($fp_php,"<?php // don't edit by hand!\n");
1282
1283 $fp_conf=fopen($trustee_conf,"r");
1284
1285 $groups_arr = array();
1286 $perm_arr = array();
1287
1288 $tr_arr = array();
1289
1290 while (! feof($fp_conf)) {
1291 $l = trim(fgets($fp_conf,4096));
1292 if (substr($l,0,1) == "+") { // no comment
1293 $arr=explode(":",$l);
1294 $groups_arr[$arr[0]] = $arr[1] ;
1295 } elseif (substr($l,0,1) != "#") {
1296 $arr=explode(":",$l);
1297 $path=array_shift($arr);
1298 if ($path == "") continue;
1299 $sep2="";
1300 while ($user=array_shift($arr)) {
1301 $perm=0;
1302 if (substr($user,0,1) == "+") {
1303 $perm|=trmask_group;
1304 $user=substr($user,1,strlen($user)-1);
1305 }
1306 $perm_ascii=array_shift($arr);
1307 for ($i=0;$i<strlen($perm_ascii);$i++) {
1308 $ch=strtoupper($perm_ascii[$i]);
1309 if (isset($trustee_a2n[$ch])) {
1310 $perm|=$trustee_a2n[$ch];
1311 } else {
1312 $error.="trustee error in line '$l' [Unknown modifier '$ch']<br>\n";
1313 }
1314 }
1315 $tr_arr[$path][$user] |= $perm;
1316 }
1317 }
1318 }
1319
1320 fclose($fp_conf);
1321
1322 // save trustees
1323 $tr_out='$trustees = array (';
1324 $sep1="";
1325 while (list ($path, $tr) = each ($tr_arr)) {
1326 $tr_out.="$sep1\n\t'$path'=>array(";
1327 $sep2="";
1328 while (list($user,$perm)=each($tr)) {
1329 $tr_out.="$sep2\n\t\t'$user'=>$perm";
1330 $sep2=",";
1331 }
1332 $tr_out.="\n\t)";
1333 $sep1=",";
1334 }
1335 $tr_out.="\n);";
1336
1337 // save groups
1338 $gr_out='$groups = array (';
1339 $sep="";
1340 while (list ($group, $members) = each ($groups_arr)) {
1341 $gr_out.="$sep\n\t'";
1342 $gr_out.=substr($group,1,strlen($group)-1);
1343 $gr_out.="'=>array('".join("','",explode(",",$members))."')";
1344 $sep=",";
1345 }
1346 $gr_out.="\n);\n";
1347
1348 fputs($fp_php,$gr_out);
1349 fputs($fp_php,$tr_out);
1350 fputs($fp_php,"?>\n");
1351 fclose($fp_php);
1352 }
1353
1354 if ($error) {
1355 Error("Trustee error",$error,1);
1356 } else {
1357 include("$trustee_php");
1358 }
1359
1360 return 1;
1361
1362 }//init_trustee
1363
1364 function in_group($user,$group) {
1365 return in_array($groups[$group],$user);
1366 }
1367
1368 // helper function
1369 function unroll_perm($u,$t,$user,$perm) {
1370 // check user
1371 if ($t & trmask_not && ($u==$user)) continue;
1372 if (!($t & trmask_not) && ($u!=$user)) continue;
1373
1374 if ($t & trmask_deny) {
1375 if ($t & trmask_clear) {
1376 $perm[deny] &= ~$t;
1377 } else {
1378 $perm[deny] |= $t;
1379 }
1380 } elseif ($t & trmask_clear) {
1381 $perm[allow] &= ~$t;
1382 } else {
1383 $perm[allow] |= $t;
1384 }
1385 return $perm;
1386 }// end of helper function
1387
1388 function check_trustee($user,$path) {
1389 global $trustees;
1390 $perm[allow] = 0;
1391 $perm[deny] = 0;
1392 $path_arr=explode("/",$path);
1393 $path = "/";
1394 while (count($path_arr)) {
1395 if (substr($path,strlen($path)-1,1) != "/") $path.="/";
1396 $path.=array_shift($path_arr);
1397 $tr = $trustees[$path];
1398
1399 if (isset($tr)) {
1400 // first apply trustee for all
1401 if (isset($tr['*'])) {
1402 $perm = unroll_perm($user,$tr['*'],$user, $perm);
1403 unset($tr['*']);
1404 }
1405 // then apply group policies
1406 foreach ($tr as $u=>$t) {
1407 if ($t & trmask_group && in_group($user,$u)) {
1408 // resolv user
1409 $t = $t & ~trmask_group;
1410 $u = $user;
1411 $perm = unroll_perm($u,$t,$user, $perm);
1412 unset($tr[$u]);
1413 }
1414 }
1415 // then apply use policy
1416 if (isset($tr[$user])) {
1417 $perm = unroll_perm($user,$tr[$user],$user, $perm);
1418 unset($tr[$user]);
1419 }
1420
1421 }
1422
1423 }
1424 #print "<br>user: $user path: $path perm: ";
1425 #print "d: $perm[deny] (".display_trustee($perm[deny]).") a: $perm[allow] (".display_trustee($perm[allow]).")<Br>\n";
1426 return $perm;
1427 }
1428
1429 // handy functions
1430
1431 function check_perm($path,$trperm) {
1432 global $gblLogin,$HAVE_TRUSTEE;
1433 print "<br>check_perm: <tt>$path</tt> test perm ".display_trustee($perm)."<br>\n";
1434 $return = ! $HAVE_TRUSTEE;
1435 if ($HAVE_TRUSTEE) {
1436 $perm = check_trustee($gblLogin,$path);
1437 print " d: $perm[deny] (".display_trustee($perm[deny]).") a: $perm[allow] (".display_trustee($perm[allow]).") perm: $trperm";
1438 if ($perm[deny] & $trperm) $return=0;
1439 elseif ($perm[allow] & $trperm) $return=1;
1440 }
1441 print " return: $return<br>\n";
1442 return($return);
1443 }
1444
1445 //////////////////////////////////////////////////////////////////
1446
1447 function readMime() {
1448 global $mime_type;
1449
1450 if (! isset($gblMimeTypes)) {
1451 $gblMimeTypes = "/etc/mime.types";
1452 }
1453
1454 $mime = @fopen($gblMimeTypes,"r");
1455
1456 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>");
1457
1458 while($line = fgets($mime,80)) {
1459 if (substr($line,0,1) == "#") continue; // skip comment
1460 $arr = preg_split("/[\s\t]+/",$line);
1461 $type = array_shift($arr);
1462 while ($ext = array_shift($arr)) {
1463 $mime_type[$ext] = $type;
1464 }
1465 }
1466
1467 fclose($mime);
1468 }
1469
1470 //////////////////////////////////////////////////////////////////
1471 // MAIN PROGRAM
1472
1473 $gblFilePerms = 0640 ; // default for new files
1474 $gblDirPerms = 0750 ; // default for new dirs
1475
1476 if (isset($STYLE) && $STYLE == "get") {
1477 include("$html/docman.css");
1478 exit;
1479 }
1480
1481 // location of master docman configuration file
1482 $docman_conf = "/etc/docman.conf";
1483 if (! file_exists($docman_conf)) {
1484 $error = "Can't find master configuration file $docman_conf. See docman2/doc/upgrade.html#docman_conf for more informations";
1485
1486 error_log("docman: $error");
1487 Error("docman not installed completly",$error);
1488 }
1489 include($docman_conf);
1490
1491 if (! isset($fsRealmDir)) {
1492 $fsRealmDir = "$gblIncDir/realm";
1493 }
1494 $realm_config = "$fsRealmDir/$realm.conf";
1495
1496 // read user-defined configuration
1497 if (file_exists($realm_config)) {
1498 include($realm_config);
1499 } else {
1500 Error("Configuration error","Can't find configuration file at <tt>$realm_config</tt> !");
1501 }
1502
1503 if (! isset($gblRepositoryDir)) Error("Configuration error","<tt>\$gblRepositoryDir</tt> is not setuped in realm configuration file <tt>$realm_config</tt>");
1504
1505 // where do we get users from?
1506 if (file_exists("$gblIncDir/htusers/$gblUsers.php")) {
1507 include("$gblIncDir/htusers/$gblUsers.php");
1508 } else {
1509 Error("Configuration error","Can't find user handling module at <tt>$gblIncDir/htusers/$gblUsers.php</tt> ! Please fix <tt>$realm_config</tt>");
1510 }
1511
1512 // if no password, or empty password logout
1513 if (
1514 isset($gblLogin) && (
1515 !isset($relogin) || (
1516 isset($relogin) && $relogin != md5($gblLogin.$gblPasswd)
1517 )
1518 ) && (
1519 $gblPasswd == "" || !isset($gblPasswd)
1520 ) && !isset($force_login) && $gblLogin != "anonymous"
1521 ) {
1522 StartHTML("Logout completed","Your login credentials has been erased") ;
1523 EndHTML() ;
1524 exit ;
1525 }
1526
1527 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);
1528
1529 // trustee (ACL) file configuration
1530 $trustee_conf="$gblIncDir/realm/$realm.trustee";
1531 // compiled version of trustee file
1532 $trustee_php="$gblRepositoryDir/.trustee.php";
1533 // get ACL informations
1534 $HAVE_TRUSTEE = init_trustee();
1535
1536 if (strtolower($gblLogin) == "anonymous" || !isset($gblPasswd)) {
1537 $perm = check_trustee($gblLogin,$path);
1538 // browsing must be explicitly allowed for root directory
1539 // of repository for anonymous user to work!
1540 if ($perm[allow] & trperm_b) {
1541 $secHash = md5($gblLogin.$gblPasswd);
1542 $gblUserName = "Anonymous user";
1543 }
1544 }
1545
1546 // authentication failure
1547 if ( md5($gblLogin.$gblPasswd) != $secHash ||
1548 isset($relogin) && $secHash == $relogin) {
1549 header("WWW-authenticate: basic realm=\"$realm\"") ;
1550 header("HTTP/1.0 401 Unauthorized") ;
1551 phpinfo();
1552 Error("401 Unauthorized","No trespassing !");
1553 }
1554
1555 // read mime.types
1556 readMime();
1557
1558 // get current directory relative to $gblFsRoot
1559 $relDir = $DIR ; // from POST
1560 if ($relDir == "") { // not defined in POST ?
1561 $relDir = urldecode($D) ; // then use GET
1562 }
1563
1564 $relDir=stripSlashes($relDir);
1565
1566 if ($relDir == "/") $relDir = "" ;
1567 // default : website root = ""
1568
1569 if (strstr($relDir,"..")) Error("No updirs allowed");
1570
1571 // full paths contain "fs" or "Fs". Paths realitve to root of
1572 // website contain "rel" or "Rel". The script won't let you
1573 // edit anything above directory equal to http://server.com
1574 // i.e. below $gblFsRoot.
1575
1576 $relScriptDir = dirname($SCRIPT_NAME) ;
1577 // i.e. /docman
1578
1579 // start on server root
1580 $gblFsRoot = $gblRepositoryDir;
1581 // i.e. /home/httpd/html
1582
1583 $fsDir = $gblFsRoot . $relDir ; // current directory
1584 if ( !is_dir($fsDir) ) Error("Dir not found",$relDir) ;
1585
1586 if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") {
1587 $webRoot = "https://";
1588 } else {
1589 $webRoot = "http://";
1590 }
1591 $webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir;
1592
1593 // take variables from server
1594 $FN=stripSlashes($HTTP_POST_VARS["FN"]);
1595 $DIR=stripSlashes($HTTP_POST_VARS["DIR"]);
1596 $RELPATH=stripSlashes($HTTP_POST_VARS["RELPATH"]);
1597 $T=stripSlashes($HTTP_POST_VARS["T"]);
1598
1599 // get
1600 $A=stripSlashes($HTTP_GET_VARS["A"]);
1601 $D=stripSlashes($HTTP_GET_VARS["D"]);
1602
1603 // if (isset($F)) Error("Document manager system error","variable $F shouldn't be set here (re-check old code)",1);
1604 // $F=stripSlashes($HTTP_SERVER_VARS["PATH_INFO"]);
1605 $F=stripSlashes($HTTP_GET_VARS["F"]);
1606
1607 switch ($HTTP_POST_VARS["POSTACTION"]) {
1608 case "UPLOAD" :
1609 $FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]);
1610 if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;
1611 if (strstr($FN_name,"/"))
1612 Error("Non-conforming filename") ;
1613 // TODO : should rather check for escapeshellcmds
1614 // but maybe RFC 18xx asserts safe filenames ....
1615 $source = $FN_name ;
1616 print "-- $source -- $FN_name -- $HTTP_POST_FILES[FN] -- ";
1617 // phpinfo();
1618 if (! file_exists($source)) {
1619 Error("You must select file with browse to upload it!");
1620 }
1621 $FILENAME = $HTTP_POST_VARS["FILENAME"];
1622 if (! isset($FILENAME)) { // from update file
1623 $target = "$fsDir/$FN_name" ;
1624 } else {
1625 $target = "$fsDir/$FILENAME";
1626 }
1627
1628 // backup old files first
1629 $dir=dirname($target);
1630 if (! file_exists($dir."/.bak")) {
1631 mkdir($dir."/.bak",0700);
1632 }
1633 if (! file_exists($dir."/.bak/$GLOBALS[gblNumBackups]")) {
1634 mkdir($dir."/.bak/$GLOBALS[gblNumBackups]",0700);
1635 }
1636 $file=basename($target);
1637 for($i=$GLOBALS[gblNumBackups]-1;$i>0;$i--) {
1638 MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/");
1639 }
1640 MoveTo($target,$dir."/.bak/1/");
1641
1642 copy($source,$target) ;
1643 chmod($target,$gblFilePerms) ;
1644 clearstatcache() ;
1645 if (isset($FILENAME)) {
1646 LogIt($target,"check-in",trperm_r | trperm_w);
1647 Unlock($target);
1648 } else {
1649 LogIt($target,"uploaded",trperm_r | trperm_w);
1650 }
1651 break ;
1652
1653 case "SAVE" :
1654 $path = $gblFsRoot . $RELPATH ;
1655 $path=stripSlashes($path);
1656 $writable = is_writeable($path) ;
1657 $legaldir = is_writeable(dirname($path)) ;
1658 $exists = (file_exists($path)) ? 1 : 0 ;
1659 // check for legal extension here as well
1660 if (!($writable || (!$exists && $legaldir)))
1661 Error("Write denied",$RELPATH) ;
1662 $fh = fopen($path, "w") ;
1663 $FILEDATA=stripSlashes($FILEDATA);
1664 fwrite($fh,$FILEDATA) ;
1665 fclose($fh) ;
1666 clearstatcache() ;
1667 LogIt($path,"saved changes",trperm_r);
1668 break ;
1669
1670 case "CREATE" :
1671 // we know $fsDir exists
1672 if ($FN == "") break; // no filename!
1673 if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;
1674 $path = $fsDir . "/" . $FN ; // file or dir to create
1675 $relPath = $relDir . "/" . $FN ;
1676 switch ( $T ) {
1677 case "D" : // create a directory
1678 if ( ! @mkdir($path,$gblDirPerms) )
1679 Error("Mkdir failed",$relPath) ; // eg. if it exists
1680 else
1681 LogIt($path."/","dir created",trperm_w);
1682 clearstatcache() ;
1683 break ;
1684 case "F" : // create a new file
1685 // this functionality is doubled in DetailView().
1686 // better keep it here altogether
1687 // chmod perms to $gblFilePerms
1688 if ( file_exists($path) && !is_writeable($path) )
1689 Error("File not writable", $relPath) ;
1690 $fh = fopen($path, "w+") ;
1691 if ($fh) {
1692 fputs($fh,"\n");
1693 fclose($fh) ;
1694 LogIt($path,"file created",trperm_r | trperm_w);
1695 } else {
1696 Error("Creation of file $relPath failed -- $path");
1697 }
1698 $tstr = "$PHP_SELF?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;
1699 header("Location: " . $tstr) ;
1700 exit ;
1701 }
1702 break ;
1703
1704 case "DELETE" :
1705 if ( $CONFIRM != "on" ) break ;
1706
1707 $tstr = "Attempt to delete non-existing object or " ;
1708 $tstr .= "insufficient privileges: " ;
1709
1710 if ( $FN != "") { // delete file
1711 $path = $fsDir . "/" . $FN ;
1712
1713 $dir=dirname($path);
1714 $file=basename($path);
1715 if (! file_exists("$dir/.del")) {
1716 mkdir("$dir/.del",0700);
1717 }
1718
1719 // if ( ! @unlink($path) ) {
1720 if ( ! rename($path,"$dir/.del/$file") ) {
1721 LogIt($path,"file delete failed");
1722 Error("File delete failed", $tstr . $path) ;
1723 } else {
1724 LogIt($path,"file deleted",trperm_w);
1725 MoveTo("$dir/.log/$file","$dir/.del/.log/");
1726 MoveTo("$dir/.note/$file","$dir/.del/.note/");
1727 MoveTo("$dir/.lock/$file","$dir/.del/.lock/");
1728 }
1729 } else { // delete directory
1730 if ( ! @rrmdir($fsDir) ) {
1731 Error("Rmdir failed", $tstr . $fsDir) ;
1732 } else {
1733 LogIt($path,"dir deleted",trperm_w);
1734 $relDir = dirname($relDir) ; // move up
1735 }
1736 }
1737 break ;
1738
1739 case "UNDELETE" :
1740 if ( $CONFIRM != "on" ) break ;
1741
1742 if (substr($FN,0,4) != ".del") break ;
1743 $file=substr($FN,4,strlen($FN)-4);
1744
1745 LogIt("$fsDir/.del/$file","undeleted",trperm_w);
1746 MoveTo("$fsDir/.del/$file","$fsDir/");
1747 MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/");
1748 MoveTo("$fsDir/.del/.note/$file","$fsDir/.note/");
1749 MoveTo("$fsDir/.del/.lock/$file","$fsDir/.lock/");
1750
1751 break ;
1752
1753 case "RENAME" :
1754 if ( $CONFIRM != "on" ) break ;
1755
1756 LogIt("$fsDir/$FN","renamed $FN to $NEWNAME",trperm_r);
1757 safe_rename($fsDir,$FN,$NEWNAME);
1758 break ;
1759
1760 case "NOTE" :
1761 WriteNote("$fsDir/$FN","$NOTE");
1762 break ;
1763
1764 case "UNLOCK" :
1765 if ( $CONFIRM != "on" ) break ;
1766 Unlock("$fsDir/$FN");
1767 break ;
1768
1769 default :
1770 // user hit "CANCEL" or undefined action
1771 }
1772
1773 // common to all POSTs : redirect to directory view ($relDir)
1774 if ( $POSTACTION != "" ) {
1775 $tstr = $PHP_SELF . "?D=" . urlencode($relDir) ;
1776 header("Location: " . $tstr) ;
1777 exit ;
1778 }
1779
1780 // check for mode.. navigate, code display, upload, or detail?
1781 // $A=U : upload to path given in $D
1782 // $A=E : display detail of file $D/$F and edit
1783 // $A=C : display code in file $D/$F
1784 // $A=Co : checkout file $D/$F
1785 // $A=Ci : checkin file $D/$F
1786 // $A=V : view file (do nothing except log)
1787 // $A=I : include file .$F.php from $gblFsRoot
1788 // default : display directory $D
1789
1790 switch ($A) {
1791 case "U" :
1792 // upload to $relDir
1793 if (!is_writeable($gblFsRoot . $relDir))
1794 Error("Write access denied",$relDir) ;
1795 $text = "Use this page to upload a single " ;
1796 $text .= "file to <B>$realm</B>." ;
1797 StartHTML("(Upload Page)", $text) ;
1798 UploadPage($gblFsRoot, $relDir) ;
1799 EndHTML() ;
1800 exit ;
1801 case "E" :
1802 // detail of $relDir/$F
1803 if (is_file("$gblFsRoot/$relDir/$F") || is_dir("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ;
1804 exit ;
1805 case "C" :
1806 $F=stripSlashes($F);
1807 // listing of $relDir/$F
1808 DisplayCode($gblFsRoot, $relDir, $F) ;
1809 exit ;
1810 case "Co" :
1811 // checkout
1812 Lock("$gblFsRoot/$relDir/$F");
1813 Download("$gblFsRoot/$relDir/$F");
1814 exit;
1815 case "Ci" :
1816 $F=stripSlashes($F);
1817 // upload && update to $relDir
1818 if (!is_writeable($gblFsRoot . $relDir))
1819 Error("Write access denied",$relDir) ;
1820 $text = "Use this page to update a single " ;
1821 $text .= "file to <B>$realm</B>." ;
1822 StartHTML("(Update file Page)", $text) ;
1823 UploadPage($gblFsRoot, $relDir, $F) ;
1824 EndHTML() ;
1825 exit ;
1826 case "V" :
1827 // view
1828 LogIt("$gblFsRoot/$relDir/$F","viewed");
1829 if ($gblForceDownload) {
1830 Download("$gblFsRoot/$relDir/$F");
1831 } else {
1832 header("Content-Disposition: attachment; filename=$F" );
1833 Header("Location: $webRoot".urlpath("$relDir/$F"));
1834 }
1835 exit;
1836 case "Ch" :
1837 StartHTML("(File changes)","All changes chronologicaly...");
1838 DisplayChangeLog(0); // all
1839 EndHTML() ;
1840 exit;
1841 case "Ch1" :
1842 StartHTML("(File changes)","Changes to files in last day...");
1843 DisplayChangeLog(1);
1844 EndHTML() ;
1845 exit;
1846 case "I" :
1847 $F=stripSlashes($F);
1848 $inc_file="${gblFsRoot}/.${F}.php";
1849 if (!isset($F) || $F == "" || !file_exists($inc_file)) Error("Fatal error $inc_file"); // can't find file to include
1850 if (!is_readable($inc_file))
1851 Error("Read access to include file denied",".${F}.php");
1852 $text = "Your include file should define \$text variable which holds this text and \$title variable which is page title";
1853 $title = "You should define \$title variable with page title";
1854 include($inc_file);
1855 StartHTML($title, $text) ;
1856 print "<p>".GifIcon(up)." Back to <a href=$HTTP_SERVER_VARS[PHP_SELF]>front page</a>.</p>";
1857 EndHTML() ;
1858 exit ;
1859 }
1860
1861 // default: display directory $relDir
1862 Navigate($gblFsRoot,$relDir) ;
1863 exit ;
1864
1865 Error("Whooah!","By cartesian logic, this never happens",1) ;
1866 ?>

  ViewVC Help
Powered by ViewVC 1.1.26