| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface HC3_IUri |
| 3 |
{ |
| 4 |
public function setAssetsPath( $path ); |
| 5 |
public function assetsPath( $src ); |
| 6 |
public function getSlug(); |
| 7 |
public function getParams(); |
| 8 |
public function makeUrl( $slug = NULL, $proto = NULL ); |
| 9 |
public function baseUrl(); |
| 10 |
public function fromUrl( $url ); |
| 11 |
public function isFullUrl( $url ); |
| 12 |
public function currentUrl(); |
| 13 |
} |
| 14 |
|
| 15 |
if( ! defined('HC3_URI_NO_PORT') ) define( 'HC3_URI_NO_PORT', true ); |
| 16 |
|
| 17 |
class HC3_Uri implements HC3_IUri |
| 18 |
{ |
| 19 |
protected $_hca = 'hca'; |
| 20 |
protected $_hcj = 'hcj'; |
| 21 |
// protected $_hcr = 'hcr'; |
| 22 |
protected $_hcr = ''; |
| 23 |
protected $_hcrValue = NULL; |
| 24 |
|
| 25 |
protected $_separatorToParams = ':'; |
| 26 |
// protected $_separatorToParams = '*'; |
| 27 |
protected $_suffixForMulti = '_'; |
| 28 |
protected $_separatorForMulti = '|'; |
| 29 |
|
| 30 |
protected $baseUrl = NULL; |
| 31 |
protected $baseParams = array(); |
| 32 |
protected $slug = NULL; |
| 33 |
protected $rawParams = array(); |
| 34 |
protected $params = array(); |
| 35 |
|
| 36 |
protected $assetsPath = NULL; |
| 37 |
|
| 38 |
public function __construct() |
| 39 |
{ |
| 40 |
$this->fromUrl( $this->currentUrl() ); |
| 41 |
if( strlen($this->_hcr) ){ |
| 42 |
$this->_hcrValue = mt_rand(1000, 9999); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function setAssetsPath( $path ) |
| 47 |
{ |
| 48 |
$this->assetsPath = $path; |
| 49 |
return $this; |
| 50 |
} |
| 51 |
|
| 52 |
public function assetsPath( $src ) |
| 53 |
{ |
| 54 |
$return = $this->assetsPath ? $this->assetsPath : $this->baseUrl(); |
| 55 |
return $return; |
| 56 |
} |
| 57 |
|
| 58 |
public function setSlug( $slug ) |
| 59 |
{ |
| 60 |
$this->slug = $slug; |
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function getSlug() |
| 65 |
{ |
| 66 |
return $this->slug; |
| 67 |
} |
| 68 |
|
| 69 |
public function getParams() |
| 70 |
{ |
| 71 |
return $this->params; |
| 72 |
} |
| 73 |
|
| 74 |
public function makeUrl( $slug = NULL, $proto = NULL ) |
| 75 |
{ |
| 76 |
$params = array(); |
| 77 |
if( is_array($slug) ){ |
| 78 |
list( $slug, $params ) = $slug; |
| 79 |
} |
| 80 |
|
| 81 |
if( $this->isFullUrl($slug) ){ |
| 82 |
return $slug; |
| 83 |
} |
| 84 |
|
| 85 |
if( $slug == '-referrer-' ){ |
| 86 |
if( isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) ){ |
| 87 |
$this->fromUrl( $_SERVER['HTTP_REFERER'] ); |
| 88 |
$slug = $this->getSlug(); |
| 89 |
// $return = $_SERVER['HTTP_REFERER']; |
| 90 |
// return $return; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
$href_params = $this->baseParams; |
| 95 |
|
| 96 |
$hca_param = NULL; |
| 97 |
if( $slug ){ |
| 98 |
$hca_param = $slug; |
| 99 |
|
| 100 |
// pesist params within the same slug |
| 101 |
if( $slug == $this->slug && ($params !== NULL) ){ |
| 102 |
$params = array_merge( $this->params, $params ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// pesist params within the same slug |
| 107 |
if( (NULL == $slug) && (! $this->slug) ){ |
| 108 |
$params = array_merge( $this->params, $params ); |
| 109 |
} |
| 110 |
|
| 111 |
if( $params ){ |
| 112 |
$params_string = $this->_buildHcaParams( $params ); |
| 113 |
if( strlen($params_string) ){ |
| 114 |
$hca_param .= $this->_separatorToParams . $params_string; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if( $hca_param ){ |
| 119 |
// add random to avoid unnecessary caching |
| 120 |
$href_params[$this->_hca] = $hca_param; |
| 121 |
if( $this->_hcrValue ){ |
| 122 |
$href_params[$this->_hcr] = $this->_hcrValue; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$return = $this->baseUrl; |
| 127 |
if( $href_params ){ |
| 128 |
$href_params = http_build_query($href_params); |
| 129 |
$href_params = urldecode( $href_params ); |
| 130 |
|
| 131 |
$glue = (strpos($return, '?') === FALSE) ? '?' : '&'; |
| 132 |
$return .= $glue . $href_params; |
| 133 |
} |
| 134 |
|
| 135 |
if( NULL !== $proto ){ |
| 136 |
// echo "PROTO = '$proto'<br>"; |
| 137 |
$starts = array( 'https://', 'http://', '//' ); |
| 138 |
foreach( $starts as $prefix ){ |
| 139 |
if( substr($return, 0, strlen($prefix)) == $prefix ){ |
| 140 |
$return = $proto . substr($return, strlen($prefix)); |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return $return; |
| 147 |
} |
| 148 |
|
| 149 |
public function baseUrl() |
| 150 |
{ |
| 151 |
$return = $this->baseUrl; |
| 152 |
return $return; |
| 153 |
} |
| 154 |
|
| 155 |
public function fromUrl( $url ) |
| 156 |
{ |
| 157 |
$purl = parse_url( $url ); |
| 158 |
|
| 159 |
$baseUrl = $purl['scheme'] . '://'. $purl['host']; |
| 160 |
if( isset($purl['port']) && (80 != $purl['port']) ){ |
| 161 |
if( FALSE === strpos($purl['host'], ':') ){ |
| 162 |
$baseUrl .= ':' . $purl['port']; |
| 163 |
} |
| 164 |
} |
| 165 |
$baseUrl .= $purl['path']; |
| 166 |
|
| 167 |
$this->baseUrl = $baseUrl; |
| 168 |
$this->baseParams = array(); |
| 169 |
// $this->slug = NULL; |
| 170 |
|
| 171 |
if( isset($purl['query']) && $purl['query']){ |
| 172 |
parse_str( $purl['query'], $base_params ); |
| 173 |
|
| 174 |
/* grab our hca */ |
| 175 |
if( isset($base_params[$this->_hca]) ){ |
| 176 |
// $this->slug = NULL; |
| 177 |
$hca = $base_params[$this->_hca]; |
| 178 |
|
| 179 |
// trim slashes |
| 180 |
$hca = trim($hca, '/'); |
| 181 |
|
| 182 |
$slug = $hca; |
| 183 |
$params = array(); |
| 184 |
|
| 185 |
if( strpos($slug, $this->_separatorToParams) !== FALSE ){ |
| 186 |
list( $slug, $params ) = explode($this->_separatorToParams, $slug, 2); |
| 187 |
$params = explode('/', $params); |
| 188 |
} |
| 189 |
|
| 190 |
$this->slug = $slug; |
| 191 |
$this->rawParams = $params; |
| 192 |
$this->params = $this->_parseHcaParams( $params ); |
| 193 |
} |
| 194 |
|
| 195 |
/* base params */ |
| 196 |
unset( $base_params[$this->_hca] ); |
| 197 |
$this->baseParams = $base_params; |
| 198 |
} |
| 199 |
else { |
| 200 |
$this->slug = NULL; |
| 201 |
} |
| 202 |
|
| 203 |
unset( $this->baseParams[$this->_hcj] ); |
| 204 |
|
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
public function isFullUrl( $url ) |
| 209 |
{ |
| 210 |
$ret = false; |
| 211 |
|
| 212 |
if( is_array($url) ){ |
| 213 |
$url = array_shift($url); |
| 214 |
} |
| 215 |
|
| 216 |
if( null === $url ){ |
| 217 |
$url = ''; |
| 218 |
} |
| 219 |
|
| 220 |
$prfx = array( 'http://', 'https://', '//', 'webcal://' ); |
| 221 |
reset( $prfx ); |
| 222 |
foreach( $prfx as $prf ){ |
| 223 |
if( substr($url, 0, strlen($prf)) == $prf ){ |
| 224 |
$ret = true; |
| 225 |
return $ret; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if( strpos($url, '.php') !== false ){ |
| 230 |
$ret = true; |
| 231 |
return $ret; |
| 232 |
} |
| 233 |
|
| 234 |
return $ret; |
| 235 |
} |
| 236 |
|
| 237 |
public function currentUrl() |
| 238 |
{ |
| 239 |
$noPort = defined( 'HC3_URI_NO_PORT' ) && HC3_URI_NO_PORT ? true : false; |
| 240 |
|
| 241 |
$return = 'http'; |
| 242 |
if( isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on') ){ |
| 243 |
$return .= 's'; |
| 244 |
} |
| 245 |
|
| 246 |
$return .= "://"; |
| 247 |
|
| 248 |
$host = ''; |
| 249 |
if( isset($_SERVER['HTTP_HOST']) && $_SERVER['SERVER_PORT'] != '80'){ |
| 250 |
if( FALSE === strpos($_SERVER['HTTP_HOST'], ':') ){ |
| 251 |
$host .= $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT']; |
| 252 |
} |
| 253 |
else { |
| 254 |
$host .= $_SERVER['HTTP_HOST']; |
| 255 |
} |
| 256 |
} |
| 257 |
else { |
| 258 |
$host = $_SERVER['HTTP_HOST']; |
| 259 |
} |
| 260 |
|
| 261 |
if( $noPort ){ |
| 262 |
$pos = strpos( $host, ':' ); |
| 263 |
if( false !== $pos ){ |
| 264 |
$host = substr( $host, 0, $pos ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
$return .= $host; |
| 269 |
|
| 270 |
if ( ! empty($_SERVER['REQUEST_URI']) ){ |
| 271 |
$return .= $_SERVER['REQUEST_URI']; |
| 272 |
} |
| 273 |
else { |
| 274 |
$return .= $_SERVER['SCRIPT_NAME']; |
| 275 |
} |
| 276 |
|
| 277 |
$return = urldecode( $return ); |
| 278 |
return $return; |
| 279 |
} |
| 280 |
|
| 281 |
protected function _buildHcaParams( $params ) |
| 282 |
{ |
| 283 |
$return = array(); |
| 284 |
|
| 285 |
foreach( $params as $k => $v ){ |
| 286 |
if( is_array($v) ){ |
| 287 |
if( ! $v ){ |
| 288 |
continue; |
| 289 |
} |
| 290 |
$k = $k . $this->_suffixForMulti; |
| 291 |
$v = HC3_Functions::glueArray( $v ); |
| 292 |
// $v = join($this->_separatorForMulti, $v); |
| 293 |
} |
| 294 |
else { |
| 295 |
if( $v === NULL ){ |
| 296 |
continue; |
| 297 |
} |
| 298 |
} |
| 299 |
$return[] = $k . '/' . $v; |
| 300 |
} |
| 301 |
|
| 302 |
$return = join('/', $return); |
| 303 |
return $return; |
| 304 |
} |
| 305 |
|
| 306 |
protected function _parseHcaParams( $rawParams ) |
| 307 |
{ |
| 308 |
$return = array(); |
| 309 |
for( $ii = 0; $ii < count($rawParams); $ii = $ii + 2 ){ |
| 310 |
if( ! isset($rawParams[$ii + 1]) ){ |
| 311 |
break; |
| 312 |
} |
| 313 |
|
| 314 |
$key = $rawParams[$ii]; |
| 315 |
$value = $rawParams[$ii + 1]; |
| 316 |
|
| 317 |
$needArray = (substr($key, -strlen($this->_suffixForMulti)) == $this->_suffixForMulti) ? TRUE : FALSE; |
| 318 |
if( $needArray ){ |
| 319 |
$key = substr($key, 0, -strlen($this->_suffixForMulti)); |
| 320 |
$value = HC3_Functions::unglueArray( $value ); |
| 321 |
// $value = explode($this->_separatorForMulti, $value); |
| 322 |
} |
| 323 |
|
| 324 |
if( array_key_exists($key, $return) && $needArray ){ |
| 325 |
$return[$key] = array_merge( $return[$key], $value ); |
| 326 |
} |
| 327 |
else { |
| 328 |
$return[$key] = $value; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return $return; |
| 333 |
} |
| 334 |
} |
| 335 |
|