--- docman.php 2002/07/28 14:16:04 1.17
+++ docman.php 2003/04/09 15:55:15 1.34
@@ -20,7 +20,7 @@
//////////////////////////////////////////////////////////////////
// CONFIGURATION OPTIONS
-// error_reporting(4) ; // how verbose ?
+ error_reporting(E_ALL) ; // how verbose ?
// from where to include auth_*.php modules?
$gblIncDir = "/data/docman2";
@@ -81,17 +81,19 @@
// location of html files
$html = $gblIncDir."/html";
- LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]);
+ if (isset($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"])) {
+ LoadLanguage($HTTP_SERVER_VARS["HTTP_ACCEPT_LANGUAGE"]);
+ }
// for security and configuration
- $realm=$HTTP_SERVER_VARS[HTTP_HOST];
+ $realm=$HTTP_SERVER_VARS["HTTP_HOST"];
- $fsDocumentRoot = dirname($HTTP_SERVER_VARS[SCRIPT_FILENAME]);
+ $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 = $HTTP_SERVER_VARS[PHP_AUTH_USER];
- $gblPasswd = $HTTP_SERVER_VARS[PHP_AUTH_PW];
+ $gblLogin = HTTP_SERVER_VAR("PHP_AUTH_USER");
+ $gblPasswd = HTTP_SERVER_VAR("PHP_AUTH_PW");
//////////////////////////////////////////////////////////////////
@@ -109,9 +111,8 @@
function StartHTML($title,$text="") {
- global $html,$fsDocumentRoot,$HTTP_SERVER_VARS;
+ global $html,$fsDocumentRoot,$gblTitle,$HTTP_SERVER_VARS;
- $title = $gblTitle." ".$title ;
$host = $HTTP_SERVER_VARS["HTTP_HOST"] ;
$self = $HTTP_SERVER_VARS["PHP_SELF"] ;
@@ -138,7 +139,9 @@
} else {
$url .= md5($gblLogin.$gblPasswd);
}
- if (isset($gblLogin) && $gblLogin != "" && ($gblPasswd == "" || !isset($gblPasswd))) {
+ if ( ( (isset($gblLogin) && $gblLogin != "") ||
+ (!isset($gblLogin) || $gblLogin == "")
+ ) && ($gblPasswd == "" || !isset($gblPasswd))) {
$url_title="login";
$url .= "&force_login=1";
} else {
@@ -146,15 +149,19 @@
}
include("$html/footer.html");
- global $debug;
- if ($debug) print $debug;
+# global $debug;
+# if ($debug) print $debug;
} // end function EndHTML
//////////////////////////////////////////////////////////////////
function DetailPage($fsRoot,$relDir,$fn) {
- global $gblEditable, $gblImages, $webRoot, $html, $HTTP_SERVER_VARS ;
+ global $gblEditable, $gblImages,
+ $gblDateFmt, $gblTimeFmt,
+ $gblPermNote,
+ $webRoot, $html,
+ $HTTP_SERVER_VARS ;
$self = $HTTP_SERVER_VARS["PHP_SELF"] ;
$relPath = $relDir . "/" . $fn ;
@@ -184,11 +191,12 @@
$title = "("._("Detail Page").")" ;
StartHTML($title, $text) ;
- echo "
" . $relDir . "/" . $fn . "
" ;
+ print "".$relDir.$fn."
";
+
if ($exists) { // get file info
$fsize = filesize($fsPath) ;
- $fmodified = date("$GLOBALS[gblDateFmt] $GLOBALS[gblTimeFmt]", filemtime($fsPath)) ;
- $faccessed = date("$GLOBALS[gblDateFmt] $GLOBALS[gblTimeFmt]", fileatime($fsPath)) ;
+ $fmodified = date("$gblDateFmt $gblTimeFmt", filemtime($fsPath)) ;
+ $faccessed = date("$gblDateFmt $gblTimeFmt", fileatime($fsPath)) ;
$fuid=fileowner($fsPath);
$fgid=filegroup($fsPath);
$userinfo = posix_getpwuid($fuid);
@@ -203,52 +211,27 @@
$fstr = fread($fh,filesize($fsPath)) ;
fclose($fh) ;
$fstr = htmlentities( $fstr ) ;
-?>
-
-
-" ;
// echo htmlentities($tstr) . "
" . $tstr ;
echo $tstr ;
}
-?>
-
+ }
+
+ if (check_perm($relDir.$fn,$gblPermNote)) {
+ include("$html/DetailPage-note.html");
+ }
-";
$name=basename("$fsDir/$fn");
$logname=dirname("$fsDir/$fn")."/.log/$name";
@@ -476,18 +438,10 @@
//////////////////////////////////////////////////////////////////
-function GifIcon($txt) {
- global $gblIconLocation ;
+function GifIcon($txt = "") {
+ global $gblIconLocation, $gblImages ;
switch (strtolower($txt)) {
- case ".bmp" :
- case ".gif" :
- case ".jpg" :
- case ".jpeg":
- case ".tif" :
- case ".tiff":
- $d = "image2.gif" ;
- break ;
case ".doc" :
$d = "layout.gif" ;
break ;
@@ -565,9 +519,14 @@
$d = "quill.gif";
break;
default :
- $d = "generic.gif" ;
+ if (in_array(strtolower($txt),$gblImages)) {
+ $d = "image2.gif" ;
+ } else {
+ $d = "generic.gif" ;
+ }
}
+
return "" ;
} // end function GifIcon
@@ -577,17 +536,31 @@
global $gblEditable, $gblIcon, $gblModDays, $webRoot, $gblHide,
$gblIgnoreUnknownFileType, $gblRepositoryDir,
+ $gblLogin, $gblUserName, $gblDateFmt, $gblTimeFmt,
+ $gblPermNote,
$fsRealmDir, $realm, $realm_sep,
- $HTTP_GET_VARS, $html, $realm_config;
+ $html, $realm_config,
+ $HTTP_GET_VARS, $HTTP_SERVER_VARS;
$self = $HTTP_SERVER_VARS["PHP_SELF"] ;
- $fsDir = $fsRoot.$relDir."/"; // current directory
+ $relDir = chopsl($relDir)."/";
+ $fsDir = $fsRoot.$relDir; // current directory
+
+ if (!is_dir($fsDir)) Error("Dir not found","Directory $relDir not found on filesystem at $fsDir",1) ;
- if (!is_dir($fsDir)) Error("Dir not found",$relDir,1) ;
+ global $debug;
+ $debug .= "[$gblLogin|$relDir] before >";
+
+ if (! check_perm($relDir,trperm_b))
+ Error("Access denied","User $gblLogin tried to access $relDir without valid trustee.",1);
+ $debug .= "< afeter";
$hide_items=",$gblHide,";
+ $dirList = array();
+ $fileList = array();
+
// read directory contents
if ( !($dir = @opendir($fsDir)) )
Error("Read Access denied",$relDir,1) ;
@@ -612,13 +585,18 @@
closedir($dir) ;
// scan deleted files
- if ( $HTTP_GET_VARS["show_deleted"] == 1 && ($dir = @opendir("$fsDir/.del")) ) {
+ if ( HTTP_GET_VAR("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" ;
- $fileDate[$item] = filemtime($fsDir.".del/$path") ;
- $fileSize[$item] = filesize($fsDir.".del/$path") ;
- $fileNote[$item] = ReadNote($fsDir.".del/$item");
+ if (is_file($fsDir.".del/$item")) {
+ $fileList[$item] = ".del/$item" ;
+ $fileDate[$item] = filemtime($fsDir.".del/$item") ;
+ $fileSize[$item] = filesize($fsDir.".del/$item") ;
+ $fileNote[$item] = ReadNote($fsDir.".del/$item");
+ } else {
+ $dirList[$item] = ".del/$item" ;
+ $dirNote[$item] = ReadNote($fsDir.".del/$item");
+ }
}
closedir($dir) ;
}
@@ -627,15 +605,14 @@
// start navigation page
$text = "Use this page to add, delete";
- if (! isset($HTTP_GET_VARS[show_deleted])) {
+ if (! isset($HTTP_GET_VARS["show_deleted"])) {
$text .= ", undelete";
}
$text .= " or revise files on this web site." ;
$text .= "
Examine list of files changed in last day or all changes.";
StartHTML("(Navigate)",$text) ;
- echo "" ;
+ print "" ;
// updir (parent) bar
if (chopsl($fsDir) != chopsl($fsRoot)) {
@@ -653,8 +630,8 @@
return $out;
}
- $dsort = $HTTP_GET_VARS[dsort];
- if (! isset($dsort)) $dsort = "name"; // default directory sort
+ $dsort = HTTP_GET_VAR("dsort");
+ if (! isset($dsort)) $dsort = "name"; // default directory sort
$dsort_arr = array(
"name" => array ("rname", "note"),
@@ -663,8 +640,8 @@
"rnote" => array ("name", "note")
);
- $fsort = $HTTP_GET_VARS[fsort];
- if (! isset($fsort)) $fsort = "name"; // default directory sort
+ $fsort = HTTP_GET_VAR("fsort");
+ if (! isset($fsort)) $fsort = "name"; // default directory sort
$fsort_arr = array(
"name" => array ("rname", "note", "date", "size"),
@@ -680,6 +657,7 @@
$D="D=".urlencode($relDir);
function self_args($arr = array()) {
+ global $self;
$arg = implode("&",$arr);
if ($arg) {
return $self."?".$arg;
@@ -717,7 +695,22 @@
$dir = $dirList[$key];
$info_url=self_args(array("A"=>"A=E", "F"=>"F=".urlencode($dir), "D"=>$D));
+ if (substr($dir,0,5) == ".del/") {
+ $dir = substr($dir,5,strlen($dir)-5);
+ $deleted = " deleted";
+ } else {
+ $deleted = "";
+ }
+
$dir_url=$self."?D=".urlencode(chopsl($relDir)."/".$dir);
+ if (check_perm($relDir.$dir,$gblPermNote)) {
+ $note_html="".$gblIcon("note")."".$dirNote[$key];
+ } else {
+ $note_html=$dirNote[$key];
+ }
+
+ $dir_html = isBlank($dir,"directory");
+
include("$html/Navigate-dirEntry.html");
} // iterate over dirs
@@ -793,17 +786,21 @@
$file_url_html.="\" TITLE=\"View file\">" ;
if (substr($file,0,5) != ".del/") {
- $file_url_html .= $file . "" . $a ;
+ $file_url_html .= isBlank($file) . "" . $a ;
} else {
- $file_url_html .= substr($file,5,strlen($file)-5) . " deleted ";
+ $file_url_html .= isBlank(substr($file,5,strlen($file)-5)) . " deleted";
}
- $note_html="".$gblIcon("note")."".ReadNote($path);
+ if (check_perm($relDir.$file,$gblPermNote)) {
+ $note_html="".$gblIcon("note")."".$fileNote[$key];
+ } else {
+ $note_html=$fileNote[$key];
+ }
$ext = strtolower(strrchr($file,".")) ;
if ($file_lock) {
- if ($file_lock == $GLOBALS[gblUserName]) {
+ if ($file_lock == $gblUserName) {
$b.="" ;
$file_url_html=$b;
@@ -818,9 +815,11 @@
$file_url_html = "$file $a";
}
} else {
- $b.="" ;
- $b.=$gblIcon("checkout")."";
+ if (check_perm($relDir.$file,trperm_w)) {
+ $b.="" ;
+ $b.=$gblIcon("checkout")."";
+ }
if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) {
$b.="
- |
- No files in this directory
- |
-
+ include("$html/Navigate-noFiles.html");
}
if ($emptyDir && $relDir != "") {
-?>
-
-
-
-
- |
|
+ include("$html/Navigate-hr.html");
-
-
-if (file_exists("$fsRealmDir/$realm".$realm_sep."info.inc")) {
- print " | ";
- include("$fsRealmDir/$realm".$realm_sep."info.inc");
- print " |
|
|
";
-} elseif (file_exists("$gblRepositoryDir/.info.inc")) {
- print " | ";
- include("$gblRepositoryDir/.info.inc");
- print " |
|
|
";
-}
+ if (file_exists("$fsRealmDir/$realm".$realm_sep."info.inc")) {
+ print " | ";
+ include("$fsRealmDir/$realm".$realm_sep."info.inc");
+ print " |
";
+ include("$html/Navigate-hr.html");
+ } elseif (file_exists("$gblRepositoryDir/.info.inc")) {
+ print " | ";
+ include("$gblRepositoryDir/.info.inc");
+ print " |
";
+ include("$html/Navigate-hr.html");
+ }
+ include("$html/Navigate-createNew.html");
-?>
-
-
-
+ print "
";
-$dir/.log",1);
@@ -950,18 +921,17 @@
$file=basename($target);
$log=fopen("$dir/.log/$file","a+");
- fputs($log,date("$GLOBALS[gblDateFmt]\t$GLOBALS[gblTimeFmt]").
- "\t$GLOBALS[gblUserName]\t$msg\n");
+ fputs($log,date("$gblDateFmt\t$gblTimeFmt").
+ "\t$gblUserName\t$msg\n");
fclose($log);
if (! $changelog) return;
- global $gblFsRoot;
$log=fopen("$gblFsRoot/.changelog","a+");
if (substr($target,0,strlen($gblFsRoot)) == $gblFsRoot)
$target=substr($target,strlen($gblFsRoot),strlen($target)-strlen($gblFsRoot));
$msg=str_replace("\t"," ",$msg);
- fputs($log,time()."\t$target\t$GLOBALS[gblUserName]\t$msg\n");
+ fputs($log,time()."\t$target\t$gblUserName\t$msg\n");
fclose($log);
// FIX: implement e-mail notification based on $changelog
@@ -981,6 +951,9 @@
$file=basename($target);
$note=fopen("$dir/.note/$file","w");
+ if (! $note) {
+ Error("Error writing note","Can't open note file $dir/.note/$file for writing",1);
+ }
fputs($note,"$msg\n");
fclose($note);
@@ -1021,6 +994,8 @@
function Lock($target) {
+ global $gblUserName;
+
$target=stripSlashes($target);
$dir=dirname($target);
if (! file_exists($dir."/.lock")) {
@@ -1032,7 +1007,7 @@
LogIt($target,"attempt to locked allready locked file!");
} else {
$lock=fopen("$dir/.lock/$file","w");
- fputs($lock,"$GLOBALS[gblUserName]\n");
+ fputs($lock,"$gblUserName\n");
fclose($lock);
LogIt($target,"file locked");
@@ -1081,6 +1056,9 @@
//////////////////////////////////////////////////////////////////
function safe_rename($fromdir,$fromfile,$tofile) {
+
+ global $gblNumBackups;
+
function try_rename($from,$to) {
# print "$from -> $to\n";
if (file_exists($from) && is_writeable(dirname($to))) {
@@ -1110,7 +1088,7 @@
try_dir("$todir/.lock");
try_rename("$fromdir/.lock/$fromfile","$todir/.lock/$tofile");
try_dir("$todir/.bak");
- for($i=0;$i<=$GLOBALS[gblNumBackups];$i++) {
+ for($i=0;$i<=$gblNumBackups;$i++) {
try_rename("$fromdir/.bak/$i/$fromfile","$todir/.bak/$i/$tofile");
}
}
@@ -1138,7 +1116,10 @@
function DisplayChangeLog($day) {
- global $gblFsRoot,$HTTP_SERVER_VARS;
+ global $gblFsRoot, $gblDateFmt, $gblTimeFmt,
+ $HTTP_SERVER_VARS;
+
+ $self = $HTTP_SERVER_VARS["PHP_SELF"];
if (!file_exists("$gblFsRoot/.changelog")) return;
$log=fopen("$gblFsRoot/.changelog","r");
@@ -1157,14 +1138,14 @@
print "\n";
while ($e = array_shift($logarr)) {
$cl=$cl1; $cl1=$cl2; $cl2=$cl;
- $date = date("$GLOBALS[gblDateFmt]", $e[0]);
- $time = date("$GLOBALS[gblTimeFmt]", $e[0]);
+ $date = date($gblDateFmt, $e[0]);
+ $time = date($gblTimeFmt, $e[0]);
$dir = dirname($e[1]);
$file = basename($e[1]);
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.
";
}
//////////////////////////////////////////////////////////////////
@@ -1186,7 +1167,7 @@
$ext_arr = explode(".",$file);
$ext = array_pop($ext_arr);
- if ($encoding_ext[$ext]) {
+ if (isset($encoding_ext[$ext])) {
$encoding = $encoding_ext[$ext];
$ext = array_pop($ext_arr);
}
@@ -1198,7 +1179,7 @@
}
// IE5.5 just downloads index.php if we don't do this
- if(preg_match("/MSIE 5.5/", $HTTP_SERVER_VARS[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");
@@ -1228,16 +1209,16 @@
by Vyacheslav Zavadsky
*/
-define(trmask_not,1 << 0);
-define(trmask_clear,1 << 1);
-define(trmask_deny,1 << 2);
-define(trmask_one_level,1 << 3);
-define(trmask_group,1 << 4);
-
-define(trperm_r,1 << 5);
-define(trperm_w,1 << 6);
-define(trperm_b,1 << 7);
-define(trperm_n,1 << 8);
+define('trmask_not',1 << 0);
+define('trmask_clear',1 << 1);
+define('trmask_deny',1 << 2);
+define('trmask_one_level',1 << 3);
+define('trmask_group',1 << 4);
+
+define('trperm_r',1 << 5);
+define('trperm_w',1 << 6);
+define('trperm_b',1 << 7);
+define('trperm_n',1 << 8);
$trustee_a2n = array(
'!' => trmask_not,
@@ -1285,7 +1266,7 @@
$error="".dirname($trustee_php)." must be writable by web server user";
} elseif (file_exists($trustee_php) && !is_writable($trustee_php)) {
$error="trustees cache file $trustee_php exists, but is not writable by web server";
-} elseif (1 || filemtime($trustee_conf) >= filemtime($trustee_php)) {
+} elseif (@filemtime($trustee_conf) >= @filemtime($trustee_php)) {
$fp_php=@fopen($trustee_php,"w");
fputs($fp_php,"\n";
}
}
- $tr_arr[$path][$user] |= $perm;
+ if (isset($tr_arr[$path][$user])) {
+ $tr_arr[$path][$user] |= $perm;
+ } else {
+ $tr_arr[$path][$user] = $perm;
+ }
}
}
}
@@ -1360,10 +1345,10 @@
fclose($fp_php);
}
-if ($error) {
+if (isset($error)) {
Error("Trustee error",$error,1);
} else {
- include("$trustee_php");
+ include_once("$trustee_php");
}
return 1;
@@ -1376,61 +1361,80 @@
}
// helper function
-function unroll_perm($u,$t,$user,$perm) {
+function unroll_perm($u,$t,$perm,$one_level) {
- // check user FIX
- if ($t & trmask_not && ($u==$user)) continue;
- if (!($t & trmask_not) && ($u!=$user)) continue;
+ if ($t & trmask_one_level && !$one_level) return $perm;
if ($t & trmask_deny) {
if ($t & trmask_clear) {
- $perm[deny] &= ~$t;
+ $perm['deny'] &= ~$t;
} else {
- $perm[deny] |= $t;
+ $perm['deny'] |= $t;
}
} elseif ($t & trmask_clear) {
- $perm[allow] &= ~$t;
+ $perm['allow'] &= ~$t;
} else {
- $perm[allow] |= $t;
+ $perm['allow'] |= $t;
}
return $perm;
}// end of helper function
function check_trustee($user,$path) {
- global $trustees;
- $perm[allow] = 0;
- $perm[deny] = 0;
+ global $trustees,$HAVE_TRUSTEE;
+ $perm['allow'] = 0;
+ $perm['deny'] = 0;
+
+ // do we use trustees?
+ if (! $HAVE_TRUSTEE) return $perm;
+
+ if (! isset($trustees)) Error("Trustees not found","Can't find in-memory trustee structure \$trustees. Probably bug in code. Contact dpavlin@rot13.org",1);
+
+global $debug;
+$debug .= "
check_trustee $path ... ";
+
$path_arr=explode("/",$path);
- $path = "/";
+ $tmppath = "/";
while (count($path_arr)) {
- if (substr($path,strlen($path)-1,1) != "/") $path.="/";
- $tr = $trustees[$path];
+ $tmppath.=array_shift($path_arr);
+$debug.= ">> $tmppath ";
+
+ if (! isset($trustees[$tmppath])) continue;
+ $tr = $trustees[$tmppath];
+
+ $one_level = (!count($path_arr));
+$debug.=" O($one_level) ";
+
if (isset($tr)) {
// first apply trustee for all
- if (isset($tr['*'])) {
- $perm = unroll_perm($user,$tr['*'],$user, $perm);
+ if (isset($tr['*']) && $user!="anonymous") {
+ $perm = unroll_perm($user,$tr['*'],$perm, $one_level);
unset($tr['*']);
}
- // then apply group policies
+ // then apply not and group policies
foreach ($tr as $g=>$t) {
- if ($t & trmask_group && in_group($user,$g)) {
+ if ($t & trmask_not && $g != $user) {
+ $t = $t & ~trmask_not;
+ $perm = unroll_perm($user,$t,$perm, $one_level);
+ unset($tr[$g]);
+
+ } elseif ($t & trmask_group && in_group($user,$g)) {
// resolv user
$t = $t & ~trmask_group;
- $perm = unroll_perm($user,$t,$user, $perm);
+ $perm = unroll_perm($user,$t,$perm, $one_level);
unset($tr[$g]);
}
}
// then apply user policy
if (isset($tr[$user])) {
- $perm = unroll_perm($user,$tr[$user],$user, $perm);
+ $perm = unroll_perm($user,$tr[$user],$perm,$one_level);
unset($tr[$user]);
}
- $path.=array_shift($path_arr);
}
+$debug.="d(".display_trustee($perm['deny']).") a(".display_trustee($perm['allow']).") ";
}
-#print "
user: $user path: $path perm: ";
-#print "d: $perm[deny] (".display_trustee($perm[deny]).") a: $perm[allow] (".display_trustee($perm[allow]).")
\n";
+$debug.="
check_trustee: user: $user path: $path==$tmppath perm: ";
+$debug.="d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).")
\n";
return $perm;
}
@@ -1439,15 +1443,17 @@
function check_perm($path,$trperm) {
global $gblLogin,$HAVE_TRUSTEE;
+ $path = str_replace("//","/",$path);
+
global $debug;
-$debug.="
check_perm: $path test perm ".display_trustee($perm)."
\n";
+$debug.="
check_perm: on $path for perm ".display_trustee($trperm)."
\n";
$return = ! $HAVE_TRUSTEE;
if ($HAVE_TRUSTEE) {
$perm = check_trustee($gblLogin,$path);
-$debug.=" d: $perm[deny] (".display_trustee($perm[deny]).") a: $perm[allow] (".display_trustee($perm[allow]).") perm: $trperm";
- if ($perm[deny] & $trperm) $return=0;
- elseif ($perm[allow] & $trperm) $return=1;
+$debug.=" d: ".$perm['deny']." (".display_trustee($perm['deny']).") a: ".$perm['allow']." (".display_trustee($perm['allow']).") perm to have: $trperm (".display_trustee($trperm).")";
+ if ($perm['deny'] & $trperm) $return=0;
+ elseif (($perm['allow'] & $trperm) == $trperm) $return=1;
}
$debug.=" return: $return
\n";
return($return);
@@ -1488,17 +1494,69 @@
function check_filename($file) {
if (strstr($file,"..")) Error("Security violation","No parent dir .. allowed in file name $file",1);
+ // remove deleted directory (for undelete to work)
+ $file = str_replace(".del/","",$file);
if (strstr($file,"/")) Error("Security violation","No slashes / allowed in file name $file",1);
}
+// bla/blo/../foo will return bla/foo
+function remove_parent($path) {
+ while (preg_match(",/[^/]+/\.\./,",$path)) {
+ $path = preg_replace(",/[^/]+/\.\./,","",$path);
+ }
+ if (substr($path,0,1) != "/") $path = "/".$path;
+ return $path;
+}
+
+//////////////////////////////////////////////////////////////////
+
+// functions to move HTTP server variables to global namespace
+// [replacement for register_globals in php.ini]
+
+function HTTP_GET_VAR($var) {
+ global $HTTP_GET_VARS, ${$var};
+ if (isset($HTTP_GET_VARS[$var])) {
+ $$var = stripSlashes($HTTP_GET_VARS[$var]);
+ return $$var;
+ }
+}
+
+function HTTP_POST_VAR($var) {
+ global $HTTP_POST_VARS, ${$var};
+ if (isset($HTTP_POST_VARS[$var])) {
+ $$var = $HTTP_POST_VARS[$var];
+ return $$var;
+ }
+}
+
+function HTTP_SERVER_VAR($var) {
+ global $HTTP_SERVER_VARS, ${$var};
+ if (isset($HTTP_SERVER_VARS[$var])) {
+ $$var = $HTTP_SERVER_VARS[$var];
+ return $$var;
+ }
+}
+
+//////////////////////////////////////////////////////////////////
+
+function Warn($text) {
+}
+
+//////////////////////////////////////////////////////////////////
+
+function isBlank($file,$what = "filename") {
+ if (trim($file) == "") return "whitespace $what";
+ if ($file == "") return "no $what";
+ return $file;
+}
+
//////////////////////////////////////////////////////////////////
// MAIN PROGRAM
$gblFilePerms = 0640 ; // default for new files
$gblDirPerms = 0750 ; // default for new dirs
- $STYLE = $HTTP_GET_VARS[STYLE];
- if (isset($STYLE) && $STYLE == "get") {
+ if (isset($HTTP_GET_VARS["STYLE"]) && $HTTP_GET_VARS["STYLE"] == "get") {
include("$html/docman.css");
exit;
}
@@ -1506,7 +1564,7 @@
// location of master docman configuration file
$docman_conf = "/etc/docman.conf";
if (! file_exists($docman_conf)) {
- $error = "Can't find master configuration file $docman_conf. See docman2/doc/upgrade.html#docman_conf for more informations";
+ $error = "Can't find master configuration file $docman_conf. See docman2/doc/upgrade.html#docman_conf for more informations";
error_log("docman: $error");
Error("docman not installed completly",$error);
@@ -1518,8 +1576,8 @@
}
// try to add dir to script name to realm var
- if (is_dir("$fsRealmDir/$realm/".dirname($HTTP_SERVER_VARS[SCRIPT_NAME]))) {
- $realm .= dirname($HTTP_SERVER_VARS[SCRIPT_NAME]);
+ if (is_dir("$fsRealmDir/$realm/".dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]))) {
+ $realm .= dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]);
$realm_sep = "/";
} else {
$realm_sep = ".";
@@ -1543,6 +1601,10 @@
Error("Configuration error","Can't find user handling module at $gblIncDir/htusers/$gblUsers.php ! Please fix $realm_config");
}
+ // take additional login vars
+ HTTP_GET_VAR("relogin");
+ HTTP_GET_VAR("force_login");
+
// if no password, or empty password logout
if (
isset($gblLogin) && (
@@ -1567,15 +1629,12 @@
// get ACL informations
$HAVE_TRUSTEE = init_trustee();
- // take additional login vars
- $relogin = $HTTP_GET_VARS[relogin];
- $force_login = $HTTP_GET_VARS[force_login];
-
- if (strtolower($gblLogin) == "anonymous" || !isset($gblPasswd)) {
- $perm = check_trustee("anonymous",$path);
+ if (strtolower($gblLogin) == "anonymous" || !isset($gblLogin)) {
+ $perm = check_trustee("anonymous","/");
// browsing must be explicitly allowed for root directory
// of repository for anonymous user to work!
- if ($perm[allow] & trperm_b) {
+ if ($perm['allow'] & trperm_b) {
+ $gblLogin = $gblPasswd = "anonymous";
$secHash = md5($gblLogin.$gblPasswd);
$gblUserName = "Anonymous user";
}
@@ -1593,32 +1652,41 @@
// read mime.types
readMime();
+ if (! isset($gblPermNote)) {
+ $gblPermNote = trperm_r;
+ }
+
+ HTTP_POST_VAR("FN");
+
if ($HTTP_SERVER_VARS["REQUEST_METHOD"] == "POST") {
// 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"]);
-
- check_filename($FN);
- check_dirname($DIR);
- check_dirname($RELPATH);
-
- $relDir = $DIR;
+ if (HTTP_POST_VAR("FN"))
+ check_filename($FN);
+ if (HTTP_POST_VAR("DIR")) {
+ check_dirname($DIR);
+ $relDir = $DIR;
+ } else {
+ trigger_error("Can't get DIR",E_USER_WARNING);
+ $relDir = "/";
+ }
+ if (HTTP_POST_VAR("RELPATH")) check_dirname($RELPATH);
+ HTTP_POST_VAR("T");
+ HTTP_POST_VAR("CONFIRM");
} else {
// get
- $A=stripSlashes($HTTP_GET_VARS["A"]);
- $D=stripSlashes(urldecode($HTTP_GET_VARS["D"]));
- $F=stripSlashes($HTTP_GET_VARS["F"]);
-
- check_filename($F);
- check_dirname($D);
-
- $relDir = $D;
+ HTTP_GET_VAR("A");
+ if (HTTP_GET_VAR("D")) {
+ check_dirname($D);
+ $D=urldecode($D);
+ $relDir = $D;
+ } else {
+ //trigger_error("Can't get D",E_USER_WARNING);
+ $relDir = "/";
+ }
+ if (HTTP_GET_VAR("F")) check_filename($F);
}
- $relScriptDir = dirname($SCRIPT_NAME) ;
+ $relScriptDir = dirname($HTTP_SERVER_VARS["SCRIPT_NAME"]) ;
// i.e. /docman
// start on server root
@@ -1628,6 +1696,8 @@
$fsDir = $gblFsRoot . $relDir ; // current directory
if ( !is_dir($fsDir) ) Error("Dir not found","Can't find $relDir which points to $fsDir",1) ;
+ if ($relDir == "") $relDir="/";
+
if (isset($HTTP_SERVER_VARS["HTTPS"]) && $HTTP_SERVER_VARS["HTTPS"] == "on") {
$webRoot = "https://";
} else {
@@ -1635,7 +1705,7 @@
}
$webRoot .= $HTTP_SERVER_VARS["HTTP_HOST"] . $relScriptDir;
- switch ($HTTP_POST_VARS["POSTACTION"]) {
+ if (HTTP_POST_VAR("POSTACTION")) switch ($POSTACTION) {
case "UPLOAD" :
$FN_name=stripSlashes($HTTP_POST_FILES["FN"]["tmp_name"]);
$FN=stripSlashes($HTTP_POST_FILES["FN"]["name"]);
@@ -1643,11 +1713,10 @@
$source = $FN_name ;
if (! file_exists($source)) {
- Error("You must select file with browse to upload it!");
+ Error("You must select file with browse to upload it!","If file is too big, you might need to modify php configuration options post_max_size and upload_max_filesize",1);
}
- $FILENAME = $HTTP_POST_VARS["FILENAME"];
- check_filename($FILENAME);
+ if (HTTP_POST_VAR("FILENAME")) check_filename($FILENAME);
if (! isset($FILENAME)) { // from update file
$target = "$fsDir/".basename($FN);
@@ -1655,16 +1724,19 @@
$target = "$fsDir/$FILENAME";
}
+ if (! check_perm("$relDir/".basename($target), trperm_w))
+ Error("Access denied","User $gblLogin tried to upload $relDir/".basename($target)." without valid trustee.",1);
+
// backup old files first
$dir=dirname($target);
if (! file_exists($dir."/.bak")) {
mkdir($dir."/.bak",0700);
}
- if (! file_exists($dir."/.bak/$GLOBALS[gblNumBackups]")) {
- mkdir($dir."/.bak/$GLOBALS[gblNumBackups]",0700);
+ if (! file_exists($dir."/.bak/$gblNumBackups")) {
+ mkdir($dir."/.bak/$gblNumBackups",0700);
}
$file=basename($target);
- for($i=$GLOBALS[gblNumBackups]-1;$i>0;$i--) {
+ for($i=$gblNumBackups-1;$i>0;$i--) {
MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/");
}
MoveTo($target,$dir."/.bak/1/");
@@ -1683,14 +1755,18 @@
case "SAVE" :
$path = $gblFsRoot . $RELPATH ;
$path=stripSlashes($path);
+
+ if (! check_perm("$RELPATH", trperm_w))
+ Error("Access denied","User $gblLogin tried to save $RELPATH without valid trustee.",1);
+
$writable = is_writeable($path) ;
$legaldir = is_writeable(dirname($path)) ;
$exists = (file_exists($path)) ? 1 : 0 ;
-// check for legal extension here as well
+ // FIX: more verbose error message
if (!($writable || (!$exists && $legaldir)))
Error("Write denied",$RELPATH) ;
$fh = fopen($path, "w") ;
- $FILEDATA=stripSlashes($FILEDATA);
+ HTTP_POST_VAR("FILEDATA");
fwrite($fh,$FILEDATA) ;
fclose($fh) ;
clearstatcache() ;
@@ -1699,10 +1775,18 @@
case "CREATE" :
// we know $fsDir exists
- if ($FN == "") break; // no filename!
- if (!is_writeable($fsDir)) Error("Write denied",$relDir) ;
- $path = $fsDir . "/" . $FN ; // file or dir to create
- $relPath = $relDir . "/" . $FN ;
+ if (! check_perm($relDir, trperm_w))
+ Error("Write access denied","You don't have permission to write in $relDir");
+ if ($T == "D") $type = "directory";
+ else $type ="file";
+ if ($FN == "") Error("Can't create $type","You must enter name of $type to create it.");
+ if (!is_writeable($fsDir)) Error("Write denied","User $gblLogin has trustee to write in $relDir but permissions on $fsDir are wrong!", 1) ;
+ $path = "$fsDir/$FN"; // file or dir to create
+ $relPath = "$relDir/$FN";
+
+ if (file_exists($path))
+ Error("Can't create $type","Object $relPath allready exists");
+
switch ( $T ) {
case "D" : // create a directory
if ( ! @mkdir($path,$gblDirPerms) )
@@ -1716,53 +1800,54 @@
// better keep it here altogether
// chmod perms to $gblFilePerms
if ( file_exists($path) && !is_writeable($path) )
- Error("File not writable", $relPath) ;
+ Error("File not writable", "User $gblLogin has trustee to write in $relPath but permissions on $path are wrong!", 1) ;
$fh = fopen($path, "w+") ;
if ($fh) {
fputs($fh,"\n");
fclose($fh) ;
LogIt($path,"file created",trperm_r | trperm_w);
} else {
- Error("Creation of file $relPath failed -- $path");
+ Error("Creation of file $relPath failed", "User $gblLogin has trustee to write in $relPath but creation of $path failed!", 1) ;
}
- $tstr = "$PHP_SELF?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;
+ $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ;
header("Location: " . $tstr) ;
exit ;
}
break ;
case "DELETE" :
- if ( $CONFIRM != "on" ) break ;
+ if ( $CONFIRM != "on" ) break;
- $tstr = "Attempt to delete non-existing object or " ;
- $tstr .= "insufficient privileges: " ;
+ if ( isset($FN) && $FN != "") {
+ $path=$fsDir."/".$FN;
+ $what = "file";
+ } elseif (isset($DIR)) {
+ $path=$gblFsRoot."/".$DIR;
+ $what = "directory";
+ } else {
+ Error("Can't delete object","Can't find filename \$FN or dirname in \$DIR",1);
+ }
- if ( $FN != "") { // delete file
- $path = $fsDir . "/" . $FN ;
-
- $dir=dirname($path);
- $file=basename($path);
- if (! file_exists("$dir/.del")) {
- mkdir("$dir/.del",0700);
- }
+ if (! check_perm("$relDir/$FN", trperm_w))
+ Error("Access denied","User $gblLogin tried to erase $what $relDir/$FN without valid trustee.",1);
-// if ( ! @unlink($path) ) {
- if ( ! rename($path,"$dir/.del/$file") ) {
- LogIt($path,"file delete failed");
- Error("File delete failed", $tstr . $path) ;
- } else {
- LogIt($path,"file deleted",trperm_w);
- MoveTo("$dir/.log/$file","$dir/.del/.log/");
- MoveTo("$dir/.note/$file","$dir/.del/.note/");
- MoveTo("$dir/.lock/$file","$dir/.del/.lock/");
- }
- } else { // delete directory
- if ( ! @rrmdir($fsDir) ) {
- Error("Rmdir failed", $tstr . $fsDir) ;
- } else {
- LogIt($path,"dir deleted",trperm_w);
- $relDir = dirname($relDir) ; // move up
- }
+ $tstr = "Attempt to delete non-existing object or insufficient privileges: " ;
+
+ $dir=dirname($path);
+ $file=basename($path);
+
+ if (! file_exists("$dir/.del")) {
+ mkdir("$dir/.del",0700);
+ }
+
+ if ( ! @rename($path,"$dir/.del/$file") ) {
+ LogIt($path,"$what delete failed");
+ Error("Can't delete $what",$tstr."".$relDir."/".$FN."") ;
+ } else {
+ LogIt($path,"$what deleted",trperm_w);
+ MoveTo("$dir/.log/$file","$dir/.del/.log/");
+ MoveTo("$dir/.note/$file","$dir/.del/.note/");
+ MoveTo("$dir/.lock/$file","$dir/.del/.lock/");
}
break ;
@@ -1772,6 +1857,9 @@
if (substr($FN,0,4) != ".del") break ;
$file=substr($FN,4,strlen($FN)-4);
+ if (! check_perm("$relDir/$file", trperm_w))
+ Error("Access denied","User $gblLogin tried to undelete $relDir/$file without valid trustee.",1);
+
LogIt("$fsDir/.del/$file","undeleted",trperm_w);
MoveTo("$fsDir/.del/$file","$fsDir/");
MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/");
@@ -1783,18 +1871,31 @@
case "RENAME" :
if ( $CONFIRM != "on" ) break ;
- $NEWNAME=stripSlashes($HTTP_POST_VARS["NEWNAME"]);
+ if (HTTP_POST_VAR("NEWNAME")) {
+ $dest = remove_parent($relDir.$NEWNAME);
+ if (! check_perm($relDir.$FN, trperm_w) ||
+ ! check_perm($dest, trperm_w) )
+ Error("Access denied","User $gblLogin tried to rename $relDir$FN to $dest without valid trustee.",1);
+ } else {
+ Error("Rename error","Can't find new name in var \$NEWNAME",1);
+ }
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");
+ case "NOTE" :
+ if (! HTTP_POST_VAR("NOTE"))
+ Error("Can't add note to object","Can't find var \$NOTE",1);
+ if (! check_perm("$relDir/$FN", trperm_w))
+ Error("Access denied","User $gblLogin tried to add note to $relDir/$FN without valid trustee.",1);
+
+ WriteNote("$fsDir/$FN",$NOTE);
break ;
case "UNLOCK" :
if ( $CONFIRM != "on" ) break ;
+ if (! check_perm("$relDir/$FN", trperm_w))
+ Error("Access denied","User $gblLogin tried to unlock $relDir/$FN without valid trustee.",1);
Unlock("$fsDir/$FN");
break ;
@@ -1803,12 +1904,12 @@
}
// common to all POSTs : redirect to directory view ($relDir)
- if ( $POSTACTION != "" ) {
- $tstr = $PHP_SELF . "?D=" . urlencode($relDir) ;
- header("Location: " . $tstr) ;
+ if (isset($POSTACTION)) {
+ $tstr = $HTTP_SERVER_VARS["PHP_SELF"]."?D=".urlencode($relDir);
+ header("Location: ".$tstr) ;
exit ;
}
-
+
// check for mode.. navigate, code display, upload, or detail?
// $A=U : upload to path given in $D
// $A=E : display detail of file $D/$F and edit
@@ -1819,7 +1920,7 @@
// $A=I : include file .$F.php from [$gblIncDir|realm]/include_php
// default : display directory $D
- switch ($A) {
+ if (isset($A)) switch ($A) {
case "U" :
// upload to $relDir
if (! check_perm($relDir, trperm_w))