From 1ca31cfe69e2d10cce65c4323eb652b355f4c904 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 4 Mar 2006 16:40:38 +0100 Subject: simplified file permission handling This patch simpliefies the configuration of the file and directory creation modes. There is no need to set the umask anymore. Only the wanted permissions for files and directories are set. An init function compares the wanted modes with the ones that would be choosen by the system automatically (consulting the system's umask) and sets the modes for chmod when needed. darcs-hash:20060304154038-7ad00-5ef1db3a87e42563a602f9d050c681d2ea74682f.gz --- inc/init.php | 59 +++++++++++++++++++++++++++++++++-------------------------- inc/io.php | 14 +++++++------- 2 files changed, 40 insertions(+), 33 deletions(-) (limited to 'inc') diff --git a/inc/init.php b/inc/init.php index 1557070e5..10c7240d8 100644 --- a/inc/init.php +++ b/inc/init.php @@ -77,31 +77,8 @@ $conf['usegzip'] = 0; } - // Legacy support for old umask/dmask scheme - if(isset($conf['dmask'])) { - unset($conf['dmask']); - unset($conf['fmask']); - unset($conf['umask']); - } - - // Set defaults for fmode, dmode and umask. - if(!isset($conf['fmode']) || $conf['fmode'] === '') { - $conf['fmode'] = 0666; - } - if(!isset($conf['dmode']) || $conf['dmode'] === '') { - $conf['dmode'] = 0777; - } - if(!isset($conf['umask']) || $conf['umask'] === '') { - $conf['umask'] = umask(); - } - - // Precalculate the fmask and dmask, so we can set later. - if(($conf['umask'] != umask()) or ($conf['fmode'] != 0666)) { - $conf['fmask'] = $conf['fmode'] & ~$conf['umask']; - } - if(($conf['umask'] != umask()) or ($conf['dmode'] != 0666)) { - $conf['dmask'] = $conf['dmode'] & ~$conf['umask']; - } + // precalculate file creation modes + init_creationmodes(); // make real paths and check them init_paths(); @@ -149,7 +126,7 @@ function init_files(){ $fh = @fopen($file,'a'); if($fh){ fclose($fh); - if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); } + if($conf['fperm']) chmod($file, $conf['fperm']); }else{ nice_die("$file is not writable. Check your permissions settings!"); } @@ -188,6 +165,36 @@ function init_path($path){ return $p; } +/** + * Sets the internal config values fperm and dperm which, when set, + * will be used to change the permission of a newly created dir or + * file with chmod. Considers the influence of the system's umask + * setting the values only if needed. + */ +function init_creationmodes(){ + global $conf; + + // Legacy support for old umask/dmask scheme + unset($conf['dmask']); + unset($conf['fmask']); + unset($conf['umask']); + unset($conf['fperm']); + unset($conf['dperm']); + + // get system umask + $umask = umask(); + + // check what is set automatically by the system on file creation + // and set the fperm param if it's not what we want + $auto_fmode = 0666 & ~$umask; + if($auto_fmode != $conf['fmode']) $conf['fperm'] = $conf['fmode']; + + // check what is set automatically by the system on file creation + // and set the dperm param if it's not what we want + $auto_dmode = $conf['dmode'] & ~$umask; + if($auto_dmode != $conf['dmode']) $conf['dperm'] = $conf['dmode']; +} + /** * remove magic quotes recursivly * diff --git a/inc/io.php b/inc/io.php index 1b8d2dadf..9160f4718 100644 --- a/inc/io.php +++ b/inc/io.php @@ -87,7 +87,7 @@ function io_saveFile($file,$content,$append=false){ fclose($fh); } - if(!$fileexists and isset($conf['fmask'])) { chmod($file, $conf['fmask']); } + if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']); io_unlock($file); return true; } @@ -178,7 +178,7 @@ function io_lock($file){ //waited longer than 3 seconds? -> stale lock if ((time() - $timeStart) > 3) break; $locked = @mkdir($lockDir, $conf['dmode']); - if($locked and isset($conf['dmask'])) { chmod($lockDir, $conf['dmask']); } + if($locked && $conf['dperm']) chmod($lockDir, $conf['dperm']); } while ($locked === false); } @@ -229,7 +229,7 @@ function io_mkdir_p($target){ return io_mkdir_ftp($dir); }else{ $ret = @mkdir($target,$conf['dmode']); // crawl back up & create dir tree - if($ret and isset($conf['dmask'])) { chmod($target, $conf['dmask']); } + if($ret && $conf['dperm']) chmod($target, $conf['dperm']); return $ret; } } @@ -264,8 +264,8 @@ function io_mkdir_ftp($dir){ //create directory $ok = @ftp_mkdir($conn, $dir); - //set permissions (using the directory umask and dmode) - @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmask'],$dir)); + //set permissions + @ftp_site($conn,sprintf("CHMOD %04o %s",$conf['dmode'],$dir)); @ftp_close($conn); return $ok; @@ -320,7 +320,7 @@ function io_download($url,$file,$useAttachment=false,$defaultName=''){ if(!$fp) return false; fwrite($fp,$data); fclose($fp); - if(!$fileexists and isset($conf['fmask'])) { chmod($file, $conf['fmask']); } + if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']); if ($useAttachment) return $name; return true; } @@ -335,7 +335,7 @@ function io_rename($from,$to){ global $conf; if(!@rename($from,$to)){ if(@copy($from,$to)){ - if(isset($conf['fmask'])) { chmod($file, $conf['fmask']); } + if($conf['fperm']) chmod($file, $conf['fperm']); @unlink($from); return true; } -- cgit v1.2.3