]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/functions.php
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 define to retrieve url content
27 function get_external_file ( $url )
30 // spoofing FireFox 18.0
31 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0" ;
33 if ( in_array ( 'curl' , get_loaded_extensions ())) {
34 // Fetch feed from URL
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 );
42 // FeedBurner requires a proper USER-AGENT...
43 curl_setopt ( $curl , CURL_HTTP_VERSION_1_1
, true );
44 curl_setopt ( $curl , CURLOPT_ENCODING
, "gzip, deflate" );
45 curl_setopt ( $curl , CURLOPT_USERAGENT
, $useragent );
47 $data = curl_exec ( $curl );
49 $httpcode = curl_getinfo ( $curl , CURLINFO_HTTP_CODE
);
51 $httpcodeOK = isset ( $httpcode ) and ( $httpcode == 200 or $httpcode == 301 );
56 // create http context and add timeout and user-agent
57 $context = stream_context_create ( array ( 'http' => array ( 'timeout' => $timeout , 'header' => "User-Agent: " . $useragent , /*spoot Mozilla Firefox*/ 'follow_location' => true )));
59 // only download page lesser than 4MB
60 $data = @ file_get_contents ( $url , false , $context , - 1 , 4000000 ); // We download at most 4 MB from source.
62 if ( isset ( $http_response_header ) and isset ( $http_response_header [ 0 ])) {
63 $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 ));
67 // if response is not empty and response is OK
68 if ( isset ( $data ) and isset ( $httpcodeOK ) and $httpcodeOK ) {
70 // take charset of page and get it
71 preg_match ( '#<meta .*charset=.*>#Usi' , $data , $meta );
73 // if meta tag is found
74 if (! empty ( $meta [ 0 ])) {
75 // retrieve encoding in $enc
76 preg_match ( '#charset="?(.*)"#si' , $meta [ 0 ], $enc );
78 // if charset is found set it otherwise, set it to utf-8
79 $html_charset = (! empty ( $enc [ 1 ])) ? strtolower ( $enc [ 1 ]) : 'utf-8' ;
82 $html_charset = 'utf-8' ;
86 // replace charset of url to charset of page
87 $data = str_replace ( 'charset=' . $enc [ 1 ], 'charset=' . $html_charset , $data );
97 * Préparation de l'URL avec récupération du contenu avant insertion en base
99 function prepare_url ( $url )
101 $parametres = array ();
102 $url = html_entity_decode ( trim ( $url ));
104 // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...)
105 // from shaarli, by sebsauvage
106 $i = strpos ( $url , '&utm_source=' ); if ( $i !== false ) $url = substr ( $url , 0 , $i );
107 $i = strpos ( $url , '?utm_source=' ); if ( $i !== false ) $url = substr ( $url , 0 , $i );
108 $i = strpos ( $url , '#xtor=RSS-' ); if ( $i !== false ) $url = substr ( $url , 0 , $i );
111 if (! preg_match ( '!^https?://!i' , $url ))
112 $url = 'http://' . $url ;
114 $html = Encoding
:: toUTF8 ( get_external_file ( $url , 15 ));
115 if ( isset ( $html ) and strlen ( $html ) > 0 )
117 $r = new Readability ( $html , $url );
118 $r- > convertLinksToFootnotes
= CONVERT_LINKS_FOOTNOTES
;
121 $content = $r- > articleContent
-> innerHTML
;
122 $parametres [ 'title' ] = $r- > articleTitle
-> innerHTML
;
123 $parametres [ 'content' ] = $content ;
128 $msg- > add ( 'e' , 'error during url preparation' );
129 logm ( 'error during url preparation' );
134 * On modifie les URLS des images dans le corps de l'article
136 function filtre_picture ( $content , $url , $id )
139 preg_match_all ( '#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si' , $content , $matches , PREG_SET_ORDER
);
140 foreach ( $matches as $i => $link )
142 $link [ 1 ] = trim ( $link [ 1 ]);
143 if (! preg_match ( '#^(([a-z]+://)|(\#))#' , $link [ 1 ]) )
145 $absolute_path = get_absolute_link ( $link [ 2 ], $url );
146 $filename = basename ( parse_url ( $absolute_path , PHP_URL_PATH
));
147 $directory = create_assets_directory ( $id );
148 $fullpath = $directory . '/' . $filename ;
149 download_pictures ( $absolute_path , $fullpath );
150 $content = str_replace ( $matches [ $i ][ 2 ], $fullpath , $content );
159 * Retourne le lien absolu
161 function get_absolute_link ( $relative_link , $url )
163 /* return if already absolute URL */
164 if ( parse_url ( $relative_link , PHP_URL_SCHEME
) != '' ) return $relative_link ;
166 /* queries and anchors */
167 if ( $relative_link [ 0 ]== '#' || $relative_link [ 0 ]== '?' ) return $url . $relative_link ;
169 /* parse base URL and convert to local variables:
170 $scheme, $host, $path */
171 extract ( parse_url ( $url ));
173 /* remove non-directory element from path */
174 $path = preg_replace ( '#/[^/]*$#' , '' , $path );
176 /* destroy path if relative url points to root */
177 if ( $relative_link [ 0 ] == '/' ) $path = '' ;
179 /* dirty absolute URL */
180 $abs = $host . $path . '/' . $relative_link ;
182 /* replace '//' or '/./' or '/foo/../' with '/' */
183 $re = array ( '#(/\.?/)#' , '#/(?!\.\.)[^/]+/\.\./#' );
184 for ( $n = 1 ; $n > 0 ; $abs = preg_replace ( $re , '/' , $abs , - 1 , $n )) {}
186 /* absolute URL is ready! */
187 return $scheme . '://' . $abs ;
191 * Téléchargement des images
194 function download_pictures ( $absolute_path , $fullpath )
196 $rawdata = get_external_file ( $absolute_path );
198 if ( file_exists ( $fullpath )) {
201 $fp = fopen ( $fullpath , 'x' );
202 fwrite ( $fp , $rawdata );
207 * Crée un répertoire de médias pour l'article
209 function create_assets_directory ( $id )
211 $assets_path = ABS_PATH
;
212 if (! is_dir ( $assets_path )) {
213 mkdir ( $assets_path , 0705 );
216 $article_directory = $assets_path . $id ;
217 if (! is_dir ( $article_directory )) {
218 mkdir ( $article_directory , 0705 );
221 return $article_directory ;
225 * Suppression du répertoire d'images
227 function remove_directory ( $directory )
229 if ( is_dir ( $directory )) {
230 $files = array_diff ( scandir ( $directory ), array ( '.' , '..' ));
231 foreach ( $files as $file ) {
232 ( is_dir ( " $directory / $file" )) ? remove_directory(" $directory / $file" ) : unlink ( " $directory / $file" );
234 return rmdir( $directory );
238 function display_view( $view , $id = 0, $full_head = 'yes')
240 global $tpl , $store , $msg ;
245 $entries = $store- >retrieveAll();
246 $tpl- >assign('export', myTool::renderJson( $entries ));
247 $tpl- >draw('export');
251 $tpl- >assign('load_all_js', 0);
254 $tpl- >draw('config');
256 $tpl- >draw('footer');
260 $entry = $store- >retrieveOneById( $id );
262 if ( $entry != NULL) {
263 $tpl- >assign('id', $entry ['id']);
264 $tpl- >assign('url', $entry ['url']);
265 $tpl- >assign('title', $entry ['title']);
266 $tpl- >assign('content', $entry ['content']);
267 $tpl- >assign('is_fav', $entry ['is_fav']);
268 $tpl- >assign('is_read', $entry ['is_read']);
269 $tpl- >assign('load_all_js', 0);
273 logm('error in view call : entry is NULL');
276 logm('view link #' . $id );
279 $entries = $store- >getEntriesByView( $view );
281 $tpl- >assign('entries', $entries );
283 if ( $full_head == 'yes') {
284 $tpl- >assign('load_all_js', 1);
289 $tpl- >draw('entries');
291 if ( $full_head == 'yes') {
293 $tpl- >draw('footer');
300 * Appel d'une action (mark as fav, archive, delete)
302 function action_to_do( $action , $url , $id = 0)
312 if (MyTool::isUrl( $url )) {
313 if( $parametres_url = prepare_url( $url )) {
314 $store- >add( $url , $parametres_url ['title'], $parametres_url ['content']);
315 $last_id = $store- >getLastId();
316 if (DOWNLOAD_PICTURES) {
317 $content = filtre_picture( $parametres_url ['content'], $url , $last_id );
319 $msg- >add('s', 'the link has been added successfully');
323 $msg- >add('e', 'the link has been added successfully');
324 logm( $url . ' is not a valid url');
327 logm('add link ' . $url );
330 remove_directory(ABS_PATH . $id );
331 $store- >deleteById( $id );
332 $msg- >add('s', 'the link has been deleted successfully');
333 logm('delete link #' . $id );
336 $store- >favoriteById( $id );
337 $msg- >add('s', 'the favorite toggle has been done successfully');
338 logm('mark as favorite link #' . $id );
340 case 'toggle_archive' :
341 $store- >archiveById( $id );
342 $msg- >add('s', 'the archive toggle has been done successfully');
343 logm('archive link #' . $id );
350 function logm( $message )
352 $t = strval(date('Y/m/d_H:i:s')).' - '. $_SERVER [" REMOTE_ADDR
"].' - '.strval( $message )." \n
";
353 file_put_contents('./log.txt', $t ,FILE_APPEND);