diff options
Diffstat (limited to 'application')
-rw-r--r-- | application/LinkDB.php | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php index cf91ad48..ff82446f 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php | |||
@@ -27,6 +27,15 @@ | |||
27 | */ | 27 | */ |
28 | class LinkDB implements Iterator, Countable, ArrayAccess | 28 | class LinkDB implements Iterator, Countable, ArrayAccess |
29 | { | 29 | { |
30 | // Links are stored as a PHP serialized string | ||
31 | private $datastore; | ||
32 | |||
33 | // Datastore PHP prefix | ||
34 | protected static $phpPrefix = '<?php /* '; | ||
35 | |||
36 | // Datastore PHP suffix | ||
37 | protected static $phpSuffix = ' */ ?>'; | ||
38 | |||
30 | // List of links (associative array) | 39 | // List of links (associative array) |
31 | // - key: link date (e.g. "20110823_124546"), | 40 | // - key: link date (e.g. "20110823_124546"), |
32 | // - value: associative array (keys: title, description...) | 41 | // - value: associative array (keys: title, description...) |
@@ -55,9 +64,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
55 | * | 64 | * |
56 | * @param $isLoggedIn is the user logged in? | 65 | * @param $isLoggedIn is the user logged in? |
57 | */ | 66 | */ |
58 | function __construct($isLoggedIn, $hidePublicLinks) | 67 | function __construct($datastore, $isLoggedIn, $hidePublicLinks) |
59 | { | 68 | { |
60 | // FIXME: do not access $GLOBALS, pass the datastore instead | 69 | $this->datastore = $datastore; |
61 | $this->loggedIn = $isLoggedIn; | 70 | $this->loggedIn = $isLoggedIn; |
62 | $this->hidePublicLinks = $hidePublicLinks; | 71 | $this->hidePublicLinks = $hidePublicLinks; |
63 | $this->checkDB(); | 72 | $this->checkDB(); |
@@ -172,7 +181,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
172 | */ | 181 | */ |
173 | private function checkDB() | 182 | private function checkDB() |
174 | { | 183 | { |
175 | if (file_exists($GLOBALS['config']['DATASTORE'])) { | 184 | if (file_exists($this->datastore)) { |
176 | return; | 185 | return; |
177 | } | 186 | } |
178 | 187 | ||
@@ -201,9 +210,8 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
201 | // Write database to disk | 210 | // Write database to disk |
202 | // TODO: raise an exception if the file is not write-able | 211 | // TODO: raise an exception if the file is not write-able |
203 | file_put_contents( | 212 | file_put_contents( |
204 | // FIXME: do not use $GLOBALS | 213 | $this->datastore, |
205 | $GLOBALS['config']['DATASTORE'], | 214 | self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix |
206 | PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX | ||
207 | ); | 215 | ); |
208 | } | 216 | } |
209 | 217 | ||
@@ -222,13 +230,12 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
222 | // Read data | 230 | // Read data |
223 | // Note that gzinflate is faster than gzuncompress. | 231 | // Note that gzinflate is faster than gzuncompress. |
224 | // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 | 232 | // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 |
225 | // FIXME: do not use $GLOBALS | ||
226 | $this->links = array(); | 233 | $this->links = array(); |
227 | 234 | ||
228 | if (file_exists($GLOBALS['config']['DATASTORE'])) { | 235 | if (file_exists($this->datastore)) { |
229 | $this->links = unserialize(gzinflate(base64_decode( | 236 | $this->links = unserialize(gzinflate(base64_decode( |
230 | substr(file_get_contents($GLOBALS['config']['DATASTORE']), | 237 | substr(file_get_contents($this->datastore), |
231 | strlen(PHPPREFIX), -strlen(PHPSUFFIX))))); | 238 | strlen(self::$phpPrefix), -strlen(self::$phpSuffix))))); |
232 | } | 239 | } |
233 | 240 | ||
234 | // If user is not logged in, filter private links. | 241 | // If user is not logged in, filter private links. |
@@ -266,8 +273,8 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
266 | die('You are not authorized to change the database.'); | 273 | die('You are not authorized to change the database.'); |
267 | } | 274 | } |
268 | file_put_contents( | 275 | file_put_contents( |
269 | $GLOBALS['config']['DATASTORE'], | 276 | $this->datastore, |
270 | PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX | 277 | self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix |
271 | ); | 278 | ); |
272 | invalidateCaches(); | 279 | invalidateCaches(); |
273 | } | 280 | } |