]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/LinkDB.php
install: check file/directory permissions for Shaarli resources
[github/shaarli/Shaarli.git] / application / LinkDB.php
index 1e16fef179fefa843ef7f10eb62d3471e593928c..15fadbc3a6baa67e552517b429635aa5f0c79dd1 100644 (file)
@@ -212,11 +212,7 @@ You use the community supported version of the original Shaarli project, by Seba
         $this->_links[$link['linkdate']] = $link;
 
         // Write database to disk
-        // TODO: raise an exception if the file is not write-able
-        file_put_contents(
-            $this->_datastore,
-            self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix
-        );
+        $this->writeDB();
     }
 
     /**
@@ -269,22 +265,49 @@ You use the community supported version of the original Shaarli project, by Seba
 
     /**
      * Saves the database from memory to disk
+     *
+     * @throws IOException the datastore is not writable
      */
-    public function savedb()
+    private function writeDB()
     {
-        if (!$this->_loggedIn) {
-            // TODO: raise an Exception instead
-            die('You are not authorized to change the database.');
+        if (is_file($this->_datastore) && !is_writeable($this->_datastore)) {
+            // The datastore exists but is not writeable
+            throw new IOException($this->_datastore);
+        } else if (!is_file($this->_datastore) && !is_writeable(dirname($this->_datastore))) {
+            // The datastore does not exist and its parent directory is not writeable
+            throw new IOException(dirname($this->_datastore));
         }
+
         file_put_contents(
             $this->_datastore,
             self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix
         );
-        invalidateCaches();
+
+    }
+
+    /**
+     * Saves the database from memory to disk
+     *
+     * @param string $pageCacheDir page cache directory
+     */
+    public function savedb($pageCacheDir)
+    {
+        if (!$this->_loggedIn) {
+            // TODO: raise an Exception instead
+            die('You are not authorized to change the database.');
+        }
+
+        $this->writeDB();
+
+        invalidateCaches($pageCacheDir);
     }
 
     /**
      * Returns the link for a given URL, or False if it does not exist.
+     *
+     * @param string $url URL to search for
+     *
+     * @return mixed the existing link if it exists, else 'false'
      */
     public function getLinkFromUrl($url)
     {
@@ -439,4 +462,3 @@ You use the community supported version of the original Shaarli project, by Seba
         return $linkDays;
     }
 }
-?>