]>
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; | |
263d6c67 | 123 | } |
124 | ||
125 | /** | |
126 | * Appel d'une action (mark as fav, archive, delete) | |
127 | */ | |
128 | function action_to_do($action, $id) | |
129 | { | |
130 | global $db; | |
131 | ||
132 | switch ($action) | |
133 | { | |
134 | case 'add': | |
135 | if ($url == '') | |
136 | continue; | |
137 | ||
138 | $parametres_url = prepare_url($url); | |
139 | $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)'; | |
140 | $params_action = array($url, $parametres_url['title'], $parametres_url['content']); | |
141 | break; | |
142 | case 'delete': | |
143 | $sql_action = "DELETE FROM entries WHERE id=?"; | |
144 | $params_action = array($id); | |
145 | break; | |
146 | default: | |
147 | break; | |
148 | } | |
149 | ||
150 | try | |
151 | { | |
152 | # action query | |
153 | if (isset($sql_action)) | |
154 | { | |
155 | $query = $db->getHandle()->prepare($sql_action); | |
156 | $query->execute($params_action); | |
157 | } | |
158 | } | |
159 | catch (Exception $e) | |
160 | { | |
161 | die('action query error : '.$e->getMessage()); | |
162 | } | |
163 | } | |
164 | ||
165 | /** | |
166 | * Détermine quels liens afficher : home, fav ou archives | |
167 | */ | |
168 | function display_view($view) | |
169 | { | |
170 | global $db; | |
171 | ||
172 | switch ($view) | |
173 | { | |
174 | case 'archive': | |
175 | $sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc"; | |
176 | $params = array(-1); | |
177 | break; | |
178 | case 'fav' : | |
179 | $sql = "SELECT * FROM entries WHERE is_fav=? ORDER BY id desc"; | |
180 | $params = array(-1); | |
181 | break; | |
182 | default: | |
183 | $sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc"; | |
184 | $params = array(0); | |
185 | break; | |
186 | } | |
187 | ||
188 | # view query | |
189 | try | |
190 | { | |
191 | $query = $db->getHandle()->prepare($sql); | |
192 | $query->execute($params); | |
193 | $entries = $query->fetchAll(); | |
194 | } | |
195 | catch (Exception $e) | |
196 | { | |
197 | die('view query error : '.$e->getMessage()); | |
198 | } | |
199 | ||
200 | return $entries; | |
201 | } | |
202 | ||
203 | /** | |
204 | * Récupère un article en fonction d'un ID | |
205 | */ | |
206 | function get_article($id) | |
207 | { | |
208 | global $db; | |
209 | ||
210 | $entry = NULL; | |
211 | $sql = "SELECT * FROM entries WHERE id=?"; | |
212 | $params = array(intval($id)); | |
213 | ||
214 | # view article query | |
215 | try | |
216 | { | |
217 | $query = $db->getHandle()->prepare($sql); | |
218 | $query->execute($params); | |
219 | $entry = $query->fetchAll(); | |
220 | } | |
221 | catch (Exception $e) | |
222 | { | |
223 | die('query error : '.$e->getMessage()); | |
224 | } | |
225 | ||
226 | return $entry; | |
c8bbe19b | 227 | } |