3 * poche, a read it later open source system
6 * @author Nicolas Lœuillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
12 * Permet de générer l'URL de poche pour le bookmarklet
14 function get_poche_url()
17 if(isset($_SERVER['HTTPS'])) {
18 if($_SERVER['HTTPS'] != "off" && $_SERVER['HTTPS'] != "") {
23 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
26 function encode_string($string)
28 return sha1($string . SALT
);
31 // function define to retrieve url content
32 function get_external_file($url)
35 // spoofing FireFox 18.0
36 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
38 if (in_array ('curl', get_loaded_extensions())) {
39 // Fetch feed from URL
41 curl_setopt($curl, CURLOPT_URL
, $url);
42 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
43 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
44 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
45 curl_setopt($curl, CURLOPT_HEADER
, false);
47 // FOR SSL do not verified certificate
48 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
49 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
51 // FeedBurner requires a proper USER-AGENT...
52 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
53 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
54 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
56 $data = curl_exec($curl);
58 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
60 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
65 // create http context and add timeout and user-agent
66 $context = stream_context_create(array(
67 'http'=>array('timeout' => $timeout,
68 'header'=> "User-Agent: ".$useragent, /*spoot Mozilla Firefox*/
69 'follow_location' => true),
70 // FOR SSL do not verified certificate
71 'ssl' => array('verify_peer' => false,
72 'allow_self_signed' => true)
76 // only download page lesser than 4MB
77 $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source.
79 if(isset($http_response_header) and isset($http_response_header[0])) {
80 $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));
84 // if response is not empty and response is OK
85 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
87 // take charset of page and get it
88 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
90 // if meta tag is found
91 if (!empty($meta[0])) {
92 // retrieve encoding in $enc
93 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
95 // if charset is found set it otherwise, set it to utf-8
96 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
99 $html_charset = 'utf-8';
103 // replace charset of url to charset of page
104 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
114 * Préparation de l'URL avec récupération du contenu avant insertion en base
116 function prepare_url($url)
118 $parametres = array();
119 $url = html_entity_decode(trim($url));
121 // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...)
122 // from shaarli, by sebsauvage
123 $i=strpos($url,'&utm_source='); if ($i!==false) $url=substr($url,0,$i);
124 $i=strpos($url,'?utm_source='); if ($i!==false) $url=substr($url,0,$i);
125 $i=strpos($url,'#xtor=RSS-'); if ($i!==false) $url=substr($url,0,$i);
128 $html = Encoding
::toUTF8(get_external_file($url,15));
129 // If get_external_file if not able to retrieve HTTPS content try the same URL with HTTP protocol
130 if (!preg_match('!^https?://!i', $url) && (!isset($html) || strlen($html) <= 0)) {
131 $url = 'http://' . $url;
132 $html = Encoding
::toUTF8(get_external_file($url,15));
135 if (function_exists('tidy_parse_string')) {
136 $tidy = tidy_parse_string($html, array(), 'UTF8');
137 $tidy->cleanRepair();
138 $html = $tidy->value
;
141 if (isset($html) and strlen($html) > 0)
143 $r = new Readability($html, $url);
145 $r->convertLinksToFootnotes
= CONVERT_LINKS_FOOTNOTES
;
146 $r->revertForcedParagraphElements
= REVERT_FORCED_PARAGRAPH_ELEMENTS
;
150 $content = $r->articleContent
->innerHTML
;
151 $parametres['title'] = $r->articleTitle
->innerHTML
;
152 $parametres['content'] = $content;
161 * On modifie les URLS des images dans le corps de l'article
163 function filtre_picture($content, $url, $id)
166 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER
);
167 foreach($matches as $i => $link)
169 $link[1] = trim($link[1]);
170 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) )
172 $absolute_path = get_absolute_link($link[2],$url);
173 $filename = basename(parse_url($absolute_path, PHP_URL_PATH
));
174 $directory = create_assets_directory($id);
175 $fullpath = $directory . '/' . $filename;
176 download_pictures($absolute_path, $fullpath);
177 $content = str_replace($matches[$i][2], $fullpath, $content);
186 * Retourne le lien absolu
188 function get_absolute_link($relative_link, $url)
190 /* return if already absolute URL */
191 if (parse_url($relative_link, PHP_URL_SCHEME
) != '') return $relative_link;
193 /* queries and anchors */
194 if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link;
196 /* parse base URL and convert to local variables:
197 $scheme, $host, $path */
198 extract(parse_url($url));
200 /* remove non-directory element from path */
201 $path = preg_replace('#/[^/]*$#', '', $path);
203 /* destroy path if relative url points to root */
204 if ($relative_link[0] == '/') $path = '';
206 /* dirty absolute URL */
207 $abs = $host . $path . '/' . $relative_link;
209 /* replace '//' or '/./' or '/foo/../' with '/' */
210 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
211 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
213 /* absolute URL is ready! */
214 return $scheme.'://'.$abs;
218 * Téléchargement des images
221 function download_pictures($absolute_path, $fullpath)
223 $rawdata = get_external_file($absolute_path);
225 if(file_exists($fullpath)) {
228 $fp = fopen($fullpath, 'x');
229 fwrite($fp, $rawdata);
234 * Crée un répertoire de médias pour l'article
236 function create_assets_directory($id)
238 $assets_path = ABS_PATH
;
239 if(!is_dir($assets_path)) {
240 mkdir($assets_path, 0705);
243 $article_directory = $assets_path . $id;
244 if(!is_dir($article_directory)) {
245 mkdir($article_directory, 0705);
248 return $article_directory;
252 * Suppression du répertoire d'images
254 function remove_directory($directory)
256 if(is_dir($directory)) {
257 $files = array_diff(scandir($directory), array('.','..'));
258 foreach ($files as $file) {
259 (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file");
261 return rmdir($directory);
265 function display_view($view, $id = 0, $full_head = 'yes')
267 global $tpl, $store, $msg;
272 $entries = $store->retrieveAll();
273 $tpl->assign('export', myTool::renderJson($entries));
274 $tpl->draw('export');
278 $tpl->assign('load_all_js', 0);
281 $tpl->draw('config');
283 $tpl->draw('footer');
287 $entry = $store->retrieveOneById($id);
289 if ($entry != NULL) {
290 $tpl->assign('id', $entry['id']);
291 $tpl->assign('url', $entry['url']);
292 $tpl->assign('title', $entry['title']);
293 $content = $entry['content'];
294 if (function_exists('tidy_parse_string')) {
295 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
296 $tidy->cleanRepair();
297 $content = $tidy->value;
299 $tpl->assign('content', $content);
300 $tpl->assign('is_fav', $entry['is_fav']);
301 $tpl->assign('is_read', $entry['is_read']);
302 $tpl->assign('load_all_js', 0);
306 logm('error in view call : entry is NULL');
309 logm('view link #' . $id);
312 $entries = $store->getEntriesByView($view);
314 $tpl->assign('entries', $entries);
316 if ($full_head == 'yes') {
317 $tpl->assign('load_all_js', 1);
322 $tpl->draw('entries');
324 if ($full_head == 'yes') {
326 $tpl->draw('footer');
333 * Appel d'une action (mark as fav, archive, delete)
335 function action_to_do($action, $url, $id = 0)
345 if (MyTool::isUrl($url)) {
346 if($parametres_url = prepare_url($url)) {
347 if ($store->add($url, $parametres_url['title'], $parametres_url['content'])) {
348 $last_id = $store->getLastId();
349 if (DOWNLOAD_PICTURES) {
350 $content = filtre_picture($parametres_url['content'], $url, $last_id);
352 $msg->add('s', 'the link has been added successfully');
355 $msg->add('e', 'error during insertion : the link wasn\'t added');
359 $msg->add('e', 'error during url preparation : the link wasn\'t added');
360 logm('error during url preparation');
364 $msg->add('e', 'error during url preparation : the link is not valid');
365 logm($url . ' is not a valid url');
368 logm('add link ' . $url);
371 if ($store->deleteById($id)) {
372 remove_directory(ABS_PATH . $id);
373 $msg->add('s', 'the link has been deleted successfully');
374 logm('delete link #' . $id);
377 $msg->add('e', 'the link wasn\'t deleted');
378 logm('error : can\'t delete link #' . $id);
382 $store->favoriteById($id);
383 logm('mark as favorite link #' . $id);
385 case 'toggle_archive' :
386 $store->archiveById($id);
387 logm('archive link #' . $id);
394 function logm($message)
396 $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR
"].' - '.strval($message)."\n
";
397 file_put_contents('./log.txt',$t,FILE_APPEND);