]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Tools.class.php
[fix] RSS feeds were buggy when I update full-text RSS
[github/wallabag/wallabag.git] / inc / poche / Tools.class.php
CommitLineData
eb1af592
NL
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Tools
12{
13 public static function initPhp()
14 {
15 define('START_TIME', microtime(true));
16
17 if (phpversion() < 5) {
18 die(_('Oops, it seems you don\'t have PHP 5.'));
19 }
20
21 error_reporting(E_ALL);
22
23 function stripslashesDeep($value) {
24 return is_array($value)
25 ? array_map('stripslashesDeep', $value)
26 : stripslashes($value);
27 }
28
29 if (get_magic_quotes_gpc()) {
30 $_POST = array_map('stripslashesDeep', $_POST);
31 $_GET = array_map('stripslashesDeep', $_GET);
32 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
33 }
34
35 ob_start();
36 register_shutdown_function('ob_end_flush');
37 }
38
39 public static function getPocheUrl()
40 {
41 $https = (!empty($_SERVER['HTTPS'])
42 && (strtolower($_SERVER['HTTPS']) == 'on'))
43 || (isset($_SERVER["SERVER_PORT"])
125f9ee8 44 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
2916d24b
JCSD
45 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
46 && $_SERVER["SERVER_PORT"] == SSL_PORT);
125f9ee8 47
eb1af592
NL
48 $serverport = (!isset($_SERVER["SERVER_PORT"])
49 || $_SERVER["SERVER_PORT"] == '80'
50 || ($https && $_SERVER["SERVER_PORT"] == '443')
2916d24b 51 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection
eb1af592
NL
52 ? '' : ':' . $_SERVER["SERVER_PORT"]);
53
54 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
55
45e9e0f5 56 if (!isset($_SERVER["HTTP_HOST"])) {
eb1af592
NL
57 return $scriptname;
58 }
59
60 return 'http' . ($https ? 's' : '') . '://'
45e9e0f5 61 . $_SERVER["HTTP_HOST"] . $serverport . $scriptname;
eb1af592
NL
62 }
63
64 public static function redirect($url = '')
65 {
66 if ($url === '') {
67 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
68 if (isset($_POST['returnurl'])) {
69 $url = $_POST['returnurl'];
70 }
71 }
72
73 # prevent loop
74 if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
75 $url = Tools::getPocheUrl();
76 }
77
78 if (substr($url, 0, 1) !== '?') {
79 $ref = Tools::getPocheUrl();
80 if (substr($url, 0, strlen($ref)) !== $ref) {
81 $url = $ref;
82 }
83 }
bc1ee852 84 self::logm('redirect to ' . $url);
eb1af592
NL
85 header('Location: '.$url);
86 exit();
87 }
88
89 public static function getTplFile($view)
90 {
00dbaf90
NL
91 $default_tpl = 'home.twig';
92
93 switch ($view) {
eb1af592
NL
94 case 'install':
95 $tpl_file = 'install.twig';
96 break;
97 case 'import';
98 $tpl_file = 'import.twig';
99 break;
100 case 'export':
101 $tpl_file = 'export.twig';
102 break;
103 case 'config':
104 $tpl_file = 'config.twig';
105 break;
106 case 'view':
107 $tpl_file = 'view.twig';
108 break;
00dbaf90
NL
109
110 case 'login':
111 $tpl_file = 'login.twig';
112 break;
113
114 case 'error':
115 $tpl_file = 'error.twig';
116 break;
117
eb1af592 118 default:
00dbaf90
NL
119 $tpl_file = $default_tpl;
120 break;
eb1af592 121 }
00dbaf90 122
eb1af592
NL
123 return $tpl_file;
124 }
125
126 public static function getFile($url)
127 {
128 $timeout = 15;
129 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
130
131 if (in_array ('curl', get_loaded_extensions())) {
132 # Fetch feed from URL
133 $curl = curl_init();
134 curl_setopt($curl, CURLOPT_URL, $url);
135 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
f2d3ee98
NL
136 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
137 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
138 }
eb1af592
NL
139 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
140 curl_setopt($curl, CURLOPT_HEADER, false);
141
142 # for ssl, do not verified certificate
143 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
144 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
145
146 # FeedBurner requires a proper USER-AGENT...
147 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
148 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
149 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
150
151 $data = curl_exec($curl);
152 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
153 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
154 curl_close($curl);
155 } else {
156 # create http context and add timeout and user-agent
157 $context = stream_context_create(
158 array(
159 'http' => array(
160 'timeout' => $timeout,
161 'header' => "User-Agent: " . $useragent,
162 'follow_location' => true
163 ),
164 'ssl' => array(
165 'verify_peer' => false,
166 'allow_self_signed' => true
167 )
168 )
169 );
170
171 # only download page lesser than 4MB
172 $data = @file_get_contents($url, false, $context, -1, 4000000);
173
174 if (isset($http_response_header) and isset($http_response_header[0])) {
175 $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));
176 }
177 }
178
179 # if response is not empty and response is OK
180 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
181
182 # take charset of page and get it
183 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
184
185 # if meta tag is found
186 if (!empty($meta[0])) {
187 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
188 # if charset is found set it otherwise, set it to utf-8
189 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
5f9bff0f 190 if (empty($encoding[1])) $encoding[1] = 'utf-8';
eb1af592
NL
191 } else {
192 $html_charset = 'utf-8';
193 $encoding[1] = '';
194 }
195
196 # replace charset of url to charset of page
197 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
198
199 return $data;
200 }
201 else {
202 return FALSE;
203 }
204 }
205
206 public static function renderJson($data)
207 {
208 header('Cache-Control: no-cache, must-revalidate');
209 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
210 header('Content-type: application/json; charset=UTF-8');
211 echo json_encode($data);
212 exit();
213 }
214
215 public static function logm($message)
216 {
217 if (DEBUG_POCHE) {
218 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
6a361945 219 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
bc1ee852 220 error_log('DEBUG POCHE : ' . $message);
eb1af592
NL
221 }
222 }
223
224 public static function encodeString($string)
225 {
226 return sha1($string . SALT);
227 }
63c35580 228
7f959169 229 public static function checkVar($var, $default = '')
63c35580 230 {
7f959169 231 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
63c35580 232 }
55821e04
NL
233
234 public static function getDomain($url)
235 {
d7c2f0cc 236 return parse_url($url, PHP_URL_HOST);
55821e04 237 }
d9178758
NL
238
239 public static function getReadingTime($text) {
240 $word = str_word_count(strip_tags($text));
241 $minutes = floor($word / 200);
242 $seconds = floor($word % 200 / (200 / 60));
243 $time = array('minutes' => $minutes, 'seconds' => $seconds);
244
245 return $minutes;
246 }
bb5a7d9e 247
1b2abab6
N
248 public static function getDocLanguage($userlanguage) {
249 $lang = explode('.', $userlanguage);
250 return str_replace('_', '-', $lang[0]);
251 }
125f9ee8 252}