[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/ozh/http_build_url/src/ -> http_build_url.php (source)

   1  <?php
   2  
   3  /**
   4   * URL constants as defined in the PHP Manual under "Constants usable with
   5   * http_build_url()".
   6   *
   7   * @see https://www.php.net/manual/en/http.constants.php#http.constants.url
   8   */
   9  if (!defined('HTTP_URL_REPLACE')) {
  10      define('HTTP_URL_REPLACE', 1);
  11  }
  12  if (!defined('HTTP_URL_JOIN_PATH')) {
  13      define('HTTP_URL_JOIN_PATH', 2);
  14  }
  15  if (!defined('HTTP_URL_JOIN_QUERY')) {
  16      define('HTTP_URL_JOIN_QUERY', 4);
  17  }
  18  if (!defined('HTTP_URL_STRIP_USER')) {
  19      define('HTTP_URL_STRIP_USER', 8);
  20  }
  21  if (!defined('HTTP_URL_STRIP_PASS')) {
  22      define('HTTP_URL_STRIP_PASS', 16);
  23  }
  24  if (!defined('HTTP_URL_STRIP_AUTH')) {
  25      define('HTTP_URL_STRIP_AUTH', 32);
  26  }
  27  if (!defined('HTTP_URL_STRIP_PORT')) {
  28      define('HTTP_URL_STRIP_PORT', 64);
  29  }
  30  if (!defined('HTTP_URL_STRIP_PATH')) {
  31      define('HTTP_URL_STRIP_PATH', 128);
  32  }
  33  if (!defined('HTTP_URL_STRIP_QUERY')) {
  34      define('HTTP_URL_STRIP_QUERY', 256);
  35  }
  36  if (!defined('HTTP_URL_STRIP_FRAGMENT')) {
  37      define('HTTP_URL_STRIP_FRAGMENT', 512);
  38  }
  39  if (!defined('HTTP_URL_STRIP_ALL')) {
  40      define('HTTP_URL_STRIP_ALL', 1024);
  41  }
  42  
  43  if (!function_exists('http_build_url')) {
  44  
  45      /**
  46       * Build a URL.
  47       *
  48       * The parts of the second URL will be merged into the first according to
  49       * the flags argument.
  50       *
  51       * @param string|array $url     (part(s) of) a URL in form of a string or
  52       *                              associative array like parse_url() returns
  53       * @param string|array|null $parts   same as the first argument
  54       * @param int          $flags   a bitmask of binary or'ed HTTP_URL constants;
  55       *                              HTTP_URL_REPLACE is the default
  56       * @param array|null   $new_url if set, it will be filled with the parts of the
  57       *                              composed url like parse_url() would return
  58       */
  59  	function http_build_url(
  60          string|array $url,
  61          string|array|null $parts = [],
  62          int $flags = HTTP_URL_REPLACE,
  63          ?array &$new_url = null
  64      ): string {
  65          $url   = is_array($url) ? $url : (parse_url($url) ?: []);
  66          $parts = is_array($parts) ? $parts : (parse_url((string) $parts) ?: []);
  67  
  68          isset($url['query']) && is_string($url['query']) || $url['query'] = null;
  69          isset($parts['query']) && is_string($parts['query']) || $parts['query'] = null;
  70  
  71          $keys = ['user', 'pass', 'port', 'path', 'query', 'fragment'];
  72  
  73          // HTTP_URL_STRIP_ALL and HTTP_URL_STRIP_AUTH cover several other flags.
  74          if ($flags & HTTP_URL_STRIP_ALL) {
  75              $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS
  76                  | HTTP_URL_STRIP_PORT | HTTP_URL_STRIP_PATH
  77                  | HTTP_URL_STRIP_QUERY | HTTP_URL_STRIP_FRAGMENT;
  78          } elseif ($flags & HTTP_URL_STRIP_AUTH) {
  79              $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS;
  80          }
  81  
  82          // Scheme and host are always replaced
  83          foreach (['scheme', 'host'] as $part) {
  84              if (isset($parts[$part])) {
  85                  $url[$part] = $parts[$part];
  86              }
  87          }
  88  
  89          if ($flags & HTTP_URL_REPLACE) {
  90              foreach ($keys as $key) {
  91                  if (isset($parts[$key])) {
  92                      $url[$key] = $parts[$key];
  93                  }
  94              }
  95          } else {
  96              if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) {
  97                  if (isset($url['path']) && !str_starts_with($parts['path'], '/')) {
  98                      // Workaround for trailing slashes
  99                      $url['path'] .= "\0";
 100                      $url['path'] = rtrim(
 101                              str_replace(basename($url['path']), '', $url['path']),
 102                              '/'
 103                          ) . '/' . ltrim($parts['path'], '/');
 104                  } else {
 105                      $url['path'] = $parts['path'];
 106                  }
 107              }
 108  
 109              if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
 110                  if (isset($url['query'])) {
 111                      parse_str($url['query'], $url_query);
 112                      parse_str($parts['query'], $parts_query);
 113  
 114                      $url['query'] = http_build_query(
 115                          array_replace_recursive(
 116                              $url_query,
 117                              $parts_query
 118                          )
 119                      );
 120                  } else {
 121                      $url['query'] = $parts['query'];
 122                  }
 123              }
 124          }
 125  
 126          if (isset($url['path']) && $url['path'] !== '' && !str_starts_with($url['path'], '/')) {
 127              $url['path'] = '/' . $url['path'];
 128          }
 129  
 130          foreach ($keys as $key) {
 131              if ($flags & constant('HTTP_URL_STRIP_' . strtoupper($key))) {
 132                  unset($url[$key]);
 133              }
 134          }
 135  
 136          $parsed_string = '';
 137  
 138          if (!empty($url['scheme'])) {
 139              $parsed_string .= $url['scheme'] . '://';
 140          }
 141  
 142          if (!empty($url['user'])) {
 143              $parsed_string .= $url['user'];
 144  
 145              if (isset($url['pass'])) {
 146                  $parsed_string .= ':' . $url['pass'];
 147              }
 148  
 149              $parsed_string .= '@';
 150          }
 151  
 152          if (!empty($url['host'])) {
 153              $parsed_string .= $url['host'];
 154          }
 155  
 156          if (!empty($url['port'])) {
 157              $parsed_string .= ':' . $url['port'];
 158          }
 159  
 160          if (!empty($url['path'])) {
 161              $parsed_string .= $url['path'];
 162          }
 163  
 164          if (!empty($url['query'])) {
 165              $parsed_string .= '?' . $url['query'];
 166          }
 167  
 168          if (!empty($url['fragment'])) {
 169              $parsed_string .= '#' . $url['fragment'];
 170          }
 171  
 172          $new_url = $url;
 173  
 174          return $parsed_string;
 175      }
 176  }


Generated: Wed Sep 30 05:10:25 2026 Cross-referenced by PHPXref 0.7.1