]>
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
6 function get_poche_url()
9 if(isset($_SERVER['HTTPS'])) {
10 if($_SERVER['HTTPS'] != "off") {
15 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
18 // function define to retrieve url content
19 function get_external_file($url, $timeout)
21 // spoofing FireFox 18.0
22 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
24 if (in_array ('curl', get_loaded_extensions())) {
25 // Fetch feed from URL
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);
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);
38 $data = curl_exec($curl);
40 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
42 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
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
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.
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));
61 // if response is not empty and response is OK
62 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
64 // take charset of page and get it
65 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
67 // if meta tag is found
68 if (!empty($meta[0])) {
69 // retrieve encoding in $enc
70 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
72 // if charset is found set it otherwise, set it to utf-8
73 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
76 $html_charset = 'utf-8';
80 // replace charset of url to charset of page
81 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
91 * Préparation de l'URL avec récupération du contenu avant insertion en base
93 function prepare_url($url)
95 $parametres = array();
96 $url = html_entity_decode(trim($url));
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);
105 if (!preg_match('!^https?://!i', $url))
106 $url = 'http://' . $url;
108 $html = Encoding
::toUTF8(get_external_file($url,15));
109 if (isset($html) and strlen($html) > 0)
111 $r = new Readability($html, $url);
114 $title = $r->articleTitle
->innerHTML
;
118 $parametres['title'] = $title;
119 $parametres['content'] = $r->articleContent
->innerHTML
;
125 * Appel d'une action (mark as fav, archive, delete)
127 function action_to_do($action, $id, $url, $token)
137 $parametres_url = prepare_url($url);
138 $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
139 $params_action = array($url, $parametres_url['title'], $parametres_url['content']);
140 logm('add link ' . $url);
143 if (verif_token($token)) {
144 $sql_action = "DELETE FROM entries WHERE id=?";
145 $params_action = array($id);
146 logm('delete link #' . $id);
148 else logm('csrf problem while deleting entry');
151 if (verif_token($token)) {
152 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=?";
153 $params_action = array($id);
154 logm('mark as favorite link #' . $id);
156 else logm('csrf problem while fav entry');
158 case 'toggle_archive' :
159 if (verif_token($token)) {
160 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=?";
161 $params_action = array($id);
162 logm('archive link #' . $id);
164 else logm('csrf problem while archive entry');
173 if (isset($sql_action))
175 $query = $db->getHandle()->prepare($sql_action);
176 $query->execute($params_action);
181 logm('action query error : '.$e->getMessage());
186 * Détermine quels liens afficher : home, fav ou archives
188 function display_view($view)
192 switch ($_SESSION['sort'])
195 $order = 'ORDER BY id';
198 $order = 'ORDER BY id DESC';
201 $order = 'ORDER BY lower(title)';
204 $order = 'ORDER BY lower(title) DESC';
207 $order = 'ORDER BY id';
214 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
218 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
222 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
230 $query = $db->getHandle()->prepare($sql);
231 $query->execute($params);
232 $entries = $query->fetchAll();
236 logm('view query error : '.$e->getMessage());
243 * Récupère un article en fonction d'un ID
245 function get_article($id)
250 $sql = "SELECT * FROM entries WHERE id=?";
251 $params = array(intval($id));
256 $query = $db->getHandle()->prepare($sql);
257 $query->execute($params);
258 $entry = $query->fetchAll();
262 logm('get article query error : '.$e->getMessage());
269 * Vérifie si le jeton passé en $_POST correspond à celui en session
271 function verif_token($token)
273 if(isset($_SESSION['token_poche']) && isset($_SESSION['token_time_poche']) && isset($token))
275 if($_SESSION['token_poche'] == $token)
277 $old_timestamp = time() - (15*60);
278 if($_SESSION['token_time_poche'] >= $old_timestamp)
284 logm('session expired');
288 logm('token error : the token is different');
293 logm('token error : the token is not here');
298 function logm($message)
300 $t = strval(date('Y/m/d_H:i:s')).' - '.$_SERVER["REMOTE_ADDR"].' - '.strval($message)."\n";
301 file_put_contents('./log.txt',$t,FILE_APPEND
);