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