Beispiel #1
0
 /**
  * @param Uri $uri
  * @return array
  */
 protected static function getParserChain(Uri $uri)
 {
     $result = array();
     if (isset(static::$metadataParsersByHost[$uri->getHost()])) {
         $result[] = static::$metadataParsersByHost[$uri->getHost()];
     }
     $result = array_merge($result, static::$metadataParsers);
     return $result;
 }
Beispiel #2
0
 /**
  * Returns true if document's site is allowed to be embedded.
  * @return bool
  */
 protected function isEmbeddingAllowed()
 {
     $result = false;
     $domainNameParts = explode('.', $this->uri->getHost());
     if (is_array($domainNameParts) && ($partsCount = count($domainNameParts)) >= 2) {
         $domainName = $domainNameParts[$partsCount - 2] . '.' . $domainNameParts[$partsCount - 1];
         $result = in_array($domainName, $this->hostsAllowedToEmbed);
     }
     return $result;
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * 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;
 }
Beispiel #5
0
 protected function initializeSite()
 {
     $context = $this->application->getContext();
     $request = $context->getRequest();
     $currentDirectory = $request->getRequestedPageDirectory();
     $currentHost = "";
     $currentHostScheme = "";
     if ($request instanceof IHttpRequest) {
         /** @var $request \Bitrix\Main\HttpRequest */
         $currentHost = $request->getHttpHost();
         $currentHostScheme = $request->isHttps() ? "https://" : "http://";
     }
     $url = new Web\Uri($currentHostScheme . $currentHost, Web\UriType::ABSOLUTE);
     $currentDomain = $url->parse(Web\UriPart::HOST);
     $currentDomain = trim($currentDomain, "\t\r\n .");
     $connection = Application::getDbConnection();
     $helper = $connection->getSqlHelper();
     $sql = "\n\t\t\tSELECT L.*, L.LID as ID, L.LID as SITE_ID\n\t\t\tFROM b_lang L\n\t\t\t\tLEFT JOIN b_lang_domain LD ON L.LID=LD.LID AND '" . $helper->forSql($currentDomain, 255) . "' LIKE CONCAT('%', LD.DOMAIN)\n\t\t\tWHERE ('" . $helper->forSql($currentDirectory) . "' LIKE CONCAT(L.DIR, '%') OR LD.LID IS NOT NULL)\n\t\t\t\tAND L.ACTIVE='Y'\n\t\t\tORDER BY\n\t\t\t\tIF((L.DOMAIN_LIMITED='Y' AND LD.LID IS NOT NULL) OR L.DOMAIN_LIMITED<>'Y',\n\t\t\t\t\tIF('" . $helper->forSql($currentDomain) . "' LIKE CONCAT(L.DIR, '%'), 3, 1),\n\t\t\t\t\tIF('" . $helper->forSql($currentDirectory) . "' LIKE CONCAT(L.DIR, '%'), 2, 0)\n\t\t\t\t) DESC,\n\t\t\t\tLENGTH(L.DIR) DESC,\n\t\t\t\tL.DOMAIN_LIMITED DESC,\n\t\t\t\tSORT,\n\t\t\t\tLENGTH(LD.DOMAIN) DESC\n\t\t";
     //get site by path and domain
     $siteList = $connection->query($sql);
     $site = $siteList->fetch();
     //get site by default sorting
     if ($site === false) {
         $sql = "\n\t\t\t\tSELECT L.*, L.LID as ID, L.LID as SITE_ID\n\t\t\t\tFROM b_lang L\n\t\t\t\tWHERE L.ACTIVE='Y'\n\t\t\t\tORDER BY L.DEF DESC, L.SORT\n\t\t\t";
         $siteList = $connection->query($sql);
         $site = $siteList->fetch();
     }
     if ($site !== false) {
         $culture = Context\Culture::wakeUp($site["CULTURE_ID"]);
         if ($culture === null) {
             $culture = new Context\Culture();
         }
         $this->site = new Context\Site($site);
         $this->site->setCulture($culture);
     } else {
         throw new SystemException("Site not found.");
     }
 }
Beispiel #6
0
 /**
  * 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;
 }
Beispiel #7
0
 protected function sendRequest($method, Uri $url, $postData = null)
 {
     $this->status = 0;
     $this->result = '';
     $this->responseHeaders->clear();
     $this->responseCookies->clear();
     if ($this->proxyHost != '') {
         $path = $url->getUrl();
         if ($this->proxyUser != '') {
             $this->setHeader("Proxy-Authorization", "Basic " . base64_encode($this->proxyUser . ":" . $this->proxyPassword));
         }
     } else {
         $path = $url->getPathQuery();
     }
     $request = $method . " " . $path . " HTTP/" . $this->version . "\r\n";
     $this->setHeader("Host", $url->getHost());
     $this->setHeader("Connection", "close", false);
     $this->setHeader("Accept", "*/*", false);
     $this->setHeader("Accept-Language", "en", false);
     if (($user = $url->getUser()) != '') {
         $this->setAuthorization($user, $url->getPass());
     }
     $cookies = $this->requestCookies->toString();
     if ($cookies != '') {
         $this->setHeader("Cookie", $cookies);
     }
     if ($this->compress) {
         $this->setHeader("Accept-Encoding", "gzip");
     }
     if (!is_resource($postData) && ($method == self::HTTP_POST || $method == self::HTTP_PUT)) {
         if ($method != self::HTTP_PUT && $this->requestHeaders->get("Content-Type") === null) {
             $contentType = "application/x-www-form-urlencoded";
             if ($this->requestCharset != '') {
                 $contentType .= "; charset=" . $this->requestCharset;
             }
             $this->setHeader("Content-Type", $contentType);
         }
         if ($this->requestHeaders->get("Content-Length") === null) {
             $this->setHeader("Content-Length", String::getBinaryLength($postData));
         }
     }
     $request .= $this->requestHeaders->toString();
     $request .= "\r\n";
     $this->send($request);
     if ($method == self::HTTP_POST || $method == self::HTTP_PUT) {
         if (is_resource($postData)) {
             //PUT data can be file resource
             while (!feof($postData)) {
                 $this->send(fread($postData, self::BUF_POST_LEN));
             }
         } else {
             $this->send($postData);
         }
     }
 }
