]>
Commit | Line | Data |
---|---|---|
24619534 | 1 | <?php |
2 | ||
c8bbe19b | 3 | /** |
4 | * Permet de générer l'URL de poche pour le bookmarklet | |
5 | */ | |
8046748b | 6 | function get_poche_url() |
c8bbe19b | 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 | ||
24619534 | 18 | // function define to retrieve url content |
c8bbe19b | 19 | function get_external_file($url, $timeout) |
20 | { | |
24619534 | 21 | // spoofing FireFox 18.0 |
22 | $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | |
23 | ||
24 | if (in_array ('curl', get_loaded_extensions())) { | |
25 | // Fetch feed from URL | |
26 | $curl = curl_init(); | |
27 | curl_setopt($curl, CURLOPT_URL, $url); | |
28 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
29 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
30 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
31 | curl_setopt($curl, CURLOPT_HEADER, false); | |
32 | ||
33 | // FeedBurner requires a proper USER-AGENT... | |
34 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | |
35 | curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate"); | |
36 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
37 | ||
38 | $data = curl_exec($curl); | |
39 | ||
40 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
41 | ||
42 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
43 | ||
44 | curl_close($curl); | |
45 | } else { | |
46 | ||
47 | // create http context and add timeout and user-agent | |
48 | $context = stream_context_create(array('http'=>array('timeout' => $timeout, // Timeout : time until we stop waiting for the response. | |
49 | 'header'=> "User-Agent: ".$useragent, // spoot Mozilla Firefox | |
50 | 'follow_location' => true | |
51 | ))); | |
52 | ||
53 | // only download page lesser than 4MB | |
54 | $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source. | |
55 | // echo "<pre>http_response_header : ".print_r($http_response_header); | |
56 | ||
57 | if(isset($http_response_header) and isset($http_response_header[0])) { | |
58 | $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)); | |
59 | } | |
60 | } | |
61 | ||
62 | // if response is not empty and response is OK | |
63 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) { | |
64 | ||
65 | // take charset of page and get it | |
66 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
67 | ||
68 | // if meta tag is found | |
69 | if (!empty($meta[0])) { | |
70 | // retrieve encoding in $enc | |
71 | preg_match('#charset="?(.*)"#si', $meta[0], $enc); | |
72 | ||
73 | // if charset is found set it otherwise, set it to utf-8 | |
74 | $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8'; | |
75 | ||
76 | } else { | |
77 | $html_charset = 'utf-8'; | |
78 | $enc[1] = ''; | |
79 | } | |
80 | ||
81 | // replace charset of url to charset of page | |
82 | $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data); | |
83 | ||
84 | return $data; | |
85 | } | |
86 | else { | |
87 | return FALSE; | |
88 | } | |
3c8d80ae | 89 | } |
90 | ||
8046748b | 91 | /** |
92 | * Préparation de l'URL avec récupération du contenu avant insertion en base | |
93 | */ | |
3c8d80ae | 94 | function prepare_url($url) |
95 | { | |
96 | $parametres = array(); | |
97 | $url = html_entity_decode(trim($url)); | |
98 | ||
99 | // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...) | |
100 | // from shaarli, by sebsauvage | |
101 | $i=strpos($url,'&utm_source='); if ($i!==false) $url=substr($url,0,$i); | |
102 | $i=strpos($url,'?utm_source='); if ($i!==false) $url=substr($url,0,$i); | |
103 | $i=strpos($url,'#xtor=RSS-'); if ($i!==false) $url=substr($url,0,$i); | |
104 | ||
105 | $title = $url; | |
106 | if (!preg_match('!^https?://!i', $url)) | |
107 | $url = 'http://' . $url; | |
108 | ||
109 | $html = Encoding::toUTF8(get_external_file($url,15)); | |
110 | if (isset($html) and strlen($html) > 0) | |
111 | { | |
112 | $r = new Readability($html, $url); | |
113 | if($r->init()) | |
114 | { | |
115 | $title = $r->articleTitle->innerHTML; | |
116 | } | |
117 | } | |
118 | ||
119 | $parametres['title'] = $title; | |
120 | $parametres['content'] = $r->articleContent->innerHTML; | |
121 | ||
122 | return $parametres; | |
c8bbe19b | 123 | } |