| 1 |
<?php |
| 2 |
/* |
| 3 |
Belokan Micro Framework |
| 4 |
Licensed under GNU GPL |
| 5 |
http://gitorious.org/belokan |
| 6 |
--- |
| 7 |
Belokan/Request.php: |
| 8 |
Request tool |
| 9 |
*/ |
| 10 |
|
| 11 |
class Belokan_Request { |
| 12 |
|
| 13 |
private static |
| 14 |
$_instance, |
| 15 |
$_controller, |
| 16 |
$_action, |
| 17 |
$_format, |
| 18 |
$_params, |
| 19 |
$_browser, |
| 20 |
$_method, |
| 21 |
$_xhr, |
| 22 |
$_reload, |
| 23 |
$_fromElsewhere, |
| 24 |
$_headers, |
| 25 |
$_body; |
| 26 |
|
| 27 |
public static function getInstance () |
| 28 |
{ |
| 29 |
if (self::$_instance === null) |
| 30 |
self::$_instance = new self; |
| 31 |
return self::$_instance; |
| 32 |
} |
| 33 |
|
| 34 |
public function __construct () |
| 35 |
{ |
| 36 |
if (!isset($_SERVER['REDIRECT_URL']) || empty($_SERVER['REDIRECT_URL'])) { |
| 37 |
$uri = $_SERVER['REQUEST_URI']; |
| 38 |
if (!empty($_SERVER['QUERY_STRING'])) |
| 39 |
$uri = substr($uri, 0, -strlen('?'.$_SERVER['QUERY_STRING'])); |
| 40 |
} else $uri = $_SERVER['REDIRECT_URL']; |
| 41 |
|
| 42 |
$uri = substr($uri, strlen(Belokan_Config::getInstance()->get('path'))); |
| 43 |
$uri = explode('/', $uri); |
| 44 |
|
| 45 |
$formatConfig = Belokan_Config::getInstance()->get('format'); |
| 46 |
$count = count($uri); |
| 47 |
if ($formatConfig['enabled'] && ($dot = strrpos($uri[$count-1], '.'))) { |
| 48 |
$this->_format = substr($uri[$count - 1], $dot+1); |
| 49 |
$this->_params['_lastWithFormat'] = $uri[$count - 1]; |
| 50 |
$uri[$count - 1] = substr($uri[$count - 1], 0, $dot); |
| 51 |
} else $this->_format = $formatConfig['default']; |
| 52 |
|
| 53 |
$this->_controller = (isset($uri[1]) && !empty($uri[1])) ? ucfirst($uri[1]) : 'Home'; |
| 54 |
|
| 55 |
$this->_action = (isset($uri[2]) && !empty($uri[2])) ? $uri[2] : 'default'; |
| 56 |
|
| 57 |
if (isset($uri[3]) && strlen($uri[3])) { |
| 58 |
$this->_params = array(); |
| 59 |
for ($i=3; $i < $count; $i++) $this->_params[$i-3] = $uri[$i]; |
| 60 |
} else $this->_params = null; |
| 61 |
|
| 62 |
$this->_browser = null; |
| 63 |
$this->_method = null; |
| 64 |
$this->_xhr = null; |
| 65 |
$this->_reload = null; |
| 66 |
$this->_fromElsewhere = null; |
| 67 |
$this->_headers = null; |
| 68 |
$this->_body = null; |
| 69 |
} |
| 70 |
|
| 71 |
public function getController () |
| 72 |
{ |
| 73 |
return $this->_controller; |
| 74 |
} |
| 75 |
|
| 76 |
public function getAction () |
| 77 |
{ |
| 78 |
return $this->_action; |
| 79 |
} |
| 80 |
|
| 81 |
public function getFormat () |
| 82 |
{ |
| 83 |
return $this->_format; |
| 84 |
} |
| 85 |
|
| 86 |
public function getParams () |
| 87 |
{ |
| 88 |
return $this->_params; |
| 89 |
} |
| 90 |
|
| 91 |
public function paramExists ($param) |
| 92 |
{ |
| 93 |
if ($this->_params != null) { |
| 94 |
if (array_key_exists($param, $this->_params)) return true; |
| 95 |
if (($pos = array_search($param, $this->_params)) !== false) { |
| 96 |
$this->_params[$param] = $this->_params[$pos+1]; |
| 97 |
return true; |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
public function getParam ($param) |
| 105 |
{ |
| 106 |
if ($this->paramExists($param)) return $this->_params[$param]; |
| 107 |
else return null; |
| 108 |
} |
| 109 |
|
| 110 |
public function getBrowser () |
| 111 |
{ |
| 112 |
if ($this->_browser === null) { |
| 113 |
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) |
| 114 |
$this->_browser = 'ie'; |
| 115 |
else if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false) |
| 116 |
$this->_browser = 'opera'; |
| 117 |
else if (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) |
| 118 |
$this->_browser = 'chrome'; |
| 119 |
else if (strpos($_SERVER['HTTP_USER_AGENT'], 'WebKit') !== false) |
| 120 |
$this->_browser = 'webkit'; |
| 121 |
else if (strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false) |
| 122 |
$this->_browser = 'gecko'; |
| 123 |
else $this->_browser = 'other'; |
| 124 |
} |
| 125 |
return $this->_browser; |
| 126 |
} |
| 127 |
|
| 128 |
public function browserIsMSIE () |
| 129 |
{ |
| 130 |
return $this->getBrowser() == 'ie'; |
| 131 |
} |
| 132 |
|
| 133 |
public function getMethod () |
| 134 |
{ |
| 135 |
if ($this->_method === null) |
| 136 |
$this->_method = strtoupper($_SERVER['REQUEST_METHOD']); |
| 137 |
return $this->_method; |
| 138 |
} |
| 139 |
|
| 140 |
public function isPost () |
| 141 |
{ |
| 142 |
return $this->getMethod() == 'POST'; |
| 143 |
} |
| 144 |
|
| 145 |
public function isGet () |
| 146 |
{ |
| 147 |
return $this->getMethod() == 'GET'; |
| 148 |
} |
| 149 |
|
| 150 |
public function isPut () |
| 151 |
{ |
| 152 |
return $this->getMethod() == 'PUT'; |
| 153 |
} |
| 154 |
|
| 155 |
public function isDelete () |
| 156 |
{ |
| 157 |
return $this->getMethod() == 'DELETE'; |
| 158 |
} |
| 159 |
|
| 160 |
public function isHead () |
| 161 |
{ |
| 162 |
return $this->getMethod() == 'HEAD'; |
| 163 |
} |
| 164 |
|
| 165 |
public function isAjax () |
| 166 |
{ |
| 167 |
if ($this->_xhr === null) |
| 168 |
$this->_xhr = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && |
| 169 |
$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); |
| 170 |
return $this->_xhr; |
| 171 |
} |
| 172 |
|
| 173 |
public function isReload () |
| 174 |
{ |
| 175 |
if ($this->_reload === null) |
| 176 |
$this->_reload = (isset($_SERVER['HTTP_CACHE_CONTROL']) && strpos($_SERVER['HTTP_CACHE_CONTROL'], 'max-age=0') !== false) || |
| 177 |
(isset($_SERVER['HTTP_PRAGMA']) && strpos($_SERVER['HTTP_PRAGMA'], 'no-cache')); |
| 178 |
return $this->_reload; |
| 179 |
} |
| 180 |
|
| 181 |
public function comeFromElsewhere () |
| 182 |
{ |
| 183 |
if ($this->_fromElsewhere === null) |
| 184 |
$this->_fromElsewhere = (!isset($_SERVER['HTTP_REFERER']) || |
| 185 |
strpos($_SERVER['HTTP_REFERER'], 'http://'.$_SERVER['HTTP_HOST']) === false); |
| 186 |
return $this->_fromElsewhere; |
| 187 |
} |
| 188 |
|
| 189 |
public function getHeaders () |
| 190 |
{ |
| 191 |
if ($this->_headers === null) |
| 192 |
foreach ($_SERVER as $k => $v) |
| 193 |
if (substr($k, 0, 5) == 'HTTP_') |
| 194 |
$this->_headers[substr($k, 5)] = $v; |
| 195 |
return $this->_headers; |
| 196 |
} |
| 197 |
|
| 198 |
public function getBody () |
| 199 |
{ |
| 200 |
if ($this->_body === null) |
| 201 |
$this->_body = file_get_contents('php://input'); |
| 202 |
return $this->_body; |
| 203 |
} |
| 204 |
|
| 205 |
} |