]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge remote-tracking branch 'virtualtam/linkdb/remove-globals'
authornodiscc <nodiscc@gmail.com>
Fri, 26 Jun 2015 20:03:10 +0000 (22:03 +0200)
committernodiscc <nodiscc@gmail.com>
Fri, 26 Jun 2015 20:03:10 +0000 (22:03 +0200)
1  2 
application/LinkDB.php

diff --combined application/LinkDB.php
index cf91ad481ddb84db88da8ed312b3c9ba0a7b3858,7e29ee8eefaf8e65d44eae2e741ed343f04b2874..ff82446f0d7a892c63d04d5231cdf0e74b12ccce
   */
  class LinkDB implements Iterator, Countable, ArrayAccess
  {
+     // Links are stored as a PHP serialized string
+     private $datastore;
+     // Datastore PHP prefix
+     protected static $phpPrefix = '<?php /* ';
+     // Datastore PHP suffix
+     protected static $phpSuffix = ' */ ?>';
      // List of links (associative array)
      //  - key:   link date (e.g. "20110823_124546"),
      //  - value: associative array (keys: title, description...)
@@@ -55,9 -64,9 +64,9 @@@
       *
       * @param $isLoggedIn is the user logged in?
       */
-     function __construct($isLoggedIn, $hidePublicLinks)
+     function __construct($datastore, $isLoggedIn, $hidePublicLinks)
      {
-         // FIXME: do not access $GLOBALS, pass the datastore instead
+         $this->datastore = $datastore;
          $this->loggedIn = $isLoggedIn;
          $this->hidePublicLinks = $hidePublicLinks;
          $this->checkDB();
       */
      private function checkDB()
      {
-         if (file_exists($GLOBALS['config']['DATASTORE'])) {
+         if (file_exists($this->datastore)) {
              return;
          }
  
          // Write database to disk
          // TODO: raise an exception if the file is not write-able
          file_put_contents(
-             // FIXME: do not use $GLOBALS
-             $GLOBALS['config']['DATASTORE'],
-             PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
+             $this->datastore,
+             self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
          );
      }
  
          // Read data
          // Note that gzinflate is faster than gzuncompress.
          // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
-         // FIXME: do not use $GLOBALS
          $this->links = array();
  
-         if (file_exists($GLOBALS['config']['DATASTORE'])) {
+         if (file_exists($this->datastore)) {
              $this->links = unserialize(gzinflate(base64_decode(
-                 substr(file_get_contents($GLOBALS['config']['DATASTORE']),
-                        strlen(PHPPREFIX), -strlen(PHPSUFFIX)))));
+                 substr(file_get_contents($this->datastore),
+                        strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
          }
  
          // If user is not logged in, filter private links.
              die('You are not authorized to change the database.');
          }
          file_put_contents(
-             $GLOBALS['config']['DATASTORE'],
-             PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
+             $this->datastore,
+             self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
          );
          invalidateCaches();
      }
          // FIXME: accept double-quotes to search for a string "as is"?
          $filtered = array();
          $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
 -        $keys = ['title', 'description', 'url', 'tags'];
 +        $keys = array('title', 'description', 'url', 'tags');
  
          foreach ($this->links as $link) {
              $found = false;