• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/component_util.php
  • Класс: CComponentUtil
  • Вызов: CComponentUtil::CopyComponent
static function CopyComponent($componentName, $newNamespace, $newName = false, $bRewrite = false)
{
	/** @global CMain $APPLICATION */
	global $APPLICATION;

	$componentName = trim($componentName);
	if ($componentName == '')
	{
		$APPLICATION->ThrowException(GetMessage("comp_util_err1"), "EMPTY_COMPONENT_NAME");
		return false;
	}

	$path2Comp = CComponentEngine::MakeComponentPath($componentName);
	if ($path2Comp == '')
	{
		$APPLICATION->ThrowException(str_replace("#NAME#", $componentName, GetMessage("comp_util_err2")), "ERROR_NOT_COMPONENT");
		return false;
	}

	$componentPath = getLocalPath("components".$path2Comp);

	if (!CComponentUtil::isComponent($componentPath))
	{
		$APPLICATION->ThrowException(str_replace("#NAME#", $componentName, GetMessage("comp_util_err2")), "ERROR_NOT_COMPONENT");
		return false;
	}

	$newNamespace = trim($newNamespace);
	if ($newNamespace <> '')
	{
		if (preg_match("#[^a-z0-9_.-]#i", $newNamespace))
		{
			$APPLICATION->ThrowException(str_replace("#NAME#", $newNamespace, GetMessage("comp_util_err3")), "ERROR_NEW_NAMESPACE");
			return false;
		}
	}

	if ($newName == '')
		$newName = false;

	if ($newName !== false)
	{
		if (!preg_match("#^([a-z0-9_-]+\.)*([a-z0-9_-]+)$#i", $newName))
		{
			$APPLICATION->ThrowException(str_replace("#NAME#", $newName, GetMessage("comp_util_err4")), "ERROR_NEW_NAME");
			return false;
		}
	}

	$namespace = "";
	$name = $componentName;
	if (($pos = mb_strpos($componentName, ":")) !== false)
	{
		$namespace = mb_substr($componentName, 0, $pos);
		$name = mb_substr($componentName, $pos + 1);
	}

	if ($namespace == $newNamespace
		&& ($newName === false || $name == $newName))
	{
		$APPLICATION->ThrowException(GetMessage("comp_util_err5"), "ERROR_DUPL1");
		return false;
	}

	if ($newName !== false)
		$componentNameNew = $newNamespace.":".$newName;
	else
		$componentNameNew = $newNamespace.":".$name;

	$path2CompNew = CComponentEngine::MakeComponentPath($componentNameNew);
	if ($path2CompNew == '')
	{
		$APPLICATION->ThrowException(str_replace("#NAME#", $componentNameNew, GetMessage("comp_util_err2")), "ERROR_NOT_COMPONENT");
		return false;
	}

	$componentPathNew = getLocalPath("components".$path2CompNew);

	if (file_exists($_SERVER["DOCUMENT_ROOT"].$componentPathNew))
	{
		if (!$bRewrite)
		{
			$APPLICATION->ThrowException(str_replace("#NAME#", $componentNameNew, GetMessage("comp_util_err6")), "ERROR_EXISTS");
			return false;
		}
		else
		{
			DeleteDirFilesEx($componentPathNew);
		}
	}

	CheckDirPath($_SERVER["DOCUMENT_ROOT"].$componentPathNew);

	CopyDirFiles($_SERVER["DOCUMENT_ROOT"].$componentPath, $_SERVER["DOCUMENT_ROOT"].$componentPathNew, true, true, false);

	return null;
}