]>
Commit | Line | Data |
---|---|---|
f6c9baab 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 | ||
11 | class pocheTools | |
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 isUrl($url) | |
40 | { | |
41 | // http://neo22s.com/check-if-url-exists-and-is-online-php/ | |
42 | $pattern='|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i'; | |
43 | ||
44 | return preg_match($pattern, $url); | |
45 | } | |
46 | ||
47 | public static function getUrl() | |
48 | { | |
49 | $https = (!empty($_SERVER['HTTPS']) | |
50 | && (strtolower($_SERVER['HTTPS']) == 'on')) | |
51 | || (isset($_SERVER["SERVER_PORT"]) | |
52 | && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection. | |
53 | $serverport = (!isset($_SERVER["SERVER_PORT"]) | |
54 | || $_SERVER["SERVER_PORT"] == '80' | |
55 | || ($https && $_SERVER["SERVER_PORT"] == '443') | |
56 | ? '' : ':' . $_SERVER["SERVER_PORT"]); | |
57 | ||
58 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); | |
59 | ||
60 | if (!isset($_SERVER["SERVER_NAME"])) { | |
61 | return $scriptname; | |
62 | } | |
63 | ||
64 | return 'http' . ($https ? 's' : '') . '://' | |
65 | . $_SERVER["SERVER_NAME"] . $serverport . $scriptname; | |
66 | } | |
67 | ||
68 | public static function renderJson($data) | |
69 | { | |
70 | header('Cache-Control: no-cache, must-revalidate'); | |
71 | header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | |
72 | header('Content-type: application/json; charset=UTF-8'); | |
73 | ||
74 | echo json_encode($data); | |
75 | exit(); | |
76 | } | |
77 | ||
78 | public static function redirect($rurl = '') | |
79 | { | |
80 | if ($rurl === '') { | |
81 | $rurl = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']); | |
82 | if (isset($_POST['returnurl'])) { | |
83 | $rurl = $_POST['returnurl']; | |
84 | } | |
85 | } | |
86 | ||
87 | // prevent loop | |
88 | if (empty($rurl) || parse_url($rurl, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) { | |
89 | $rurl = pocheTool::getUrl(); | |
90 | } | |
91 | ||
92 | if (substr($rurl, 0, 1) !== '?') { | |
93 | $ref = pocheTool::getUrl(); | |
94 | if (substr($rurl, 0, strlen($ref)) !== $ref) { | |
95 | $rurl = $ref; | |
96 | } | |
97 | } | |
98 | header('Location: '.$rurl); | |
99 | exit(); | |
100 | } | |
101 | } |