<commit_before><?php
/*
 *	Made by Samerton
 *  https://github.com/NamelessMC/Nameless/
 *  NamelessMC version 2.0.0-pr12
 *
 *  License: MIT
 *
 *  Panel API page
 */

if(!$user->handlePanelPageLoad('admincp.core.api')) {
    require_once(ROOT_PATH . '/403.php');
    die();
}

define('PAGE', 'panel');
define('PARENT_PAGE', 'core_configuration');
define('PANEL_PAGE', 'api');
$page_title = $language->get('admin', 'api');
require_once(ROOT_PATH . '/core/templates/backend_init.php');

if (!isset($_GET['view'])) {
    if (Input::exists()) {
        $errors = array();

        if (Token::check()) {
            if (isset($_POST['action']) && $_POST['action'] == 'regen') {
                // Regenerate new API key
                // Generate new key
                $new_api_key = substr(str_shuffle("<KEY>"), 0, 32);

                $plugin_api = $queries->getWhere('settings', array('name', '=', 'mc_api_key'));
                $plugin_api = $plugin_api[0]->id;

                // Update key
                $queries->update(
                    'settings',
                    $plugin_api,
                    array(
                        'value' => $new_api_key
                    )
                );

                // Cache
                file_put_contents(ROOT_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . sha1('apicache') . '.cache', $new_api_key);

                // Redirect
                Session::flash('api_success', $language->get('admin', 'api_key_regenerated'));
                Redirect::to(URL::build('/panel/core/api'));
                die();
            }

            $plugin_id = $queries->getWhere('settings', array('name', '=', 'use_api'));
            $plugin_id = $plugin_id[0]->id;
            $queries->update(
                'settings',
                $plugin_id,
                array(
                    'value' => Input::get('enable_api')
                )
            );

            $verification = isset($_POST['verification']) && $_POST['verification'] == 'on' ? 1 : 0;

            $verification_id = $queries->getWhere('settings', array('name', '=', 'email_verification'))[0]->id;
            try {
                $queries->update(
                    'settings',
                    $verification_id,
                    array(
                        'value' => $verification
                    )
                );
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
            }

            $api_verification = isset($_POST['api_verification']) && $_POST['api_verification'] == 'on' ? 1 : 0;
            $api_verification_id = $queries->getWhere('settings', array('name', '=', 'api_verification'))[0]->id;

            try {
                $queries->update(
                    'settings',
                    $api_verification_id,
                    array(
                        'value' => $api_verification
                    )
                );
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
            }

            $username_sync = isset($_POST['username_sync']) && $_POST['username_sync'] == 'on' ? 1 : 0;
            $username_sync_id = $queries->getWhere('settings', array('name', '=', 'username_sync'))[0]->id;

            try {
                $queries->update(
                    'settings',
                    $username_sync_id,
                    array(
                        'value' => $username_sync
                    )
                );
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
            }

            Session::flash('api_success', $language->get('admin', 'api_settings_updated_successfully'));

            //Log::getInstance()->log(Log::Action('admin/api/change'));
        } else {
            $errors[] = $language->get('general', 'invalid_token');
        }
    }
} else {
    // Group sync
    if (Input::exists()) {
        if (Token::check()) {
            if ($_POST['action'] == 'create') {
                $validate = new Validate();
                $validation = $validate->check($_POST, [
                        'ingame_rank_name' => [
                            Validate::MIN => 2,
                            Validate::MAX => 64,
                            Validate::UNIQUE => 'group_sync'
                        ],
                        'discord_role_id' => [
                            Validate::MIN => 18,
                            Validate::MAX => 18,
                            Validate::NUMERIC => true,
                            Validate::UNIQUE => 'group_sync'
                        ],
                        'website_group' => [
                            Validate::REQUIRED => true
                        ]
                    ]
                )->messages([
                    'ingame_rank_name' => [
                        Validate::MIN => $language->get('admin', 'group_name_minimum'),
                        Validate::MAX => $language->get('admin', 'ingame_group_maximum'),
                        Validate::UNIQUE => $language->get('admin', 'ingame_group_already_exists')
                    ],
                    'discord_role_id' => [
                        Validate::MIN => $language->get('admin', 'discord_role_id_length'),
                        Validate::MAX => $language->get('admin', 'discord_role_id_length'),
                        Validate::NUMERIC => $language->get('admin', 'discord_role_id_numeric'),
                        Validate::UNIQUE => $language->get('user', 'discord_id_taken')
                    ]
                ]);

                $errors = array();

                if (empty($_POST['ingame_rank_name']) && empty($_POST['discord_role_id'])) {
                    $errors[] = $language->get('admin', 'at_least_one_external');
                } else if ($validation->passed()) {

                    $discord_role_id = intval(Input::get('discord_role_id'));
                    if ($discord_role_id == 0) {
                        $discord_role_id = null;
                    }
                    $fields = array();
                    $fields['website_group_id']  = intval(Input::get('website_group'));
                    $fields['discord_role_id'] = $discord_role_id;

                    $ingame_rank_name = $_POST['ingame_rank_name'];

                    if (!empty($ingame_rank_name)) {
                        if (strlen(str_replace(' ', '', $ingame_rank_name)) > 1 && strlen(str_replace(' ', '', $ingame_rank_name)) < 65) {
                            $fields['ingame_rank_name'] = $ingame_rank_name;
                        } else {
                            $errors[] = $language->get('admin', 'group_name_minimum');
                            $errors[] = $language->get('admin', 'ingame_group_maximum');
                        }
                    } else {
                        $fields['ingame_rank_name'] = null;
                    }<commit_msg>Allow syncing multiple groups into multiple website groups<commit_after><?php
/*
 *	Made by Samerton
 *  https://github.com/NamelessMC/Nameless/
 *  NamelessMC version 2.0.0-pr12
 *
 *  License: MIT
 *
 *  Panel API page
 */

if(!$user->handlePanelPageLoad('admincp.core.api')) {
    require_once(ROOT_PATH . '/403.php');
    die();
}

define('PAGE', 'panel');
define('PARENT_PAGE', 'core_configuration');
define('PANEL_PAGE', 'api');
$page_title = $language->get('admin', 'api');
require_once(ROOT_PATH . '/core/templates/backend_init.php');

if (!isset($_GET['view'])) {
    if (Input::exists()) {
        $errors = array();

        if (Token::check()) {
            if (isset($_POST['action']) && $_POST['action'] == 'regen') {
                // Regenerate new API key
                // Generate new key
                $new_api_key = substr(str_shuffle("<KEY>"), 0, 32);

                $plugin_api = $queries->getWhere('settings', array('name', '=', 'mc_api_key'));
                $plugin_api = $plugin_api[0]->id;

                // Update key
                $queries->update(
                    'settings',
                    $plugin_api,
                    array(
                        'value' => $new_api_key
                    )
                );

                // Cache
                file_put_contents(ROOT_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . sha1('apicache') . '.cache', $new_api_key);

                // Redirect
                Session::flash('api_success', $language->get('admin', 'api_key_regenerated'));
                Redirect::to(URL::build('/panel/core/api'));
                die();
            }

            $plugin_id = $queries->getWhere('settings', array('name', '=', 'use_api'));
            $plugin_id = $plugin_id[0]->id;
            $queries->update(
                'settings',
                $plugin_id,
                array(
                    'value' => Input::get('enable_api')
                )
            );

            $verification = isset($_POST['verification']) && $_POST['verification'] == 'on' ? 1 : 0;

            $verification_id = $queries->getWhere('settings', array('name', '=', 'email_verification'))[0]->id;
            try {
                $queries->update(
                    'settings',
                    $verification_id,
                    array(
                        'value' => $verification
                    )
                );
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
            }

            $api_verification = isset($_POST['api_verification']) && $_POST['api_verification'] == 'on' ? 1 : 0;
            $api_verification_id = $queries->getWhere('settings', array('name', '=', 'api_verification'))[0]->id;

            try {
                $queries->update(
                    'settings',
                    $api_verification_id,
                    array(
                        'value' => $api_verification
                    )
                );
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
            }

            $username_sync = isset($_POST['username_sync']) && $_POST['username_sync'] == 'on' ? 1 : 0;
            $username_sync_id = $queries->getWhere('settings', array('name', '=', 'username_sync'))[0]->id;

            try {
                $queries->update(
                    'settings',
                    $username_sync_id,
                    array(
                        'value' => $username_sync
                    )
                );
            } catch (Exception $e) {
                $errors[] = $e->getMessage();
            }

            Session::flash('api_success', $language->get('admin', 'api_settings_updated_successfully'));

            //Log::getInstance()->log(Log::Action('admin/api/change'));
        } else {
            $errors[] = $language->get('general', 'invalid_token');
        }
    }
} else {
    // Group sync
    if (Input::exists()) {
        if (Token::check()) {
            if ($_POST['action'] == 'create') {
                $validate = new Validate();
                $validation = $validate->check($_POST, [
                        'ingame_rank_name' => [
                            Validate::MIN => 2,
                            Validate::MAX => 64
                        ],
                        'discord_role_id' => [
                            Validate::MIN => 18,
                            Validate::MAX => 18,
                            Validate::NUMERIC => true
                        ],
                        'website_group' => [
                            Validate::REQUIRED => true
                        ]
                    ]
                )->messages([
                    'ingame_rank_name' => [
                        Validate::MIN => $language->get('admin', 'group_name_minimum'),
                        Validate::MAX => $language->get('admin', 'ingame_group_maximum')
                    ],
                    'discord_role_id' => [
                        Validate::MIN => $language->get('admin', 'discord_role_id_length'),
                        Validate::MAX => $language->get('admin', 'discord_role_id_length'),
                        Validate::NUMERIC => $language->get('admin', 'discord_role_id_numeric')
                    ]
                ]);

                $errors = array();

                if (empty($_POST['ingame_rank_name']) && empty($_POST['discord_role_id'])) {
                    $errors[] = $language->get('admin', 'at_least_one_external');
                } else if ($validation->passed()) {

                    $discord_role_id = intval(Input::get('discord_role_id'));
                    if ($discord_role_id == 0) {
                        $discord_role_id = null;
                    }
                    $fields = array();
                    $fields['website_group_id']  = intval(Input::get('website_group'));
                    $fields['discord_role_id'] = $discord_role_id;

                    $ingame_rank_name = $_POST['ingame_rank_name'];

                    if (!empty($ingame_rank_name)) {
                        if (strlen(str_replace(' ', '', $ingame_rank_name)) > 1 && strlen(str_replace(' ', '', $ingame_rank_name)) < 65) {
                            $fields['ingame_rank_name'] = $ingame_rank_name;
                        } else {
                            $errors[] = $language->get('admin', 'group_name_minimum');
                            $errors[] = $language->get('admin', 'ingame_group_maximum');
                        }
                    } else {
                        $fields['ingame_rank_name'] = null;
                    }
{'fraction_non_alphanumeric': 0.1515749470145436, 'fraction_numerical': 0.004969670393919462, 'mean_word_length': 1.3544390915347557, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 125, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5194685', 'n_tokens_mistral': 3797, 'n_tokens_neox': 3547, 'n_words': 537}