Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Http.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Uri.php';
26 
30 #require_once 'Zend/Validate/Hostname.php';
31 
41 class Zend_Uri_Http extends Zend_Uri
42 {
46  const CHAR_ALNUM = 'A-Za-z0-9';
47  const CHAR_MARK = '-_.!~*\'()\[\]';
48  const CHAR_RESERVED = ';\/?:@&=+$,';
49  const CHAR_SEGMENT = ':@&=+$,;';
50  const CHAR_UNWISE = '{}|\\\\^`';
51 
57  protected $_username = '';
58 
64  protected $_password = '';
65 
71  protected $_host = '';
72 
78  protected $_port = '';
79 
85  protected $_path = '';
86 
92  protected $_query = '';
93 
99  protected $_fragment = '';
100 
106  protected $_regex = array();
107 
116  protected function __construct($scheme, $schemeSpecific = '')
117  {
118  // Set the scheme
119  $this->_scheme = $scheme;
120 
121  // Set up grammar rules for validation via regular expressions. These
122  // are to be used with slash-delimited regular expression strings.
123 
124  // Escaped special characters (eg. '%25' for '%')
125  $this->_regex['escaped'] = '%[[:xdigit:]]{2}';
126 
127  // Unreserved characters
128  $this->_regex['unreserved'] = '[' . self::CHAR_ALNUM . self::CHAR_MARK . ']';
129 
130  // Segment can use escaped, unreserved or a set of additional chars
131  $this->_regex['segment'] = '(?:' . $this->_regex['escaped'] . '|[' .
132  self::CHAR_ALNUM . self::CHAR_MARK . self::CHAR_SEGMENT . '])*';
133 
134  // Path can be a series of segmets char strings seperated by '/'
135  $this->_regex['path'] = '(?:\/(?:' . $this->_regex['segment'] . ')?)+';
136 
137  // URI characters can be escaped, alphanumeric, mark or reserved chars
138  $this->_regex['uric'] = '(?:' . $this->_regex['escaped'] . '|[' .
139  self::CHAR_ALNUM . self::CHAR_MARK . self::CHAR_RESERVED .
140 
141  // If unwise chars are allowed, add them to the URI chars class
142  (self::$_config['allow_unwise'] ? self::CHAR_UNWISE : '') . '])';
143 
144  // If no scheme-specific part was supplied, the user intends to create
145  // a new URI with this object. No further parsing is required.
146  if (strlen($schemeSpecific) === 0) {
147  return;
148  }
149 
150  // Parse the scheme-specific URI parts into the instance variables.
151  $this->_parseUri($schemeSpecific);
152 
153  // Validate the URI
154  if ($this->valid() === false) {
155  #require_once 'Zend/Uri/Exception.php';
156  throw new Zend_Uri_Exception('Invalid URI supplied');
157  }
158  }
159 
170  public static function fromString($uri)
171  {
172  if (is_string($uri) === false) {
173  #require_once 'Zend/Uri/Exception.php';
174  throw new Zend_Uri_Exception('$uri is not a string');
175  }
176 
177  $uri = explode(':', $uri, 2);
178  $scheme = strtolower($uri[0]);
179  $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';
180 
181  if (in_array($scheme, array('http', 'https')) === false) {
182  #require_once 'Zend/Uri/Exception.php';
183  throw new Zend_Uri_Exception("Invalid scheme: '$scheme'");
184  }
185 
186  $schemeHandler = new Zend_Uri_Http($scheme, $schemeSpecific);
187  return $schemeHandler;
188  }
189 
198  protected function _parseUri($schemeSpecific)
199  {
200  // High-level decomposition parser
201  $pattern = '~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~';
202  $status = @preg_match($pattern, $schemeSpecific, $matches);
203  if ($status === false) {
204  #require_once 'Zend/Uri/Exception.php';
205  throw new Zend_Uri_Exception('Internal error: scheme-specific decomposition failed');
206  }
207 
208  // Failed decomposition; no further processing needed
209  if ($status === false) {
210  return;
211  }
212 
213  // Save URI components that need no further decomposition
214  $this->_path = isset($matches[4]) === true ? $matches[4] : '';
215  $this->_query = isset($matches[6]) === true ? $matches[6] : '';
216  $this->_fragment = isset($matches[8]) === true ? $matches[8] : '';
217 
218  // Additional decomposition to get username, password, host, and port
219  $combo = isset($matches[3]) === true ? $matches[3] : '';
220  $pattern = '~^(([^:@]*)(:([^@]*))?@)?((?(?=[[])[[][^]]+[]]|[^:]+))(:(.*))?$~';
221  $status = @preg_match($pattern, $combo, $matches);
222  if ($status === false) {
223  #require_once 'Zend/Uri/Exception.php';
224  throw new Zend_Uri_Exception('Internal error: authority decomposition failed');
225  }
226 
227  // Save remaining URI components
228  $this->_username = isset($matches[2]) === true ? $matches[2] : '';
229  $this->_password = isset($matches[4]) === true ? $matches[4] : '';
230  $this->_host = isset($matches[5]) === true
231  ? preg_replace('~^\[([^]]+)\]$~', '\1', $matches[5]) // Strip wrapper [] from IPv6 literal
232  : '';
233  $this->_port = isset($matches[7]) === true ? $matches[7] : '';
234  }
235 
243  public function getUri()
244  {
245  if ($this->valid() === false) {
246  #require_once 'Zend/Uri/Exception.php';
247  throw new Zend_Uri_Exception('One or more parts of the URI are invalid');
248  }
249 
250  $password = strlen($this->_password) > 0 ? ":$this->_password" : '';
251  $auth = strlen($this->_username) > 0 ? "$this->_username$password@" : '';
252  $port = strlen($this->_port) > 0 ? ":$this->_port" : '';
253  $query = strlen($this->_query) > 0 ? "?$this->_query" : '';
254  $fragment = strlen($this->_fragment) > 0 ? "#$this->_fragment" : '';
255 
256  return $this->_scheme
257  . '://'
258  . $auth
259  . $this->_host
260  . $port
261  . $this->_path
262  . $query
263  . $fragment;
264  }
265 
272  public function valid()
273  {
274  // Return true if and only if all parts of the URI have passed validation
275  return $this->validateUsername()
276  and $this->validatePassword()
277  and $this->validateHost()
278  and $this->validatePort()
279  and $this->validatePath()
280  and $this->validateQuery()
281  and $this->validateFragment();
282  }
283 
289  public function getUsername()
290  {
291  return strlen($this->_username) > 0 ? $this->_username : false;
292  }
293 
303  public function validateUsername($username = null)
304  {
305  if ($username === null) {
306  $username = $this->_username;
307  }
308 
309  // If the username is empty, then it is considered valid
310  if (strlen($username) === 0) {
311  return true;
312  }
313 
314  // Check the username against the allowed values
315  $status = @preg_match('/^(?:' . $this->_regex['escaped'] . '|[' .
316  self::CHAR_ALNUM . self::CHAR_MARK . ';:&=+$,' . '])+$/', $username);
317 
318  if ($status === false) {
319  #require_once 'Zend/Uri/Exception.php';
320  throw new Zend_Uri_Exception('Internal error: username validation failed');
321  }
322 
323  return $status === 1;
324  }
325 
333  public function setUsername($username)
334  {
335  if ($this->validateUsername($username) === false) {
336  #require_once 'Zend/Uri/Exception.php';
337  throw new Zend_Uri_Exception("Username \"$username\" is not a valid HTTP username");
338  }
339 
340  $oldUsername = $this->_username;
341  $this->_username = $username;
342 
343  return $oldUsername;
344  }
345 
351  public function getPassword()
352  {
353  return strlen($this->_password) > 0 ? $this->_password : false;
354  }
355 
365  public function validatePassword($password = null)
366  {
367  if ($password === null) {
368  $password = $this->_password;
369  }
370 
371  // If the password is empty, then it is considered valid
372  if (strlen($password) === 0) {
373  return true;
374  }
375 
376  // If the password is nonempty, but there is no username, then it is considered invalid
377  if (strlen($password) > 0 and strlen($this->_username) === 0) {
378  return false;
379  }
380 
381  // Check the password against the allowed values
382  $status = @preg_match('/^(?:' . $this->_regex['escaped'] . '|[' .
383  self::CHAR_ALNUM . self::CHAR_MARK . ';:&=+$,' . '])+$/', $password);
384 
385  if ($status === false) {
386  #require_once 'Zend/Uri/Exception.php';
387  throw new Zend_Uri_Exception('Internal error: password validation failed.');
388  }
389 
390  return $status == 1;
391  }
392 
400  public function setPassword($password)
401  {
402  if ($this->validatePassword($password) === false) {
403  #require_once 'Zend/Uri/Exception.php';
404  throw new Zend_Uri_Exception("Password \"$password\" is not a valid HTTP password.");
405  }
406 
407  $oldPassword = $this->_password;
408  $this->_password = $password;
409 
410  return $oldPassword;
411  }
412 
418  public function getHost()
419  {
420  return strlen($this->_host) > 0 ? $this->_host : false;
421  }
422 
431  public function validateHost($host = null)
432  {
433  if ($host === null) {
434  $host = $this->_host;
435  }
436 
437  // If the host is empty, then it is considered invalid
438  if (strlen($host) === 0) {
439  return false;
440  }
441 
442  // Check the host against the allowed values; delegated to Zend_Filter.
444 
445  return $validate->isValid($host);
446  }
447 
455  public function setHost($host)
456  {
457  if ($this->validateHost($host) === false) {
458  #require_once 'Zend/Uri/Exception.php';
459  throw new Zend_Uri_Exception("Host \"$host\" is not a valid HTTP host");
460  }
461 
462  $oldHost = $this->_host;
463  $this->_host = $host;
464 
465  return $oldHost;
466  }
467 
473  public function getPort()
474  {
475  return strlen($this->_port) > 0 ? $this->_port : false;
476  }
477 
485  public function validatePort($port = null)
486  {
487  if ($port === null) {
488  $port = $this->_port;
489  }
490 
491  // If the port is empty, then it is considered valid
492  if (strlen($port) === 0) {
493  return true;
494  }
495 
496  // Check the port against the allowed values
497  return ctype_digit((string) $port) and 1 <= $port and $port <= 65535;
498  }
499 
507  public function setPort($port)
508  {
509  if ($this->validatePort($port) === false) {
510  #require_once 'Zend/Uri/Exception.php';
511  throw new Zend_Uri_Exception("Port \"$port\" is not a valid HTTP port.");
512  }
513 
514  $oldPort = $this->_port;
515  $this->_port = $port;
516 
517  return $oldPort;
518  }
519 
525  public function getPath()
526  {
527  return strlen($this->_path) > 0 ? $this->_path : '/';
528  }
529 
538  public function validatePath($path = null)
539  {
540  if ($path === null) {
541  $path = $this->_path;
542  }
543 
544  // If the path is empty, then it is considered valid
545  if (strlen($path) === 0) {
546  return true;
547  }
548 
549  // Determine whether the path is well-formed
550  $pattern = '/^' . $this->_regex['path'] . '$/';
551  $status = @preg_match($pattern, $path);
552  if ($status === false) {
553  #require_once 'Zend/Uri/Exception.php';
554  throw new Zend_Uri_Exception('Internal error: path validation failed');
555  }
556 
557  return (boolean) $status;
558  }
559 
567  public function setPath($path)
568  {
569  if ($this->validatePath($path) === false) {
570  #require_once 'Zend/Uri/Exception.php';
571  throw new Zend_Uri_Exception("Path \"$path\" is not a valid HTTP path");
572  }
573 
574  $oldPath = $this->_path;
575  $this->_path = $path;
576 
577  return $oldPath;
578  }
579 
585  public function getQuery()
586  {
587  return strlen($this->_query) > 0 ? $this->_query : false;
588  }
589 
597  public function getQueryAsArray()
598  {
599  $query = $this->getQuery();
600  $querryArray = array();
601  if ($query !== false) {
602  parse_str($query, $querryArray);
603  }
604  return $querryArray;
605  }
606 
616  public function validateQuery($query = null)
617  {
618  if ($query === null) {
619  $query = $this->_query;
620  }
621 
622  // If query is empty, it is considered to be valid
623  if (strlen($query) === 0) {
624  return true;
625  }
626 
627  // Determine whether the query is well-formed
628  $pattern = '/^' . $this->_regex['uric'] . '*$/';
629  $status = @preg_match($pattern, $query);
630  if ($status === false) {
631  #require_once 'Zend/Uri/Exception.php';
632  throw new Zend_Uri_Exception('Internal error: query validation failed');
633  }
634 
635  return $status == 1;
636  }
637 
645  public function addReplaceQueryParameters(array $queryParams)
646  {
647  $queryParams = array_merge($this->getQueryAsArray(), $queryParams);
648  return $this->setQuery($queryParams);
649  }
650 
658  public function removeQueryParameters(array $queryParamKeys)
659  {
660  $queryParams = array_diff_key($this->getQueryAsArray(), array_fill_keys($queryParamKeys, 0));
661  return $this->setQuery($queryParams);
662  }
663 
672  public function setQuery($query)
673  {
674  $oldQuery = $this->_query;
675 
676  // If query is empty, set an empty string
677  if (empty($query) === true) {
678  $this->_query = '';
679  return $oldQuery;
680  }
681 
682  // If query is an array, make a string out of it
683  if (is_array($query) === true) {
684  $query = http_build_query($query, '', '&');
685  } else {
686  // If it is a string, make sure it is valid. If not parse and encode it
687  $query = (string) $query;
688  if ($this->validateQuery($query) === false) {
689  parse_str($query, $queryArray);
690  $query = http_build_query($queryArray, '', '&');
691  }
692  }
693 
694  // Make sure the query is valid, and set it
695  if ($this->validateQuery($query) === false) {
696  #require_once 'Zend/Uri/Exception.php';
697  throw new Zend_Uri_Exception("'$query' is not a valid query string");
698  }
699 
700  $this->_query = $query;
701 
702  return $oldQuery;
703  }
704 
710  public function getFragment()
711  {
712  return strlen($this->_fragment) > 0 ? $this->_fragment : false;
713  }
714 
724  public function validateFragment($fragment = null)
725  {
726  if ($fragment === null) {
727  $fragment = $this->_fragment;
728  }
729 
730  // If fragment is empty, it is considered to be valid
731  if (strlen($fragment) === 0) {
732  return true;
733  }
734 
735  // Determine whether the fragment is well-formed
736  $pattern = '/^' . $this->_regex['uric'] . '*$/';
737  $status = @preg_match($pattern, $fragment);
738  if ($status === false) {
739  #require_once 'Zend/Uri/Exception.php';
740  throw new Zend_Uri_Exception('Internal error: fragment validation failed');
741  }
742 
743  return (boolean) $status;
744  }
745 
753  public function setFragment($fragment)
754  {
755  if ($this->validateFragment($fragment) === false) {
756  #require_once 'Zend/Uri/Exception.php';
757  throw new Zend_Uri_Exception("Fragment \"$fragment\" is not a valid HTTP fragment");
758  }
759 
760  $oldFragment = $this->_fragment;
761  $this->_fragment = $fragment;
762 
763  return $oldFragment;
764  }
765 }
$pattern
Definition: website.php:22
__construct($scheme, $schemeSpecific='')
$status
Definition: order_status.php:8
else function()