diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-06-24 23:01:21 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-06-24 23:26:52 +0200 |
commit | 9c8752a2061e67c719125edb6e0d6717d1af8553 (patch) | |
tree | d7f05ff1ce871105bb44625baeacc8e8b2bfec2b | |
parent | 64bc92e3ac8f5e66d2bc14206ede31e6679d8c13 (diff) | |
download | Shaarli-9c8752a2061e67c719125edb6e0d6717d1af8553.tar.gz Shaarli-9c8752a2061e67c719125edb6e0d6717d1af8553.tar.zst Shaarli-9c8752a2061e67c719125edb6e0d6717d1af8553.zip |
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 <virtualtam@flibidi.net>
-rw-r--r-- | application/LinkDB.php | 31 | ||||
-rw-r--r-- | index.php | 8 | ||||
-rw-r--r-- | tests/LinkDBTest.php | 32 | ||||
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 4 |
4 files changed, 40 insertions, 35 deletions
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 @@ | |||
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 | } |
@@ -41,8 +41,6 @@ $GLOBALS['config']['HIDE_PUBLIC_LINKS'] = false; | |||
41 | if (is_file($GLOBALS['config']['DATADIR'].'/options.php')) require($GLOBALS['config']['DATADIR'].'/options.php'); | 41 | if (is_file($GLOBALS['config']['DATADIR'].'/options.php')) require($GLOBALS['config']['DATADIR'].'/options.php'); |
42 | 42 | ||
43 | define('shaarli_version','0.0.45beta'); | 43 | define('shaarli_version','0.0.45beta'); |
44 | define('PHPPREFIX','<?php /* '); // Prefix to encapsulate data in PHP code. | ||
45 | define('PHPSUFFIX',' */ ?>'); // Suffix to encapsulate data in PHP code. | ||
46 | // http://server.com/x/shaarli --> /shaarli/ | 44 | // http://server.com/x/shaarli --> /shaarli/ |
47 | define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); | 45 | define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); |
48 | 46 | ||
@@ -700,6 +698,7 @@ function showRSS() | |||
700 | 698 | ||
701 | // If cached was not found (or not usable), then read the database and build the response: | 699 | // If cached was not found (or not usable), then read the database and build the response: |
702 | $LINKSDB = new LinkDB( | 700 | $LINKSDB = new LinkDB( |
701 | $GLOBALS['config']['DATASTORE'], | ||
703 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 702 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
704 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] | 703 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] |
705 | ); | 704 | ); |
@@ -780,6 +779,7 @@ function showATOM() | |||
780 | 779 | ||
781 | // Read links from database (and filter private links if used it not logged in). | 780 | // Read links from database (and filter private links if used it not logged in). |
782 | $LINKSDB = new LinkDB( | 781 | $LINKSDB = new LinkDB( |
782 | $GLOBALS['config']['DATASTORE'], | ||
783 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 783 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
784 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] | 784 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] |
785 | ); | 785 | ); |
@@ -866,6 +866,7 @@ function showDailyRSS() | |||
866 | 866 | ||
867 | // Read links from database (and filter private links if used it not logged in). | 867 | // Read links from database (and filter private links if used it not logged in). |
868 | $LINKSDB = new LinkDB( | 868 | $LINKSDB = new LinkDB( |
869 | $GLOBALS['config']['DATASTORE'], | ||
869 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 870 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
870 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] | 871 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] |
871 | ); | 872 | ); |
@@ -937,6 +938,7 @@ function showDailyRSS() | |||
937 | function showDaily() | 938 | function showDaily() |
938 | { | 939 | { |
939 | $LINKSDB = new LinkDB( | 940 | $LINKSDB = new LinkDB( |
941 | $GLOBALS['config']['DATASTORE'], | ||
940 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 942 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
941 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] | 943 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] |
942 | ); | 944 | ); |
@@ -1006,6 +1008,7 @@ function showDaily() | |||
1006 | function renderPage() | 1008 | function renderPage() |
1007 | { | 1009 | { |
1008 | $LINKSDB = new LinkDB( | 1010 | $LINKSDB = new LinkDB( |
1011 | $GLOBALS['config']['DATASTORE'], | ||
1009 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 1012 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
1010 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] | 1013 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] |
1011 | ); | 1014 | ); |
@@ -1587,6 +1590,7 @@ function importFile() | |||
1587 | { | 1590 | { |
1588 | if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); } | 1591 | if (!(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'])) { die('Not allowed.'); } |
1589 | $LINKSDB = new LinkDB( | 1592 | $LINKSDB = new LinkDB( |
1593 | $GLOBALS['config']['DATASTORE'], | ||
1590 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 1594 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
1591 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] | 1595 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'] |
1592 | ); | 1596 | ); |
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index f67d4d9b..0d343359 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -7,9 +7,6 @@ require_once 'application/LinkDB.php'; | |||
7 | require_once 'application/Utils.php'; | 7 | require_once 'application/Utils.php'; |
8 | require_once 'tests/utils/ReferenceLinkDB.php'; | 8 | require_once 'tests/utils/ReferenceLinkDB.php'; |
9 | 9 | ||
10 | define('PHPPREFIX', '<?php /* '); | ||
11 | define('PHPSUFFIX', ' */ ?>'); | ||
12 | |||
13 | 10 | ||
14 | /** | 11 | /** |
15 | * Unitary tests for LinkDB | 12 | * Unitary tests for LinkDB |
@@ -38,11 +35,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
38 | public static function setUpBeforeClass() | 35 | public static function setUpBeforeClass() |
39 | { | 36 | { |
40 | self::$refDB = new ReferenceLinkDB(); | 37 | self::$refDB = new ReferenceLinkDB(); |
41 | self::$refDB->write(self::$testDatastore, PHPPREFIX, PHPSUFFIX); | 38 | self::$refDB->write(self::$testDatastore); |
42 | 39 | ||
43 | $GLOBALS['config']['DATASTORE'] = self::$testDatastore; | 40 | self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false); |
44 | self::$publicLinkDB = new LinkDB(false, false); | 41 | self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false); |
45 | self::$privateLinkDB = new LinkDB(true, false); | ||
46 | } | 42 | } |
47 | 43 | ||
48 | /** | 44 | /** |
@@ -50,7 +46,6 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
50 | */ | 46 | */ |
51 | protected function setUp() | 47 | protected function setUp() |
52 | { | 48 | { |
53 | $GLOBALS['config']['DATASTORE'] = self::$testDatastore; | ||
54 | if (file_exists(self::$testDatastore)) { | 49 | if (file_exists(self::$testDatastore)) { |
55 | unlink(self::$testDatastore); | 50 | unlink(self::$testDatastore); |
56 | } | 51 | } |
@@ -76,7 +71,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
76 | */ | 71 | */ |
77 | public function testConstructLoggedIn() | 72 | public function testConstructLoggedIn() |
78 | { | 73 | { |
79 | new LinkDB(true, false); | 74 | new LinkDB(self::$testDatastore, true, false); |
80 | $this->assertFileExists(self::$testDatastore); | 75 | $this->assertFileExists(self::$testDatastore); |
81 | } | 76 | } |
82 | 77 | ||
@@ -85,7 +80,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
85 | */ | 80 | */ |
86 | public function testConstructLoggedOut() | 81 | public function testConstructLoggedOut() |
87 | { | 82 | { |
88 | new LinkDB(false, false); | 83 | new LinkDB(self::$testDatastore, false, false); |
89 | $this->assertFileExists(self::$testDatastore); | 84 | $this->assertFileExists(self::$testDatastore); |
90 | } | 85 | } |
91 | 86 | ||
@@ -97,8 +92,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
97 | */ | 92 | */ |
98 | public function testConstructDatastoreNotWriteable() | 93 | public function testConstructDatastoreNotWriteable() |
99 | { | 94 | { |
100 | $GLOBALS['config']['DATASTORE'] = 'null/store.db'; | 95 | new LinkDB('null/store.db', false, false); |
101 | new LinkDB(false, false); | ||
102 | } | 96 | } |
103 | 97 | ||
104 | /** | 98 | /** |
@@ -106,7 +100,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
106 | */ | 100 | */ |
107 | public function testCheckDBNew() | 101 | public function testCheckDBNew() |
108 | { | 102 | { |
109 | $linkDB = new LinkDB(false, false); | 103 | $linkDB = new LinkDB(self::$testDatastore, false, false); |
110 | unlink(self::$testDatastore); | 104 | unlink(self::$testDatastore); |
111 | $this->assertFileNotExists(self::$testDatastore); | 105 | $this->assertFileNotExists(self::$testDatastore); |
112 | 106 | ||
@@ -126,7 +120,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
126 | */ | 120 | */ |
127 | public function testCheckDBLoad() | 121 | public function testCheckDBLoad() |
128 | { | 122 | { |
129 | $linkDB = new LinkDB(false, false); | 123 | $linkDB = new LinkDB(self::$testDatastore, false, false); |
130 | $this->assertEquals( | 124 | $this->assertEquals( |
131 | self::$dummyDatastoreSHA1, | 125 | self::$dummyDatastoreSHA1, |
132 | sha1_file(self::$testDatastore) | 126 | sha1_file(self::$testDatastore) |
@@ -147,8 +141,8 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
147 | */ | 141 | */ |
148 | public function testReadEmptyDB() | 142 | public function testReadEmptyDB() |
149 | { | 143 | { |
150 | file_put_contents(self::$testDatastore, PHPPREFIX.'S7QysKquBQA='.PHPSUFFIX); | 144 | file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); |
151 | $emptyDB = new LinkDB(false, false); | 145 | $emptyDB = new LinkDB(self::$testDatastore, false, false); |
152 | $this->assertEquals(0, sizeof($emptyDB)); | 146 | $this->assertEquals(0, sizeof($emptyDB)); |
153 | $this->assertEquals(0, count($emptyDB)); | 147 | $this->assertEquals(0, count($emptyDB)); |
154 | } | 148 | } |
@@ -180,7 +174,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
180 | */ | 174 | */ |
181 | public function testSaveDB() | 175 | public function testSaveDB() |
182 | { | 176 | { |
183 | $testDB = new LinkDB(true, false); | 177 | $testDB = new LinkDB(self::$testDatastore, true, false); |
184 | $dbSize = sizeof($testDB); | 178 | $dbSize = sizeof($testDB); |
185 | 179 | ||
186 | $link = array( | 180 | $link = array( |
@@ -198,7 +192,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
198 | 192 | ||
199 | $testDB->savedb(); | 193 | $testDB->savedb(); |
200 | 194 | ||
201 | $testDB = new LinkDB(true, false); | 195 | $testDB = new LinkDB(self::$testDatastore, true, false); |
202 | $this->assertEquals($dbSize + 1, sizeof($testDB)); | 196 | $this->assertEquals($dbSize + 1, sizeof($testDB)); |
203 | } | 197 | } |
204 | 198 | ||
@@ -222,7 +216,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
222 | */ | 216 | */ |
223 | public function testCountHiddenPublic() | 217 | public function testCountHiddenPublic() |
224 | { | 218 | { |
225 | $linkDB = new LinkDB(false, true); | 219 | $linkDB = new LinkDB(self::$testDatastore, false, true); |
226 | 220 | ||
227 | $this->assertEquals( | 221 | $this->assertEquals( |
228 | 0, | 222 | 0, |
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 2cb05bae..59ba671f 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php | |||
@@ -93,11 +93,11 @@ class ReferenceLinkDB | |||
93 | /** | 93 | /** |
94 | * Writes data to the datastore | 94 | * Writes data to the datastore |
95 | */ | 95 | */ |
96 | public function write($filename, $prefix, $suffix) | 96 | public function write($filename) |
97 | { | 97 | { |
98 | file_put_contents( | 98 | file_put_contents( |
99 | $filename, | 99 | $filename, |
100 | $prefix.base64_encode(gzdeflate(serialize($this->links))).$suffix | 100 | '<?php /* '.base64_encode(gzdeflate(serialize($this->links))).' */ ?>' |
101 | ); | 101 | ); |
102 | } | 102 | } |
103 | 103 | ||