]>
Commit | Line | Data |
---|---|---|
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 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"]) | |
44 | && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection. | |
45 | $serverport = (!isset($_SERVER["SERVER_PORT"]) | |
46 | || $_SERVER["SERVER_PORT"] == '80' | |
47 | || ($https && $_SERVER["SERVER_PORT"] == '443') | |
48 | ? '' : ':' . $_SERVER["SERVER_PORT"]); | |
49 | ||
50 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); | |
51 | ||
52 | if (!isset($_SERVER["SERVER_NAME"])) { | |
53 | return $scriptname; | |
54 | } | |
55 | ||
56 | return 'http' . ($https ? 's' : '') . '://' | |
57 | . $_SERVER["SERVER_NAME"] . $serverport . $scriptname; | |
58 | } | |
59 | ||
60 | public static function redirect($url = '') | |
61 | { | |
62 | if ($url === '') { | |
63 | $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']); | |
64 | if (isset($_POST['returnurl'])) { | |
65 | $url = $_POST['returnurl']; | |
66 | } | |
67 | } | |
68 | ||
69 | # prevent loop | |
70 | if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) { | |
71 | $url = Tools::getPocheUrl(); | |
72 | } | |
73 | ||
74 | if (substr($url, 0, 1) !== '?') { | |
75 | $ref = Tools::getPocheUrl(); | |
76 | if (substr($url, 0, strlen($ref)) !== $ref) { | |
77 | $url = $ref; | |
78 | } | |
79 | } | |
80 | self::logm('redirect to ' . $url); | |
81 | header('Location: '.$url); | |
82 | exit(); | |
83 | } | |
84 | ||
85 | public static function getTplFile($view) | |
86 | { | |
87 | $default_tpl = 'home.twig'; | |
88 | ||
89 | switch ($view) { | |
90 | case 'install': | |
91 | $tpl_file = 'install.twig'; | |
92 | break; | |
93 | case 'import'; | |
94 | $tpl_file = 'import.twig'; | |
95 | break; | |
96 | case 'export': | |
97 | $tpl_file = 'export.twig'; | |
98 | break; | |
99 | case 'config': | |
100 | $tpl_file = 'config.twig'; | |
101 | break; | |
102 | case 'view': | |
103 | $tpl_file = 'view.twig'; | |
104 | break; | |
105 | ||
106 | case 'login': | |
107 | $tpl_file = 'login.twig'; | |
108 | break; | |
109 | ||
110 | case 'error': | |
111 | $tpl_file = 'error.twig'; | |
112 | break; | |
113 | ||
114 | default: | |
115 | $tpl_file = $default_tpl; | |
116 | break; | |
117 | } | |
118 | ||
119 | return $tpl_file; | |
120 | } | |
121 | ||
122 | public static function getFile($url) | |
123 | { | |
124 | $timeout = 15; | |
125 | $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | |
126 | ||
127 | if (in_array ('curl', get_loaded_extensions())) { | |
128 | # Fetch feed from URL | |
129 | $curl = curl_init(); | |
130 | curl_setopt($curl, CURLOPT_URL, $url); | |
131 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
132 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
133 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
134 | curl_setopt($curl, CURLOPT_HEADER, false); | |
135 | ||
136 | # for ssl, do not verified certificate | |
137 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | |
138 | curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE ); | |
139 | ||
140 | # FeedBurner requires a proper USER-AGENT... | |
141 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | |
142 | curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate"); | |
143 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
144 | ||
145 | $data = curl_exec($curl); | |
146 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
147 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
148 | curl_close($curl); | |
149 | } else { | |
150 | # create http context and add timeout and user-agent | |
151 | $context = stream_context_create( | |
152 | array( | |
153 | 'http' => array( | |
154 | 'timeout' => $timeout, | |
155 | 'header' => "User-Agent: " . $useragent, | |
156 | 'follow_location' => true | |
157 | ), | |
158 | 'ssl' => array( | |
159 | 'verify_peer' => false, | |
160 | 'allow_self_signed' => true | |
161 | ) | |
162 | ) | |
163 | ); | |
164 | ||
165 | # only download page lesser than 4MB | |
166 | $data = @file_get_contents($url, false, $context, -1, 4000000); | |
167 | ||
168 | if (isset($http_response_header) and isset($http_response_header[0])) { | |
169 | $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)); | |
170 | } | |
171 | } | |
172 | ||
173 | # if response is not empty and response is OK | |
174 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { | |
175 | ||
176 | # take charset of page and get it | |
177 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
178 | ||
179 | # if meta tag is found | |
180 | if (!empty($meta[0])) { | |
181 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); | |
182 | # if charset is found set it otherwise, set it to utf-8 | |
183 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; | |
184 | if (empty($encoding[1])) $encoding[1] = 'utf-8'; | |
185 | } else { | |
186 | $html_charset = 'utf-8'; | |
187 | $encoding[1] = ''; | |
188 | } | |
189 | ||
190 | # replace charset of url to charset of page | |
191 | $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data); | |
192 | ||
193 | return $data; | |
194 | } | |
195 | else { | |
196 | return FALSE; | |
197 | } | |
198 | } | |
199 | ||
200 | public static function renderJson($data) | |
201 | { | |
202 | header('Cache-Control: no-cache, must-revalidate'); | |
203 | header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | |
204 | header('Content-type: application/json; charset=UTF-8'); | |
205 | echo json_encode($data); | |
206 | exit(); | |
207 | } | |
208 | ||
209 | public static function logm($message) | |
210 | { | |
211 | if (DEBUG_POCHE) { | |
212 | $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; | |
213 | file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND); | |
214 | error_log('DEBUG POCHE : ' . $message); | |
215 | } | |
216 | } | |
217 | ||
218 | public static function encodeString($string) | |
219 | { | |
220 | return sha1($string . SALT); | |
221 | } | |
222 | ||
223 | public static function checkVar($var, $default = '') | |
224 | { | |
225 | return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default); | |
226 | } | |
227 | ||
228 | public static function getDomain($url) | |
229 | { | |
230 | return parse_url($url, PHP_URL_HOST); | |
231 | } | |
232 | ||
233 | public static function getReadingTime($text) { | |
234 | $word = str_word_count(strip_tags($text)); | |
235 | $minutes = floor($word / 200); | |
236 | $seconds = floor($word % 200 / (200 / 60)); | |
237 | $time = array('minutes' => $minutes, 'seconds' => $seconds); | |
238 | ||
239 | return $minutes; | |
240 | } | |
241 | ||
242 | public static function getDocLanguage($userlanguage) { | |
243 | $lang = explode('.', $userlanguage); | |
244 | return str_replace('_', '-', $lang[0]); | |
245 | } | |
246 | } |