if (!CModule::IncludeModule('clouds')) {
    $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
}
IncludeModuleLangFile(__FILE__);
$aTabs = array(array("DIV" => "edit1", "TAB" => GetMessage("CLO_STORAGE_EDIT_TAB"), "ICON" => "main_user_edit", "TITLE" => GetMessage("CLO_STORAGE_EDIT_TAB_TITLE")), array("DIV" => "edit2", "TAB" => GetMessage("CLO_STORAGE_EDIT_TAB2"), "ICON" => "main_user_edit", "TITLE" => GetMessage("CLO_STORAGE_EDIT_TAB2_TITLE")));
$tabControl = new CAdminTabControl("tabControl", $aTabs);
$ID = intval($_REQUEST["ID"]);
// Id of the edited record
$bVarsFromForm = false;
$message = null;
if ($_SERVER["REQUEST_METHOD"] === "POST" && check_bitrix_sessid()) {
    if (isset($_REQUEST["save"]) || isset($_REQUEST["apply"])) {
        $ob = new CCloudStorageBucket($ID);
        $arFields = array("ACTIVE" => $_POST["ACTIVE"] === "Y" ? "Y" : "N", "READ_ONLY" => $_POST["READ_ONLY"] === "Y" ? "Y" : "N", "SORT" => $_POST["SORT"], "SERVICE_ID" => $_POST["SERVICE_ID"], "BUCKET" => $_POST["BUCKET"], "LOCATION" => $_POST["LOCATION"][$_POST["SERVICE_ID"]], "SETTINGS" => $_POST["SETTINGS"][$_POST["SERVICE_ID"]], "CNAME" => $_POST["CNAME"], "FILE_RULES" => CCloudStorageBucket::ConvertPOST($_POST));
        if ($ID > 0) {
            $res = $ob->Update($arFields);
        } else {
            $res = $ob->Add($arFields);
        }
        if ($res > 0) {
            if (isset($_REQUEST["apply"])) {
                LocalRedirect("/bitrix/admin/clouds_storage_edit.php?ID=" . $res . "&lang=" . LANGUAGE_ID . "&" . $tabControl->ActiveTabParam());
            } else {
                LocalRedirect("/bitrix/admin/clouds_storage_list.php?lang=" . LANGUAGE_ID);
            }
        } else {
            $e = $APPLICATION->GetException();
            if (is_object($e)) {
                $message = new CAdminMessage(GetMessage("CLO_STORAGE_EDIT_SAVE_ERROR"), $e);
            }
            $bVarsFromForm = true;
                         //An error occured
                         @unlink($absTempPath);
                         $bFileMoved = false;
                     }
                 }
             }
             if ($bNextFile) {
                 $_SESSION["last_file_id"] = $ar["ID"];
                 $_SESSION["last_file_pos"] = 0.0;
                 if ($bFileMoved) {
                     rename($absTempPath, $absPath);
                     $ob->DeleteFile($filePath);
                     $DB->Query("\n\t\t\t\t\t\t\t\tUPDATE b_file\n\t\t\t\t\t\t\t\tSET HANDLER_ID = null\n\t\t\t\t\t\t\t\tWHERE ID = " . intval($arFile["ID"]) . "\n\t\t\t\t\t\t\t");
                     CFile::CleanCache($arFile["ID"]);
                     $ob->DecFileCounter((double) $arFile["FILE_SIZE"]);
                     $ob->Update(array("LAST_FILE_ID" => 0));
                 }
             }
             CAdminMessage::ShowMessage(array("TYPE" => "PROGRESS", "MESSAGE" => GetMessage("CLO_STORAGE_LIST_DOWNLOAD_IN_PROGRESS"), "DETAILS" => GetMessage("CLO_STORAGE_LIST_DOWNLOAD_PROGRESS", array("#remain#" => $ar["CNT"] - $bNextFile, "#bytes#" => CFile::FormatSize($ar["FILE_SIZE"] - $last_file_pos))), "HTML" => true, "BUTTONS" => array(array("VALUE" => GetMessage("CLO_STORAGE_LIST_STOP"), "ONCLICK" => 'window.location = \'/bitrix/admin/clouds_storage_list.php?lang=' . LANGUAGE_ID . '\''))));
             $bOnTheMove = true;
             echo '<script>', $lAdmin->ActionDoGroup($ID, "download", "themove=y"), '</script>';
         } else {
             unset($_SESSION["last_file_id"]);
             unset($_SESSION["last_file_pos"]);
             CAdminMessage::ShowMessage(array("MESSAGE" => GetMessage("CLO_STORAGE_LIST_DOWNLOAD_DONE"), "TYPE" => "OK", "HTML" => true));
             $bOnTheMove = false;
         }
         $lAdmin->EndPrologContent();
     }
     break;
 case "move":