]>
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.
55 // echo "<pre>http_response_header : ".print_r($http_response_header);
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));
62 // if response is not empty and response is OK
63 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
65 // take charset of page and get it
66 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
68 // if meta tag is found
69 if (!empty($meta[0])) {
70 // retrieve encoding in $enc
71 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
73 // if charset is found set it otherwise, set it to utf-8
74 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
77 $html_charset = 'utf-8';
81 // replace charset of url to charset of page
82 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
92 * Préparation de l'URL avec récupération du contenu avant insertion en base
94 function prepare_url($url)
96 $parametres = array();
97 $url = html_entity_decode(trim($url));
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);
106 if (!preg_match('!^https?://!i', $url))
107 $url = 'http://' . $url;
109 $html = Encoding
::toUTF8(get_external_file($url,15));
110 if (isset($html) and strlen($html) > 0)
112 $r = new Readability($html, $url);
115 $title = $r->articleTitle
->innerHTML
;
119 $parametres['title'] = $title;
120 $parametres['content'] = $r->articleContent
->innerHTML
;
126 * Appel d'une action (mark as fav, archive, delete)
128 function action_to_do($action, $id, $url, $token)
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']);
143 if (verif_token($token)) {
144 $sql_action = "DELETE FROM entries WHERE id=?";
145 $params_action = array($id);
147 else die('CSRF problem');
156 if (isset($sql_action))
158 $query = $db->getHandle()->prepare($sql_action);
159 $query->execute($params_action);
164 die('action query error : '.$e->getMessage());
169 * Détermine quels liens afficher : home, fav ou archives
171 function display_view($view)
178 $sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc";
182 $sql = "SELECT * FROM entries WHERE is_fav=? ORDER BY id desc";
186 $sql = "SELECT * FROM entries WHERE is_read=? ORDER BY id desc";
194 $query = $db->getHandle()->prepare($sql);
195 $query->execute($params);
196 $entries = $query->fetchAll();
200 die('view query error : '.$e->getMessage());
207 * Récupère un article en fonction d'un ID
209 function get_article($id)
214 $sql = "SELECT * FROM entries WHERE id=?";
215 $params = array(intval($id));
220 $query = $db->getHandle()->prepare($sql);
221 $query->execute($params);
222 $entry = $query->fetchAll();
226 die('query error : '.$e->getMessage());
233 * Vérifie si le jeton passé en $_POST correspond à celui en session
235 function verif_token($token)
237 if(isset($_SESSION['token_poche']) && isset($_SESSION['token_time_poche']) && isset($token))
239 if($_SESSION['token_poche'] == $token)
241 $old_timestamp = time() - (15*60);
242 if($_SESSION['token_time_poche'] >= $old_timestamp)