• Модуль: clouds
  • Путь к файлу: ~/bitrix/modules/clouds/classes/general/storage_service_openstack.php
  • Класс: CCloudStorageService_OpenStackStorage
  • Вызов: CCloudStorageService_OpenStackStorage::ListFiles
function ListFiles($arBucket, $filePath, $bRecursive = false)
{
	global $APPLICATION;

	$result = array(
		"dir" => array(),
		"file" => array(),
		"file_size" => array(),
		"file_mtime" => array(),
		"file_hash" => array(),
		"last_key" => "",
	);

	$filePath = trim($filePath, '/');
	if($filePath <> '')
	{
		$filePath .= '/';
	}

	if($arBucket["PREFIX"])
	{
		if(mb_substr($filePath, 0, mb_strlen($arBucket["PREFIX"]) + 2) != "/".$arBucket["PREFIX"]."/")
			$filePath = $arBucket["PREFIX"]."/".ltrim($filePath, "/");
	}
	$filePath = $APPLICATION->ConvertCharset($filePath, LANG_CHARSET, "UTF-8");
	$filePath = str_replace(" ", "+", $filePath);

	$marker = '';
	$new_marker = false;
	while(true)
	{
		$this->SendRequest(
			$arBucket["SETTINGS"],
			"GET",
			$arBucket["BUCKET"],
			'/',
			$s='?format=xml&'.($bRecursive? '': '&delimiter=/').'&prefix='.urlencode($filePath).'&marker='.urlencode($marker)
		);
		$bFound = false;
		if($this->result && $this->status == 200)
		{
			$obXML = new CDataXML;
			$text = preg_replace("/<"."\?XML.*?\?".">/i", "", $this->result);
			if($obXML->LoadString($text))
			{
				$arXML = $obXML->GetArray();
				if(
					isset($arXML["container"])
					&& is_array($arXML["container"])
					&& isset($arXML["container"]["#"])
					&& is_array($arXML["container"]["#"])
					&& !empty($arXML["container"]["#"])
				)
				{
					if(
						isset($arXML["container"]["#"]["object"])
						&& is_array($arXML["container"]["#"]["object"])
						&& !empty($arXML["container"]["#"]["object"])
					)
					{
						$bFound = true;
						foreach($arXML["container"]["#"]["object"] as $a)
						{
							$new_marker = $a["#"]["name"][0]["#"];
							if($a["#"]["content_type"][0]["#"] === "application/directory")
							{
								$dir_name = trim(mb_substr($a["#"]["name"][0]["#"], mb_strlen($filePath)), "/");
								$result["dir"][$APPLICATION->ConvertCharset($dir_name, "UTF-8", LANG_CHARSET)] = true;
							}
							else
							{
								$file_name = mb_substr($a["#"]["name"][0]["#"], mb_strlen($filePath));
								$file_name = $APPLICATION->ConvertCharset($file_name, "UTF-8", LANG_CHARSET);
								if (!in_array($file_name, $result["file"]))
								{
									$result["file"][] = $file_name;
									$result["file_size"][] = $a["#"]["bytes"][0]["#"];
									$result["file_mtime"][] = mb_substr($a["#"]["last_modified"][0]["#"], 0, 19);
									$result["file_hash"][] = $a["#"]["hash"][0]["#"];
									$result["last_key"] = $file_name;
								}
							}
						}
					}

					if(
						isset($arXML["container"]["#"]["subdir"])
						&& is_array($arXML["container"]["#"]["subdir"])
						&& !empty($arXML["container"]["#"]["subdir"])
					)
					{
						$bFound = true;
						foreach($arXML["container"]["#"]["subdir"] as $a)
						{
							$new_marker = $a["@"]["name"];
							$dir_name = trim(mb_substr($a["@"]["name"], mb_strlen($filePath)), "/");
							$result["dir"][$APPLICATION->ConvertCharset(rawurldecode($dir_name), "UTF-8", LANG_CHARSET)] = true;
						}
					}
				}
			}
		}
		else
		{
			return false;
		}

		if($new_marker === $marker)
			break;

		if(!$bFound)
			break;

		$marker = $new_marker;
	}

	$result["dir"] = array_keys($result["dir"]);

	return $result;
}