diff options
author | Nicolas Lœuillet <nicolas.loeuillet@smile.fr> | 2014-10-10 13:33:54 +0200 |
---|---|---|
committer | Nicolas Lœuillet <nicolas.loeuillet@smile.fr> | 2014-10-10 13:33:54 +0200 |
commit | 44d35257e805856b4913c63fcbed3c0acb64bae8 (patch) | |
tree | 11e9d276c34b1b287706cb61182bdc71729661e2 /inc/poche/Tools.class.php | |
parent | af8292c1de1886cd975d79f0f42df40e0bd1c5bd (diff) | |
parent | cf8a5e1eedbed484dbcb1ddc9f7a13fc19b7a27b (diff) | |
download | wallabag-44d35257e805856b4913c63fcbed3c0acb64bae8.tar.gz wallabag-44d35257e805856b4913c63fcbed3c0acb64bae8.tar.zst wallabag-44d35257e805856b4913c63fcbed3c0acb64bae8.zip |
Merge branch 'dev'1.8.0
Diffstat (limited to 'inc/poche/Tools.class.php')
-rwxr-xr-x | inc/poche/Tools.class.php | 194 |
1 files changed, 141 insertions, 53 deletions
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php index cc01f403..93ec3fc6 100755 --- a/inc/poche/Tools.class.php +++ b/inc/poche/Tools.class.php | |||
@@ -5,19 +5,18 @@ | |||
5 | * @category wallabag | 5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | 6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> |
7 | * @copyright 2013 | 7 | * @copyright 2013 |
8 | * @license http://www.wtfpl.net/ see COPYING file | 8 | * @license http://opensource.org/licenses/MIT see COPYING file |
9 | */ | 9 | */ |
10 | 10 | ||
11 | class Tools | 11 | final class Tools |
12 | { | 12 | { |
13 | /** | ||
14 | * Initialize PHP environment | ||
15 | */ | ||
13 | public static function initPhp() | 16 | public static function initPhp() |
14 | { | 17 | { |
15 | define('START_TIME', microtime(true)); | 18 | define('START_TIME', microtime(true)); |
16 | 19 | ||
17 | if (phpversion() < 5) { | ||
18 | die(_('Oops, it seems you don\'t have PHP 5.')); | ||
19 | } | ||
20 | |||
21 | function stripslashesDeep($value) { | 20 | function stripslashesDeep($value) { |
22 | return is_array($value) | 21 | return is_array($value) |
23 | ? array_map('stripslashesDeep', $value) | 22 | ? array_map('stripslashesDeep', $value) |
@@ -34,6 +33,11 @@ class Tools | |||
34 | register_shutdown_function('ob_end_flush'); | 33 | register_shutdown_function('ob_end_flush'); |
35 | } | 34 | } |
36 | 35 | ||
36 | /** | ||
37 | * Get wallabag instance URL | ||
38 | * | ||
39 | * @return string | ||
40 | */ | ||
37 | public static function getPocheUrl() | 41 | public static function getPocheUrl() |
38 | { | 42 | { |
39 | $https = (!empty($_SERVER['HTTPS']) | 43 | $https = (!empty($_SERVER['HTTPS']) |
@@ -50,6 +54,10 @@ class Tools | |||
50 | || ($https && $_SERVER["SERVER_PORT"] == '443') | 54 | || ($https && $_SERVER["SERVER_PORT"] == '443') |
51 | || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection | 55 | || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection |
52 | ? '' : ':' . $_SERVER["SERVER_PORT"]); | 56 | ? '' : ':' . $_SERVER["SERVER_PORT"]); |
57 | |||
58 | if (isset($_SERVER["HTTP_X_FORWARDED_PORT"])) { | ||
59 | $serverport = ':' . $_SERVER["HTTP_X_FORWARDED_PORT"]; | ||
60 | } | ||
53 | 61 | ||
54 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); | 62 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); |
55 | 63 | ||
@@ -67,6 +75,11 @@ class Tools | |||
67 | . $host . $serverport . $scriptname; | 75 | . $host . $serverport . $scriptname; |
68 | } | 76 | } |
69 | 77 | ||
78 | /** | ||
79 | * Redirects to a URL | ||
80 | * | ||
81 | * @param string $url | ||
82 | */ | ||
70 | public static function redirect($url = '') | 83 | public static function redirect($url = '') |
71 | { | 84 | { |
72 | if ($url === '') { | 85 | if ($url === '') { |
@@ -87,11 +100,18 @@ class Tools | |||
87 | $url = $ref; | 100 | $url = $ref; |
88 | } | 101 | } |
89 | } | 102 | } |
103 | |||
90 | self::logm('redirect to ' . $url); | 104 | self::logm('redirect to ' . $url); |
91 | header('Location: '.$url); | 105 | header('Location: '.$url); |
92 | exit(); | 106 | exit(); |
93 | } | 107 | } |
94 | 108 | ||
109 | /** | ||
110 | * Returns name of the template file to display | ||
111 | * | ||
112 | * @param $view | ||
113 | * @return string | ||
114 | */ | ||
95 | public static function getTplFile($view) | 115 | public static function getTplFile($view) |
96 | { | 116 | { |
97 | $views = array( | 117 | $views = array( |
@@ -99,13 +119,15 @@ class Tools | |||
99 | 'edit-tags', 'view', 'login', 'error' | 119 | 'edit-tags', 'view', 'login', 'error' |
100 | ); | 120 | ); |
101 | 121 | ||
102 | if (in_array($view, $views)) { | 122 | return (in_array($view, $views) ? $view . '.twig' : 'home.twig'); |
103 | return $view . '.twig'; | ||
104 | } | ||
105 | |||
106 | return 'home.twig'; | ||
107 | } | 123 | } |
108 | 124 | ||
125 | /** | ||
126 | * Download a file (typically, for downloading pictures on web server) | ||
127 | * | ||
128 | * @param $url | ||
129 | * @return bool|mixed|string | ||
130 | */ | ||
109 | public static function getFile($url) | 131 | public static function getFile($url) |
110 | { | 132 | { |
111 | $timeout = 15; | 133 | $timeout = 15; |
@@ -186,6 +208,11 @@ class Tools | |||
186 | } | 208 | } |
187 | } | 209 | } |
188 | 210 | ||
211 | /** | ||
212 | * Headers for JSON export | ||
213 | * | ||
214 | * @param $data | ||
215 | */ | ||
189 | public static function renderJson($data) | 216 | public static function renderJson($data) |
190 | { | 217 | { |
191 | header('Cache-Control: no-cache, must-revalidate'); | 218 | header('Cache-Control: no-cache, must-revalidate'); |
@@ -195,6 +222,11 @@ class Tools | |||
195 | exit(); | 222 | exit(); |
196 | } | 223 | } |
197 | 224 | ||
225 | /** | ||
226 | * Create new line in log file | ||
227 | * | ||
228 | * @param $message | ||
229 | */ | ||
198 | public static function logm($message) | 230 | public static function logm($message) |
199 | { | 231 | { |
200 | if (DEBUG_POCHE && php_sapi_name() != 'cli') { | 232 | if (DEBUG_POCHE && php_sapi_name() != 'cli') { |
@@ -204,36 +236,57 @@ class Tools | |||
204 | } | 236 | } |
205 | } | 237 | } |
206 | 238 | ||
239 | /** | ||
240 | * Encode a URL by using a salt | ||
241 | * | ||
242 | * @param $string | ||
243 | * @return string | ||
244 | */ | ||
207 | public static function encodeString($string) | 245 | public static function encodeString($string) |
208 | { | 246 | { |
209 | return sha1($string . SALT); | 247 | return sha1($string . SALT); |
210 | } | 248 | } |
211 | 249 | ||
250 | /** | ||
251 | * Cleans a variable | ||
252 | * | ||
253 | * @param $var | ||
254 | * @param string $default | ||
255 | * @return string | ||
256 | */ | ||
212 | public static function checkVar($var, $default = '') | 257 | public static function checkVar($var, $default = '') |
213 | { | 258 | { |
214 | return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default); | 259 | return ((isset($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default); |
215 | } | 260 | } |
216 | 261 | ||
262 | /** | ||
263 | * Returns the domain name for a URL | ||
264 | * | ||
265 | * @param $url | ||
266 | * @return string | ||
267 | */ | ||
217 | public static function getDomain($url) | 268 | public static function getDomain($url) |
218 | { | 269 | { |
219 | return parse_url($url, PHP_URL_HOST); | 270 | return parse_url($url, PHP_URL_HOST); |
220 | } | 271 | } |
221 | 272 | ||
222 | public static function getReadingTime($text) { | 273 | /** |
223 | $word = str_word_count(strip_tags($text)); | 274 | * For a given text, we calculate reading time for an article |
224 | $minutes = floor($word / 200); | 275 | * |
225 | $seconds = floor($word % 200 / (200 / 60)); | 276 | * @param $text |
226 | $time = array('minutes' => $minutes, 'seconds' => $seconds); | 277 | * @return float |
227 | 278 | */ | |
228 | return $minutes; | 279 | public static function getReadingTime($text) |
229 | } | 280 | { |
230 | 281 | return floor(str_word_count(strip_tags($text)) / 200); | |
231 | public static function getDocLanguage($userlanguage) { | ||
232 | $lang = explode('.', $userlanguage); | ||
233 | return str_replace('_', '-', $lang[0]); | ||
234 | } | 282 | } |
235 | 283 | ||
236 | public static function status($status_code) | 284 | /** |
285 | * Returns the correct header for a status code | ||
286 | * | ||
287 | * @param $status_code | ||
288 | */ | ||
289 | private static function _status($status_code) | ||
237 | { | 290 | { |
238 | if (strpos(php_sapi_name(), 'apache') !== false) { | 291 | if (strpos(php_sapi_name(), 'apache') !== false) { |
239 | 292 | ||
@@ -245,29 +298,24 @@ class Tools | |||
245 | } | 298 | } |
246 | } | 299 | } |
247 | 300 | ||
248 | public static function download_db() { | 301 | /** |
249 | header('Content-Disposition: attachment; filename="poche.sqlite.gz"'); | 302 | * Get the content for a given URL (by a call to FullTextFeed) |
250 | self::status(200); | 303 | * |
251 | 304 | * @param Url $url | |
252 | header('Content-Transfer-Encoding: binary'); | 305 | * @return mixed |
253 | header('Content-Type: application/octet-stream'); | 306 | */ |
254 | echo gzencode(file_get_contents(STORAGE_SQLITE)); | ||
255 | |||
256 | exit; | ||
257 | } | ||
258 | |||
259 | public static function getPageContent(Url $url) | 307 | public static function getPageContent(Url $url) |
260 | { | 308 | { |
261 | // Saving and clearing context | 309 | // Saving and clearing context |
262 | $REAL = array(); | 310 | $REAL = array(); |
263 | foreach( $GLOBALS as $key => $value ) { | 311 | foreach( $GLOBALS as $key => $value ) { |
264 | if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) { | 312 | if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) { |
265 | $GLOBALS[$key] = array(); | 313 | $GLOBALS[$key] = array(); |
266 | $REAL[$key] = $value; | 314 | $REAL[$key] = $value; |
267 | } | 315 | } |
268 | } | 316 | } |
269 | // Saving and clearing session | 317 | // Saving and clearing session |
270 | if ( isset($_SESSION) ) { | 318 | if (isset($_SESSION)) { |
271 | $REAL_SESSION = array(); | 319 | $REAL_SESSION = array(); |
272 | foreach( $_SESSION as $key => $value ) { | 320 | foreach( $_SESSION as $key => $value ) { |
273 | $REAL_SESSION[$key] = $value; | 321 | $REAL_SESSION[$key] = $value; |
@@ -279,12 +327,12 @@ class Tools | |||
279 | $scope = function() { | 327 | $scope = function() { |
280 | extract( func_get_arg(1) ); | 328 | extract( func_get_arg(1) ); |
281 | $_GET = $_REQUEST = array( | 329 | $_GET = $_REQUEST = array( |
282 | "url" => $url->getUrl(), | 330 | "url" => $url->getUrl(), |
283 | "max" => 5, | 331 | "max" => 5, |
284 | "links" => "preserve", | 332 | "links" => "preserve", |
285 | "exc" => "", | 333 | "exc" => "", |
286 | "format" => "json", | 334 | "format" => "json", |
287 | "submit" => "Create Feed" | 335 | "submit" => "Create Feed" |
288 | ); | 336 | ); |
289 | ob_start(); | 337 | ob_start(); |
290 | require func_get_arg(0); | 338 | require func_get_arg(0); |
@@ -292,23 +340,26 @@ class Tools | |||
292 | ob_end_clean(); | 340 | ob_end_clean(); |
293 | return $json; | 341 | return $json; |
294 | }; | 342 | }; |
295 | $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) ); | 343 | |
344 | $json = $scope("inc/3rdparty/makefulltextfeed.php", array("url" => $url)); | ||
296 | 345 | ||
297 | // Clearing and restoring context | 346 | // Clearing and restoring context |
298 | foreach( $GLOBALS as $key => $value ) { | 347 | foreach ($GLOBALS as $key => $value) { |
299 | if( $key != "GLOBALS" && $key != "_SESSION" ) { | 348 | if($key != "GLOBALS" && $key != "_SESSION" ) { |
300 | unset($GLOBALS[$key]); | 349 | unset($GLOBALS[$key]); |
301 | } | 350 | } |
302 | } | 351 | } |
303 | foreach( $REAL as $key => $value ) { | 352 | foreach ($REAL as $key => $value) { |
304 | $GLOBALS[$key] = $value; | 353 | $GLOBALS[$key] = $value; |
305 | } | 354 | } |
355 | |||
306 | // Clearing and restoring session | 356 | // Clearing and restoring session |
307 | if ( isset($REAL_SESSION) ) { | 357 | if (isset($REAL_SESSION)) { |
308 | foreach( $_SESSION as $key => $value ) { | 358 | foreach($_SESSION as $key => $value) { |
309 | unset($_SESSION[$key]); | 359 | unset($_SESSION[$key]); |
310 | } | 360 | } |
311 | foreach( $REAL_SESSION as $key => $value ) { | 361 | |
362 | foreach($REAL_SESSION as $key => $value) { | ||
312 | $_SESSION[$key] = $value; | 363 | $_SESSION[$key] = $value; |
313 | } | 364 | } |
314 | } | 365 | } |
@@ -318,11 +369,48 @@ class Tools | |||
318 | 369 | ||
319 | /** | 370 | /** |
320 | * Returns whether we handle an AJAX (XMLHttpRequest) request. | 371 | * Returns whether we handle an AJAX (XMLHttpRequest) request. |
372 | * | ||
321 | * @return boolean whether we handle an AJAX (XMLHttpRequest) request. | 373 | * @return boolean whether we handle an AJAX (XMLHttpRequest) request. |
322 | */ | 374 | */ |
323 | public static function isAjaxRequest() | 375 | public static function isAjaxRequest() |
324 | { | 376 | { |
325 | return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest'; | 377 | return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest'; |
378 | } | ||
379 | |||
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); | ||
326 | } | 414 | } |
327 | 415 | ||
328 | } | 416 | } |