aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/.htaccess2
-rw-r--r--application/LinkDB.php419
-rw-r--r--application/Utils.php45
3 files changed, 466 insertions, 0 deletions
diff --git a/application/.htaccess b/application/.htaccess
new file mode 100644
index 00000000..b584d98c
--- /dev/null
+++ b/application/.htaccess
@@ -0,0 +1,2 @@
1Allow from none
2Deny from all
diff --git a/application/LinkDB.php b/application/LinkDB.php
new file mode 100644
index 00000000..137f42e5
--- /dev/null
+++ b/application/LinkDB.php
@@ -0,0 +1,419 @@
1<?php
2/**
3 * Data storage for links.
4 *
5 * This object behaves like an associative array.
6 *
7 * Example:
8 * $myLinks = new LinkDB();
9 * echo $myLinks['20110826_161819']['title'];
10 * foreach ($myLinks as $link)
11 * echo $link['title'].' at url '.$link['url'].'; description:'.$link['description'];
12 *
13 * Available keys:
14 * - description: description of the entry
15 * - linkdate: date of the creation of this entry, in the form YYYYMMDD_HHMMSS
16 * (e.g.'20110914_192317')
17 * - private: Is this link private? 0=no, other value=yes
18 * - tags: tags attached to this entry (separated by spaces)
19 * - title Title of the link
20 * - url URL of the link. Can be absolute or relative.
21 * Relative URLs are permalinks (e.g.'?m-ukcw')
22 *
23 * Implements 3 interfaces:
24 * - ArrayAccess: behaves like an associative array;
25 * - Countable: there is a count() method;
26 * - Iterator: usable in foreach () loops.
27 */
28class LinkDB implements Iterator, Countable, ArrayAccess
29{
30 // List of links (associative array)
31 // - key: link date (e.g. "20110823_124546"),
32 // - value: associative array (keys: title, description...)
33 private $links;
34
35 // List of all recorded URLs (key=url, value=linkdate)
36 // for fast reserve search (url-->linkdate)
37 private $urls;
38
39 // List of linkdate keys (for the Iterator interface implementation)
40 private $keys;
41
42 // Position in the $this->keys array (for the Iterator interface)
43 private $position;
44
45 // Is the user logged in? (used to filter private links)
46 private $loggedIn;
47
48 /**
49 * Creates a new LinkDB
50 *
51 * Checks if the datastore exists; else, attempts to create a dummy one.
52 *
53 * @param $isLoggedIn is the user logged in?
54 */
55 function __construct($isLoggedIn)
56 {
57 // FIXME: do not access $GLOBALS, pass the datastore instead
58 $this->loggedIn = $isLoggedIn;
59 $this->checkDB();
60 $this->readdb();
61 }
62
63 /**
64 * Countable - Counts elements of an object
65 */
66 public function count()
67 {
68 return count($this->links);
69 }
70
71 /**
72 * ArrayAccess - Assigns a value to the specified offset
73 */
74 public function offsetSet($offset, $value)
75 {
76 // TODO: use exceptions instead of "die"
77 if (!$this->loggedIn) {
78 die('You are not authorized to add a link.');
79 }
80 if (empty($value['linkdate']) || empty($value['url'])) {
81 die('Internal Error: A link should always have a linkdate and URL.');
82 }
83 if (empty($offset)) {
84 die('You must specify a key.');
85 }
86 $this->links[$offset] = $value;
87 $this->urls[$value['url']]=$offset;
88 }
89
90 /**
91 * ArrayAccess - Whether or not an offset exists
92 */
93 public function offsetExists($offset)
94 {
95 return array_key_exists($offset, $this->links);
96 }
97
98 /**
99 * ArrayAccess - Unsets an offset
100 */
101 public function offsetUnset($offset)
102 {
103 if (!$this->loggedIn) {
104 // TODO: raise an exception
105 die('You are not authorized to delete a link.');
106 }
107 $url = $this->links[$offset]['url'];
108 unset($this->urls[$url]);
109 unset($this->links[$offset]);
110 }
111
112 /**
113 * ArrayAccess - Returns the value at specified offset
114 */
115 public function offsetGet($offset)
116 {
117 return isset($this->links[$offset]) ? $this->links[$offset] : null;
118 }
119
120 /**
121 * Iterator - Returns the current element
122 */
123 function current()
124 {
125 return $this->links[$this->keys[$this->position]];
126 }
127
128 /**
129 * Iterator - Returns the key of the current element
130 */
131 function key()
132 {
133 return $this->keys[$this->position];
134 }
135
136 /**
137 * Iterator - Moves forward to next element
138 */
139 function next()
140 {
141 ++$this->position;
142 }
143
144 /**
145 * Iterator - Rewinds the Iterator to the first element
146 *
147 * Entries are sorted by date (latest first)
148 */
149 function rewind()
150 {
151 $this->keys = array_keys($this->links);
152 rsort($this->keys);
153 $this->position = 0;
154 }
155
156 /**
157 * Iterator - Checks if current position is valid
158 */
159 function valid()
160 {
161 return isset($this->keys[$this->position]);
162 }
163
164 /**
165 * Checks if the DB directory and file exist
166 *
167 * If no DB file is found, creates a dummy DB.
168 */
169 private function checkDB()
170 {
171 if (file_exists($GLOBALS['config']['DATASTORE'])) {
172 return;
173 }
174
175 // Create a dummy database for example
176 $this->links = array();
177 $link = array(
178 'title'=>'Shaarli - sebsauvage.net',
179 'url'=>'http://sebsauvage.net/wiki/doku.php?id=php:shaarli',
180 'description'=>'Welcome to Shaarli! This is a bookmark. To edit or delete me, you must first login.',
181 'private'=>0,
182 'linkdate'=>'20110914_190000',
183 'tags'=>'opensource software'
184 );
185 $this->links[$link['linkdate']] = $link;
186
187 $link = array(
188 'title'=>'My secret stuff... - Pastebin.com',
189 'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=',
190 'description'=>'SShhhh!! I\'m a private link only YOU can see. You can delete me too.',
191 'private'=>1,
192 'linkdate'=>'20110914_074522',
193 'tags'=>'secretstuff'
194 );
195 $this->links[$link['linkdate']] = $link;
196
197 // Write database to disk
198 // TODO: raise an exception if the file is not write-able
199 file_put_contents(
200 // FIXME: do not use $GLOBALS
201 $GLOBALS['config']['DATASTORE'],
202 PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
203 );
204 }
205
206 /**
207 * Reads database from disk to memory
208 */
209 private function readdb()
210 {
211
212 // Public links are hidden and user not logged in => nothing to show
213 if ($GLOBALS['config']['HIDE_PUBLIC_LINKS'] && !isLoggedIn()) {
214 $this->links = array();
215 return;
216 }
217
218 // Read data
219 // Note that gzinflate is faster than gzuncompress.
220 // See: http://www.php.net/manual/en/function.gzdeflate.php#96439
221 // FIXME: do not use $GLOBALS
222 $this->links = array();
223
224 if (file_exists($GLOBALS['config']['DATASTORE'])) {
225 $this->links = unserialize(gzinflate(base64_decode(
226 substr(file_get_contents($GLOBALS['config']['DATASTORE']),
227 strlen(PHPPREFIX), -strlen(PHPSUFFIX)))));
228 }
229
230 // If user is not logged in, filter private links.
231 if (!$this->loggedIn) {
232 $toremove = array();
233 foreach ($this->links as $link) {
234 if ($link['private'] != 0) {
235 $toremove[] = $link['linkdate'];
236 }
237 }
238 foreach ($toremove as $linkdate) {
239 unset($this->links[$linkdate]);
240 }
241 }
242
243 // Keep the list of the mapping URLs-->linkdate up-to-date.
244 $this->urls = array();
245 foreach ($this->links as $link) {
246 $this->urls[$link['url']] = $link['linkdate'];
247 }
248 }
249
250 /**
251 * Saves the database from memory to disk
252 */
253 public function savedb()
254 {
255 if (!$this->loggedIn) {
256 // TODO: raise an Exception instead
257 die('You are not authorized to change the database.');
258 }
259 file_put_contents(
260 $GLOBALS['config']['DATASTORE'],
261 PHPPREFIX.base64_encode(gzdeflate(serialize($this->links))).PHPSUFFIX
262 );
263 invalidateCaches();
264 }
265
266 /**
267 * Returns the link for a given URL, or False if it does not exist.
268 */
269 public function getLinkFromUrl($url)
270 {
271 if (isset($this->urls[$url])) {
272 return $this->links[$this->urls[$url]];
273 }
274 return false;
275 }
276
277 /**
278 * Returns the list of links corresponding to a full-text search
279 *
280 * Searches:
281 * - in the URLs, title and description;
282 * - are case-insensitive.
283 *
284 * Example:
285 * print_r($mydb->filterFulltext('hollandais'));
286 *
287 * mb_convert_case($val, MB_CASE_LOWER, 'UTF-8')
288 * - allows to perform searches on Unicode text
289 * - see https://github.com/shaarli/Shaarli/issues/75 for examples
290 */
291 public function filterFulltext($searchterms)
292 {
293 // FIXME: explode(' ',$searchterms) and perform a AND search.
294 // FIXME: accept double-quotes to search for a string "as is"?
295 $filtered = array();
296 $search = mb_convert_case($searchterms, MB_CASE_LOWER, 'UTF-8');
297 $keys = ['title', 'description', 'url', 'tags'];
298
299 foreach ($this->links as $link) {
300 $found = false;
301
302 foreach ($keys as $key) {
303 if (strpos(mb_convert_case($link[$key], MB_CASE_LOWER, 'UTF-8'),
304 $search) !== false) {
305 $found = true;
306 }
307 }
308
309 if ($found) {
310 $filtered[$link['linkdate']] = $link;
311 }
312 }
313 krsort($filtered);
314 return $filtered;
315 }
316
317 /**
318 * Returns the list of links associated with a given list of tags
319 *
320 * You can specify one or more tags, separated by space or a comma, e.g.
321 * print_r($mydb->filterTags('linux programming'));
322 */
323 public function filterTags($tags, $casesensitive=false)
324 {
325 // Same as above, we use UTF-8 conversion to handle various graphemes (i.e. cyrillic, or greek)
326 // FIXME: is $casesensitive ever true?
327 $t = str_replace(
328 ',', ' ',
329 ($casesensitive ? $tags : mb_convert_case($tags, MB_CASE_LOWER, 'UTF-8'))
330 );
331
332 $searchtags = explode(' ', $t);
333 $filtered = array();
334
335 foreach ($this->links as $l) {
336 $linktags = explode(
337 ' ',
338 ($casesensitive ? $l['tags']:mb_convert_case($l['tags'], MB_CASE_LOWER, 'UTF-8'))
339 );
340
341 if (count(array_intersect($linktags, $searchtags)) == count($searchtags)) {
342 $filtered[$l['linkdate']] = $l;
343 }
344 }
345 krsort($filtered);
346 return $filtered;
347 }
348
349
350 /**
351 * Returns the list of articles for a given day, chronologically sorted
352 *
353 * Day must be in the form 'YYYYMMDD' (e.g. '20120125'), e.g.
354 * print_r($mydb->filterDay('20120125'));
355 */
356 public function filterDay($day)
357 {
358 // TODO: check input format
359 $filtered = array();
360 foreach ($this->links as $l) {
361 if (startsWith($l['linkdate'], $day)) {
362 $filtered[$l['linkdate']] = $l;
363 }
364 }
365 ksort($filtered);
366 return $filtered;
367 }
368
369 /**
370 * Returns the article corresponding to a smallHash
371 */
372 public function filterSmallHash($smallHash)
373 {
374 $filtered = array();
375 foreach ($this->links as $l) {
376 if ($smallHash == smallHash($l['linkdate'])) {
377 // Yes, this is ugly and slow
378 $filtered[$l['linkdate']] = $l;
379 return $filtered;
380 }
381 }
382 return $filtered;
383 }
384
385 /**
386 * Returns the list of all tags
387 * Output: associative array key=tags, value=0
388 */
389 public function allTags()
390 {
391 $tags = array();
392 foreach ($this->links as $link) {
393 foreach (explode(' ', $link['tags']) as $tag) {
394 if (!empty($tag)) {
395 $tags[$tag] = (empty($tags[$tag]) ? 1 : $tags[$tag] + 1);
396 }
397 }
398 }
399 // Sort tags by usage (most used tag first)
400 arsort($tags);
401 return $tags;
402 }
403
404 /**
405 * Returns the list of days containing articles (oldest first)
406 * Output: An array containing days (in format YYYYMMDD).
407 */
408 public function days()
409 {
410 $linkDays = array();
411 foreach (array_keys($this->links) as $day) {
412 $linkDays[substr($day, 0, 8)] = 0;
413 }
414 $linkDays = array_keys($linkDays);
415 sort($linkDays);
416 return $linkDays;
417 }
418}
419?>
diff --git a/application/Utils.php b/application/Utils.php
new file mode 100644
index 00000000..737f1502
--- /dev/null
+++ b/application/Utils.php
@@ -0,0 +1,45 @@
1<?php
2/**
3 * Shaarli utilities
4 */
5
6/**
7 * Returns the small hash of a string, using RFC 4648 base64url format
8 *
9 * Small hashes:
10 * - are unique (well, as unique as crc32, at last)
11 * - are always 6 characters long.
12 * - only use the following characters: a-z A-Z 0-9 - _ @
13 * - are NOT cryptographically secure (they CAN be forged)
14 *
15 * In Shaarli, they are used as a tinyurl-like link to individual entries,
16 * e.g. smallHash('20111006_131924') --> yZH23w
17 */
18function smallHash($text)
19{
20 $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
21 return strtr($t, '+/', '-_');
22}
23
24/**
25 * Tells if a string start with a substring
26 */
27function startsWith($haystack, $needle, $case=true)
28{
29 if ($case) {
30 return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
31 }
32 return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
33}
34
35/**
36 * Tells if a string ends with a substring
37 */
38function endsWith($haystack, $needle, $case=true)
39{
40 if ($case) {
41 return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
42 }
43 return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
44}
45?>