]>
Commit | Line | Data |
---|---|---|
24619534 | 1 | <?php |
2 | ||
c8bbe19b | 3 | /** |
4 | * Permet de générer l'URL de poche pour le bookmarklet | |
5 | */ | |
8046748b | 6 | function get_poche_url() |
c8bbe19b | 7 | { |
8 | $protocol = "http"; | |
9 | if(isset($_SERVER['HTTPS'])) { | |
f0fc5011 | 10 | if($_SERVER['HTTPS'] != "off" && $_SERVER['HTTPS'] != "") { |
c8bbe19b | 11 | $protocol = "https"; |
12 | } | |
13 | } | |
24619534 | 14 | |
c8bbe19b | 15 | return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
24619534 | 16 | } |
17 | ||
24619534 | 18 | // function define to retrieve url content |
1c182b6c | 19 | function get_external_file($url) |
c8bbe19b | 20 | { |
1c182b6c | 21 | $timeout = 15; |
24619534 | 22 | // spoofing FireFox 18.0 |
23 | $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | |
24 | ||
25 | if (in_array ('curl', get_loaded_extensions())) { | |
26 | // Fetch feed from URL | |
27 | $curl = curl_init(); | |
28 | curl_setopt($curl, CURLOPT_URL, $url); | |
29 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
30 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
31 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
32 | curl_setopt($curl, CURLOPT_HEADER, false); | |
33 | ||
34 | // FeedBurner requires a proper USER-AGENT... | |
35 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | |
36 | curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate"); | |
37 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
38 | ||
39 | $data = curl_exec($curl); | |
40 | ||
41 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
42 | ||
43 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
44 | ||
45 | curl_close($curl); | |
46 | } else { | |
47 | ||
48 | // create http context and add timeout and user-agent | |
1c182b6c | 49 | $context = stream_context_create(array('http'=>array('timeout' => $timeout,'header'=> "User-Agent: ".$useragent,/*spoot Mozilla Firefox*/'follow_location' => true))); |
24619534 | 50 | |
51 | // only download page lesser than 4MB | |
52 | $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source. | |
24619534 | 53 | |
54 | if(isset($http_response_header) and isset($http_response_header[0])) { | |
55 | $httpcodeOK = isset($http_response_header) and isset($http_response_header[0]) and ((strpos($http_response_header[0], '200 OK') !== FALSE) or (strpos($http_response_header[0], '301 Moved Permanently') !== FALSE)); | |
56 | } | |
57 | } | |
58 | ||
59 | // if response is not empty and response is OK | |
60 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) { | |
61 | ||
62 | // take charset of page and get it | |
63 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
64 | ||
65 | // if meta tag is found | |
66 | if (!empty($meta[0])) { | |
67 | // retrieve encoding in $enc | |
68 | preg_match('#charset="?(.*)"#si', $meta[0], $enc); | |
69 | ||
70 | // if charset is found set it otherwise, set it to utf-8 | |
71 | $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8'; | |
72 | ||
73 | } else { | |
74 | $html_charset = 'utf-8'; | |
75 | $enc[1] = ''; | |
76 | } | |
77 | ||
78 | // replace charset of url to charset of page | |
79 | $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data); | |
80 | ||
81 | return $data; | |
82 | } | |
83 | else { | |
84 | return FALSE; | |
85 | } | |
3c8d80ae | 86 | } |
87 | ||
8046748b | 88 | /** |
89 | * Préparation de l'URL avec récupération du contenu avant insertion en base | |
90 | */ | |
d06f30ef | 91 | function prepare_url($url) |
3c8d80ae | 92 | { |
93 | $parametres = array(); | |
e4d2565e | 94 | $url = html_entity_decode(trim($url)); |
3c8d80ae | 95 | |
96 | // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...) | |
97 | // from shaarli, by sebsauvage | |
98 | $i=strpos($url,'&utm_source='); if ($i!==false) $url=substr($url,0,$i); | |
99 | $i=strpos($url,'?utm_source='); if ($i!==false) $url=substr($url,0,$i); | |
100 | $i=strpos($url,'#xtor=RSS-'); if ($i!==false) $url=substr($url,0,$i); | |
101 | ||
e4d2565e | 102 | $title = $url; |
3c8d80ae | 103 | if (!preg_match('!^https?://!i', $url)) |
104 | $url = 'http://' . $url; | |
105 | ||
106 | $html = Encoding::toUTF8(get_external_file($url,15)); | |
107 | if (isset($html) and strlen($html) > 0) | |
108 | { | |
109 | $r = new Readability($html, $url); | |
d06f30ef | 110 | $r->convertLinksToFootnotes = CONVERT_LINKS_FOOTNOTES; |
3c8d80ae | 111 | if($r->init()) |
112 | { | |
1c182b6c | 113 | $content = $r->articleContent->innerHTML; |
114 | $parametres['title'] = $r->articleTitle->innerHTML; | |
64458521 | 115 | $parametres['content'] = $content; |
1c182b6c | 116 | return $parametres; |
3c8d80ae | 117 | } |
118 | } | |
119 | ||
1c182b6c | 120 | logm('error during url preparation'); |
121 | return FALSE; | |
122 | } | |
123 | ||
124 | /** | |
125 | * On modifie les URLS des images dans le corps de l'article | |
126 | */ | |
127 | function filtre_picture($content, $url, $id) | |
128 | { | |
129 | $matches = array(); | |
130 | preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER); | |
131 | foreach($matches as $i => $link) | |
132 | { | |
133 | $link[1] = trim($link[1]); | |
134 | if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) | |
135 | { | |
136 | $absolute_path = get_absolute_link($link[2],$url); | |
137 | $filename = basename(parse_url($absolute_path, PHP_URL_PATH)); | |
138 | $directory = create_assets_directory($id); | |
139 | $fullpath = $directory . '/' . $filename; | |
140 | download_pictures($absolute_path, $fullpath); | |
141 | $content = str_replace($matches[$i][2], $fullpath, $content); | |
142 | } | |
143 | ||
144 | } | |
145 | ||
146 | return $content; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Retourne le lien absolu | |
151 | */ | |
152 | function get_absolute_link($relative_link, $url) | |
153 | { | |
154 | /* return if already absolute URL */ | |
155 | if (parse_url($relative_link, PHP_URL_SCHEME) != '') return $relative_link; | |
156 | ||
157 | /* queries and anchors */ | |
158 | if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link; | |
159 | ||
160 | /* parse base URL and convert to local variables: | |
161 | $scheme, $host, $path */ | |
162 | extract(parse_url($url)); | |
163 | ||
164 | /* remove non-directory element from path */ | |
165 | $path = preg_replace('#/[^/]*$#', '', $path); | |
166 | ||
167 | /* destroy path if relative url points to root */ | |
168 | if ($relative_link[0] == '/') $path = ''; | |
169 | ||
170 | /* dirty absolute URL */ | |
171 | $abs = $host . $path . '/' . $relative_link; | |
172 | ||
173 | /* replace '//' or '/./' or '/foo/../' with '/' */ | |
174 | $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); | |
175 | for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} | |
176 | ||
177 | /* absolute URL is ready! */ | |
178 | return $scheme.'://'.$abs; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Téléchargement des images | |
183 | */ | |
184 | ||
185 | function download_pictures($absolute_path, $fullpath) | |
186 | { | |
187 | $rawdata = get_external_file($absolute_path); | |
3c8d80ae | 188 | |
1c182b6c | 189 | if(file_exists($fullpath)) { |
190 | unlink($fullpath); | |
191 | } | |
192 | $fp = fopen($fullpath, 'x'); | |
193 | fwrite($fp, $rawdata); | |
194 | fclose($fp); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Crée un répertoire de médias pour l'article | |
199 | */ | |
200 | function create_assets_directory($id) | |
201 | { | |
202 | $assets_path = ABS_PATH; | |
203 | if(!is_dir($assets_path)) { | |
204 | mkdir($assets_path, 0705); | |
205 | } | |
206 | ||
207 | $article_directory = $assets_path . $id; | |
208 | if(!is_dir($article_directory)) { | |
209 | mkdir($article_directory, 0705); | |
210 | } | |
211 | ||
212 | return $article_directory; | |
213 | } | |
214 | ||
215 | /** | |
216 | * Suppression du répertoire d'images | |
217 | */ | |
218 | function remove_directory($directory) | |
219 | { | |
220 | if(is_dir($directory)) { | |
221 | $files = array_diff(scandir($directory), array('.','..')); | |
222 | foreach ($files as $file) { | |
07a19252 | 223 | (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file"); |
1c182b6c | 224 | } |
225 | return rmdir($directory); | |
226 | } | |
263d6c67 | 227 | } |
228 | ||
a1953dff | 229 | function display_view($view, $id = 0, $full_head = 'yes') |
230 | { | |
231 | global $tpl; | |
232 | ||
233 | switch ($view) | |
234 | { | |
235 | case 'view': | |
236 | $entry = get_article($id); | |
237 | ||
238 | if ($entry != NULL) { | |
239 | $tpl->assign('id', $entry[0]['id']); | |
240 | $tpl->assign('url', $entry[0]['url']); | |
241 | $tpl->assign('title', $entry[0]['title']); | |
242 | $tpl->assign('content', $entry[0]['content']); | |
243 | $tpl->assign('is_fav', $entry[0]['is_fav']); | |
244 | $tpl->assign('is_read', $entry[0]['is_read']); | |
245 | $tpl->assign('load_all_js', 0); | |
246 | $tpl->draw('view'); | |
247 | } | |
248 | else { | |
249 | logm('error in view call : entry is NULL'); | |
250 | } | |
251 | ||
252 | logm('view link #' . $id); | |
253 | break; | |
254 | default: # home view | |
255 | $entries = get_entries($view); | |
256 | ||
257 | $tpl->assign('entries', $entries); | |
258 | ||
259 | if ($full_head == 'yes') { | |
260 | $tpl->assign('load_all_js', 1); | |
261 | $tpl->draw('head'); | |
262 | $tpl->draw('home'); | |
263 | } | |
264 | ||
265 | $tpl->draw('entries'); | |
266 | ||
267 | if ($full_head == 'yes') { | |
268 | $tpl->draw('js'); | |
269 | $tpl->draw('footer'); | |
270 | } | |
271 | break; | |
272 | } | |
273 | } | |
274 | ||
263d6c67 | 275 | /** |
276 | * Appel d'une action (mark as fav, archive, delete) | |
277 | */ | |
e4d2565e | 278 | function action_to_do($action, $url, $id = 0) |
263d6c67 | 279 | { |
280 | global $db; | |
281 | ||
282 | switch ($action) | |
283 | { | |
284 | case 'add': | |
285 | if ($url == '') | |
286 | continue; | |
287 | ||
d06f30ef | 288 | if($parametres_url = prepare_url($url)) { |
289 | $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; | |
290 | $params_action = array($url, $parametres_url['title'], $parametres_url['content']); | |
1c182b6c | 291 | } |
292 | ||
a81cd067 | 293 | logm('add link ' . $url); |
263d6c67 | 294 | break; |
295 | case 'delete': | |
e4d2565e | 296 | remove_directory(ABS_PATH . $id); |
297 | $sql_action = "DELETE FROM entries WHERE id=?"; | |
298 | $params_action = array($id); | |
299 | logm('delete link #' . $id); | |
263d6c67 | 300 | break; |
139769aa | 301 | case 'toggle_fav' : |
e4d2565e | 302 | $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?"; |
303 | $params_action = array($id); | |
304 | logm('mark as favorite link #' . $id); | |
139769aa | 305 | break; |
306 | case 'toggle_archive' : | |
e4d2565e | 307 | $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?"; |
308 | $params_action = array($id); | |
309 | logm('archive link #' . $id); | |
139769aa | 310 | break; |
263d6c67 | 311 | default: |
312 | break; | |
313 | } | |
314 | ||
315 | try | |
316 | { | |
317 | # action query | |
318 | if (isset($sql_action)) | |
319 | { | |
320 | $query = $db->getHandle()->prepare($sql_action); | |
321 | $query->execute($params_action); | |
d06f30ef | 322 | # if we add a link, we have to download pictures |
323 | if ($action == 'add') { | |
324 | $last_id = $db->getHandle()->lastInsertId(); | |
325 | if (DOWNLOAD_PICTURES) { | |
326 | $content = filtre_picture($parametres_url['content'], $url, $last_id); | |
327 | $sql_update = "UPDATE entries SET content=? WHERE id=?"; | |
328 | $params_update = array($content, $last_id); | |
329 | $query_update = $db->getHandle()->prepare($sql_update); | |
330 | $query_update->execute($params_update); | |
331 | } | |
332 | } | |
263d6c67 | 333 | } |
334 | } | |
335 | catch (Exception $e) | |
336 | { | |
713b2d69 | 337 | logm('action query error : '.$e->getMessage()); |
263d6c67 | 338 | } |
339 | } | |
340 | ||
341 | /** | |
342 | * Détermine quels liens afficher : home, fav ou archives | |
343 | */ | |
e4d2565e | 344 | function get_entries($view) |
263d6c67 | 345 | { |
346 | global $db; | |
347 | ||
139769aa | 348 | switch ($_SESSION['sort']) |
349 | { | |
350 | case 'ia': | |
351 | $order = 'ORDER BY id'; | |
352 | break; | |
353 | case 'id': | |
354 | $order = 'ORDER BY id DESC'; | |
355 | break; | |
356 | case 'ta': | |
357 | $order = 'ORDER BY lower(title)'; | |
358 | break; | |
359 | case 'td': | |
360 | $order = 'ORDER BY lower(title) DESC'; | |
361 | break; | |
362 | default: | |
363 | $order = 'ORDER BY id'; | |
364 | break; | |
365 | } | |
366 | ||
9fee2e72 | 367 | switch ($view) |
263d6c67 | 368 | { |
369 | case 'archive': | |
139769aa | 370 | $sql = "SELECT * FROM entries WHERE is_read=? " . $order; |
263d6c67 | 371 | $params = array(-1); |
372 | break; | |
373 | case 'fav' : | |
139769aa | 374 | $sql = "SELECT * FROM entries WHERE is_fav=? " . $order; |
263d6c67 | 375 | $params = array(-1); |
376 | break; | |
377 | default: | |
139769aa | 378 | $sql = "SELECT * FROM entries WHERE is_read=? " . $order; |
263d6c67 | 379 | $params = array(0); |
380 | break; | |
381 | } | |
382 | ||
383 | # view query | |
384 | try | |
385 | { | |
386 | $query = $db->getHandle()->prepare($sql); | |
387 | $query->execute($params); | |
388 | $entries = $query->fetchAll(); | |
389 | } | |
390 | catch (Exception $e) | |
391 | { | |
713b2d69 | 392 | logm('view query error : '.$e->getMessage()); |
263d6c67 | 393 | } |
394 | ||
395 | return $entries; | |
396 | } | |
397 | ||
398 | /** | |
399 | * Récupère un article en fonction d'un ID | |
400 | */ | |
401 | function get_article($id) | |
402 | { | |
403 | global $db; | |
404 | ||
405 | $entry = NULL; | |
406 | $sql = "SELECT * FROM entries WHERE id=?"; | |
407 | $params = array(intval($id)); | |
408 | ||
409 | # view article query | |
410 | try | |
411 | { | |
412 | $query = $db->getHandle()->prepare($sql); | |
413 | $query->execute($params); | |
414 | $entry = $query->fetchAll(); | |
415 | } | |
416 | catch (Exception $e) | |
417 | { | |
713b2d69 | 418 | logm('get article query error : '.$e->getMessage()); |
263d6c67 | 419 | } |
420 | ||
421 | return $entry; | |
cf3180f6 | 422 | } |
423 | ||
713b2d69 | 424 | function logm($message) |
425 | { | |
426 | $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n"; | |
a81cd067 | 427 | file_put_contents('./log.txt',$t,FILE_APPEND); |
c8bbe19b | 428 | } |