Пример #1
0
 public static function OnBeforeProlog()
 {
     if (defined("BX_CHECK_SHORT_URI") && BX_CHECK_SHORT_URI) {
         $upload_dir = "/" . trim(COption::GetOptionString("main", "upload_dir", "upload"), "/") . "/";
         $request_uri = urldecode($_SERVER["REQUEST_URI"]);
         $request_uri = CCloudUtil::URLEncode($request_uri, LANG_CHARSET);
         foreach (CCloudStorageBucket::GetAllBuckets() as $arBucket) {
             if ($arBucket["ACTIVE"] == "Y") {
                 $obBucket = new CCloudStorageBucket($arBucket["ID"]);
                 if ($obBucket->Init()) {
                     $match = array();
                     if ($obBucket->FileExists($request_uri)) {
                         if (COption::GetOptionString("clouds", "log_404_errors") === "Y") {
                             CEventLog::Log("WARNING", "CLOUDS_404", "clouds", $_SERVER["REQUEST_URI"], $_SERVER["HTTP_REFERER"]);
                         }
                         LocalRedirect($obBucket->GetFileSRC($request_uri), true);
                     } elseif (strpos($request_uri, $upload_dir) === 0) {
                         $check_url = substr($request_uri, strlen($upload_dir) - 1);
                         if ($obBucket->FileExists($check_url)) {
                             if (COption::GetOptionString("clouds", "log_404_errors") === "Y") {
                                 CEventLog::Log("WARNING", "CLOUDS_404", "clouds", $_SERVER["REQUEST_URI"], $_SERVER["HTTP_REFERER"]);
                             }
                             LocalRedirect($obBucket->GetFileSRC($check_url), true);
                         }
                     } elseif (COption::GetOptionString("clouds", "delayed_resize") === "Y" && preg_match("#^(/" . $obBucket->PREFIX . "|)(/resize_cache/.*\$)#", $request_uri, $match)) {
                         session_write_close();
                         $to_file = $obBucket->GetFileSRC(urldecode($match[2]));
                         if (CCloudStorage::ResizeImageFileCheck($obBucket, $to_file)) {
                             LocalRedirect($to_file, true);
                         }
                     }
                 }
             }
         }
     }
 }
 function UploadPart($arBucket, &$NS, $data)
 {
     global $APPLICATION;
     $filePath = '/' . trim($NS["filePath"], '/');
     if ($arBucket["PREFIX"]) {
         if (substr($filePath, 0, strlen($arBucket["PREFIX"]) + 2) != "/" . $arBucket["PREFIX"] . "/") {
             $filePath = "/" . $arBucket["PREFIX"] . $filePath;
         }
     }
     $filePathU = CCloudUtil::URLEncode($filePath, "UTF-8");
     $response = $this->SendRequest($arBucket["SETTINGS"]["ACCESS_KEY"], $arBucket["SETTINGS"]["SECRET_KEY"], 'PUT', $arBucket["BUCKET"], $filePathU . '?upload_id=' . urlencode($NS["upload_id"]), '', '', array("Content-Range" => "bytes */" . $NS["fileSize"]));
     $data_len = CUtil::BinStrlen($data);
     $response = $this->SendRequest($arBucket["SETTINGS"]["ACCESS_KEY"], $arBucket["SETTINGS"]["SECRET_KEY"], 'PUT', $arBucket["BUCKET"], $filePathU . '?upload_id=' . urlencode($NS["upload_id"]), '', $data, array("Content-Range" => "bytes " . $NS["filePos"] . "-" . ($NS["filePos"] + $data_len - 1) . "/" . $NS["fileSize"]));
     if ($this->status == 308 && is_array($this->headers) && preg_match("/^bytes=(\\d+)-(\\d+)\$/", $this->headers["Range"], $match)) {
         $APPLICATION->ResetException();
         $NS["filePos"] = $match[2] + 1;
         return true;
     } elseif ($this->status == 200) {
         return true;
     } else {
         return false;
     }
 }
	function GetFileSRC($arBucket, $arFile)
	{
		global $APPLICATION;
		$proto = $APPLICATION->IsHTTPS()? "https": "http";

		if($arBucket["CNAME"])
		{
			$host = $proto."://".$arBucket["CNAME"];
		}
		else
		{
			$result = false;
			$cache_id = md5(serialize($arBucket));
			$obCache = new CPHPCache;
			if($obCache->InitCache(3600, $cache_id, "/"))
			{
				$result = $obCache->GetVars();
			}
			else
			{
				$obCDNRequest = $this->SendCDNRequest(
					$arBucket["SETTINGS"],
					"HEAD",
					$arBucket["BUCKET"]
				);
				if(is_object($obCDNRequest))
				{
					if($obCDNRequest->status == 204)
					{
						$result = array();
						foreach($obCDNRequest->headers as $key => $value)
							$result[strtolower($key)] = $value;
					}
				}
			}

			if($obCache->StartDataCache())
				$obCache->EndDataCache($result);

			if(is_array($result))
				$host = $result["x-cdn-uri"];
			else
				return "/404.php";
		}

		if(is_array($arFile))
			$URI = ltrim($arFile["SUBDIR"]."/".$arFile["FILE_NAME"], "/");
		else
			$URI = ltrim($arFile, "/");

		if($arBucket["PREFIX"])
		{
			if(substr($URI, 0, strlen($arBucket["PREFIX"])+1) !== $arBucket["PREFIX"]."/")
				$URI = $arBucket["PREFIX"]."/".$URI;
		}

		return $host."/".CCloudUtil::URLEncode($URI, "UTF-8");
	}
	public function CompleteMultipartUpload($arBucket, &$NS)
	{
		global $APPLICATION;

		$obRequest = $this->SendRequest(
			$arBucket["SETTINGS"],
			"PUT",
			$arBucket["BUCKET"],
			$NS["fileTemp"],
			"",
			false,
			array(
				"Content-Type" => $NS["Content-Type"],
				"X-Object-Manifest" => $arBucket["BUCKET"].$NS["fileTemp"]."/",
			)
		);

		if(is_object($obRequest) && $this->status == 201)
		{
			$obRequest = $this->SendRequest(
				$arBucket["SETTINGS"],
				"PUT",
				$arBucket["BUCKET"],
				CCloudUtil::URLEncode($NS["filePath"], "UTF-8"),
				'',
				false,
				array(
					"Content-Type" => $NS["Content-Type"],
					"X-Copy-From" => "/".$arBucket["BUCKET"].$NS["fileTemp"],
				)
			);

			if(is_object($obRequest) && $this->status == 201)
				$result = true;
			else
				$result = false;

			$this->DeleteFile($arBucket, $NS["fileTemp"]);
			for($part = $NS["partsCount"]; $part > 0; $part--)
				$this->DeleteFile($arBucket, $NS["fileTemp"]."/".sprintf("%06d", $part));

			return $result;
		}
		else
		{
			//May be delete uploaded tmp file?
			return false;
		}
	}
