2013-08-02 19:40:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* poche, a read it later open source system
|
|
|
|
*
|
|
|
|
* @category poche
|
|
|
|
* @author Nicolas Lœuillet <support@inthepoche.com>
|
|
|
|
* @copyright 2013
|
|
|
|
* @license http://www.wtfpl.net/ see COPYING file
|
|
|
|
*/
|
|
|
|
|
|
|
|
class pocheTools
|
|
|
|
{
|
|
|
|
public static function initPhp()
|
|
|
|
{
|
|
|
|
define('START_TIME', microtime(true));
|
|
|
|
|
|
|
|
if (phpversion() < 5) {
|
|
|
|
die(_('Oops, it seems you don\'t have PHP 5.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
|
|
function stripslashesDeep($value) {
|
|
|
|
return is_array($value)
|
|
|
|
? array_map('stripslashesDeep', $value)
|
|
|
|
: stripslashes($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_magic_quotes_gpc()) {
|
|
|
|
$_POST = array_map('stripslashesDeep', $_POST);
|
|
|
|
$_GET = array_map('stripslashesDeep', $_GET);
|
|
|
|
$_COOKIE = array_map('stripslashesDeep', $_COOKIE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
register_shutdown_function('ob_end_flush');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isUrl($url)
|
|
|
|
{
|
2013-08-02 20:40:51 +00:00
|
|
|
$pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
|
2013-08-02 19:40:29 +00:00
|
|
|
|
|
|
|
return preg_match($pattern, $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getUrl()
|
|
|
|
{
|
|
|
|
$https = (!empty($_SERVER['HTTPS'])
|
|
|
|
&& (strtolower($_SERVER['HTTPS']) == 'on'))
|
|
|
|
|| (isset($_SERVER["SERVER_PORT"])
|
|
|
|
&& $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection.
|
|
|
|
$serverport = (!isset($_SERVER["SERVER_PORT"])
|
|
|
|
|| $_SERVER["SERVER_PORT"] == '80'
|
|
|
|
|| ($https && $_SERVER["SERVER_PORT"] == '443')
|
|
|
|
? '' : ':' . $_SERVER["SERVER_PORT"]);
|
|
|
|
|
|
|
|
$scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
|
|
|
|
|
|
|
|
if (!isset($_SERVER["SERVER_NAME"])) {
|
|
|
|
return $scriptname;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'http' . ($https ? 's' : '') . '://'
|
|
|
|
. $_SERVER["SERVER_NAME"] . $serverport . $scriptname;
|
|
|
|
}
|
|
|
|
|
2013-08-02 20:40:51 +00:00
|
|
|
public static function redirect($url = '')
|
2013-08-02 19:40:29 +00:00
|
|
|
{
|
2013-08-02 20:40:51 +00:00
|
|
|
if ($url === '') {
|
|
|
|
$url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
|
2013-08-02 19:40:29 +00:00
|
|
|
if (isset($_POST['returnurl'])) {
|
2013-08-02 20:40:51 +00:00
|
|
|
$url = $_POST['returnurl'];
|
2013-08-02 19:40:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 20:40:51 +00:00
|
|
|
# prevent loop
|
|
|
|
if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
|
|
|
|
$url = pocheTool::getUrl();
|
2013-08-02 19:40:29 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 20:40:51 +00:00
|
|
|
if (substr($url, 0, 1) !== '?') {
|
2013-08-02 19:40:29 +00:00
|
|
|
$ref = pocheTool::getUrl();
|
2013-08-02 20:40:51 +00:00
|
|
|
if (substr($url, 0, strlen($ref)) !== $ref) {
|
|
|
|
$url = $ref;
|
2013-08-02 19:40:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-02 20:40:51 +00:00
|
|
|
header('Location: '.$url);
|
2013-08-02 19:40:29 +00:00
|
|
|
exit();
|
|
|
|
}
|
2013-08-02 20:40:51 +00:00
|
|
|
|
|
|
|
public static function cleanURL($url)
|
|
|
|
{
|
|
|
|
|
|
|
|
$url = html_entity_decode(trim($url));
|
|
|
|
|
|
|
|
$stuff = strpos($url,'&utm_source=');
|
|
|
|
if ($stuff !== FALSE)
|
|
|
|
$url = substr($url, 0, $stuff);
|
|
|
|
$stuff = strpos($url,'?utm_source=');
|
|
|
|
if ($stuff !== FALSE)
|
|
|
|
$url = substr($url, 0, $stuff);
|
|
|
|
$stuff = strpos($url,'#xtor=RSS-');
|
|
|
|
if ($stuff !== FALSE)
|
|
|
|
$url = substr($url, 0, $stuff);
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderJson($data)
|
|
|
|
{
|
|
|
|
header('Cache-Control: no-cache, must-revalidate');
|
|
|
|
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
|
|
|
|
header('Content-type: application/json; charset=UTF-8');
|
|
|
|
|
|
|
|
echo json_encode($data);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function logm($message)
|
|
|
|
{
|
2013-08-02 21:00:57 +00:00
|
|
|
if (DEBUG_POCHE) {
|
|
|
|
$t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
|
|
|
|
file_put_contents('./log.txt', $t, FILE_APPEND);
|
|
|
|
}
|
2013-08-02 20:40:51 +00:00
|
|
|
}
|
2013-08-02 19:40:29 +00:00
|
|
|
}
|