]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/functions.php
display flash message in article view
[github/wallabag/wallabag.git] / inc / functions.php
1 <?php
2 /**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11 /**
12 * Permet de générer l'URL de poche pour le bookmarklet
13 */
14 function get_poche_url()
15 {
16 $protocol = "http";
17 if(isset($_SERVER['HTTPS'])) {
18 if($_SERVER['HTTPS'] != "off" && $_SERVER['HTTPS'] != "") {
19 $protocol = "https";
20 }
21 }
22
23 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
24 }
25
26 // function define to retrieve url content
27 function get_external_file($url)
28 {
29 $timeout = 15;
30 // spoofing FireFox 18.0
31 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
32
33 if (in_array ('curl', get_loaded_extensions())) {
34 // Fetch feed from URL
35 $curl = curl_init();
36 curl_setopt($curl, CURLOPT_URL, $url);
37 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
38 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
39 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
40 curl_setopt($curl, CURLOPT_HEADER, false);
41
42 // FOR SSL do not verified certificate
43 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
44 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
45
46 // FeedBurner requires a proper USER-AGENT...
47 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
48 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
49 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
50
51 $data = curl_exec($curl);
52
53 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
54
55 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
56
57 curl_close($curl);
58 } else {
59
60 // create http context and add timeout and user-agent
61 $context = stream_context_create(array(
62 'http'=>array('timeout' => $timeout,
63 'header'=> "User-Agent: ".$useragent, /*spoot Mozilla Firefox*/
64 'follow_location' => true),
65 // FOR SSL do not verified certificate
66 'ssl' => array('verify_peer' => false,
67 'allow_self_signed' => true)
68 )
69 );
70
71 // only download page lesser than 4MB
72 $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source.
73
74 if(isset($http_response_header) and isset($http_response_header[0])) {
75 $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));
76 }
77 }
78
79 // if response is not empty and response is OK
80 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
81
82 // take charset of page and get it
83 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
84
85 // if meta tag is found
86 if (!empty($meta[0])) {
87 // retrieve encoding in $enc
88 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
89
90 // if charset is found set it otherwise, set it to utf-8
91 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
92
93 } else {
94 $html_charset = 'utf-8';
95 $enc[1] = '';
96 }
97
98 // replace charset of url to charset of page
99 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
100
101 return $data;
102 }
103 else {
104 return FALSE;
105 }
106 }
107
108 /**
109 * Préparation de l'URL avec récupération du contenu avant insertion en base
110 */
111 function prepare_url($url)
112 {
113 $parametres = array();
114 $url = html_entity_decode(trim($url));
115
116 // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...)
117 // from shaarli, by sebsauvage
118 $i=strpos($url,'&utm_source='); if ($i!==false) $url=substr($url,0,$i);
119 $i=strpos($url,'?utm_source='); if ($i!==false) $url=substr($url,0,$i);
120 $i=strpos($url,'#xtor=RSS-'); if ($i!==false) $url=substr($url,0,$i);
121
122 $title = $url;
123 $html = Encoding::toUTF8(get_external_file($url,15));
124 // If get_external_file if not able to retrieve HTTPS content try the same URL with HTTP protocol
125 if (!preg_match('!^https?://!i', $url) && (!isset($html) || strlen($html) <= 0)) {
126 $url = 'http://' . $url;
127 $html = Encoding::toUTF8(get_external_file($url,15));
128 }
129
130 if (isset($html) and strlen($html) > 0)
131 {
132 $r = new Readability($html, $url);
133
134 $r->convertLinksToFootnotes = CONVERT_LINKS_FOOTNOTES;
135 $r->revertForcedParagraphElements = REVERT_FORCED_PARAGRAPH_ELEMENTS;
136
137 if($r->init())
138 {
139 $content = $r->articleContent->innerHTML;
140 $parametres['title'] = $r->articleTitle->innerHTML;
141 $parametres['content'] = $content;
142 return $parametres;
143 }
144 }
145
146 return FALSE;
147 }
148
149 /**
150 * On modifie les URLS des images dans le corps de l'article
151 */
152 function filtre_picture($content, $url, $id)
153 {
154 $matches = array();
155 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER);
156 foreach($matches as $i => $link)
157 {
158 $link[1] = trim($link[1]);
159 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) )
160 {
161 $absolute_path = get_absolute_link($link[2],$url);
162 $filename = basename(parse_url($absolute_path, PHP_URL_PATH));
163 $directory = create_assets_directory($id);
164 $fullpath = $directory . '/' . $filename;
165 download_pictures($absolute_path, $fullpath);
166 $content = str_replace($matches[$i][2], $fullpath, $content);
167 }
168
169 }
170
171 return $content;
172 }
173
174 /**
175 * Retourne le lien absolu
176 */
177 function get_absolute_link($relative_link, $url)
178 {
179 /* return if already absolute URL */
180 if (parse_url($relative_link, PHP_URL_SCHEME) != '') return $relative_link;
181
182 /* queries and anchors */
183 if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link;
184
185 /* parse base URL and convert to local variables:
186 $scheme, $host, $path */
187 extract(parse_url($url));
188
189 /* remove non-directory element from path */
190 $path = preg_replace('#/[^/]*$#', '', $path);
191
192 /* destroy path if relative url points to root */
193 if ($relative_link[0] == '/') $path = '';
194
195 /* dirty absolute URL */
196 $abs = $host . $path . '/' . $relative_link;
197
198 /* replace '//' or '/./' or '/foo/../' with '/' */
199 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
200 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
201
202 /* absolute URL is ready! */
203 return $scheme.'://'.$abs;
204 }
205
206 /**
207 * Téléchargement des images
208 */
209
210 function download_pictures($absolute_path, $fullpath)
211 {
212 $rawdata = get_external_file($absolute_path);
213
214 if(file_exists($fullpath)) {
215 unlink($fullpath);
216 }
217 $fp = fopen($fullpath, 'x');
218 fwrite($fp, $rawdata);
219 fclose($fp);
220 }
221
222 /**
223 * Crée un répertoire de médias pour l'article
224 */
225 function create_assets_directory($id)
226 {
227 $assets_path = ABS_PATH;
228 if(!is_dir($assets_path)) {
229 mkdir($assets_path, 0705);
230 }
231
232 $article_directory = $assets_path . $id;
233 if(!is_dir($article_directory)) {
234 mkdir($article_directory, 0705);
235 }
236
237 return $article_directory;
238 }
239
240 /**
241 * Suppression du répertoire d'images
242 */
243 function remove_directory($directory)
244 {
245 if(is_dir($directory)) {
246 $files = array_diff(scandir($directory), array('.','..'));
247 foreach ($files as $file) {
248 (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file");
249 }
250 return rmdir($directory);
251 }
252 }
253
254 function display_view($view, $id = 0, $full_head = 'yes')
255 {
256 global $tpl, $store, $msg;
257
258 switch ($view)
259 {
260 case 'export':
261 $entries = $store->retrieveAll();
262 $tpl->assign('export', myTool::renderJson($entries));
263 $tpl->draw('export');
264 logm('export view');
265 break;
266 case 'config':
267 $tpl->assign('load_all_js', 0);
268 $tpl->draw('head');
269 $tpl->draw('home');
270 $tpl->draw('config');
271 $tpl->draw('js');
272 $tpl->draw('footer');
273 logm('config view');
274 break;
275 case 'view':
276 $entry = $store->retrieveOneById($id);
277
278 if ($entry != NULL) {
279 $tpl->assign('id', $entry['id']);
280 $tpl->assign('url', $entry['url']);
281 $tpl->assign('title', $entry['title']);
282 $tpl->assign('content', $entry['content']);
283 $tpl->assign('is_fav', $entry['is_fav']);
284 $tpl->assign('is_read', $entry['is_read']);
285 $tpl->assign('load_all_js', 0);
286 $tpl->draw('view');
287 }
288 else {
289 logm('error in view call : entry is NULL');
290 }
291
292 logm('view link #' . $id);
293 break;
294 default: # home view
295 $entries = $store->getEntriesByView($view);
296
297 $tpl->assign('entries', $entries);
298
299 if ($full_head == 'yes') {
300 $tpl->assign('load_all_js', 1);
301 $tpl->draw('head');
302 $tpl->draw('home');
303 }
304
305 $tpl->draw('entries');
306
307 if ($full_head == 'yes') {
308 $tpl->draw('js');
309 $tpl->draw('footer');
310 }
311 break;
312 }
313 }
314
315 /**
316 * Appel d'une action (mark as fav, archive, delete)
317 */
318 function action_to_do($action, $url, $id = 0)
319 {
320 global $store, $msg;
321
322 switch ($action)
323 {
324 case 'add':
325 if ($url == '')
326 continue;
327
328 if (MyTool::isUrl($url)) {
329 if($parametres_url = prepare_url($url)) {
330 if ($store->add($url, $parametres_url['title'], $parametres_url['content'])) {
331 $last_id = $store->getLastId();
332 if (DOWNLOAD_PICTURES) {
333 $content = filtre_picture($parametres_url['content'], $url, $last_id);
334 }
335 $msg->add('s', 'the link has been added successfully');
336 }
337 else {
338 $msg->add('e', 'error during insertion : the link wasn\'t added');
339 }
340 }
341 else {
342 $msg->add('e', 'error during url preparation : the link wasn\'t added');
343 logm('error during url preparation');
344 }
345 }
346 else {
347 $msg->add('e', 'error during url preparation : the link is not valid');
348 logm($url . ' is not a valid url');
349 }
350
351 logm('add link ' . $url);
352 break;
353 case 'delete':
354 if ($store->deleteById($id)) {
355 remove_directory(ABS_PATH . $id);
356 $msg->add('s', 'the link has been deleted successfully');
357 logm('delete link #' . $id);
358 }
359 else {
360 $msg->add('e', 'the link wasn\'t deleted');
361 logm('error : can\'t delete link #' . $id);
362 }
363 break;
364 case 'toggle_fav' :
365 $store->favoriteById($id);
366 $msg->add('s', 'the favorite toggle has been done successfully');
367 logm('mark as favorite link #' . $id);
368 break;
369 case 'toggle_archive' :
370 $store->archiveById($id);
371 $msg->add('s', 'the archive toggle has been done successfully');
372 logm('archive link #' . $id);
373 break;
374 default:
375 break;
376 }
377 }
378
379 function logm($message)
380 {
381 $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n";
382 file_put_contents('./log.txt',$t,FILE_APPEND);
383 }