Пример #5
0
	function OnBeforeProlog()
	{
		if(defined("BX_CHECK_SHORT_URI") && BX_CHECK_SHORT_URI)
		{
			$upload_dir = "/".trim(COption::GetOptionString("main", "upload_dir", "upload"), "/")."/";
			$request_uri = urldecode($_SERVER["REQUEST_URI"]);
			$request_uri = CCloudUtil::URLEncode($request_uri, LANG_CHARSET);
			foreach(CCloudStorageBucket::GetAllBuckets() as $arBucket)
			{
				if($arBucket["ACTIVE"] == "Y")
				{
					$obBucket = new CCloudStorageBucket($arBucket["ID"]);
					if($obBucket->Init())
					{
						$match = array();
						if(
							COption::GetOptionString("clouds", "delayed_resize") === "Y"
							&& preg_match("#^(/".$obBucket->PREFIX."|)(/resize_cache/.*\$)#", $request_uri, $match)
						)
						{
							session_write_close();
							$to_file = $obBucket->GetFileSRC(urldecode($match[2]));
							if (CCloudStorage::ResizeImageFileCheck($obBucket, $to_file))
							{
								$cache_time = 3600*24*30; // 30 days
								header("Cache-Control: max-age=".$cache_time);
								header("Expires: ".gmdate("D, d M Y H:i:s", time()+$cache_time)." GMT");
								header_remove("Pragma");
								LocalRedirect($to_file, true, "301 Moved Permanently");
							}
						}
						elseif($obBucket->FileExists($request_uri))
						{
							if(COption::GetOptionString("clouds", "log_404_errors") === "Y")
								CEventLog::Log("WARNING", "CLOUDS_404", "clouds", $_SERVER["REQUEST_URI"], $_SERVER["HTTP_REFERER"]);
							LocalRedirect($obBucket->GetFileSRC($request_uri), true);
						}
						elseif(strpos($request_uri, $upload_dir) === 0)
						{
							$check_url = substr($request_uri, strlen($upload_dir)-1);
							if($obBucket->FileExists($check_url))
							{
								if(COption::GetOptionString("clouds", "log_404_errors") === "Y")
									CEventLog::Log("WARNING", "CLOUDS_404", "clouds", $_SERVER["REQUEST_URI"], $_SERVER["HTTP_REFERER"]);
								LocalRedirect($obBucket->GetFileSRC($check_url), true);
							}
						}
					}
				}
			}
		}
	}
