• Модуль: webdav
  • Путь к файлу: ~/bitrix/modules/webdav/classes/file.php
  • Класс: CWebDavFile
  • Вызов: CWebDavFile::VirtualIO_ConvertCharset
function VirtualIO_ConvertCharset($arParams=array())
{
	static $resultCache = array();
	static $bxLang = null;
	$io = self::GetIo();

	if ($bxLang === null) // from VirtualIO
	{
		if (defined('BX_UTF'))
			$bxLang = "utf-8";
		elseif (defined("SITE_CHARSET") && (SITE_CHARSET <> ''))
			$bxLang = SITE_CHARSET;
		elseif (defined("LANG_CHARSET") && (LANG_CHARSET <> ''))
			$bxLang = LANG_CHARSET;
		else
			$bxLang = "windows-1251";

		$bxLang = mb_strtolower($bxLang);
	}

	if (
		isset($arParams['original'])
		&& (mb_strpos($arParams['original'], $this->real_path_full) === 0)
		&& ($arParams['converted'] !== $arParams['original'])
	)
	{
		if (!isset($resultCache[$arParams['original']]))
		{
			$libPath = mb_substr($arParams['original'], mb_strlen($this->real_path_full) + 1);
			$libPath = explode('/', $libPath);

			$pathLength = sizeof($libPath);
			for ($i=0; $i<$pathLength; $i++)
			{
				$p = $libPath[$i];
				if ($i > 0)
				{
					$parent = array_slice($libPath, 0, $i);
					$parent = $io->CombinePath($this->real_path_full, implode('/', $parent));
				}
				else
				{
					$parent = $this->real_path_full;
				}


				if ($arParams['direction'] == CBXVirtualIoFileSystem::directionEncode) // bx to system
				{
					if (file_exists($io->CombinePath($parent, $p)))
					{
						$libPath[$i] = $p;
					}
					else
					{
						$pc = CBXVirtualIoFileSystem::ConvertCharset($p, CBXVirtualIoFileSystem::directionEncode, true);
						if (file_exists($io->CombinePath($parent, $pc)))
						{
							$libPath[$i] = $pc;
						}
						else
						{
							$libPath[$i] = $pc; // creating new ?
						}
					}
				}
				elseif ($arParams['direction'] == CBXVirtualIoFileSystem::directionDecode) /// system to bx
				{
					$decoded = CBXVirtualIoFileSystem::ConvertCharset($p, CBXVirtualIoFileSystem::directionDecode, true);
					$restored = CBXVirtualIoFileSystem::ConvertCharset($decoded, CBXVirtualIoFileSystem::directionEncode, true);
					if ($restored == $p)
					{
						$reverse = CBXVirtualIoFileSystem::ConvertCharset($p, CBXVirtualIoFileSystem::directionEncode, true);
						$restored_reverse = CBXVirtualIoFileSystem::ConvertCharset($reverse, CBXVirtualIoFileSystem::directionDecode, true);
						if (
							($restored_reverse == $p)
							&& ($bxLang != "windows-1251")
						)
						{
							$libPath[$i] = $p;
						}
						else
						{
							$libPath[$i] = $decoded;
						}
					}
					else // conversion failed, assume original was correct
					{
						$libPath[$i] = $p;
					}
				}
				else
				{
					die('invalid encoding direction');
				}
			}
			$resultCache[$arParams['original']] = $io->CombinePath($this->real_path_full, implode('/', $libPath));
		}
		return $resultCache[$arParams['original']];
	}
	return false;
}