Changeset 21237 for branches/version-2_11-dev/data/module/Net
- Timestamp:
- 2011/09/09 15:29:59 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_11-dev/data/module/Net/URL.php
r20116 r21237 37 37 // Net_URL Class 38 38 39 39 40 class Net_URL 40 41 { 42 var $options = array('encode_query_keys' => false); 41 43 /** 42 44 * Full url … … 122 124 function __construct($url = null, $useBrackets = true) 123 125 { 126 $this->url = $url; 127 $this->useBrackets = $useBrackets; 128 129 $this->initialize(); 130 } 131 132 function initialize() 133 { 124 134 $HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS']; 125 135 126 $this->useBrackets = $useBrackets;127 $this->url = $url;128 136 $this->user = ''; 129 137 $this->pass = ''; … … 135 143 136 144 // Only use defaults if not an absolute URL given 137 if (!preg_match('/^[a-z0-9]+:\/\//i', $url)) { 138 139 $this->protocol = (@$HTTP_SERVER_VARS['HTTPS'] == 'on' ? 'https' : 'http'); 145 if (!preg_match('/^[a-z0-9]+:\/\//i', $this->url)) { 146 $this->protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http'); 140 147 141 148 /** 142 149 * Figure out host/port 143 150 */ 144 if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) { 151 if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) && 152 preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) 153 { 145 154 $host = $matches[1]; 146 155 if (!empty($matches[3])) { … … 161 170 162 171 // Parse the url and store the various parts 163 if (!empty($ url)) {164 $urlinfo = parse_url($ url);172 if (!empty($this->url)) { 173 $urlinfo = parse_url($this->url); 165 174 166 175 // Default querystring … … 201 210 } 202 211 } 203 204 212 /** 205 213 * Returns full url … … 224 232 225 233 /** 226 * Adds a querystring item 234 * Adds or updates a querystring item (URL parameter). 235 * Automatically encodes parameters with rawurlencode() if $preencoded 236 * is false. 237 * You can pass an array to $value, it gets mapped via [] in the URL if 238 * $this->useBrackets is activated. 227 239 * 228 240 * @param string $name Name of item … … 233 245 function addQueryString($name, $value, $preencoded = false) 234 246 { 247 if ($this->getOption('encode_query_keys')) { 248 $name = rawurlencode($name); 249 } 250 235 251 if ($preencoded) { 236 252 $this->querystring[$name] = $value; … … 248 264 function removeQueryString($name) 249 265 { 266 if ($this->getOption('encode_query_keys')) { 267 $name = rawurlencode($name); 268 } 269 250 270 if (isset($this->querystring[$name])) { 251 271 unset($this->querystring[$name]); … … 274 294 if (!empty($this->querystring)) { 275 295 foreach ($this->querystring as $name => $value) { 296 // Encode var name 297 $name = rawurlencode($name); 298 276 299 if (is_array($value)) { 277 300 foreach ($value as $k => $v) { … … 312 335 $key = $part; 313 336 } 314 if (substr($key, -2) == '[]') { 315 $key = substr($key, 0, -2); 316 if (@!is_array($return[$key])) { 317 $return[$key] = array(); 337 338 if (!$this->getOption('encode_query_keys')) { 339 $key = rawurldecode($key); 340 } 341 342 if (preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) { 343 $key = $matches[1]; 344 $idx = $matches[2]; 345 346 // Ensure is an array 347 if (empty($return[$key]) || !is_array($return[$key])) { 348 $return[$key] = array(); 349 } 350 351 // Add data 352 if ($idx === '') { 318 353 $return[$key][] = $value; 319 354 } else { 320 $return[$key][ ] = $value;355 $return[$key][$idx] = $value; 321 356 } 322 357 } elseif (!$this->useBrackets AND !empty($return[$key])) { … … 341 376 * This method can also be called statically. 342 377 * 343 * @param string $ urlURL path to resolve378 * @param string $path URL path to resolve 344 379 * @return string The result 345 380 */ … … 404 439 { 405 440 $this->protocol = $protocol; 406 $this->port = is_null($port) ? $this->getStandardPort() : $port; 441 $this->port = is_null($port) ? $this->getStandardPort($protocol) : $port; 442 } 443 444 /** 445 * Set an option 446 * 447 * This function set an option 448 * to be used thorough the script. 449 * 450 * @access public 451 * @param string $optionName The optionname to set 452 * @param string $value The value of this option. 453 */ 454 function setOption($optionName, $value) 455 { 456 if (!array_key_exists($optionName, $this->options)) { 457 return false; 458 } 459 460 $this->options[$optionName] = $value; 461 $this->initialize(); 462 } 463 464 /** 465 * Get an option 466 * 467 * This function gets an option 468 * from the $this->options array 469 * and return it's value. 470 * 471 * @access public 472 * @param string $opionName The name of the option to retrieve 473 * @see $this->options 474 */ 475 function getOption($optionName) 476 { 477 if (!isset($this->options[$optionName])) { 478 return false; 479 } 480 481 return $this->options[$optionName]; 407 482 } 408 483
Note: See TracChangeset
for help on using the changeset viewer.
