]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/functions.php
4 * Permet de générer l'URL de poche pour le bookmarklet
9 if(isset($_SERVER['HTTPS'])) {
10 if($_SERVER['HTTPS'] != "off") {
15 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
19 * Génération de la page "vue d'un article"
21 function generate_page($entry)
23 raintpl
::$tpl_dir = './tpl/';
24 raintpl
::$cache_dir = "./cache/";
25 raintpl
::$base_url = url();
26 raintpl
::configure( 'path_replace', false );
27 raintpl
::configure('debug', false);
31 $tpl->assign("id", $entry['id']);
32 $tpl->assign("url", $entry['url']);
33 $tpl->assign("title", $entry['title']);
34 $tpl->assign("content", $entry['content']);
35 $tpl->assign("is_fav", $entry['is_fav']);
36 $tpl->assign("is_read", $entry['is_read']);
41 // function define to retrieve url content
42 function get_external_file($url, $timeout)
44 // spoofing FireFox 18.0
45 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
47 if (in_array ('curl', get_loaded_extensions())) {
48 // Fetch feed from URL
50 curl_setopt($curl, CURLOPT_URL
, $url);
51 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
52 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
53 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
54 curl_setopt($curl, CURLOPT_HEADER
, false);
56 // FeedBurner requires a proper USER-AGENT...
57 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
58 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
59 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
61 $data = curl_exec($curl);
63 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
65 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
70 // create http context and add timeout and user-agent
71 $context = stream_context_create(array('http'=>array('timeout' => $timeout, // Timeout : time until we stop waiting for the response.
72 'header'=> "User-Agent: ".$useragent, // spoot Mozilla Firefox
73 'follow_location' => 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.
78 // echo "<pre>http_response_header : ".print_r($http_response_header);
80 if(isset($http_response_header) and isset($http_response_header[0])) {
81 $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));
85 // if response is not empty and response is OK
86 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
88 // take charset of page and get it
89 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
91 // if meta tag is found
92 if (!empty($meta[0])) {
93 // retrieve encoding in $enc
94 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
96 // if charset is found set it otherwise, set it to utf-8
97 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
100 $html_charset = 'utf-8';
104 // replace charset of url to charset of page
105 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
114 function prepare_url($url)
116 $parametres = array();
117 $url = html_entity_decode(trim($url));
119 // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...)
120 // from shaarli, by sebsauvage
121 $i=strpos($url,'&utm_source='); if ($i!==false) $url=substr($url,0,$i);
122 $i=strpos($url,'?utm_source='); if ($i!==false) $url=substr($url,0,$i);
123 $i=strpos($url,'#xtor=RSS-'); if ($i!==false) $url=substr($url,0,$i);
126 if (!preg_match('!^https?://!i', $url))
127 $url = 'http://' . $url;
129 $html = Encoding
::toUTF8(get_external_file($url,15));
130 if (isset($html) and strlen($html) > 0)
132 $r = new Readability($html, $url);
135 $title = $r->articleTitle
->innerHTML
;
139 $parametres['title'] = $title;
140 $parametres['content'] = $r->articleContent
->innerHTML
;