aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Tools.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/poche/Tools.class.php')
-rwxr-xr-x[-rw-r--r--]inc/poche/Tools.class.php93
1 files changed, 83 insertions, 10 deletions
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php
index 4ed28ed1..cc01f403 100644..100755
--- a/inc/poche/Tools.class.php
+++ b/inc/poche/Tools.class.php
@@ -7,7 +7,7 @@
7 * @copyright 2013 7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file 8 * @license http://www.wtfpl.net/ see COPYING file
9 */ 9 */
10 10
11class Tools 11class Tools
12{ 12{
13 public static function initPhp() 13 public static function initPhp()
@@ -18,8 +18,6 @@ class Tools
18 die(_('Oops, it seems you don\'t have PHP 5.')); 18 die(_('Oops, it seems you don\'t have PHP 5.'));
19 } 19 }
20 20
21 error_reporting(E_ALL);
22
23 function stripslashesDeep($value) { 21 function stripslashesDeep($value) {
24 return is_array($value) 22 return is_array($value)
25 ? array_map('stripslashesDeep', $value) 23 ? array_map('stripslashesDeep', $value)
@@ -42,7 +40,7 @@ class Tools
42 && (strtolower($_SERVER['HTTPS']) == 'on')) 40 && (strtolower($_SERVER['HTTPS']) == 'on'))
43 || (isset($_SERVER["SERVER_PORT"]) 41 || (isset($_SERVER["SERVER_PORT"])
44 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection. 42 && $_SERVER["SERVER_PORT"] == '443') // HTTPS detection.
45 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection 43 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
46 && $_SERVER["SERVER_PORT"] == SSL_PORT) 44 && $_SERVER["SERVER_PORT"] == SSL_PORT)
47 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) 45 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
48 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'); 46 && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
@@ -59,8 +57,14 @@ class Tools
59 return $scriptname; 57 return $scriptname;
60 } 58 }
61 59
60 $host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']));
61
62 if (strpos($host, ':') !== false) {
63 $serverport = '';
64 }
65
62 return 'http' . ($https ? 's' : '') . '://' 66 return 'http' . ($https ? 's' : '') . '://'
63 . $_SERVER["HTTP_HOST"] . $serverport . $scriptname; 67 . $host . $serverport . $scriptname;
64 } 68 }
65 69
66 public static function redirect($url = '') 70 public static function redirect($url = '')
@@ -148,7 +152,7 @@ class Tools
148 ); 152 );
149 153
150 # only download page lesser than 4MB 154 # only download page lesser than 4MB
151 $data = @file_get_contents($url, false, $context, -1, 4000000); 155 $data = @file_get_contents($url, false, $context, -1, 4000000);
152 156
153 if (isset($http_response_header) and isset($http_response_header[0])) { 157 if (isset($http_response_header) and isset($http_response_header[0])) {
154 $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)); 158 $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));
@@ -193,14 +197,14 @@ class Tools
193 197
194 public static function logm($message) 198 public static function logm($message)
195 { 199 {
196 if (DEBUG_POCHE) { 200 if (DEBUG_POCHE && php_sapi_name() != 'cli') {
197 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; 201 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
198 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND); 202 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
199 error_log('DEBUG POCHE : ' . $message); 203 error_log('DEBUG POCHE : ' . $message);
200 } 204 }
201 } 205 }
202 206
203 public static function encodeString($string) 207 public static function encodeString($string)
204 { 208 {
205 return sha1($string . SALT); 209 return sha1($string . SALT);
206 } 210 }
@@ -212,7 +216,7 @@ class Tools
212 216
213 public static function getDomain($url) 217 public static function getDomain($url)
214 { 218 {
215 return parse_url($url, PHP_URL_HOST); 219 return parse_url($url, PHP_URL_HOST);
216 } 220 }
217 221
218 public static function getReadingTime($text) { 222 public static function getReadingTime($text) {
@@ -241,7 +245,6 @@ class Tools
241 } 245 }
242 } 246 }
243 247
244
245 public static function download_db() { 248 public static function download_db() {
246 header('Content-Disposition: attachment; filename="poche.sqlite.gz"'); 249 header('Content-Disposition: attachment; filename="poche.sqlite.gz"');
247 self::status(200); 250 self::status(200);
@@ -252,4 +255,74 @@ class Tools
252 255
253 exit; 256 exit;
254 } 257 }
258
259 public static function getPageContent(Url $url)
260 {
261 // Saving and clearing context
262 $REAL = array();
263 foreach( $GLOBALS as $key => $value ) {
264 if( $key != 'GLOBALS' && $key != '_SESSION' && $key != 'HTTP_SESSION_VARS' ) {
265 $GLOBALS[$key] = array();
266 $REAL[$key] = $value;
267 }
268 }
269 // Saving and clearing session
270 if ( isset($_SESSION) ) {
271 $REAL_SESSION = array();
272 foreach( $_SESSION as $key => $value ) {
273 $REAL_SESSION[$key] = $value;
274 unset($_SESSION[$key]);
275 }
276 }
277
278 // Running code in different context
279 $scope = function() {
280 extract( func_get_arg(1) );
281 $_GET = $_REQUEST = array(
282 "url" => $url->getUrl(),
283 "max" => 5,
284 "links" => "preserve",
285 "exc" => "",
286 "format" => "json",
287 "submit" => "Create Feed"
288 );
289 ob_start();
290 require func_get_arg(0);
291 $json = ob_get_contents();
292 ob_end_clean();
293 return $json;
294 };
295 $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) );
296
297 // Clearing and restoring context
298 foreach( $GLOBALS as $key => $value ) {
299 if( $key != "GLOBALS" && $key != "_SESSION" ) {
300 unset($GLOBALS[$key]);
301 }
302 }
303 foreach( $REAL as $key => $value ) {
304 $GLOBALS[$key] = $value;
305 }
306 // Clearing and restoring session
307 if ( isset($REAL_SESSION) ) {
308 foreach( $_SESSION as $key => $value ) {
309 unset($_SESSION[$key]);
310 }
311 foreach( $REAL_SESSION as $key => $value ) {
312 $_SESSION[$key] = $value;
313 }
314 }
315
316 return json_decode($json, true);
317 }
318
319 /**
320 * Returns whether we handle an AJAX (XMLHttpRequest) request.
321 * @return boolean whether we handle an AJAX (XMLHttpRequest) request.
322 */
323 public static function isAjaxRequest()
324 {
325 return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
326 }
327
255} 328}