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