aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/LinkDB.php33
1 files changed, 20 insertions, 13 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index ca619bba..a673b086 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -27,6 +27,15 @@
27 */ 27 */
28class LinkDB implements Iterator, Countable, ArrayAccess 28class 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
@@ -205,9 +214,8 @@ You use the community supported version of the original Shaarli project, by Seba
205 // Write database to disk 214 // Write database to disk
206 // TODO: raise an exception if the file is not write-able 215 // TODO: raise an exception if the file is not write-able
207 file_put_contents( 216 file_put_contents(
208 // FIXME: do not use $GLOBALS 217 $this->datastore,
209 $GLOBALS['config']['DATASTORE'], 218 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
210 PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
211 ); 219 );
212 } 220 }
213 221
@@ -226,13 +234,12 @@ You use the community supported version of the original Shaarli project, by Seba
226 // Read data 234 // Read data
227 // Note that gzinflate is faster than gzuncompress. 235 // Note that gzinflate is faster than gzuncompress.
228 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 236 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
229 // FIXME: do not use $GLOBALS
230 $this->links = array(); 237 $this->links = array();
231 238
232 if (file_exists($GLOBALS['config']['DATASTORE'])) { 239 if (file_exists($this->datastore)) {
233 $this->links = unserialize(gzinflate(base64_decode( 240 $this->links = unserialize(gzinflate(base64_decode(
234 substr(file_get_contents($GLOBALS['config']['DATASTORE']), 241 substr(file_get_contents($this->datastore),
235 strlen(PHPPREFIX), -strlen(PHPSUFFIX))))); 242 strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
236 } 243 }
237 244
238 // If user is not logged in, filter private links. 245 // If user is not logged in, filter private links.
@@ -270,8 +277,8 @@ You use the community supported version of the original Shaarli project, by Seba
270 die('You are not authorized to change the database.'); 277 die('You are not authorized to change the database.');
271 } 278 }
272 file_put_contents( 279 file_put_contents(
273 $GLOBALS['config']['DATASTORE'], 280 $this->datastore,
274 PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX 281 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
275 ); 282 );
276 invalidateCaches(); 283 invalidateCaches();
277 } 284 }
@@ -307,7 +314,7 @@ You use the community supported version of the original Shaarli project, by Seba
307 // FIXME: accept double-quotes to search for a string "as is"? 314 // FIXME: accept double-quotes to search for a string "as is"?
308 $filtered = array(); 315 $filtered = array();
309 $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8'); 316 $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
310 $keys = ['title', 'description', 'url', 'tags']; 317 $keys = array('title', 'description', 'url', 'tags');
311 318
312 foreach ($this->links as $link) { 319 foreach ($this->links as $link) {
313 $found = false; 320 $found = false;