public static function import($arParams) { global $APPLICATION; // check fields $aMsg = array(); $arParams['SET_ID'] = intval($arParams['SET_ID']); $arParams['IMPORT_IF_FILE_EXISTS'] = isset($arParams['IMPORT_IF_FILE_EXISTS']) && $arParams['IMPORT_IF_FILE_EXISTS'] == 'Y' ? true : false; if (isset($arParams['FILE']) && GetFileExtension($arParams['FILE']) != 'zip') { $aMsg["FILE_EXT"] = array("id" => "FILE_EXT", "text" => GetMessage("MAIN_SMILE_IMPORT_FILE_EXT_ERROR")); } else { if (!isset($arParams['FILE']) || !file_exists($arParams['FILE'])) { $aMsg["FILE"] = array("id" => "FILE", "text" => GetMessage("MAIN_SMILE_IMPORT_FILE_ERROR")); } else { if ($arParams['SET_ID'] <= 0) { $aMsg["SET_ID"] = array("id" => "SET_ID", "text" => GetMessage("MAIN_SMILE_IMPORT_SET_ID_ERROR")); } } } if (!empty($aMsg)) { $e = new CAdminException($aMsg); $APPLICATION->ThrowException($e); return false; } $sUnpackDir = CTempFile::GetDirectoryName(1); CheckDirPath($sUnpackDir); /** @var IBXArchive $oArchiver */ $oArchiver = CBXArchive::GetArchive($arParams['FILE'], "ZIP"); $oArchiver->SetOptions(array("STEP_TIME" => 300)); if (!$oArchiver->Unpack($sUnpackDir)) { $aMsg["UNPACK"] = array("id" => "UNPACK", "text" => GetMessage("MAIN_SMILE_IMPORT_UNPACK_ERROR")); $e = new CAdminException($aMsg); $APPLICATION->ThrowException($e); return false; } $arSmiles = array(); if (file_exists($sUnpackDir . 'install.csv')) { $arLang = array(); $db_res = CLanguage::GetList($b = "sort", $o = "asc"); while ($res = $db_res->Fetch()) { if (file_exists($sUnpackDir . 'install_lang_' . $res["LID"] . '.csv')) { $arSmiles = array(); $csvFile = new CCSVData(); $csvFile->LoadFile($sUnpackDir . 'install_lang_' . $res["LID"] . '.csv'); $csvFile->SetFieldsType("R"); $csvFile->SetFirstHeader(false); while ($smile = $csvFile->Fetch()) { if (defined('BX_UTF') && BX_UTF && $res["LID"] == 'ru') { $smile[1] = $APPLICATION->ConvertCharset($smile[1], 'windows-1251', 'utf-8'); } $arLang[$smile[0]][$res["LID"]] = $smile[1]; } } } $csvFile = new CCSVData(); $csvFile->LoadFile($sUnpackDir . 'install.csv'); $csvFile->SetFieldsType("R"); $csvFile->SetFirstHeader(false); while ($smileRes = $csvFile->Fetch()) { $smile = array('TYPE' => $smileRes[0], 'CLICKABLE' => $smileRes[1] == 'Y' ? 'Y' : 'N', 'SORT' => intval($smileRes[2]), 'IMAGE' => $smileRes[3], 'IMAGE_WIDTH' => intval($smileRes[4]), 'IMAGE_HEIGHT' => intval($smileRes[5]), 'IMAGE_DEFINITION' => in_array($smileRes[6], array(self::IMAGE_SD, self::IMAGE_HD, self::IMAGE_UHD)) ? $smileRes[6] : ($smileRes[6] == 'Y' ? self::IMAGE_HD : self::IMAGE_SD), 'HIDDEN' => in_array($smileRes[7], array('Y', 'N')) ? $smileRes[7] : 'N', 'IMAGE_LANG' => in_array($smileRes[7], array('Y', 'N')) ? $smileRes[8] : $smileRes[7], 'TYPING' => in_array($smileRes[7], array('Y', 'N')) ? $smileRes[9] : $smileRes[8]); if (!in_array($smile['TYPE'], array(CSmile::TYPE_SMILE, CSmile::TYPE_ICON))) { continue; } $smile['IMAGE'] = GetFileName($smile['IMAGE']); $imgArray = CFile::GetImageSize($sUnpackDir . $smile['IMAGE']); if (!is_array($imgArray)) { continue; } $arInsert = array('TYPE' => $smile['TYPE'], 'SET_ID' => $arParams['SET_ID'], 'CLICKABLE' => $smile['CLICKABLE'], 'SORT' => $smile['SORT'], 'IMAGE' => $smile['IMAGE'], 'IMAGE_WIDTH' => $smile['IMAGE_WIDTH'], 'IMAGE_HEIGHT' => $smile['IMAGE_HEIGHT'], 'IMAGE_DEFINITION' => $smile['IMAGE_DEFINITION'], 'HIDDEN' => $smile['HIDDEN'], 'TYPING' => $smile['TYPING']); if (isset($arLang[$smile['IMAGE_LANG']])) { $arInsert['LANG'] = $arLang[$smile['IMAGE_LANG']]; } $arSmiles[] = $arInsert; } } else { $smileSet = CSmileSet::getById($arParams['SET_ID']); if ($handle = @opendir($sUnpackDir)) { $sort = 300; while (($file = readdir($handle)) !== false) { if ($file == "." || $file == "..") { continue; } if (is_file($sUnpackDir . $file)) { $imgArray = CFile::GetImageSize($sUnpackDir . $file); if (is_array($imgArray)) { $smileHR = self::IMAGE_SD; $smileType = CSmile::TYPE_SMILE; $smileCode = GetFileNameWithoutExtension($file); if (strpos($file, 'smile_') !== false && strpos($file, 'smile_') == 0) { $smileCode = substr($smileCode, 6); } elseif (strpos($file, 'smile') !== false && strpos($file, 'smile') == 0) { $smileCode = substr($smileCode, 5); } elseif (strpos($file, 'icon_') !== false && strpos($file, 'icon_') == 0) { $smileType = CSmile::TYPE_ICON; $smileCode = substr($smileCode, 5); } else { if (strpos($file, 'icon') !== false && strpos($file, 'icon') == 0) { $smileType = CSmile::TYPE_ICON; $smileCode = substr($smileCode, 4); } } if (strrpos($smileCode, '_hr') !== false && strrpos($smileCode, '_hr') == strlen($smileCode) - 3) { $smileHR = self::IMAGE_HD; $smileCode = substr($smileCode, 0, strrpos($smileCode, '_hr')); } if ($pos = strpos($smileCode, '_hr_')) { $smileHR = self::IMAGE_HD; $smileCode = substr($smileCode, 0, $pos) . '_' . substr($smileCode, $pos + 4); } $arSmiles[] = array('TYPE' => $smileType, 'SET_ID' => $arParams['SET_ID'], 'CLICKABLE' => 'Y', 'SORT' => $sort, 'IMAGE' => $file, 'IMAGE_WIDTH' => intval($imgArray[0]), 'IMAGE_HEIGHT' => intval($imgArray[1]), 'IMAGE_DEFINITION' => $smileHR, 'TYPING' => ':' . (isset($smileSet['STRING_ID']) ? $smileSet['STRING_ID'] : $smileSet['ID']) . '/' . $smileCode . ':'); $sort = $sort + 5; } } } @closedir($handle); } } $importSmile = 0; foreach ($arSmiles as $smile) { $sUploadDir = ($smile['TYPE'] == CSmile::TYPE_ICON ? CSmile::PATH_TO_ICON : CSmile::PATH_TO_SMILE) . intval($smile["SET_ID"]) . '/'; if (file_exists($sUnpackDir . $smile['IMAGE']) && ($arParams['IMPORT_IF_FILE_EXISTS'] || !file_exists($_SERVER["DOCUMENT_ROOT"] . $sUploadDir . $smile['IMAGE']))) { if (CheckDirPath($_SERVER["DOCUMENT_ROOT"] . $sUploadDir)) { $insertId = CSmile::add($smile); if ($insertId) { if ($arParams['IMPORT_IF_FILE_EXISTS'] && file_exists($_SERVER["DOCUMENT_ROOT"] . $sUploadDir . $smile['IMAGE'])) { $importSmile++; } else { if (copy($sUnpackDir . $smile['IMAGE'], $_SERVER["DOCUMENT_ROOT"] . $sUploadDir . $smile['IMAGE'])) { @chmod($_SERVER["DOCUMENT_ROOT"] . $sUploadDir . $smile['IMAGE'], BX_FILE_PERMISSIONS); $importSmile++; } else { CSmile::delete($insertId); } } } $APPLICATION->ResetException(); } } } return $importSmile; }
$GLOBALS["APPLICATION"]->ResetException(); $arFields = array("SET_ID" => $_REQUEST["SET_ID"], "SORT" => $_REQUEST["SORT"], "TYPE" => $_REQUEST["TYPE"], "HIDDEN" => isset($_REQUEST["HIDDEN"]) ? 'Y' : 'N', "TYPING" => trim($_REQUEST["TYPING"]), "LANG" => array()); if (!empty($_FILES["IMAGE"]["tmp_name"])) { $arFields["IMAGE"] = $fileName; $arFields["IMAGE_WIDTH"] = $arImageSize['WIDTH']; $arFields["IMAGE_HEIGHT"] = $arImageSize['HEIGHT']; $arFields["IMAGE_DEFINITION"] = $_REQUEST["IMAGE_DEFINITION"]; } foreach ($arLang as $key => $val) { $arFields["LANG"][$key] = $_REQUEST["LANG"][$key]; } if ($ID > 0) { $arSmile = empty($arSmile) ? CSmile::getByID($ID) : $arSmile; CSmile::update($ID, $arFields); } else { $ID = CSmile::add($arFields); } if ($e = $GLOBALS["APPLICATION"]->GetException()) { $arError[] = array("id" => "", "text" => $e->getString()); if (!empty($_FILES["IMAGE"]["tmp_name"]) && isset($sUploadDir)) { @unlink($_SERVER["DOCUMENT_ROOT"] . $sUploadDir . $fileName); unset($arFields["IMAGE"]); } } else { if (!empty($arSmile)) { $res = CSmile::getByID($ID); if ($arSmile["IMAGE"] != $res["IMAGE"]) { @unlink($_SERVER["DOCUMENT_ROOT"] . ($arSmile['TYPE'] == CSmile::TYPE_ICON ? CSmile::PATH_TO_ICON : CSmile::PATH_TO_SMILE) . intval($arSmile["SET_ID"]) . '/' . $arSmile["IMAGE"]); } elseif ($arSmile["TYPE"] != $res["TYPE"] || $arSmile["SET_ID"] != $res["SET_ID"]) { CopyDirFiles($_SERVER["DOCUMENT_ROOT"] . ($arSmile['TYPE'] == CSmile::TYPE_ICON ? CSmile::PATH_TO_ICON : CSmile::PATH_TO_SMILE) . intval($arSmile["SET_ID"]) . '/' . $arSmile["IMAGE"], $_SERVER["DOCUMENT_ROOT"] . ($res['TYPE'] == CSmile::TYPE_ICON ? CSmile::PATH_TO_ICON : CSmile::PATH_TO_SMILE) . intval($res["SET_ID"]) . '/' . $arSmile["IMAGE"], false, false, true, ""); }