/** * Converts relative url to the absolute, considering document's url. * @param string $uri Relative url. * @return null|string Absolute url or null if relative url contains errors. */ protected function convertRelativeUriToAbsolute($uri) { if (strpos($uri, '//') === 0) { $uri = $this->uri->getScheme() . ":" . $uri; } if (preg_match('#^https?://#', $uri)) { return $uri; } $pars = parse_url($uri); if ($pars === false) { return null; } if (isset($pars['host'])) { $result = $uri; } else { if (isset($pars['path'])) { if (substr($pars['path'], 0, 1) !== '/') { $pathPrefix = preg_replace('/^(.+?)([^\\/]*)$/', '$1', $this->uri->getPath()); $pars['path'] = $pathPrefix . $pars['path']; } $uriPort = ''; if ($this->uri->getScheme() === 'http' && $this->uri->getPort() != '80' || $this->uri->getScheme() === 'https' && $this->uri->getPort() != '443') { $uriPort = ':' . $this->uri->getPort(); } $result = $this->uri->getScheme() . '://' . $this->uri->getHost() . $uriPort . $pars['path'] . (isset($pars['query']) ? '?' . $pars['query'] : '') . (isset($pars['fragment']) ? '#' . $pars['fragment'] : ''); } else { $result = null; } } return $result; }
/** * Returns script filename by URL * * @param string $site Site ID. * @param string $url URL. * @return string|null */ public static function getRealPath($site, $url) { $docRoot = rtrim(\Bitrix\Main\SiteTable::getDocumentRoot($site), '/'); $url = str_replace('\\', '/', $url); $url = \CHTTP::urnEncode($url); $uri = new Web\Uri($url); $path = \CHTTP::urnDecode($uri->getPath()); if (substr($path, -1, 1) == '/') { $path .= 'index.php'; } $file = new IO\File($docRoot . $path); if ($file->isExists()) { return substr($file->getPath(), strlen($docRoot)); } if ($rewriteRules = AdminHelper::getRewriteRules($site)) { $pathQuery = \CHTTP::urnDecode($uri->getPathQuery()); foreach ($rewriteRules as &$item) { if (preg_match($item['CONDITION'], $pathQuery)) { $url = empty($item['PATH']) && !empty($item['RULE']) ? preg_replace($item['CONDITION'], $item['RULE'], $pathQuery) : $item['PATH']; $url = \CHTTP::urnEncode($url); $uri = new Web\Uri($url); $path = \CHTTP::urnDecode($uri->getPath()); $file = new IO\File($docRoot . $path); if ($file->isExists()) { $pathTmp = str_replace('.', '', strtolower(ltrim($path, '/\\'))); $pathTmp7 = substr($pathTmp, 0, 7); if ($pathTmp7 == 'upload/' || $pathTmp7 == 'bitrix/') { continue; } if ($file->getExtension() != 'php') { continue; } return substr($file->getPath(), strlen($docRoot)); } } } } return null; }
/** * Returns handler for the url * * @param Uri $uri Absolute or relative URL. * @return array|false Handler for this URL if found, false otherwise. */ public static function dispatch(Uri $uri) { static::init(); $urlPath = $uri->getPath(); //todo: replace cycle with compiled regexp for all routes foreach (static::$routeTable as $routeRecord) { if (preg_match($routeRecord['REGEXP'], $urlPath, $matches)) { $result = $routeRecord; //replace parameters variables with values foreach ($result['PARAMETERS'] as $parameterName => &$parameterValue) { if (strpos($parameterValue, '$') === 0) { $variableName = substr($parameterValue, 1); if (isset($matches[$variableName])) { $parameterValue = $matches[$variableName]; } } } return $result; } } return false; }
/** * Checks file params * @param $file * @param $arFile * @return mixed|null|string */ protected function checkFile($file, &$arFile) { $status = new Status("checked"); if ($file["error"] > 0) { $status = new Error("BXU347.2", $file["error"]); } else { if (array_key_exists("tmp_url", $file)) { $url = new Uri($file["tmp_url"]); if ($url->getHost() == '' && ($tmp = \CFile::MakeFileArray($url->getPath())) && is_array($tmp)) { $file = array_merge($tmp, $file); } else { if ($url->getHost() != '' && $this->http->query("HEAD", $file["tmp_url"]) && $this->http->getStatus() == "200") { $file = array_merge($file, array("size" => (int) $this->http->getHeaders()->get("content-length"), "type" => $this->http->getHeaders()->get("content-type"))); } else { $status = new Error("BXU347.2"); } } } else { if (!is_uploaded_file($file['tmp_name']) || !file_exists($file['tmp_name'])) { $status = new Error("BXU347.2"); } } } if ($status instanceof Error) { // } elseif ($this->params["allowUpload"] == "I") { $error = \CFile::CheckFile($file, $this->params["uploadMaxFilesize"], "image/", \CFile::GetImageExtensions()); if (!empty($error)) { $status = new Error("BXU347.3", $error); } } elseif ($this->params["allowUpload"] == "F") { $error = \CFile::CheckFile($file, $this->params["uploadMaxFilesize"], false, $this->params["allowUploadExt"]); if (!empty($error)) { $status = new Error("BXU347.3", $error); } } else { $error = \CFile::CheckFile($file, $this->params["uploadMaxFilesize"]); if (!empty($error)) { $status = new Error("BXU347.3", $error); } } if ($status instanceof Status) { $matches = array(); $name = $file["~name"]; if (preg_match("/^(.+?)\\.ch(\\d+)\\.(\\d+)\\.chs(\\d+)\$/", $file["~name"], $matches)) { $name = $matches[1]; } $key = !empty($name) ? $name : 'default'; $file["copy"] = $key; if (empty($matches)) { $arFile["files"][$key] = $file; } else { $fileAddInfo = array("chunks" => array(), "chunksInfo" => array("count" => $matches[4], "uploaded" => array(), "written" => array())); if (array_key_exists($key, $arFile["files"])) { $fileAddInfo = $arFile["files"][$key]; } $file["status"] = "inprogress"; $file["number"] = $matches[2]; $file["start"] = $matches[3]; $fileAddInfo["chunks"][self::getChunkKey($fileAddInfo["chunksInfo"]["count"], $file["number"])] = $file; $arFile["files"][$key] = $fileAddInfo; } } return $status; }
/** * Returns url-decoded and converted to the current encoding URI of the request (except the query string). * * @return string */ public function getDecodedUri() { $parsedUri = new Web\Uri("http://" . $this->server->getHttpHost() . $this->getRequestUri()); $uri = static::decode($parsedUri->getPath()); if (($query = $parsedUri->getQuery()) != '') { $uri .= "?" . $query; } return $uri; }
/** * Clears an URI from navigation parameters and returns it. * @param Web\Uri $uri * @param bool $sef SEF mode. * @return Web\Uri */ public function clearParams(Web\Uri $uri, $sef) { if ($sef == true) { $path = $uri->getPath(); $path = preg_replace("'/" . preg_quote($this->id, "'") . "/page-([\\d]|all)+(/size-([\\d]+))?'", "", $path); $uri->setPath($path); } else { $uri->deleteParams(array($this->id)); } return $uri; }