Skip to content

feat(admin): config editor#933

Closed
n1rwana wants to merge 1 commit intomasterfrom
cfg-editor
Closed

feat(admin): config editor#933
n1rwana wants to merge 1 commit intomasterfrom
cfg-editor

Conversation

@n1rwana
Copy link
Member

@n1rwana n1rwana commented Jul 28, 2023

Этот PR добавляет возможность редактировать конфиг (openvk.yml) через интерфейс админ-панели. Для более удобного редактирования названия ключей были переведены. Также, если файл с конфигом не существует, интерфейс попытается скопировать его из openvk-example.yml, или, если и он не найден, скачать из репозитория.
image

@WerySkok
Copy link
Member

WerySkok commented Aug 9, 2023

+шаг к инсталлеру, получается

Comment on lines +579 to +599
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_write_fail")]);
}
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_create_fail")]);
}
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_create_fail")]);
}
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_tmp_read_fail_yaml")]);
}
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_tmp_read_fail")]);
}
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_file_write_err")]);
}
} else {
$this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_file_create_err")]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

что это за порно

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Спросил ChatGPT, ответ убил:

function renderTuning(): void
{
    $mode = in_array($this->queryParam("act"), ["cfg"]) ? $this->queryParam("act") : "cfg";

    if ($_SERVER["REQUEST_METHOD"] !== "POST") {
        $this->template->mode = $mode;

        if ($mode === "cfg") {
            $file_path = __DIR__ . "../../../openvk.yml";

            if (file_exists($file_path)) {
                $this->template->cfg = chandler_parse_yaml($file_path);
                return;
            }

            $example_path = __DIR__ . "../../../openvk-example.yml";
            if (file_exists($example_path)) {
                $this->flash("warn", tr("admin_tuning_cfg_not_found"), tr("admin_tuning_cfg_not_found_description"));
                $this->template->cfg = chandler_parse_yaml($example_path);
                return;
            }

            $repo_file = file_get_contents("https://raw.githubusercontent.com/openvk/openvk/master/openvk-example.yml");
            if (!$repo_file) {
                $this->flash("err", tr("admin_tuning_cfg_file_not_found_in_repo"), tr("admin_tuning_cfg_error_description"));
                return;
            }

            $file = fopen($file_path, "x");
            if (!$file) {
                $this->flash("err", tr("admin_tuning_cfg_file_create_err"), tr("admin_tuning_cfg_error_description"));
                return;
            }

            if (!fwrite($file, $repo_file)) {
                fclose($file);
                $this->flash("err", tr("admin_tuning_cfg_file_write_err"), tr("admin_tuning_cfg_error_description"));
                return;
            }
            fclose($file);

            $this->template->cfg = chandler_parse_yaml($file_path);
        }

        return;
    }

    // POST Request
    $this->assertNoCSRF();

    if ($mode !== "cfg") {
        return; // Or handle other modes if needed
    }

    $yaml = $this->postParam("yaml");
    if (!$yaml) {
        $this->notFound();
    }

    $temp_file_path = __DIR__ . "../../../openvk." . time() . ".yml";
    $temp_file = fopen($temp_file_path, "x");
    if (!$temp_file) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_file_create_err")]);
        return;
    }

    if (!fwrite($temp_file, $yaml)) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_file_write_err")]);
        return;
    }

    $temp_file_content = file_get_contents($temp_file_path);
    if (!$temp_file_content) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_tmp_read_fail")]);
        return;
    }

    if (!chandler_parse_yaml($temp_file_path)) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_tmp_read_fail_yaml")]);
        return;
    }

    $cfg_file_path = __DIR__ . "../../../openvk.yml";
    $backup_path = $cfg_file_path . ".tmp";
    if (!rename($cfg_file_path, $backup_path)) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_create_fail")]);
        return;
    }

    $cfg_file = fopen($cfg_file_path, "x");
    if (!$cfg_file) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_create_fail")]);
        return;
    }

    if (!file_put_contents($cfg_file_path, $temp_file_content)) {
        $this->returnJson(["success" => false, "error" => tr("admin_tuning_cfg_write_fail")]);
        return;
    }

    unlink($backup_path);
    unlink($temp_file_path);
    $this->returnJson(["success" => true]);
}

Copy link
Member

@WerySkok WerySkok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Советую решить конфликт слияния

@ghost ghost changed the title Редактор конфига feat(admin): config editor Dec 12, 2024
@n1rwana n1rwana closed this Nov 23, 2025
@WerySkok WerySkok deleted the cfg-editor branch November 27, 2025 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants