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