2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
class Http
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* HTTP redirect
|
|
|
|
*
|
2016-08-08 17:49:47 +08:00
|
|
|
* @param string $url
|
|
|
|
* @param string $msg Write message to session
|
|
|
|
* @param boolean $use_js Use javascript to redirect
|
|
|
|
* @return void
|
2016-07-21 22:01:57 +08:00
|
|
|
*/
|
|
|
|
public static function redirect($url, $msg = "", $use_js = false)
|
|
|
|
{
|
|
|
|
if ($msg != "") $_SESSION['msg'] = $msg;
|
|
|
|
|
|
|
|
if ($use_js)
|
|
|
|
echo "<script>window.location = '$url';</script>";
|
|
|
|
else
|
|
|
|
header('Location: '.$url);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2016-08-08 17:49:47 +08:00
|
|
|
/**
|
|
|
|
* 301 Moved Permanently
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function redirectPermanently($url)
|
|
|
|
{
|
|
|
|
http_response_code(301);
|
|
|
|
header('Location: '.$url);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public static function getRealIP()
|
|
|
|
{
|
|
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
|
} else {
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
}
|
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
|
2016-07-22 08:55:41 +08:00
|
|
|
public static function setUri($uri)
|
|
|
|
{
|
|
|
|
$_SERVER["REQUEST_URI"] = $uri;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getUri()
|
|
|
|
{
|
|
|
|
return $_SERVER["REQUEST_URI"];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getCurrentUrl()
|
|
|
|
{
|
2016-07-22 13:48:33 +08:00
|
|
|
return self::getBaseUrl().$_SERVER["REQUEST_URI"];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getBaseUrl()
|
|
|
|
{
|
|
|
|
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
|
|
|
|
$base_url .= $_SERVER["SERVER_NAME"];
|
|
|
|
$base_url .= ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
|
2016-07-22 08:55:41 +08:00
|
|
|
|
2016-07-22 13:48:33 +08:00
|
|
|
return $base_url;
|
2016-07-22 08:55:41 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public static function abort($code, $msg = "Something happened.", $is_json = false)
|
|
|
|
{
|
|
|
|
http_response_code((int)$code);
|
|
|
|
if ($is_json) {
|
|
|
|
View::json($msg, $code);
|
|
|
|
} else {
|
|
|
|
$config = require BASE_DIR."/config/view.php";
|
2016-07-27 18:09:45 +08:00
|
|
|
if (file_exists($config['view_path']."/errors/".$code.".tpl")) {
|
2016-07-21 22:01:57 +08:00
|
|
|
echo View::make('errors.'.$code)->with('code', $code)->with('message', $msg);
|
2016-07-27 18:09:45 +08:00
|
|
|
} else {
|
2016-08-09 21:55:49 +08:00
|
|
|
echo View::make('errors.e')->with('code', $code)->with('message', $msg);
|
2016-07-27 18:09:45 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|