]>
Commit | Line | Data |
---|---|---|
24619534 | 1 | <?php |
2 | ||
c8bbe19b | 3 | /** |
4 | * Permet de générer l'URL de poche pour le bookmarklet | |
5 | */ | |
6 | function url() | |
7 | { | |
8 | $protocol = "http"; | |
9 | if(isset($_SERVER['HTTPS'])) { | |
10 | if($_SERVER['HTTPS'] != "off") { | |
11 | $protocol = "https"; | |
12 | } | |
13 | } | |
24619534 | 14 | |
c8bbe19b | 15 | return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
24619534 | 16 | } |
17 | ||
c8bbe19b | 18 | /** |
19 | * Génération de la page "vue d'un article" | |
20 | */ | |
21 | function generate_page($entry) | |
22 | { | |
23 | raintpl::$tpl_dir = './tpl/'; | |
24 | raintpl::$cache_dir = "./cache/"; | |
25 | raintpl::$base_url = url(); | |
24619534 | 26 | raintpl::configure( 'path_replace', false ); |
27 | raintpl::configure('debug', false); | |
28 | ||
c8bbe19b | 29 | $tpl = new raintpl(); |
24619534 | 30 | |
c8bbe19b | 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']); | |
24619534 | 37 | |
c8bbe19b | 38 | $tpl->draw( "index"); |
24619534 | 39 | } |
40 | ||
41 | // function define to retrieve url content | |
c8bbe19b | 42 | function get_external_file($url, $timeout) |
43 | { | |
24619534 | 44 | // spoofing FireFox 18.0 |
45 | $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | |
46 | ||
47 | if (in_array ('curl', get_loaded_extensions())) { | |
48 | // Fetch feed from URL | |
49 | $curl = curl_init(); | |
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); | |
55 | ||
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); | |
60 | ||
61 | $data = curl_exec($curl); | |
62 | ||
63 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
64 | ||
65 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
66 | ||
67 | curl_close($curl); | |
68 | } else { | |
69 | ||
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 | |
74 | ))); | |
75 | ||
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); | |
79 | ||
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)); | |
82 | } | |
83 | } | |
84 | ||
85 | // if response is not empty and response is OK | |
86 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) { | |
87 | ||
88 | // take charset of page and get it | |
89 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
90 | ||
91 | // if meta tag is found | |
92 | if (!empty($meta[0])) { | |
93 | // retrieve encoding in $enc | |
94 | preg_match('#charset="?(.*)"#si', $meta[0], $enc); | |
95 | ||
96 | // if charset is found set it otherwise, set it to utf-8 | |
97 | $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8'; | |
98 | ||
99 | } else { | |
100 | $html_charset = 'utf-8'; | |
101 | $enc[1] = ''; | |
102 | } | |
103 | ||
104 | // replace charset of url to charset of page | |
105 | $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data); | |
106 | ||
107 | return $data; | |
108 | } | |
109 | else { | |
110 | return FALSE; | |
111 | } | |
3c8d80ae | 112 | } |
113 | ||
114 | function prepare_url($url) | |
115 | { | |
116 | $parametres = array(); | |
117 | $url = html_entity_decode(trim($url)); | |
118 | ||
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); | |
124 | ||
125 | $title = $url; | |
126 | if (!preg_match('!^https?://!i', $url)) | |
127 | $url = 'http://' . $url; | |
128 | ||
129 | $html = Encoding::toUTF8(get_external_file($url,15)); | |
130 | if (isset($html) and strlen($html) > 0) | |
131 | { | |
132 | $r = new Readability($html, $url); | |
133 | if($r->init()) | |
134 | { | |
135 | $title = $r->articleTitle->innerHTML; | |
136 | } | |
137 | } | |
138 | ||
139 | $parametres['title'] = $title; | |
140 | $parametres['content'] = $r->articleContent->innerHTML; | |
141 | ||
142 | return $parametres; | |
c8bbe19b | 143 | } |