]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/functions.php
les liens sont maintenant transformés en note de bas de page
[github/wallabag/wallabag.git] / inc / functions.php
CommitLineData
24619534 1<?php
2
c8bbe19b 3/**
4 * Permet de générer l'URL de poche pour le bookmarklet
5 */
8046748b 6function 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 19function 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.
139769aa 49 'header'=> "User-Agent: ".$useragent, // spoot Mozilla Firefox
50 'follow_location' => true
51 )));
24619534 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.
24619534 55
56 if(isset($http_response_header) and isset($http_response_header[0])) {
57 $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));
58 }
59 }
60
61 // if response is not empty and response is OK
62 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
63
64 // take charset of page and get it
65 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
66
67 // if meta tag is found
68 if (!empty($meta[0])) {
69 // retrieve encoding in $enc
70 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
71
72 // if charset is found set it otherwise, set it to utf-8
73 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
74
75 } else {
76 $html_charset = 'utf-8';
77 $enc[1] = '';
78 }
79
80 // replace charset of url to charset of page
81 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
82
83 return $data;
84 }
85 else {
86 return FALSE;
87 }
3c8d80ae 88}
89
8046748b 90/**
91 * Préparation de l'URL avec récupération du contenu avant insertion en base
92 */
3c8d80ae 93function prepare_url($url)
94{
95 $parametres = array();
96 $url = html_entity_decode(trim($url));
97
98 // We remove the annoying parameters added by FeedBurner and GoogleFeedProxy (?utm_source=...)
99 // from shaarli, by sebsauvage
100 $i=strpos($url,'&utm_source='); if ($i!==false) $url=substr($url,0,$i);
101 $i=strpos($url,'?utm_source='); if ($i!==false) $url=substr($url,0,$i);
102 $i=strpos($url,'#xtor=RSS-'); if ($i!==false) $url=substr($url,0,$i);
103
104 $title = $url;
105 if (!preg_match('!^https?://!i', $url))
106 $url = 'http://' . $url;
107
108 $html = Encoding::toUTF8(get_external_file($url,15));
109 if (isset($html) and strlen($html) > 0)
110 {
111 $r = new Readability($html, $url);
4456315b 112 $r->convertLinksToFootnotes = TRUE;
3c8d80ae 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 */
cf3180f6 128function action_to_do($action, $id, $url, $token)
263d6c67 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']);
a81cd067 141 logm('add link ' . $url);
263d6c67 142 break;
143 case 'delete':
cf3180f6 144 if (verif_token($token)) {
145 $sql_action = "DELETE FROM entries WHERE id=?";
146 $params_action = array($id);
a81cd067 147 logm('delete link #' . $id);
cf3180f6 148 }
713b2d69 149 else logm('csrf problem while deleting entry');
263d6c67 150 break;
139769aa 151 case 'toggle_fav' :
152 if (verif_token($token)) {
153 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?";
154 $params_action = array($id);
a81cd067 155 logm('mark as favorite link #' . $id);
139769aa 156 }
713b2d69 157 else logm('csrf problem while fav entry');
139769aa 158 break;
159 case 'toggle_archive' :
160 if (verif_token($token)) {
161 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?";
162 $params_action = array($id);
a81cd067 163 logm('archive link #' . $id);
139769aa 164 }
713b2d69 165 else logm('csrf problem while archive entry');
139769aa 166 break;
263d6c67 167 default:
168 break;
169 }
170
171 try
172 {
173 # action query
174 if (isset($sql_action))
175 {
176 $query = $db->getHandle()->prepare($sql_action);
177 $query->execute($params_action);
178 }
179 }
180 catch (Exception $e)
181 {
713b2d69 182 logm('action query error : '.$e->getMessage());
263d6c67 183 }
184}
185
186/**
187 * Détermine quels liens afficher : home, fav ou archives
188 */
9fee2e72 189function display_view($view)
263d6c67 190{
191 global $db;
192
139769aa 193 switch ($_SESSION['sort'])
194 {
195 case 'ia':
196 $order = 'ORDER BY id';
197 break;
198 case 'id':
199 $order = 'ORDER BY id DESC';
200 break;
201 case 'ta':
202 $order = 'ORDER BY lower(title)';
203 break;
204 case 'td':
205 $order = 'ORDER BY lower(title) DESC';
206 break;
207 default:
208 $order = 'ORDER BY id';
209 break;
210 }
211
9fee2e72 212 switch ($view)
263d6c67 213 {
214 case 'archive':
139769aa 215 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
263d6c67 216 $params = array(-1);
217 break;
218 case 'fav' :
139769aa 219 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
263d6c67 220 $params = array(-1);
221 break;
222 default:
139769aa 223 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
263d6c67 224 $params = array(0);
225 break;
226 }
227
228 # view query
229 try
230 {
231 $query = $db->getHandle()->prepare($sql);
232 $query->execute($params);
233 $entries = $query->fetchAll();
234 }
235 catch (Exception $e)
236 {
713b2d69 237 logm('view query error : '.$e->getMessage());
263d6c67 238 }
239
240 return $entries;
241}
242
243/**
244 * Récupère un article en fonction d'un ID
245 */
246function get_article($id)
247{
248 global $db;
249
250 $entry = NULL;
251 $sql = "SELECT * FROM entries WHERE id=?";
252 $params = array(intval($id));
253
254 # view article query
255 try
256 {
257 $query = $db->getHandle()->prepare($sql);
258 $query->execute($params);
259 $entry = $query->fetchAll();
260 }
261 catch (Exception $e)
262 {
713b2d69 263 logm('get article query error : '.$e->getMessage());
263d6c67 264 }
265
266 return $entry;
cf3180f6 267}
268
269/**
270 * Vérifie si le jeton passé en $_POST correspond à celui en session
271 */
272function verif_token($token)
273{
274 if(isset($_SESSION['token_poche']) && isset($_SESSION['token_time_poche']) && isset($token))
275 {
276 if($_SESSION['token_poche'] == $token)
277 {
278 $old_timestamp = time() - (15*60);
279 if($_SESSION['token_time_poche'] >= $old_timestamp)
280 {
281 return TRUE;
282 }
643e3037 283 else {
284 session_destroy();
713b2d69 285 logm('session expired');
643e3037 286 }
cf3180f6 287 }
713b2d69 288 else {
289 logm('token error : the token is different');
290 return FALSE;
291 }
292 }
293 else {
294 logm('token error : the token is not here');
295 return FALSE;
cf3180f6 296 }
713b2d69 297}
298
299function logm($message)
300{
301 $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n";
a81cd067 302 file_put_contents('./log.txt',$t,FILE_APPEND);
c8bbe19b 303}