--- docman.php 2002/07/27 14:45:30 1.3 +++ docman.php 2002/07/27 20:56:52 1.11 @@ -25,8 +25,8 @@ // from where to include auth_*.php modules? $gblIncDir = "/data/docman2"; - // force download (so it won't open in associated application) - $gblForceDownload = 1; + // force download on view (so it won't open in associated application) + $gblForceDownload = 0; // date format $gblDateFmt="Y-m-d"; @@ -84,14 +84,14 @@ LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]); // for security and configuration - $realm="$HTTP_HOST"; // FIX + $realm=$HTTP_SERVER_VARS[HTTP_HOST]; $fsDocumentRoot = dirname($HTTP_SERVER_VARS[SCRIPT_FILENAME]); if ($fsDocumentRoot == "") Error("Configuration error","Can't get SCRIPT_FILENAME from your web server. Please set \$fsDocumentRoot in \$",1); // globals for later - $gblLogin = $PHP_AUTH_USER; - $gblPasswd = $PHP_AUTH_PW; + $gblLogin = $HTTP_SERVER_VARS[PHP_AUTH_USER]; + $gblPasswd = $HTTP_SERVER_VARS[PHP_AUTH_PW]; ////////////////////////////////////////////////////////////////// @@ -109,16 +109,16 @@ function StartHTML($title,$text="") { - global $html,$fsDocumentRoot; + global $html,$fsDocumentRoot,$HTTP_SERVER_VARS; $title = $gblTitle." ".$title ; - $host = $GLOBALS["HTTP_HOST"] ; - $self = $GLOBALS["PHP_SELF"] ; + $host = $HTTP_SERVER_VARS["HTTP_HOST"] ; + $self = $HTTP_SERVER_VARS["PHP_SELF"] ; if (file_exists("$fsDocumentRoot/docman.css")) { $css=dirname($self)."/docman.css"; } else { - $css=$self."?STYLE=get&css=$css"; + $css=$self."?STYLE=get"; } include("$html/head.html"); @@ -145,15 +145,14 @@ $url_title="relogin"; } include("$html/footer.html"); - //phpinfo(); } // end function EndHTML ////////////////////////////////////////////////////////////////// function DetailPage($fsRoot,$relDir,$fn) { - global $gblEditable, $gblImages, $webRoot, $html ; - $self = $GLOBALS["PHP_SELF"] ; + global $gblEditable, $gblImages, $webRoot, $html, $HTTP_SERVER_VARS ; + $self = $HTTP_SERVER_VARS["PHP_SELF"] ; $relPath = $relDir . "/" . $fn ; $fsPath = $fsRoot . $relPath ; @@ -572,9 +571,10 @@ function Navigate($fsRoot,$relDir) { global $gblEditable, $gblIcon, $gblModDays, $webRoot, $gblHide, - $HTTP_GET_VARS, $html; + $gblIgnoreUnknownFileType, $gblRepositoryDir, + $HTTP_GET_VARS, $html, $realm_config; - $self = $GLOBALS["PHP_SELF"] ; + $self = $HTTP_SERVER_VARS["PHP_SELF"] ; if ($relDir == "") $relDir = "/"; @@ -584,34 +584,31 @@ $hide_items=",$gblHide,"; -#display_all_trustee(); -print "-- $fsDir --"; - // read directory contents if ( !($dir = @opendir($fsDir)) ) - Error("Read Access denied",$relDir) ; + Error("Read Access denied",$relDir,1) ; while ($item = readdir($dir)) { -print "$item, "; if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") ) continue ; - if ((is_dir($fsDir.$item) || is_link ($fsDir.$item)) && check_perm($relDir.$item,trperm_b)) { - $dirList[$item] = $item ; - $dirNote[$item] = ReadNote($fsDir.$item); - } else if ( is_file($fsDir.$item) && check_perm($relDir.$item,trperm_r) ) { - $fileList[$item] = $item ; - $fileDate[$item] = filemtime($fsDir.$item) ; - $fileSize[$item] = filesize($fsDir.$item) ; - $fileNote[$item] = ReadNote($fsDir.$item); + if (is_dir($fsDir.$item) || is_link ($fsDir.$item)) { + if (check_perm($relDir.$item,trperm_b)) { + $dirList[$item] = $item ; + $dirNote[$item] = ReadNote($fsDir.$item); + } + } else if (is_file($fsDir.$item)) { + if (check_perm($relDir.$item,trperm_r)) { + $fileList[$item] = $item ; + $fileDate[$item] = filemtime($fsDir.$item) ; + $fileSize[$item] = filesize($fsDir.$item) ; + $fileNote[$item] = ReadNote($fsDir.$item); + } } else { - // unknown file type - // $text = "Could not determine file type of " ; - // Error("File Error", $text.$relDir."/".$item) ; - // exit ; + if (! $gblIgnoreUnknownFileType) Error("File Type Error", "Item ".$fsDir.$item." is not file, directory or link. If you want to ignore errors like this, set \$gblIgnoreUnknownFileType = 1 in $realm_config.",1); } } closedir($dir) ; // scan deleted files - if ( $GLOBALS[show_deleted] == 1 && ($dir = @opendir("$fsDir/.del")) ) { + if ( $HTTP_GET_VARS["show_deleted"] == 1 && ($dir = @opendir("$fsDir/.del")) ) { while ($item = readdir($dir)) { if ( substr($item,0,1) == "." || strstr($hide_items,",$item,") || !check_perm($relDir.$item,trperm_w) ) continue ; $fileList[$item] = ".del/$item" ; @@ -626,7 +623,7 @@ // start navigation page $text = "Use this page to add, delete"; - if (! isset($show_deleted)) { + if (! isset($HTTP_GET_VARS[show_deleted])) { $text .= ", undelete"; } $text .= " or revise files on this web site." ; @@ -788,8 +785,7 @@ $file_lock=CheckLock($path); - $file_url_html="" ; if (substr($file,0,5) != ".del/") { @@ -804,8 +800,7 @@ if ($file_lock) { if ($file_lock == $GLOBALS[gblUserName]) { - $b.="" ; $file_url_html=$b; $b.=$gblIcon("checkin")."" ; @@ -819,14 +814,12 @@ $file_url_html = "$file $a"; } } else { - $b.="" ; - $b.=$gblIcon("checkout")."" ; + $b.=$gblIcon("checkout").""; if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) { - $b.="" ; $b.=$gblIcon("view")."" ; } else { @@ -866,12 +859,13 @@
"; - include(".info.inc"); - print " -
"; + include("$gblRepositoryDir/.info.inc"); + print "
"; } + ?>
@@ -896,36 +890,13 @@ function UploadPage($fsRoot, $relDir, $filename="") { - $self = $GLOBALS["PHP_SELF"] ; + global $html, $HTTP_SERVER_VARS; + + $self = $HTTP_SERVER_VARS["PHP_SELF"] ; if ($relDir == "") $relDir = "/" ; -?> -

-
- -DESTINATION DIRECTORY: - -
DESTINATION FILE: - - -

PATHNAME OF LOCAL FILE
- - -

-

-

If the [BROWSE...] button is not displayed,
-you must upgrade to an RFC1867-compliant browser.

-

Your browser:

- -
-
-
- -
-

+ include("$html/UploadPage.html"); - $to\n"; if (file_exists($from) && is_writeable(dirname($to))) { - rename($from,$to); + return rename($from,$to); + } else { + return 0; } } function try_dir($todir) { if (! file_exists($todir)) { - mkdir($todir,0700); + @mkdir($todir,0700); } } @@ -1121,7 +1095,7 @@ # print "
$fromdir / $fromfile -> $todir / $tofile\n\n";
 
-	try_rename("$fromdir/$fromfile","$todir/$tofile");
+	if (! try_rename("$fromdir/$fromfile","$todir/$tofile")) Error("Rename error","Can't rename file $fromfile to $tofile",1);
 	try_dir("$todir/.log");
 	try_rename("$fromdir/.log/$fromfile","$todir/.log/$tofile");
 	try_dir("$todir/.note");
@@ -1157,7 +1131,8 @@
 
 function DisplayChangeLog($day) {
 
-	global $gblFsRoot;
+	global $gblFsRoot,$HTTP_SERVER_VARS;
+
 	if (!file_exists("$gblFsRoot/.changelog")) return;
 	$log=fopen("$gblFsRoot/.changelog","r");
 	$logarr = array();
@@ -1179,28 +1154,50 @@
 		$time = date("$GLOBALS[gblTimeFmt]", $e[0]);
 		$dir = dirname($e[1]);
 		$file = basename($e[1]);
-		print "$date$time$dir/$file$e[2]$e[3]\n";
+		print "$date$time$dir/$file$e[2]$e[3]\n";
 	}
 	print "";
-	print "

".GifIcon(up)." Back to front page.

"; + print "

".GifIcon(up)." Back to front page.

"; } ////////////////////////////////////////////////////////////////// -function Download($path) { - global $HTTP_USER_AGENT; - $file=basename($path); +function Download($path,$force=0) { + global $HTTP_SERVER_VARS,$mime_type; + + // default transfer-encoding + $encoding = "binary"; + + // known transfer encodings + $encoding_ext = array( + "gz" => "x-gzip", + "Z" => "x-compress", + ); + + $file = basename($path); $size = filesize($path); - //header("Content-Type: application/octet-stream"); - header("Content-Type: application/force-download"); - header("Content-Length: $size"); + + $ext_arr = explode(".",$file); + $ext = array_pop($ext_arr); + if ($encoding_ext[$ext]) { + $encoding = $encoding_ext[$ext]; + $ext = array_pop($ext_arr); + } + + if ($force || !isset($mime_type[$ext])) { + header("Content-Type: application/force-download"); + } else { + header("Content-Type: $mime_type[$ext]"); + } + // IE5.5 just downloads index.php if we don't do this - if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) { + if(preg_match("/MSIE 5.5/", $HTTP_SERVER_VARS[HTTP_USER_AGENT])) { header("Content-Disposition: filename=$file"); } else { header("Content-Disposition: attachment; filename=$file"); } - header("Content-Transfer-Encoding: binary"); + + header("Content-Transfer-Encoding: $encoding"); $fh = fopen($path, "r"); fpassthru($fh); } @@ -1447,6 +1444,30 @@ return($return); } +////////////////////////////////////////////////////////////////// + +function readMime() { + global $mime_type; + + if (! isset($gblMimeTypes)) { + $gblMimeTypes = "/etc/mime.types"; + } + + $mime = @fopen($gblMimeTypes,"r"); + + if (! $mime) Error("Can't read MIME types","$gblMimeTypes file not found. You can setup other mime.types file using \$gblMimeTypes in $realm_config"); + + while($line = fgets($mime,80)) { + if (substr($line,0,1) == "#") continue; // skip comment + $arr = preg_split("/[\s\t]+/",$line); + $type = array_shift($arr); + while ($ext = array_shift($arr)) { + $mime_type[$ext] = $type; + } + } + + fclose($mime); +} ////////////////////////////////////////////////////////////////// // MAIN PROGRAM @@ -1454,6 +1475,7 @@ $gblFilePerms = 0640 ; // default for new files $gblDirPerms = 0750 ; // default for new dirs + $STYLE = $HTTP_GET_VARS[STYLE]; if (isset($STYLE) && $STYLE == "get") { include("$html/docman.css"); exit; @@ -1530,9 +1552,11 @@ header("WWW-authenticate: basic realm=\"$realm\"") ; header("HTTP/1.0 401 Unauthorized") ; Error("401 Unauthorized","No trespassing !"); - exit ; } + // read mime.types + readMime(); + // get current directory relative to $gblFsRoot $relDir = $DIR ; // from POST if ($relDir == "") { // not defined in POST ? @@ -1561,28 +1585,44 @@ $fsDir = $gblFsRoot . $relDir ; // current directory if ( !is_dir($fsDir) ) Error("Dir not found",$relDir) ; - if (isset($GLOBALS["HTTPS"]) && $GLOBALS["HTTPS"] == "on") { + if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") { $webRoot = "https://"; } else { $webRoot = "http://"; } - $webRoot .= $GLOBALS["HTTP_HOST"] . $relScriptDir; + $webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir; - $FN=stripSlashes($FN); + // take variables from server + $FN=stripSlashes($HTTP_POST_VARS["FN"]); + $DIR=stripSlashes($HTTP_POST_VARS["DIR"]); + $RELPATH=stripSlashes($HTTP_POST_VARS["RELPATH"]); + $T=stripSlashes($HTTP_POST_VARS["T"]); + $CONFIRM=stripSlashes($HTTP_POST_VARS["CONFIRM"]); + + // get + $A=stripSlashes($HTTP_GET_VARS["A"]); + $D=stripSlashes($HTTP_GET_VARS["D"]); + +// if (isset($F)) Error("Document manager system error","variable $F shouldn't be set here (re-check old code)",1); +// $F=stripSlashes($HTTP_SERVER_VARS["PATH_INFO"]); + $F=stripSlashes($HTTP_GET_VARS["F"]); - switch ($POSTACTION) { + switch ($HTTP_POST_VARS["POSTACTION"]) { case "UPLOAD" : + $FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]); + $FN=stripSlashes($HTTP_POST_FILES["FN"]["name"]); if (!is_writeable($fsDir)) Error("Write denied",$relDir) ; - if (strstr($FN_name,"/")) - Error("Non-conforming filename") ; - // TODO : should rather check for escapeshellcmds - // but maybe RFC 18xx asserts safe filenames .... - $source = $FN ; + + $source = $FN_name ; if (! file_exists($source)) { Error("You must select file with browse to upload it!"); } + + $FILENAME = $HTTP_POST_VARS["FILENAME"]; + if (strstr($FILENAME,"/")) + Error("Upload error","Non-conforming filename. Filename $FILENAME has slashes (/) in it.") ; if (! isset($FILENAME)) { // from update file - $target = "$fsDir/$FN_name" ; + $target = "$fsDir/".basename($FN); } else { $target = "$fsDir/$FILENAME"; } @@ -1680,9 +1720,8 @@ // if ( ! @unlink($path) ) { if ( ! rename($path,"$dir/.del/$file") ) { - Error("File delete failed", $tstr . $path) ; LogIt($path,"file delete failed"); - exit ; + Error("File delete failed", $tstr . $path) ; } else { LogIt($path,"file deleted",trperm_w); MoveTo("$dir/.log/$file","$dir/.del/.log/"); @@ -1716,11 +1755,13 @@ case "RENAME" : if ( $CONFIRM != "on" ) break ; + $NEWNAME=stripSlashes($HTTP_POST_VARS["NEWNAME"]); LogIt("$fsDir/$FN","renamed $FN to $NEWNAME",trperm_r); safe_rename($fsDir,$FN,$NEWNAME); break ; case "NOTE" : + $NOTE=stripSlashes($HTTP_POST_VARS["NOTE"]); WriteNote("$fsDir/$FN","$NOTE"); break ; @@ -1756,13 +1797,12 @@ if (!is_writeable($gblFsRoot . $relDir)) Error("Write access denied",$relDir) ; $text = "Use this page to upload a single " ; - $text .= "file to $HTTP_HOST." ; + $text .= "file to $realm." ; StartHTML("(Upload Page)", $text) ; UploadPage($gblFsRoot, $relDir) ; EndHTML() ; exit ; case "E" : - $F=stripSlashes($F); // detail of $relDir/$F if (is_file("$gblFsRoot/$relDir/$F") || is_dir("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ; exit ; @@ -1774,7 +1814,7 @@ case "Co" : // checkout Lock("$gblFsRoot/$relDir/$F"); - Download("$gblFsRoot/$relDir/$F"); + Download("$gblFsRoot/$relDir/$F",1); exit; case "Ci" : $F=stripSlashes($F); @@ -1782,7 +1822,7 @@ if (!is_writeable($gblFsRoot . $relDir)) Error("Write access denied",$relDir) ; $text = "Use this page to update a single " ; - $text .= "file to $HTTP_HOST." ; + $text .= "file to $realm." ; StartHTML("(Update file Page)", $text) ; UploadPage($gblFsRoot, $relDir, $F) ; EndHTML() ; @@ -1790,12 +1830,7 @@ case "V" : // view LogIt("$gblFsRoot/$relDir/$F","viewed"); - if ($gblForceDownload) { - Download("$gblFsRoot/$relDir/$F"); - } else { - header("Content-Disposition: attachment; filename=$F" ); - Header("Location: $webRoot".urlpath("$relDir/$F")); - } + Download("$gblFsRoot/$relDir/$F",$gblForceDownload); exit; case "Ch" : StartHTML("(File changes)","All changes chronologicaly..."); @@ -1817,7 +1852,7 @@ $title = "You should define \$title variable with page title"; include($inc_file); StartHTML($title, $text) ; - print "

".GifIcon(up)." Back to front page.

"; + print "

".GifIcon(up)." Back to front page.

"; EndHTML() ; exit ; }