/[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.12 - (show annotations)
Sat Jul 27 22:26:30 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
Changes since 1.11: +21 -6 lines
support for docman realm according to script path

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

  ViewVC Help
Powered by ViewVC 1.1.26