diff options
Diffstat (limited to 'inc/io.php')
-rw-r--r-- | inc/io.php | 109 |
1 files changed, 44 insertions, 65 deletions
diff --git a/inc/io.php b/inc/io.php index 922d2af02..5cf50af8f 100644 --- a/inc/io.php +++ b/inc/io.php @@ -50,16 +50,21 @@ function io_readFile($file){ /** * Saves $content to $file. * + * If the third parameter is set to true the given content + * will be appended. + * * Uses gzip if extension is .gz * * @author Andreas Gohr <andi@splitbrain.org> * @return bool true on success */ -function io_saveFile($file,$content){ +function io_saveFile($file,$content,$append=false){ + $mode = ($append) ? 'ab' : 'wb'; + io_makeFileDir($file); io_lock($file); if(substr($file,-3) == '.gz'){ - $fh = @gzopen($file,'wb9'); + $fh = @gzopen($file,$mode.'9'); if(!$fh){ msg("Writing $file failed",-1); return false; @@ -67,7 +72,7 @@ function io_saveFile($file,$content){ gzwrite($fh, $content); gzclose($fh); }else{ - $fh = @fopen($file,'wb'); + $fh = @fopen($file,$mode); if(!$fh){ msg("Writing $file failed",-1); return false; @@ -80,84 +85,58 @@ function io_saveFile($file,$content){ } /** - * Appends $content to $file. + * Delete exact linematch for $badline from $file. + * + * Be sure to include the trailing newline in $badline * * Uses gzip if extension is .gz * * @author Steven Danz <steven-danz@kc.rr.com> * @return bool true on success */ -function io_appendFile($file,$content){ - io_makeFileDir($file); +function io_deleteFromFile($file,$badline){ + if (!@file_exists($file)) return true; + io_lock($file); + + // load into array if(substr($file,-3) == '.gz'){ - $fh = @gzopen($file,'ab9'); - if(!$fh){ - msg("Appending to $file failed",-1); - return false; - } - gzwrite($fh, $content); - gzclose($fh); + $lines = gzfile($file); }else{ - $fh = @fopen($file,'ab'); - if(!$fh){ - msg("Appending to $file failed",-1); - return false; - } - fwrite($fh, $content); - fclose($fh); + $lines = file($file); } - io_unlock($file); - return true; -} -/** - * Delete exact match for $content from $file. - * - * Uses gzip if extension is .gz - * - * @author Steven Danz <steven-danz@kc.rr.com> - * @return bool true on success - */ -function io_deleteFromFile($file,$remove){ - if (@file_exists($file)) { - io_lock($file); - $content = ''; + // remove all matching lines + $pos = array_search($badline,$lines); //return null or false if not found + while(is_int($pos)){ + unset($lines[$pos]); + $pos = array_search($badline,$lines); + } + + if(count($lines)){ + $content = join('',$lines); if(substr($file,-3) == '.gz'){ - $mlist = gzfile($file); - }else{ - $mlist = file($file); - } - foreach ($mlist as $entry) { - if ($entry != $remove) { - $content = $content . $entry; + $fh = @gzopen($file,'wb9'); + if(!$fh){ + msg("Removing content from $file failed",-1); + return false; } - } - - if (!empty($content)) { - if(substr($file,-3) == '.gz'){ - $fh = @gzopen($file,'wb9'); - if(!$fh){ - msg("Removing content from $file failed",-1); - return false; - } - gzwrite($fh, $content); - gzclose($fh); - }else{ - $fh = @fopen($file,'wb'); - if(!$fh){ - msg("Removing content from $file failed",-1); - return false; - } - fwrite($fh, $content); - fclose($fh); + gzwrite($fh, $content); + gzclose($fh); + }else{ + $fh = @fopen($file,'wb'); + if(!$fh){ + msg("Removing content from $file failed",-1); + return false; } - } else { - @unlink($file); + fwrite($fh, $content); + fclose($fh); } - - io_unlock($file); + }else{ + @unlink($file); } + + io_unlock($file); return true; } |