]>
Commit | Line | Data |
---|---|---|
eb1af592 NL |
1 | <?php |
2 | /** | |
c95b78a8 | 3 | * wallabag, self hostable application allowing you to not miss any content anymore |
eb1af592 | 4 | * |
c95b78a8 NL |
5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
eb1af592 | 7 | * @copyright 2013 |
3602405e | 8 | * @license http://opensource.org/licenses/MIT see COPYING file |
eb1af592 | 9 | */ |
182faf26 | 10 | |
3602405e | 11 | final class Tools |
eb1af592 | 12 | { |
3602405e NL |
13 | /** |
14 | * Initialize PHP environment | |
15 | */ | |
eb1af592 NL |
16 | public static function initPhp() |
17 | { | |
18 | define('START_TIME', microtime(true)); | |
19 | ||
eb1af592 NL |
20 | function stripslashesDeep($value) { |
21 | return is_array($value) | |
22 | ? array_map('stripslashesDeep', $value) | |
23 | : stripslashes($value); | |
24 | } | |
25 | ||
26 | if (get_magic_quotes_gpc()) { | |
27 | $_POST = array_map('stripslashesDeep', $_POST); | |
28 | $_GET = array_map('stripslashesDeep', $_GET); | |
29 | $_COOKIE = array_map('stripslashesDeep', $_COOKIE); | |
30 | } | |
31 | ||
32 | ob_start(); | |
33 | register_shutdown_function('ob_end_flush'); | |
34 | } | |
35 | ||
3602405e NL |
36 | /** |
37 | * Get wallabag instance URL | |
38 | * | |
39 | * @return string | |
40 | */ | |
eb1af592 NL |
41 | public static function getPocheUrl() |
42 | { | |
43 | $https = (!empty($_SERVER['HTTPS']) | |
44 | && (strtolower($_SERVER['HTTPS']) == 'on')) | |
45 | || (isset($_SERVER["SERVER_PORT"]) | |
125f9ee8 | 46 | && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection. |
182faf26 | 47 | || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection |
445a1a1c NL |
48 | && $_SERVER["SERVER_PORT"] == SSL_PORT) |
49 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) | |
50 | && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'); | |
125f9ee8 | 51 | |
eb1af592 NL |
52 | $serverport = (!isset($_SERVER["SERVER_PORT"]) |
53 | || $_SERVER["SERVER_PORT"] == '80' | |
54 | || ($https && $_SERVER["SERVER_PORT"] == '443') | |
2916d24b | 55 | || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection |
eb1af592 | 56 | ? '' : ':' . $_SERVER["SERVER_PORT"]); |
5af2555f AK |
57 | |
58 | if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) { | |
59 | $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"]; | |
60 | } | |
eb1af592 NL |
61 | |
62 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); | |
63 | ||
45e9e0f5 | 64 | if (!isset($_SERVER["HTTP_HOST"])) { |
eb1af592 NL |
65 | return $scriptname; |
66 | } | |
67 | ||
69c57493 | 68 | $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])); |
79024eb0 NL |
69 | |
70 | if (strpos($host, ':') !== false) { | |
71 | $serverport = ''; | |
72 | } | |
752cd4a8 | 73 | |
eb1af592 | 74 | return 'http' . ($https ? 's' : '') . '://' |
69c57493 | 75 | . $host . $serverport . $scriptname; |
eb1af592 NL |
76 | } |
77 | ||
3602405e NL |
78 | /** |
79 | * Redirects to a URL | |
80 | * | |
81 | * @param string $url | |
82 | */ | |
eb1af592 NL |
83 | public static function redirect($url = '') |
84 | { | |
85 | if ($url === '') { | |
86 | $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']); | |
87 | if (isset($_POST['returnurl'])) { | |
88 | $url = $_POST['returnurl']; | |
89 | } | |
90 | } | |
91 | ||
92 | # prevent loop | |
93 | if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) { | |
94 | $url = Tools::getPocheUrl(); | |
95 | } | |
96 | ||
97 | if (substr($url, 0, 1) !== '?') { | |
98 | $ref = Tools::getPocheUrl(); | |
99 | if (substr($url, 0, strlen($ref)) !== $ref) { | |
100 | $url = $ref; | |
101 | } | |
102 | } | |
3602405e | 103 | |
bc1ee852 | 104 | self::logm('redirect to ' . $url); |
eb1af592 NL |
105 | header('Location: '.$url); |
106 | exit(); | |
107 | } | |
108 | ||
3602405e NL |
109 | /** |
110 | * Returns name of the template file to display | |
111 | * | |
112 | * @param $view | |
113 | * @return string | |
114 | */ | |
eb1af592 NL |
115 | public static function getTplFile($view) |
116 | { | |
74ec445a NL |
117 | $views = array( |
118 | 'install', 'import', 'export', 'config', 'tags', | |
032e0ca1 | 119 | 'edit-tags', 'view', 'login', 'error' |
74ec445a NL |
120 | ); |
121 | ||
3602405e | 122 | return (in_array($view, $views) ? $view . '.twig' : 'home.twig'); |
eb1af592 NL |
123 | } |
124 | ||
3602405e NL |
125 | /** |
126 | * Download a file (typically, for downloading pictures on web server) | |
127 | * | |
128 | * @param $url | |
129 | * @return bool|mixed|string | |
130 | */ | |
eb1af592 NL |
131 | public static function getFile($url) |
132 | { | |
133 | $timeout = 15; | |
134 | $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | |
135 | ||
136 | if (in_array ('curl', get_loaded_extensions())) { | |
137 | # Fetch feed from URL | |
138 | $curl = curl_init(); | |
139 | curl_setopt($curl, CURLOPT_URL, $url); | |
140 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
f2d3ee98 NL |
141 | if (!ini_get('open_basedir') && !ini_get('safe_mode')) { |
142 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
143 | } | |
eb1af592 NL |
144 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
145 | curl_setopt($curl, CURLOPT_HEADER, false); | |
146 | ||
147 | # for ssl, do not verified certificate | |
148 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
149 | curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE ); | |
150 | ||
151 | # FeedBurner requires a proper USER-AGENT... | |
152 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | |
153 | curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate"); | |
154 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
155 | ||
156 | $data = curl_exec($curl); | |
157 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
158 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
159 | curl_close($curl); | |
160 | } else { | |
161 | # create http context and add timeout and user-agent | |
162 | $context = stream_context_create( | |
163 | array( | |
164 | 'http' => array( | |
165 | 'timeout' => $timeout, | |
166 | 'header' => "User-Agent: " . $useragent, | |
167 | 'follow_location' => true | |
168 | ), | |
169 | 'ssl' => array( | |
170 | 'verify_peer' => false, | |
171 | 'allow_self_signed' => true | |
172 | ) | |
173 | ) | |
174 | ); | |
175 | ||
176 | # only download page lesser than 4MB | |
182faf26 | 177 | $data = @file_get_contents($url, false, $context, -1, 4000000); |
eb1af592 NL |
178 | |
179 | if (isset($http_response_header) and isset($http_response_header[0])) { | |
180 | $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)); | |
181 | } | |
182 | } | |
183 | ||
184 | # if response is not empty and response is OK | |
185 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { | |
186 | ||
187 | # take charset of page and get it | |
188 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
189 | ||
190 | # if meta tag is found | |
191 | if (!empty($meta[0])) { | |
192 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); | |
193 | # if charset is found set it otherwise, set it to utf-8 | |
194 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; | |
5f9bff0f | 195 | if (empty($encoding[1])) $encoding[1] = 'utf-8'; |
eb1af592 NL |
196 | } else { |
197 | $html_charset = 'utf-8'; | |
198 | $encoding[1] = ''; | |
199 | } | |
200 | ||
201 | # replace charset of url to charset of page | |
202 | $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data); | |
203 | ||
204 | return $data; | |
205 | } | |
206 | else { | |
207 | return FALSE; | |
208 | } | |
209 | } | |
210 | ||
3602405e NL |
211 | /** |
212 | * Headers for JSON export | |
213 | * | |
214 | * @param $data | |
215 | */ | |
eb1af592 NL |
216 | public static function renderJson($data) |
217 | { | |
218 | header('Cache-Control: no-cache, must-revalidate'); | |
219 | header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | |
220 | header('Content-type: application/json; charset=UTF-8'); | |
221 | echo json_encode($data); | |
222 | exit(); | |
223 | } | |
224 | ||
3602405e NL |
225 | /** |
226 | * Create new line in log file | |
227 | * | |
228 | * @param $message | |
229 | */ | |
eb1af592 NL |
230 | public static function logm($message) |
231 | { | |
53e3158d | 232 | if (DEBUG_POCHE && php_sapi_name() != 'cli') { |
eb1af592 | 233 | $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; |
6a361945 | 234 | file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND); |
bc1ee852 | 235 | error_log('DEBUG POCHE : ' . $message); |
eb1af592 NL |
236 | } |
237 | } | |
238 | ||
3602405e NL |
239 | /** |
240 | * Encode a URL by using a salt | |
241 | * | |
242 | * @param $string | |
243 | * @return string | |
244 | */ | |
182faf26 | 245 | public static function encodeString($string) |
eb1af592 NL |
246 | { |
247 | return sha1($string . SALT); | |
248 | } | |
63c35580 | 249 | |
3602405e NL |
250 | /** |
251 | * Cleans a variable | |
252 | * | |
253 | * @param $var | |
254 | * @param string $default | |
255 | * @return string | |
256 | */ | |
7f959169 | 257 | public static function checkVar($var, $default = '') |
63c35580 | 258 | { |
3602405e | 259 | return ((isset($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default); |
63c35580 | 260 | } |
55821e04 | 261 | |
3602405e NL |
262 | /** |
263 | * Returns the domain name for a URL | |
264 | * | |
265 | * @param $url | |
266 | * @return string | |
267 | */ | |
6400371f NL |
268 | public static function getDomain($url) |
269 | { | |
270 | return parse_url($url, PHP_URL_HOST); | |
271 | } | |
272 | ||
3602405e NL |
273 | /** |
274 | * For a given text, we calculate reading time for an article | |
275 | * | |
276 | * @param $text | |
277 | * @return float | |
278 | */ | |
279 | public static function getReadingTime($text) | |
280 | { | |
281 | return floor(str_word_count(strip_tags($text)) / 200); | |
1b2abab6 | 282 | } |
d460914f | 283 | |
3602405e NL |
284 | /** |
285 | * Returns the correct header for a status code | |
286 | * | |
287 | * @param $status_code | |
288 | */ | |
289 | private static function _status($status_code) | |
d460914f NL |
290 | { |
291 | if (strpos(php_sapi_name(), 'apache') !== false) { | |
292 | ||
293 | header('HTTP/1.0 '.$status_code); | |
294 | } | |
295 | else { | |
296 | ||
297 | header('Status: '.$status_code); | |
298 | } | |
299 | } | |
300 | ||
3602405e NL |
301 | /** |
302 | * Get the content for a given URL (by a call to FullTextFeed) | |
303 | * | |
304 | * @param Url $url | |
305 | * @return mixed | |
306 | */ | |
53e3158d NL |
307 | public static function getPageContent(Url $url) |
308 | { | |
309 | // Saving and clearing context | |
310 | $REAL = array(); | |
311 | foreach( $GLOBALS as $key => $value ) { | |
312 | if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) { | |
3602405e NL |
313 | $GLOBALS[$key] = array(); |
314 | $REAL[$key] = $value; | |
53e3158d NL |
315 | } |
316 | } | |
317 | // Saving and clearing session | |
3602405e | 318 | if (isset($_SESSION)) { |
f98373cc MR |
319 | $REAL_SESSION = array(); |
320 | foreach( $_SESSION as $key => $value ) { | |
321 | $REAL_SESSION[$key] = $value; | |
322 | unset($_SESSION[$key]); | |
323 | } | |
53e3158d NL |
324 | } |
325 | ||
326 | // Running code in different context | |
327 | $scope = function() { | |
328 | extract( func_get_arg(1) ); | |
329 | $_GET = $_REQUEST = array( | |
3602405e NL |
330 | "url" => $url->getUrl(), |
331 | "max" => 5, | |
332 | "links" => "preserve", | |
333 | "exc" => "", | |
334 | "format" => "json", | |
335 | "submit" => "Create Feed" | |
53e3158d NL |
336 | ); |
337 | ob_start(); | |
338 | require func_get_arg(0); | |
f98373cc MR |
339 | $json = ob_get_contents(); |
340 | ob_end_clean(); | |
53e3158d NL |
341 | return $json; |
342 | }; | |
3602405e NL |
343 | |
344 | $json = $scope("inc/3rdparty/makefulltextfeed.php", array("url" => $url)); | |
53e3158d NL |
345 | |
346 | // Clearing and restoring context | |
3602405e NL |
347 | foreach ($GLOBALS as $key => $value) { |
348 | if($key != "GLOBALS" && $key != "_SESSION" ) { | |
53e3158d NL |
349 | unset($GLOBALS[$key]); |
350 | } | |
351 | } | |
3602405e | 352 | foreach ($REAL as $key => $value) { |
53e3158d NL |
353 | $GLOBALS[$key] = $value; |
354 | } | |
3602405e | 355 | |
53e3158d | 356 | // Clearing and restoring session |
3602405e NL |
357 | if (isset($REAL_SESSION)) { |
358 | foreach($_SESSION as $key => $value) { | |
f98373cc MR |
359 | unset($_SESSION[$key]); |
360 | } | |
3602405e NL |
361 | |
362 | foreach($REAL_SESSION as $key => $value) { | |
f98373cc MR |
363 | $_SESSION[$key] = $value; |
364 | } | |
53e3158d | 365 | } |
f98373cc | 366 | |
53e3158d NL |
367 | return json_decode($json, true); |
368 | } | |
fb26cc93 MR |
369 | |
370 | /** | |
371 | * Returns whether we handle an AJAX (XMLHttpRequest) request. | |
3602405e | 372 | * |
fb26cc93 MR |
373 | * @return boolean whether we handle an AJAX (XMLHttpRequest) request. |
374 | */ | |
375 | public static function isAjaxRequest() | |
376 | { | |
3602405e | 377 | return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest'; |
fb26cc93 MR |
378 | } |
379 | ||
2f26729c NL |
380 | /* |
381 | * Empty cache folder | |
382 | */ | |
383 | public static function emptyCache() | |
384 | { | |
385 | $files = new RecursiveIteratorIterator( | |
386 | new RecursiveDirectoryIterator(CACHE, RecursiveDirectoryIterator::SKIP_DOTS), | |
387 | RecursiveIteratorIterator::CHILD_FIRST | |
388 | ); | |
389 | ||
390 | foreach ($files as $fileInfo) { | |
391 | $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink'); | |
392 | $todo($fileInfo->getRealPath()); | |
393 | } | |
394 | ||
395 | Tools::logm('empty cache'); | |
396 | Tools::redirect(); | |
397 | } | |
398 | ||
399 | public static function generateToken() | |
400 | { | |
401 | if (ini_get('open_basedir') === '') { | |
402 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | |
403 | // alternative to /dev/urandom for Windows | |
404 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
405 | } else { | |
406 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
407 | } | |
408 | } | |
409 | else { | |
410 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
411 | } | |
412 | ||
413 | return str_replace('+', '', $token); | |
414 | } | |
415 | ||
125f9ee8 | 416 | } |