aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/LinkDB.php134
1 files changed, 67 insertions, 67 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index de9e73b0..2d42c514 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -31,7 +31,7 @@
31class LinkDB implements Iterator, Countable, ArrayAccess 31class LinkDB implements Iterator, Countable, ArrayAccess
32{ 32{
33 // Links are stored as a PHP serialized string 33 // Links are stored as a PHP serialized string
34 private $_datastore; 34 private $datastore;
35 35
36 // Link date storage format 36 // Link date storage format
37 const LINK_DATE_FORMAT = 'Ymd_His'; 37 const LINK_DATE_FORMAT = 'Ymd_His';
@@ -45,26 +45,26 @@ class LinkDB implements Iterator, Countable, ArrayAccess
45 // List of links (associative array) 45 // List of links (associative array)
46 // - key: link date (e.g. "20110823_124546"), 46 // - key: link date (e.g. "20110823_124546"),
47 // - value: associative array (keys: title, description...) 47 // - value: associative array (keys: title, description...)
48 private $_links; 48 private $links;
49 49
50 // List of all recorded URLs (key=url, value=linkdate) 50 // List of all recorded URLs (key=url, value=linkdate)
51 // for fast reserve search (url-->linkdate) 51 // for fast reserve search (url-->linkdate)
52 private $_urls; 52 private $urls;
53 53
54 // List of linkdate keys (for the Iterator interface implementation) 54 // List of linkdate keys (for the Iterator interface implementation)
55 private $_keys; 55 private $keys;
56 56
57 // Position in the $this->_keys array (for the Iterator interface) 57 // Position in the $this->keys array (for the Iterator interface)
58 private $_position; 58 private $position;
59 59
60 // Is the user logged in? (used to filter private links) 60 // Is the user logged in? (used to filter private links)
61 private $_loggedIn; 61 private $loggedIn;
62 62
63 // Hide public links 63 // Hide public links
64 private $_hidePublicLinks; 64 private $hidePublicLinks;
65 65
66 // link redirector set in user settings. 66 // link redirector set in user settings.
67 private $_redirector; 67 private $redirector;
68 68
69 /** 69 /**
70 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched. 70 * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
@@ -95,13 +95,13 @@ class LinkDB implements Iterator, Countable, ArrayAccess
95 $redirectorEncode = true 95 $redirectorEncode = true
96 ) 96 )
97 { 97 {
98 $this->_datastore = $datastore; 98 $this->datastore = $datastore;
99 $this->_loggedIn = $isLoggedIn; 99 $this->loggedIn = $isLoggedIn;
100 $this->_hidePublicLinks = $hidePublicLinks; 100 $this->hidePublicLinks = $hidePublicLinks;
101 $this->_redirector = $redirector; 101 $this->redirector = $redirector;
102 $this->redirectorEncode = $redirectorEncode === true; 102 $this->redirectorEncode = $redirectorEncode === true;
103 $this->_checkDB(); 103 $this->checkDB();
104 $this->_readDB(); 104 $this->readDB();
105 } 105 }
106 106
107 /** 107 /**
@@ -109,7 +109,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
109 */ 109 */
110 public function count() 110 public function count()
111 { 111 {
112 return count($this->_links); 112 return count($this->links);
113 } 113 }
114 114
115 /** 115 /**
@@ -118,7 +118,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
118 public function offsetSet($offset, $value) 118 public function offsetSet($offset, $value)
119 { 119 {
120 // TODO: use exceptions instead of "die" 120 // TODO: use exceptions instead of "die"
121 if (!$this->_loggedIn) { 121 if (!$this->loggedIn) {
122 die('You are not authorized to add a link.'); 122 die('You are not authorized to add a link.');
123 } 123 }
124 if (empty($value['linkdate']) || empty($value['url'])) { 124 if (empty($value['linkdate']) || empty($value['url'])) {
@@ -127,8 +127,8 @@ class LinkDB implements Iterator, Countable, ArrayAccess
127 if (empty($offset)) { 127 if (empty($offset)) {
128 die('You must specify a key.'); 128 die('You must specify a key.');
129 } 129 }
130 $this->_links[$offset] = $value; 130 $this->links[$offset] = $value;
131 $this->_urls[$value['url']]=$offset; 131 $this->urls[$value['url']]=$offset;
132 } 132 }
133 133
134 /** 134 /**
@@ -136,7 +136,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
136 */ 136 */
137 public function offsetExists($offset) 137 public function offsetExists($offset)
138 { 138 {
139 return array_key_exists($offset, $this->_links); 139 return array_key_exists($offset, $this->links);
140 } 140 }
141 141
142 /** 142 /**
@@ -144,13 +144,13 @@ class LinkDB implements Iterator, Countable, ArrayAccess
144 */ 144 */
145 public function offsetUnset($offset) 145 public function offsetUnset($offset)
146 { 146 {
147 if (!$this->_loggedIn) { 147 if (!$this->loggedIn) {
148 // TODO: raise an exception 148 // TODO: raise an exception
149 die('You are not authorized to delete a link.'); 149 die('You are not authorized to delete a link.');
150 } 150 }
151 $url = $this->_links[$offset]['url']; 151 $url = $this->links[$offset]['url'];
152 unset($this->_urls[$url]); 152 unset($this->urls[$url]);
153 unset($this->_links[$offset]); 153 unset($this->links[$offset]);
154 } 154 }
155 155
156 /** 156 /**
@@ -158,7 +158,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
158 */ 158 */
159 public function offsetGet($offset) 159 public function offsetGet($offset)
160 { 160 {
161 return isset($this->_links[$offset]) ? $this->_links[$offset] : null; 161 return isset($this->links[$offset]) ? $this->links[$offset] : null;
162 } 162 }
163 163
164 /** 164 /**
@@ -166,7 +166,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
166 */ 166 */
167 function current() 167 function current()
168 { 168 {
169 return $this->_links[$this->_keys[$this->_position]]; 169 return $this->links[$this->keys[$this->position]];
170 } 170 }
171 171
172 /** 172 /**
@@ -174,7 +174,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
174 */ 174 */
175 function key() 175 function key()
176 { 176 {
177 return $this->_keys[$this->_position]; 177 return $this->keys[$this->position];
178 } 178 }
179 179
180 /** 180 /**
@@ -182,7 +182,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
182 */ 182 */
183 function next() 183 function next()
184 { 184 {
185 ++$this->_position; 185 ++$this->position;
186 } 186 }
187 187
188 /** 188 /**
@@ -192,9 +192,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess
192 */ 192 */
193 function rewind() 193 function rewind()
194 { 194 {
195 $this->_keys = array_keys($this->_links); 195 $this->keys = array_keys($this->links);
196 rsort($this->_keys); 196 rsort($this->keys);
197 $this->_position = 0; 197 $this->position = 0;
198 } 198 }
199 199
200 /** 200 /**
@@ -202,7 +202,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
202 */ 202 */
203 function valid() 203 function valid()
204 { 204 {
205 return isset($this->_keys[$this->_position]); 205 return isset($this->keys[$this->position]);
206 } 206 }
207 207
208 /** 208 /**
@@ -210,14 +210,14 @@ class LinkDB implements Iterator, Countable, ArrayAccess
210 * 210 *
211 * If no DB file is found, creates a dummy DB. 211 * If no DB file is found, creates a dummy DB.
212 */ 212 */
213 private function _checkDB() 213 private function checkDB()
214 { 214 {
215 if (file_exists($this->_datastore)) { 215 if (file_exists($this->datastore)) {
216 return; 216 return;
217 } 217 }
218 218
219 // Create a dummy database for example 219 // Create a dummy database for example
220 $this->_links = array(); 220 $this->links = array();
221 $link = array( 221 $link = array(
222 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone', 222 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone',
223 'url'=>'https://github.com/shaarli/Shaarli/wiki', 223 'url'=>'https://github.com/shaarli/Shaarli/wiki',
@@ -230,7 +230,7 @@ You use the community supported version of the original Shaarli project, by Seba
230 'linkdate'=> date('Ymd_His'), 230 'linkdate'=> date('Ymd_His'),
231 'tags'=>'opensource software' 231 'tags'=>'opensource software'
232 ); 232 );
233 $this->_links[$link['linkdate']] = $link; 233 $this->links[$link['linkdate']] = $link;
234 234
235 $link = array( 235 $link = array(
236 'title'=>'My secret stuff... - Pastebin.com', 236 'title'=>'My secret stuff... - Pastebin.com',
@@ -240,7 +240,7 @@ You use the community supported version of the original Shaarli project, by Seba
240 'linkdate'=> date('Ymd_His', strtotime('-1 minute')), 240 'linkdate'=> date('Ymd_His', strtotime('-1 minute')),
241 'tags'=>'secretstuff' 241 'tags'=>'secretstuff'
242 ); 242 );
243 $this->_links[$link['linkdate']] = $link; 243 $this->links[$link['linkdate']] = $link;
244 244
245 // Write database to disk 245 // Write database to disk
246 $this->writeDB(); 246 $this->writeDB();
@@ -249,55 +249,55 @@ You use the community supported version of the original Shaarli project, by Seba
249 /** 249 /**
250 * Reads database from disk to memory 250 * Reads database from disk to memory
251 */ 251 */
252 private function _readDB() 252 private function readDB()
253 { 253 {
254 254
255 // Public links are hidden and user not logged in => nothing to show 255 // Public links are hidden and user not logged in => nothing to show
256 if ($this->_hidePublicLinks && !$this->_loggedIn) { 256 if ($this->hidePublicLinks && !$this->loggedIn) {
257 $this->_links = array(); 257 $this->links = array();
258 return; 258 return;
259 } 259 }
260 260
261 // Read data 261 // Read data
262 // Note that gzinflate is faster than gzuncompress. 262 // Note that gzinflate is faster than gzuncompress.
263 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 263 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
264 $this->_links = array(); 264 $this->links = array();
265 265
266 if (file_exists($this->_datastore)) { 266 if (file_exists($this->datastore)) {
267 $this->_links = unserialize(gzinflate(base64_decode( 267 $this->links = unserialize(gzinflate(base64_decode(
268 substr(file_get_contents($this->_datastore), 268 substr(file_get_contents($this->datastore),
269 strlen(self::$phpPrefix), -strlen(self::$phpSuffix))))); 269 strlen(self::$phpPrefix), -strlen(self::$phpSuffix)))));
270 } 270 }
271 271
272 // If user is not logged in, filter private links. 272 // If user is not logged in, filter private links.
273 if (!$this->_loggedIn) { 273 if (!$this->loggedIn) {
274 $toremove = array(); 274 $toremove = array();
275 foreach ($this->_links as $link) { 275 foreach ($this->links as $link) {
276 if ($link['private'] != 0) { 276 if ($link['private'] != 0) {
277 $toremove[] = $link['linkdate']; 277 $toremove[] = $link['linkdate'];
278 } 278 }
279 } 279 }
280 foreach ($toremove as $linkdate) { 280 foreach ($toremove as $linkdate) {
281 unset($this->_links[$linkdate]); 281 unset($this->links[$linkdate]);
282 } 282 }
283 } 283 }
284 284
285 $this->_urls = array(); 285 $this->urls = array();
286 foreach ($this->_links as &$link) { 286 foreach ($this->links as &$link) {
287 // Keep the list of the mapping URLs-->linkdate up-to-date. 287 // Keep the list of the mapping URLs-->linkdate up-to-date.
288 $this->_urls[$link['url']] = $link['linkdate']; 288 $this->urls[$link['url']] = $link['linkdate'];
289 289
290 // Sanitize data fields. 290 // Sanitize data fields.
291 sanitizeLink($link); 291 sanitizeLink($link);
292 292
293 // Remove private tags if the user is not logged in. 293 // Remove private tags if the user is not logged in.
294 if (! $this->_loggedIn) { 294 if (! $this->loggedIn) {
295 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']); 295 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
296 } 296 }
297 297
298 // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). 298 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
299 if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { 299 if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
300 $link['real_url'] = $this->_redirector; 300 $link['real_url'] = $this->redirector;
301 if ($this->redirectorEncode) { 301 if ($this->redirectorEncode) {
302 $link['real_url'] .= urlencode(unescape($link['url'])); 302 $link['real_url'] .= urlencode(unescape($link['url']));
303 } else { 303 } else {
@@ -317,17 +317,17 @@ You use the community supported version of the original Shaarli project, by Seba
317 */ 317 */
318 private function writeDB() 318 private function writeDB()
319 { 319 {
320 if (is_file($this->_datastore) && !is_writeable($this->_datastore)) { 320 if (is_file($this->datastore) && !is_writeable($this->datastore)) {
321 // The datastore exists but is not writeable 321 // The datastore exists but is not writeable
322 throw new IOException($this->_datastore); 322 throw new IOException($this->datastore);
323 } else if (!is_file($this->_datastore) && !is_writeable(dirname($this->_datastore))) { 323 } else if (!is_file($this->datastore) && !is_writeable(dirname($this->datastore))) {
324 // The datastore does not exist and its parent directory is not writeable 324 // The datastore does not exist and its parent directory is not writeable
325 throw new IOException(dirname($this->_datastore)); 325 throw new IOException(dirname($this->datastore));
326 } 326 }
327 327
328 file_put_contents( 328 file_put_contents(
329 $this->_datastore, 329 $this->datastore,
330 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix 330 self::$phpPrefix.base64_encode(gzdeflate(serialize($this->links))).self::$phpSuffix
331 ); 331 );
332 332
333 } 333 }
@@ -339,7 +339,7 @@ You use the community supported version of the original Shaarli project, by Seba
339 */ 339 */
340 public function savedb($pageCacheDir) 340 public function savedb($pageCacheDir)
341 { 341 {
342 if (!$this->_loggedIn) { 342 if (!$this->loggedIn) {
343 // TODO: raise an Exception instead 343 // TODO: raise an Exception instead
344 die('You are not authorized to change the database.'); 344 die('You are not authorized to change the database.');
345 } 345 }
@@ -358,8 +358,8 @@ You use the community supported version of the original Shaarli project, by Seba
358 */ 358 */
359 public function getLinkFromUrl($url) 359 public function getLinkFromUrl($url)
360 { 360 {
361 if (isset($this->_urls[$url])) { 361 if (isset($this->urls[$url])) {
362 return $this->_links[$this->_urls[$url]]; 362 return $this->links[$this->urls[$url]];
363 } 363 }
364 return false; 364 return false;
365 } 365 }
@@ -376,7 +376,7 @@ You use the community supported version of the original Shaarli project, by Seba
376 public function filterHash($request) 376 public function filterHash($request)
377 { 377 {
378 $request = substr($request, 0, 6); 378 $request = substr($request, 0, 6);
379 $linkFilter = new LinkFilter($this->_links); 379 $linkFilter = new LinkFilter($this->links);
380 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request); 380 return $linkFilter->filter(LinkFilter::$FILTER_HASH, $request);
381 } 381 }
382 382
@@ -388,7 +388,7 @@ You use the community supported version of the original Shaarli project, by Seba
388 * @return array list of shaare found. 388 * @return array list of shaare found.
389 */ 389 */
390 public function filterDay($request) { 390 public function filterDay($request) {
391 $linkFilter = new LinkFilter($this->_links); 391 $linkFilter = new LinkFilter($this->links);
392 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request); 392 return $linkFilter->filter(LinkFilter::$FILTER_DAY, $request);
393 } 393 }
394 394
@@ -430,7 +430,7 @@ You use the community supported version of the original Shaarli project, by Seba
430 $request = ''; 430 $request = '';
431 } 431 }
432 432
433 $linkFilter = new LinkFilter($this->_links); 433 $linkFilter = new LinkFilter($this->links);
434 return $linkFilter->filter($type, $request, $casesensitive, $privateonly); 434 return $linkFilter->filter($type, $request, $casesensitive, $privateonly);
435 } 435 }
436 436
@@ -442,7 +442,7 @@ You use the community supported version of the original Shaarli project, by Seba
442 { 442 {
443 $tags = array(); 443 $tags = array();
444 $caseMapping = array(); 444 $caseMapping = array();
445 foreach ($this->_links as $link) { 445 foreach ($this->links as $link) {
446 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) { 446 foreach (preg_split('/\s+/', $link['tags'], 0, PREG_SPLIT_NO_EMPTY) as $tag) {
447 if (empty($tag)) { 447 if (empty($tag)) {
448 continue; 448 continue;
@@ -467,7 +467,7 @@ You use the community supported version of the original Shaarli project, by Seba
467 public function days() 467 public function days()
468 { 468 {
469 $linkDays = array(); 469 $linkDays = array();
470 foreach (array_keys($this->_links) as $day) { 470 foreach (array_keys($this->links) as $day) {
471 $linkDays[substr($day, 0, 8)] = 0; 471 $linkDays[substr($day, 0, 8)] = 0;
472 } 472 }
473 $linkDays = array_keys($linkDays); 473 $linkDays = array_keys($linkDays);