From 9c8752a2061e67c719125edb6e0d6717d1af8553 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Wed, 24 Jun 2015 23:01:21 +0200 Subject: LinkDB: do not access global variables Relates to #218 Removes "hidden" access to the following variables: - $GLOBALS['config']['datastore'] - PHPPREFIX - PHPSUFFIX Signed-off-by: VirtualTam --- application/LinkDB.php | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'application/LinkDB.php') diff --git a/application/LinkDB.php b/application/LinkDB.php index 2b3fb60b..7e29ee8e 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -27,6 +27,15 @@ */ class LinkDB implements Iterator, Countable, ArrayAccess { + // Links are stored as a PHP serialized string + private $datastore; + + // Datastore PHP prefix + protected static $phpPrefix = ''; + // List of links (associative array) // - key: link date (e.g. "20110823_124546"), // - value: associative array (keys: title, description...) @@ -55,9 +64,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess * * @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(); @@ -172,7 +181,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess */ private function checkDB() { - if (file_exists($GLOBALS['config']['DATASTORE'])) { + if (file_exists($this->datastore)) { return; } @@ -201,9 +210,8 @@ class LinkDB implements Iterator, Countable, ArrayAccess // 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 ); } @@ -222,13 +230,12 @@ class LinkDB implements Iterator, Countable, ArrayAccess // 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. @@ -266,8 +273,8 @@ class LinkDB implements Iterator, Countable, ArrayAccess 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(); } -- cgit v1.2.3