Beispiel #8
0
 /**
  * Returns the host from the server variable without a port number.
  * @return string
  */
 public function getHttpHost()
 {
     static $host = null;
     if ($host === null) {
         //scheme can be anything, it's used only for parsing
         $url = new Web\Uri("http://" . $this->server->getHttpHost());
         $host = $url->getHost();
         $host = trim($host, "\t\r\n .");
     }
     return $host;
 }
 public function getHttpHost($raw = true)
 {
     if ($raw) {
         return $this->server->getHttpHost();
     }
     static $host = null;
     if ($host === null) {
         $host = $this->server->getHttpHost();
         $hostScheme = $this->isHttps() ? "https://" : "http://";
         $url = new Web\Uri($hostScheme . $host, Web\UriType::ABSOLUTE);
         $host = $url->parse(Web\UriPart::HOST);
         $host = trim($host, "\t\r\n .");
     }
     return $host;
 }
Beispiel #10
0
 public function getHttpHost($raw = true)
 {
     if ($raw) {
         return $this->server->getHttpHost();
     }
     static $host = null;
     if ($host === null) {
         //scheme can be anything, it's used only for parsing
         $scheme = "http://";
         $host = $this->server->getHttpHost();
         $url = new Web\Uri($scheme . $host);
         $host = $url->getHost();
         $host = trim($host, "\t\r\n .");
     }
     return $host;
 }
Beispiel #11
0
 public function getRequestedPage()
 {
     if ($this->requestedFile != null) {
         return $this->requestedFile;
     }
     $page = $this->getRequestUri();
     if ($page == "") {
         return $this->requestedFile = parent::getRequestedPage();
     }
     $page = urldecode($page);
     $page = Text\Encoding::convertEncodingToCurrent($page);
     $uri = new Web\Uri($page, Web\UriType::RELATIVE);
     return $this->requestedFile = $uri->convertToPath();
 }
Beispiel #12
0
 /**
  * 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;
 }
Beispiel #13
0
 private function fixUpRequestUriAndQueryString()
 {
     /** @var $context HttpContext */
     $context = $this->context;
     $queryString = $context->getServer()->get("QUERY_STRING");
     $requestUri = $context->getServer()->get("REQUEST_URI");
     $redirectStatus = $context->getServer()->get("REDIRECT_STATUS");
     //try to fix REQUEST_URI under IIS
     $arProtocols = array('http', 'https');
     foreach ($arProtocols as $protocol) {
         $marker = "404;" . $protocol . "://";
         if (($p = strpos($queryString, $marker)) !== false) {
             $uri = $queryString;
             if (($p = strpos($uri, "/", $p + strlen($marker))) !== false) {
                 if ($requestUri == '' || $requestUri == '/404.php' || strpos($requestUri, $marker) !== false) {
                     $requestUriTmp = substr($uri, $p);
                     if (!Uri::isPathTraversalUri($requestUriTmp)) {
                         $requestUri = $requestUriTmp;
                     }
                 }
                 $redirectStatus = '404';
                 $queryString = '';
                 break;
             }
         }
     }
     $requestUri = urldecode($requestUri);
     $requestUri = \Bitrix\Main\Text\Encoding::convertEncodingToCurrent($requestUri);
     $sefApplicationCurPageUrl = $context->getRequest()->get("SEF_APPLICATION_CUR_PAGE_URL");
     if ($redirectStatus == '404' || $sefApplicationCurPageUrl != null) {
         if ($redirectStatus != '404') {
             if (!Uri::isPathTraversalUri($sefApplicationCurPageUrl)) {
                 $requestUri = $sefApplicationCurPageUrl;
             }
         }
         if (($pos = strpos($requestUri, "?")) !== false) {
             $queryString = substr($requestUri, $pos + 1);
         }
     }
     if ($queryString != $context->getServer()->get("QUERY_STRING") || $requestUri != $context->getServer()->get("REQUEST_URI") || $redirectStatus != $context->getServer()->get("REDIRECT_STATUS")) {
         $context->rewriteUri($requestUri, $queryString, $redirectStatus);
     }
 }
Beispiel #14
0
 /**
  * If provided url does not contain scheme part, tries to add it
  *
  * @param string $url URL to be fixed.
  * @return string Fixed URL.
  */
 protected static function normalizeUrl($url)
 {
     if (!preg_match('#^https?://#i', $url)) {
         $url = 'http://' . $url;
     }
     $parsedUrl = new Uri($url);
     $parsedUrl->setHost(strtolower($parsedUrl->getHost()));
     return $parsedUrl->getUri();
 }