From c563479888808340948c200dbedfa584df857697 Mon Sep 17 00:00:00 2001
From: Johannes Meyer <johannes.meyer@netways.de>
Date: Mon, 23 Jun 2014 15:01:52 +0200
Subject: [PATCH] Use Icinga\Util\File instead of fopen

---
 library/Icinga/Application/Modules/Module.php     |  7 +++++--
 library/Icinga/Logger/Writer/FileWriter.php       | 10 ++--------
 .../Icinga/User/Preferences/Store/IniStore.php    |  2 +-
 library/Icinga/Util/File.php                      | 15 +++++++++++++++
 4 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php
index e732ba412..f0531ee48 100644
--- a/library/Icinga/Application/Modules/Module.php
+++ b/library/Icinga/Application/Modules/Module.php
@@ -17,6 +17,7 @@ use Icinga\Application\Icinga;
 use Icinga\Logger\Logger;
 use Icinga\Util\Translator;
 use Icinga\Web\Hook;
+use Icinga\Util\File;
 
 /**
  * Module handling
@@ -319,10 +320,12 @@ class Module
 
             if (file_exists($this->metadataFile)) {
 
-                $fh = fopen($this->metadataFile, 'r');
+                $file = File::open($this->metadataFile, 'r');
+                $lines = $file->readlines();
+                $file->close();
                 $key = null;
 
-                while (false !== ($line = fgets($fh))) {
+                foreach ($lines as $line) {
                     $line = rtrim($line);
 
                     if ($key === 'description') {
diff --git a/library/Icinga/Logger/Writer/FileWriter.php b/library/Icinga/Logger/Writer/FileWriter.php
index e77c0c577..cc4772269 100644
--- a/library/Icinga/Logger/Writer/FileWriter.php
+++ b/library/Icinga/Logger/Writer/FileWriter.php
@@ -6,9 +6,9 @@ namespace Icinga\Logger\Writer;
 
 use Exception;
 use Zend_Config;
+use Icinga\Util\File;
 use Icinga\Logger\Logger;
 use Icinga\Logger\LogWriter;
-use Icinga\Application\Config;
 use Icinga\Exception\ConfigurationError;
 
 /**
@@ -93,12 +93,6 @@ class FileWriter extends LogWriter
      */
     protected function write($text)
     {
-        $fd = fopen($this->path, 'a');
-
-        if ($fd === false || fwrite($fd, $text . PHP_EOL) === false) {
-            throw new Exception('Failed to write to log file "' . $this->path . '"');
-        }
-
-        fclose($fd);
+        File::open($this->path, 'a')->write($text . PHP_EOL)->close();
     }
 }
diff --git a/library/Icinga/User/Preferences/Store/IniStore.php b/library/Icinga/User/Preferences/Store/IniStore.php
index 7363f2569..cf20f3b8c 100644
--- a/library/Icinga/User/Preferences/Store/IniStore.php
+++ b/library/Icinga/User/Preferences/Store/IniStore.php
@@ -109,7 +109,7 @@ class IniStore extends PreferencesStore
                     );
                 }
 
-                File::create($this->preferencesFile);
+                File::open($this->preferencesFile, 'a')->chmod(0664)->close();
             }
 
             if (!is_writable($this->preferencesFile)) {
diff --git a/library/Icinga/Util/File.php b/library/Icinga/Util/File.php
index 6fdd96e4a..34cdcc2e4 100644
--- a/library/Icinga/Util/File.php
+++ b/library/Icinga/Util/File.php
@@ -78,6 +78,21 @@ class File
         return $content;
     }
 
+    /**
+     * Read file line by line
+     *
+     * @return  array
+     */
+    public function readlines()
+    {
+        $lines = array();
+        while (($line = fgets($this->handle)) !== false) {
+            $lines[] = $line;
+        }
+
+        return $lines;
+    }
+
     /**
      * Write contents to file
      *