/[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.7 - (show annotations)
Sat Jul 27 19:45:28 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
Changes since 1.6: +6 -5 lines
fix upload

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

  ViewVC Help
Powered by ViewVC 1.1.26