Пример #6
0
 public function CompleteMultipartUpload($arBucket, &$NS)
 {
     $filePath = '/' . trim($NS["filePath"], '/');
     if ($arBucket["PREFIX"]) {
         if (substr($filePath, 0, strlen($arBucket["PREFIX"]) + 2) != "/" . $arBucket["PREFIX"] . "/") {
             $filePath = "/" . $arBucket["PREFIX"] . $filePath;
         }
     }
     $filePath = CCloudUtil::URLEncode($filePath, "UTF-8");
     $data = "";
     foreach ($NS["Parts"] as $PartNumber => $ETag) {
         $data .= "<Part><PartNumber>" . ($PartNumber + 1) . "</PartNumber><ETag>" . $ETag . "</ETag></Part>\n";
     }
     $this->SetLocation($arBucket["LOCATION"]);
     $response = $this->SendRequest($arBucket["SETTINGS"], 'POST', $arBucket["BUCKET"], $filePath . '?uploadId=' . urlencode($NS["UploadId"]), '', "<CompleteMultipartUpload>" . $data . "</CompleteMultipartUpload>");
     return $this->status == 200;
 }
Пример #7
0
 function OnBeforeProlog()
 {
     if (defined("BX_CHECK_SHORT_URI") && BX_CHECK_SHORT_URI) {
         $upload_dir = "/" . trim(COption::GetOptionString("main", "upload_dir", "upload"), "/") . "/";
         $request_uri = urldecode($_SERVER["REQUEST_URI"]);
         $request_uri = CCloudUtil::URLEncode($request_uri, LANG_CHARSET);
         foreach (CCloudStorageBucket::GetAllBuckets() as $arBucket) {
             if ($arBucket["ACTIVE"] == "Y") {
                 $obBucket = new CCloudStorageBucket($arBucket["ID"]);
                 if ($obBucket->Init()) {
                     if ($obBucket->FileExists($request_uri)) {
                         if (COption::GetOptionString("clouds", "log_404_errors") === "Y") {
                             CEventLog::Log("WARNING", "CLOUDS_404", "clouds", $_SERVER["REQUEST_URI"], $_SERVER["HTTP_REFERER"]);
                         }
                         LocalRedirect($obBucket->GetFileSRC($request_uri), true);
                     } elseif (strpos($request_uri, $upload_dir) === 0) {
                         $check_url = substr($request_uri, strlen($upload_dir) - 1);
                         if ($obBucket->FileExists($check_url)) {
                             if (COption::GetOptionString("clouds", "log_404_errors") === "Y") {
                                 CEventLog::Log("WARNING", "CLOUDS_404", "clouds", $_SERVER["REQUEST_URI"], $_SERVER["HTTP_REFERER"]);
                             }
                             LocalRedirect($obBucket->GetFileSRC($check_url), true);
                         }
                     }
                 }
             }
         }
     }
 }