summaryrefslogtreecommitdiff
path: root/inc/io.php
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2005-08-06 23:20:00 +0200
committerAndreas Gohr <andi@splitbrain.org>2005-08-06 23:20:00 +0200
commit1380fc452d56dd6f48ddbfa3a6b0e69edd043b04 (patch)
tree0654df1e2d7a86be45be1ced2624e73e1a4e565a /inc/io.php
parentb158d625b53833ef391800a991ad93d965d9425e (diff)
downloadrpg-1380fc452d56dd6f48ddbfa3a6b0e69edd043b04.tar.gz
rpg-1380fc452d56dd6f48ddbfa3a6b0e69edd043b04.tar.bz2
changes to the page subscription feature
darcs-hash:20050806212000-7ad00-c5ab54a33289f8be0ce99443f82f0b3cf1bdbf0d.gz
Diffstat (limited to 'inc/io.php')
-rw-r--r--inc/io.php109
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;
}