]>
Commit | Line | Data |
---|---|---|
ca74886f V |
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 | |
49e62f22 A |
20 | * - url URL of the link. Used for displayable links (no redirector, relative, etc.). |
21 | * Can be absolute or relative. | |
ca74886f | 22 | * Relative URLs are permalinks (e.g.'?m-ukcw') |
49e62f22 | 23 | * - real_url Absolute processed URL. |
ca74886f V |
24 | * |
25 | * Implements 3 interfaces: | |
26 | * - ArrayAccess: behaves like an associative array; | |
27 | * - Countable: there is a count() method; | |
28 | * - Iterator: usable in foreach () loops. | |
29 | */ | |
30 | class LinkDB implements Iterator, Countable, ArrayAccess | |
31 | { | |
9c8752a2 | 32 | // Links are stored as a PHP serialized string |
07b6fa75 | 33 | private $_datastore; |
9c8752a2 V |
34 | |
35 | // Datastore PHP prefix | |
36 | protected static $phpPrefix = '<?php /* '; | |
37 | ||
38 | // Datastore PHP suffix | |
39 | protected static $phpSuffix = ' */ ?>'; | |
40 | ||
ca74886f V |
41 | // List of links (associative array) |
42 | // - key: link date (e.g. "20110823_124546"), | |
43 | // - value: associative array (keys: title, description...) | |
07b6fa75 | 44 | private $_links; |
ca74886f V |
45 | |
46 | // List of all recorded URLs (key=url, value=linkdate) | |
47 | // for fast reserve search (url-->linkdate) | |
07b6fa75 | 48 | private $_urls; |
ca74886f V |
49 | |
50 | // List of linkdate keys (for the Iterator interface implementation) | |
07b6fa75 | 51 | private $_keys; |
ca74886f | 52 | |
07b6fa75 V |
53 | // Position in the $this->_keys array (for the Iterator interface) |
54 | private $_position; | |
ca74886f V |
55 | |
56 | // Is the user logged in? (used to filter private links) | |
07b6fa75 | 57 | private $_loggedIn; |
ca74886f | 58 | |
9f15ca9e | 59 | // Hide public links |
07b6fa75 | 60 | private $_hidePublicLinks; |
9f15ca9e | 61 | |
90e5bd65 A |
62 | // link redirector set in user settings. |
63 | private $_redirector; | |
64 | ||
822bffce A |
65 | /** |
66 | * @var LinkFilter instance. | |
67 | */ | |
68 | private $linkFilter; | |
69 | ||
ca74886f V |
70 | /** |
71 | * Creates a new LinkDB | |
72 | * | |
73 | * Checks if the datastore exists; else, attempts to create a dummy one. | |
74 | * | |
90e5bd65 A |
75 | * @param string $datastore datastore file path. |
76 | * @param boolean $isLoggedIn is the user logged in? | |
77 | * @param boolean $hidePublicLinks if true all links are private. | |
78 | * @param string $redirector link redirector set in user settings. | |
ca74886f | 79 | */ |
90e5bd65 | 80 | function __construct($datastore, $isLoggedIn, $hidePublicLinks, $redirector = '') |
ca74886f | 81 | { |
07b6fa75 V |
82 | $this->_datastore = $datastore; |
83 | $this->_loggedIn = $isLoggedIn; | |
84 | $this->_hidePublicLinks = $hidePublicLinks; | |
90e5bd65 | 85 | $this->_redirector = $redirector; |
07b6fa75 V |
86 | $this->_checkDB(); |
87 | $this->_readDB(); | |
822bffce | 88 | $this->linkFilter = new LinkFilter($this->_links); |
ca74886f V |
89 | } |
90 | ||
91 | /** | |
92 | * Countable - Counts elements of an object | |
93 | */ | |
94 | public function count() | |
95 | { | |
07b6fa75 | 96 | return count($this->_links); |
ca74886f V |
97 | } |
98 | ||
99 | /** | |
100 | * ArrayAccess - Assigns a value to the specified offset | |
101 | */ | |
102 | public function offsetSet($offset, $value) | |
103 | { | |
104 | // TODO: use exceptions instead of "die" | |
07b6fa75 | 105 | if (!$this->_loggedIn) { |
ca74886f V |
106 | die('You are not authorized to add a link.'); |
107 | } | |
108 | if (empty($value['linkdate']) || empty($value['url'])) { | |
109 | die('Internal Error: A link should always have a linkdate and URL.'); | |
110 | } | |
111 | if (empty($offset)) { | |
112 | die('You must specify a key.'); | |
113 | } | |
07b6fa75 V |
114 | $this->_links[$offset] = $value; |
115 | $this->_urls[$value['url']]=$offset; | |
ca74886f V |
116 | } |
117 | ||
118 | /** | |
119 | * ArrayAccess - Whether or not an offset exists | |
120 | */ | |
121 | public function offsetExists($offset) | |
122 | { | |
07b6fa75 | 123 | return array_key_exists($offset, $this->_links); |
ca74886f V |
124 | } |
125 | ||
126 | /** | |
127 | * ArrayAccess - Unsets an offset | |
128 | */ | |
129 | public function offsetUnset($offset) | |
130 | { | |
07b6fa75 | 131 | if (!$this->_loggedIn) { |
ca74886f V |
132 | // TODO: raise an exception |
133 | die('You are not authorized to delete a link.'); | |
134 | } | |
07b6fa75 V |
135 | $url = $this->_links[$offset]['url']; |
136 | unset($this->_urls[$url]); | |
137 | unset($this->_links[$offset]); | |
ca74886f V |
138 | } |
139 | ||
140 | /** | |
141 | * ArrayAccess - Returns the value at specified offset | |
142 | */ | |
143 | public function offsetGet($offset) | |
144 | { | |
07b6fa75 | 145 | return isset($this->_links[$offset]) ? $this->_links[$offset] : null; |
ca74886f V |
146 | } |
147 | ||
148 | /** | |
149 | * Iterator - Returns the current element | |
150 | */ | |
151 | function current() | |
152 | { | |
07b6fa75 | 153 | return $this->_links[$this->_keys[$this->_position]]; |
ca74886f V |
154 | } |
155 | ||
156 | /** | |
157 | * Iterator - Returns the key of the current element | |
158 | */ | |
159 | function key() | |
160 | { | |
07b6fa75 | 161 | return $this->_keys[$this->_position]; |
ca74886f V |
162 | } |
163 | ||
164 | /** | |
165 | * Iterator - Moves forward to next element | |
166 | */ | |
167 | function next() | |
168 | { | |
07b6fa75 | 169 | ++$this->_position; |
ca74886f V |
170 | } |
171 | ||
172 | /** | |
173 | * Iterator - Rewinds the Iterator to the first element | |
174 | * | |
175 | * Entries are sorted by date (latest first) | |
176 | */ | |
177 | function rewind() | |
178 | { | |
07b6fa75 V |
179 | $this->_keys = array_keys($this->_links); |
180 | rsort($this->_keys); | |
181 | $this->_position = 0; | |
ca74886f V |
182 | } |
183 | ||
184 | /** | |
185 | * Iterator - Checks if current position is valid | |
186 | */ | |
187 | function valid() | |
188 | { | |
07b6fa75 | 189 | return isset($this->_keys[$this->_position]); |
ca74886f V |
190 | } |
191 | ||
192 | /** | |
193 | * Checks if the DB directory and file exist | |
194 | * | |
195 | * If no DB file is found, creates a dummy DB. | |
196 | */ | |
07b6fa75 | 197 | private function _checkDB() |
ca74886f | 198 | { |
07b6fa75 | 199 | if (file_exists($this->_datastore)) { |
ca74886f V |
200 | return; |
201 | } | |
202 | ||
203 | // Create a dummy database for example | |
07b6fa75 | 204 | $this->_links = array(); |
ca74886f | 205 | $link = array( |
598376d4 A |
206 | 'title'=>' Shaarli: the personal, minimalist, super-fast, no-database delicious clone', |
207 | 'url'=>'https://github.com/shaarli/Shaarli/wiki', | |
208 | 'description'=>'Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login. | |
209 | ||
210 | To learn how to use Shaarli, consult the link "Help/documentation" at the bottom of this page. | |
211 | ||
212 | You use the community supported version of the original Shaarli project, by Sebastien Sauvage.', | |
ca74886f | 213 | 'private'=>0, |
598376d4 | 214 | 'linkdate'=> date('Ymd_His'), |
ca74886f V |
215 | 'tags'=>'opensource software' |
216 | ); | |
07b6fa75 | 217 | $this->_links[$link['linkdate']] = $link; |
ca74886f V |
218 | |
219 | $link = array( | |
220 | 'title'=>'My secret stuff... - Pastebin.com', | |
221 | 'url'=>'http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=', | |
598376d4 | 222 | 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.', |
ca74886f | 223 | 'private'=>1, |
598376d4 | 224 | 'linkdate'=> date('Ymd_His', strtotime('-1 minute')), |
ca74886f V |
225 | 'tags'=>'secretstuff' |
226 | ); | |
07b6fa75 | 227 | $this->_links[$link['linkdate']] = $link; |
ca74886f V |
228 | |
229 | // Write database to disk | |
2e28269b | 230 | $this->writeDB(); |
ca74886f V |
231 | } |
232 | ||
233 | /** | |
234 | * Reads database from disk to memory | |
235 | */ | |
07b6fa75 | 236 | private function _readDB() |
ca74886f | 237 | { |
578a84bd | 238 | |
239 | // Public links are hidden and user not logged in => nothing to show | |
07b6fa75 V |
240 | if ($this->_hidePublicLinks && !$this->_loggedIn) { |
241 | $this->_links = array(); | |
578a84bd | 242 | return; |
243 | } | |
244 | ||
ca74886f V |
245 | // Read data |
246 | // Note that gzinflate is faster than gzuncompress. | |
247 | // See: http://www.php.net/manual/en/function.gzdeflate.php#96439 | |
07b6fa75 | 248 | $this->_links = array(); |
ca74886f | 249 | |
07b6fa75 V |
250 | if (file_exists($this->_datastore)) { |
251 | $this->_links = unserialize(gzinflate(base64_decode( | |
252 | substr(file_get_contents($this->_datastore), | |
9c8752a2 | 253 | strlen(self::$phpPrefix), -strlen(self::$phpSuffix))))); |
ca74886f V |
254 | } |
255 | ||
256 | // If user is not logged in, filter private links. | |
07b6fa75 | 257 | if (!$this->_loggedIn) { |
ca74886f | 258 | $toremove = array(); |
07b6fa75 | 259 | foreach ($this->_links as $link) { |
ca74886f V |
260 | if ($link['private'] != 0) { |
261 | $toremove[] = $link['linkdate']; | |
262 | } | |
263 | } | |
264 | foreach ($toremove as $linkdate) { | |
07b6fa75 | 265 | unset($this->_links[$linkdate]); |
ca74886f V |
266 | } |
267 | } | |
268 | ||
269 | // Keep the list of the mapping URLs-->linkdate up-to-date. | |
07b6fa75 V |
270 | $this->_urls = array(); |
271 | foreach ($this->_links as $link) { | |
272 | $this->_urls[$link['url']] = $link['linkdate']; | |
ca74886f | 273 | } |
5f85fcd8 A |
274 | |
275 | // Escape links data | |
07b6fa75 | 276 | foreach($this->_links as &$link) { |
90e5bd65 A |
277 | sanitizeLink($link); |
278 | // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). | |
279 | if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { | |
280 | $link['real_url'] = $this->_redirector . urlencode($link['url']); | |
281 | } | |
282 | else { | |
283 | $link['real_url'] = $link['url']; | |
284 | } | |
5f85fcd8 | 285 | } |
ca74886f V |
286 | } |
287 | ||
2e28269b V |
288 | /** |
289 | * Saves the database from memory to disk | |
290 | * | |
291 | * @throws IOException the datastore is not writable | |
292 | */ | |
293 | private function writeDB() | |
294 | { | |
295 | if (is_file($this->_datastore) && !is_writeable($this->_datastore)) { | |
296 | // The datastore exists but is not writeable | |
297 | throw new IOException($this->_datastore); | |
298 | } else if (!is_file($this->_datastore) && !is_writeable(dirname($this->_datastore))) { | |
299 | // The datastore does not exist and its parent directory is not writeable | |
300 | throw new IOException(dirname($this->_datastore)); | |
301 | } | |
302 | ||
303 | file_put_contents( | |
304 | $this->_datastore, | |
305 | self::$phpPrefix.base64_encode(gzdeflate(serialize($this->_links))).self::$phpSuffix | |
306 | ); | |
307 | ||
308 | } | |
309 | ||
ca74886f V |
310 | /** |
311 | * Saves the database from memory to disk | |
01e48f26 V |
312 | * |
313 | * @param string $pageCacheDir page cache directory | |
ca74886f | 314 | */ |
01e48f26 | 315 | public function savedb($pageCacheDir) |
ca74886f | 316 | { |
07b6fa75 | 317 | if (!$this->_loggedIn) { |
ca74886f V |
318 | // TODO: raise an Exception instead |
319 | die('You are not authorized to change the database.'); | |
320 | } | |
2e28269b V |
321 | |
322 | $this->writeDB(); | |
323 | ||
01e48f26 | 324 | invalidateCaches($pageCacheDir); |
ca74886f V |
325 | } |
326 | ||
327 | /** | |
328 | * Returns the link for a given URL, or False if it does not exist. | |
ef591e7e GV |
329 | * |
330 | * @param string $url URL to search for | |
331 | * | |
332 | * @return mixed the existing link if it exists, else 'false' | |
ca74886f V |
333 | */ |
334 | public function getLinkFromUrl($url) | |
335 | { | |
07b6fa75 V |
336 | if (isset($this->_urls[$url])) { |
337 | return $this->_links[$this->_urls[$url]]; | |
ca74886f V |
338 | } |
339 | return false; | |
340 | } | |
341 | ||
342 | /** | |
822bffce | 343 | * Filter links. |
ca74886f | 344 | * |
822bffce A |
345 | * @param string $type Type of filter. |
346 | * @param mixed $request Search request, string or array. | |
347 | * @param bool $casesensitive Optional: Perform case sensitive filter | |
348 | * @param bool $privateonly Optional: Returns private links only if true. | |
ca74886f | 349 | * |
822bffce | 350 | * @return array filtered links |
ca74886f | 351 | */ |
822bffce A |
352 | public function filter($type, $request, $casesensitive = false, $privateonly = false) { |
353 | $requestFilter = is_array($request) ? implode(' ', $request) : $request; | |
2c75f8e7 | 354 | return $this->linkFilter->filter($type, trim($requestFilter), $casesensitive, $privateonly); |
ca74886f V |
355 | } |
356 | ||
357 | /** | |
358 | * Returns the list of all tags | |
359 | * Output: associative array key=tags, value=0 | |
360 | */ | |
361 | public function allTags() | |
362 | { | |
363 | $tags = array(); | |
07b6fa75 | 364 | foreach ($this->_links as $link) { |
ca74886f V |
365 | foreach (explode(' ', $link['tags']) as $tag) { |
366 | if (!empty($tag)) { | |
367 | $tags[$tag] = (empty($tags[$tag]) ? 1 : $tags[$tag] + 1); | |
368 | } | |
369 | } | |
370 | } | |
371 | // Sort tags by usage (most used tag first) | |
372 | arsort($tags); | |
373 | return $tags; | |
374 | } | |
375 | ||
376 | /** | |
377 | * Returns the list of days containing articles (oldest first) | |
378 | * Output: An array containing days (in format YYYYMMDD). | |
379 | */ | |
380 | public function days() | |
381 | { | |
382 | $linkDays = array(); | |
07b6fa75 | 383 | foreach (array_keys($this->_links) as $day) { |
ca74886f V |
384 | $linkDays[substr($day, 0, 8)] = 0; |
385 | } | |
386 | $linkDays = array_keys($linkDays); | |
387 | sort($linkDays); | |
388 | return $linkDays; | |
389 | } | |
390 | } |