| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface HC3_Request_ |
| 3 |
{ |
| 4 |
public function initParam( $k, $v ); |
| 5 |
public function setParam( $k, $v ); |
| 6 |
public function getSlug(); |
| 7 |
public function getParams( $withoutDefault = FALSE ); |
| 8 |
|
| 9 |
public function getMethod(); |
| 10 |
public function getIpAddress(); |
| 11 |
public function getUserAgent(); |
| 12 |
public function getCookie( $index ); |
| 13 |
public function getReferrer(); |
| 14 |
|
| 15 |
public function isPrintView(); |
| 16 |
public function isAjax(); |
| 17 |
} |
| 18 |
|
| 19 |
class HC3_Request implements HC3_Request_ |
| 20 |
{ |
| 21 |
protected $url = null; |
| 22 |
protected $initParams = array(); |
| 23 |
protected $setParams = array(); |
| 24 |
public $realSlug = ''; |
| 25 |
public $post, $uri; |
| 26 |
|
| 27 |
public function __construct( |
| 28 |
HC3_Uri $uri, |
| 29 |
HC3_Post $post, |
| 30 |
|
| 31 |
HC3_UriAction $uriAction |
| 32 |
) |
| 33 |
{ |
| 34 |
if( ! defined('WPINC') ){ |
| 35 |
$this->_sanitizeGet(); |
| 36 |
$this->_sanitizeCookie(); |
| 37 |
} |
| 38 |
|
| 39 |
$method = $this->getMethod(); |
| 40 |
if( $method != 'get' ){ |
| 41 |
$this->uri = $uriAction; |
| 42 |
if( $referrer = $this->getReferrer() ){ |
| 43 |
$uri->fromUrl( $referrer ); |
| 44 |
// echo "URI FROM URL '$referrer'<br>"; |
| 45 |
// exit; |
| 46 |
} |
| 47 |
} |
| 48 |
else { |
| 49 |
$this->uri = $uri; |
| 50 |
} |
| 51 |
|
| 52 |
$this->post = $post; |
| 53 |
} |
| 54 |
|
| 55 |
public function getReferrer() |
| 56 |
{ |
| 57 |
$return = NULL; |
| 58 |
if( isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) ){ |
| 59 |
$return = $_SERVER['HTTP_REFERER']; |
| 60 |
} |
| 61 |
return $return; |
| 62 |
} |
| 63 |
|
| 64 |
public function isAjax() |
| 65 |
{ |
| 66 |
$return = FALSE; |
| 67 |
if( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') ){ |
| 68 |
$return = TRUE; |
| 69 |
} |
| 70 |
return $return; |
| 71 |
} |
| 72 |
|
| 73 |
public function initParam( $k, $v ) |
| 74 |
{ |
| 75 |
$this->initParams[ $k ] = $v; |
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
public function setParam( $k, $v ) |
| 80 |
{ |
| 81 |
$this->setParams[ $k ] = $v; |
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
public function setSlug( $slug ) |
| 86 |
{ |
| 87 |
$this->uri->setSlug( $slug ); |
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
public function getSlug() |
| 92 |
{ |
| 93 |
$hca = $this->post->get('hca'); |
| 94 |
if( $hca ){ |
| 95 |
$slug = $hca; |
| 96 |
} |
| 97 |
else { |
| 98 |
$slug = $this->uri->getSlug(); |
| 99 |
} |
| 100 |
|
| 101 |
if( null === $slug ){ |
| 102 |
$slug = ''; |
| 103 |
} |
| 104 |
|
| 105 |
return $slug; |
| 106 |
} |
| 107 |
|
| 108 |
public function setRealSlug( $slug ) |
| 109 |
{ |
| 110 |
$this->realSlug = $slug; |
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
public function getRealSlug() |
| 115 |
{ |
| 116 |
return $this->realSlug; |
| 117 |
} |
| 118 |
|
| 119 |
public function getParams( $withoutDefault = FALSE ) |
| 120 |
{ |
| 121 |
$return = $this->uri->getParams(); |
| 122 |
|
| 123 |
if( ! $withoutDefault ){ |
| 124 |
$return = array_merge( $this->initParams, $return ); |
| 125 |
} |
| 126 |
|
| 127 |
$return = array_merge( $return, $this->setParams ); |
| 128 |
return $return; |
| 129 |
} |
| 130 |
|
| 131 |
public function isPrintView() |
| 132 |
{ |
| 133 |
$ret = false; |
| 134 |
$params = $this->getParams(); |
| 135 |
if( array_key_exists('print', $params) && ($params['print']) ){ |
| 136 |
$ret = true; |
| 137 |
} |
| 138 |
|
| 139 |
return $ret; |
| 140 |
} |
| 141 |
|
| 142 |
public function getMethod() |
| 143 |
{ |
| 144 |
$return = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get'; |
| 145 |
return $return; |
| 146 |
} |
| 147 |
|
| 148 |
public function getIpAddress() |
| 149 |
{ |
| 150 |
$return = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0'; |
| 151 |
return $return; |
| 152 |
} |
| 153 |
|
| 154 |
public function getUserAgent() |
| 155 |
{ |
| 156 |
$return = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT']; |
| 157 |
return $return; |
| 158 |
} |
| 159 |
|
| 160 |
public function getCookie( $index ) |
| 161 |
{ |
| 162 |
return $this->_fetch_from_array( $_COOKIE, $index ); |
| 163 |
} |
| 164 |
|
| 165 |
protected function _fetch_from_array($array, $index = '') |
| 166 |
{ |
| 167 |
if ( ! isset($array[$index])){ |
| 168 |
return FALSE; |
| 169 |
} |
| 170 |
return $array[$index]; |
| 171 |
} |
| 172 |
|
| 173 |
protected function _sanitizeGet() |
| 174 |
{ |
| 175 |
if (is_array($_GET) AND count($_GET) > 0){ |
| 176 |
foreach ($_GET as $key => $val){ |
| 177 |
$_GET[$this->_cleanInputKeys($key)] = $this->_cleanInputData($val); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
protected function _sanitizeCookie() |
| 183 |
{ |
| 184 |
if (is_array($_COOKIE) AND count($_COOKIE) > 0){ |
| 185 |
unset($_COOKIE['$Version']); |
| 186 |
unset($_COOKIE['$Path']); |
| 187 |
unset($_COOKIE['$Domain']); |
| 188 |
|
| 189 |
foreach ($_COOKIE as $key => $val){ |
| 190 |
$_COOKIE[$this->_cleanInputKeys($key)] = $this->_cleanInputData($val); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
protected function _cleanInputKeys( $str ) |
| 196 |
{ |
| 197 |
if ( ! preg_match("/^[a-z0-9:_\/\-\~]+$/i", $str)){ |
| 198 |
exit('Disallowed Key Characters on: ' . '"' . esc_html($str) . '"' . '<br>'); |
| 199 |
} |
| 200 |
return $str; |
| 201 |
} |
| 202 |
|
| 203 |
protected function _cleanInputData( $str ) |
| 204 |
{ |
| 205 |
if( is_array($str) ){ |
| 206 |
$new_array = array(); |
| 207 |
foreach ($str as $key => $val){ |
| 208 |
$new_array[$this->_cleanInputKeys($key)] = $this->_cleanInputData($val); |
| 209 |
} |
| 210 |
return $new_array; |
| 211 |
} |
| 212 |
|
| 213 |
/* We strip slashes if magic quotes is on to keep things consistent |
| 214 |
NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and |
| 215 |
it will probably not exist in future versions at all. |
| 216 |
*/ |
| 217 |
$need_strip = FALSE; |
| 218 |
if( version_compare(PHP_VERSION, '5.4', '<') && get_magic_quotes_gpc() ){ |
| 219 |
$need_strip = TRUE; |
| 220 |
} |
| 221 |
elseif( defined('WPINC') ){ |
| 222 |
$need_strip = TRUE; |
| 223 |
} |
| 224 |
|
| 225 |
if( $need_strip ){ |
| 226 |
$str = stripslashes($str); |
| 227 |
} |
| 228 |
|
| 229 |
// Remove control characters |
| 230 |
$str = HC3_Functions::removeInvisibleCharacters($str); |
| 231 |
|
| 232 |
// Standardize newlines |
| 233 |
if (strpos($str, "\r") !== FALSE){ |
| 234 |
$str = str_replace(array("\r\n", "\r", "\r\n\n"), PHP_EOL, $str); |
| 235 |
} |
| 236 |
|
| 237 |
return $str; |
| 238 |
} |
| 239 |
} |