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