/** * Takes a path and returns an absolute path. * * This method is implemented in the way that browsers work, see * https://url.spec.whatwg.org/#relative-state for more information about the * possible cases. * * @param string $path * A path from the internal browser content. * * @return string * The $path with $base_url prepended, if necessary. */ protected function getAbsoluteUrl($path) { global $base_url, $base_path; $parts = parse_url($path); // In case the $path has a host, it is already an absolute URL and we are // done. if (!empty($parts['host'])) { return $path; } // In case the $path contains just a query, we turn it into an absolute URL // with the same scheme, host and path, see // https://url.spec.whatwg.org/#relative-state. if (array_keys($parts) === ['query']) { $current_uri = new Uri($this->getUrl()); return (string) $current_uri->withQuery($parts['query']); } if (empty($parts['host'])) { // Ensure that we have a string (and no xpath object). $path = (string) $path; // Strip $base_path, if existent. $length = strlen($base_path); if (substr($path, 0, $length) === $base_path) { $path = substr($path, $length); } // Ensure that we have an absolute path. if (empty($path) || $path[0] !== '/') { $path = '/' . $path; } // Finally, prepend the $base_url. $path = $base_url . $path; } return $path; }