<commit_before><?php
/*
 * Reset a password
 *
 * arg0 = email
 * arg1 = new password
 */
class TrainingShell extends AppShell {

    public $uses = array('User', 'Organisation');

    private $__currentUrl = false;
    private $__currentAuthKey = false;

    private $__simulate = false;
    private $__config = false;
    private $__report = array();
    private $__verbose = false;

    public function simulate()
    {
        $this->__verbose = $this->params['verbose'];
        $this->__simulate = true;
        $this->setup();
    }

    public function setup()
    {
        $this->__config = file_get_contents(APP . 'Console/Command/training.json');
        if (empty($this->__config)) {
            echo 'No config file found. Make sure that training.json exists and is configured.';
            die();
        }
        $this->__config = json_decode($this->__config, true);
        $this->__report = array();
        for ($i = $this->__config['ID_start']; $i < ($this->__config['ID_start'] + $this->__config['number_of_misps_to_configure']); $i++) {
            $id = $i;
            if ($this->__config['ID_zero_out']) {
                if ($id < 10) {
                    $id = '0' . $id;
                }
            }
            $this->__currentUrl = str_replace('$ID', $id, $this->__config['server_blueprint']);
            if ($this->__verbose) {
                echo 'INFO - Instance to configure' . $this->__currentUrl . PHP_EOL;
            }
            $org = str_replace('$ID', $id, $this->__config['org_blueprint']);
            $org_id = $this->Organisation->createOrgFromName($org, 1, true);
            $org_data = $this->Organisation->find('first', array(
                'recursive' => -1,
                'fields' => array('name', 'uuid', 'local'),
                'conditions' => array('Organisation.id' => $org_id)
            ));
            $remote_org_id = $this->__createOrg($org_data);
            $this->__setSetting('MISP.host_org_id', $remote_org_id, $id, $org);
            $this->__report['servers'][$this->__currentUrl]['host_org_id'] = $remote_org_id;
            $this->__report['remote_orgs'][] = array('id' => $remote_org_id, 'name' => $org);
            $role_id = $this->__createRole($this->__config['role_blueprint']);
            $this->__report['servers'][$this->__currentUrl]['training_role_id'] = $role_id;
            $sync_user = $this->__createSyncUserLocally($remote_org_id, $org);
            $local_host_org = $this->Organisation->find('first', array(
                'recursive' => -1,
                'conditions' => array(
                    'Organisation.id' => Configure::read('MISP.host_org_id')
                ),
                'fields' => array(
                    'name', 'id', 'uuid'
                )
            ));
            $hub_org_id_on_remote = $this->__createOrg($local_host_org);
            $external_baseurl = empty(Configure::read('MISP.external_baseurl')) ? Configure::read('MISP.baseurl') : Configure::read('MISP.external_baseurl');
            $this->__report['servers'][$this->__currentUrl]['sync_connections'][] = $this->__addSyncConnection($external_baseurl, 'Exercise hub', $local_host_org, $hub_org_id_on_remote, $sync_user);
            $this->__report['servers'][$this->__currentUrl]['users'] = $this->__createUsers($remote_org_id, $role_id, $org, $id);
            if (!empty($this->__config['settings'])) {
                foreach ($this->__config['settings'] as $key => $value)
                $this->__setSetting($key, $value, $id, $org);
            }
            if ($this->__config['reset_admin_credentials']) {
                $this->__report['servers'][$this->__currentUrl]['management_account'] = $this->__reset_admin_credentials($this->__report);
            }
        }
        echo 'Setup complete. Please find the modifications below:' . PHP_EOL . PHP_EOL;
        echo json_encode($this->__report, JSON_PRETTY_PRINT);
    }

    private function __addSyncConnection($baseurl, $name, $local_host_org, $hub_org_id_on_remote, $sync_user)
    {
        $server = array(
            'name' => $name,
            'url' => $baseurl,
            'authkey' => $sync_user['User']['authkey'],
            'remote_org_id' => $hub_org_id_on_remote,
            'push' => 1,
            'pull' => 1,
            'self_signed' => 1
        );
        $options = array(
            'url' => $this->__currentUrl . '/servers/add',
            'method' => 'POST',
            'body' => $server
        );
        $response = $this->__queryRemoteMISP($options, true);
        if ($response->code != 200) {
            $this->__responseError($response, $options);
        } else {
            $response_data = json_decode($response->body, true);
            return array(
                'url' => $response_data['Server']['url'],
                'authkey' => $response_data['Server']['authkey']
            );
        }
    }

    private function __createSyncUserLocally($remote_org_id, $org)
    {
        $sync_role = $this->User->Role->find('first', array('recursive' => -1, 'conditions' => array('Role.name' => 'Sync user')));
        $sync_role = $sync_role['Role']['id'];
        $this->User->create();
        $this->User->save(array(
                'external_auth_required' => 0,
                'external_auth_key' => '',
                'server_id' => 0,
                'gpgkey' => '',
                'certif_public' => '',
                'autoalert' => 0,
                'contactalert' => 0,
                'disabled' => 0,
                'newsread' => 0,
                'change_pw' => 1,
                'authkey' => $this->User->generateAuthKey(),
                'termsaccepted' => 0,
                'org_id' => $remote_org_id,
                'role_id' => $sync_role,
                'email' => '<EMAIL>_<EMAIL>@' . $org . '.test'
        ));
        $user = $this->User->find('first', array('recursive' => -1, 'conditions' => array('User.email' => 'sync_user@' . $org . '.test')));
        return $user;
    }

    private function __responseError($response, $options)
    {
        echo sprintf(
            "Received a non-200 response (%s). Aborting.\nQueried URL: %s\n Query type: %s\n Request payload: %s\n\n",
            $response->code,
            $options['url'],
            $options['method'],
            empty($options['body']) ? '' : json_encode($options['body'], JSON_PRETTY_PRINT)
        );
        echo 'Setup failed. Output of what has been created:' . PHP_EOL . PHP_EOL;
        echo json_encode($this->__report, JSON_PRETTY_PRINT);
        die();
    }

    private function __createUsers($remote_org_id, $role_id, $org, $i)
    {
        $summary = array();
        for ($j = 1; $j < (1+$this->__config['user_count']); $j++) {
            $email = $this->__config['user_blueprint'];
            $email = str_replace('$ID', $i, $email);
            $email = str_replace('$ORGNAME', $org, $email);
            $email = str_replace('$USER_ITERATOR', $j, $email);
            $options = array(
                'url' => $this->__currentUrl . '/admin/users/index/searchall:' . $email,
                'method' => 'GET'
            );
            $response = $this->__queryRemoteMISP($options, true);
            if ($response->code != 200) {
                $this->__responseError($response, $options);
            }
            $newKey = $this->User->generateRandomPassword(32);
            if (!empty($response->body)) {
                $user = array(
                    'email' => $email,
                    'password' => <PASSWORD>,
                    'confirm_password' => <PASSWORD>,
                    'enable_password' => 1,
                    'role_id' => $role_id,
                    'org_id' => $remote_org_id
                );
                $options = array(
                    'url' => $this->__currentUrl . '/admin/users/add',
                    'method' => 'POST',<commit_msg>fix: [Training] AAARGH<commit_after><?php
/*
 * Reset a password
 *
 * arg0 = email
 * arg1 = new password
 */
class TrainingShell extends AppShell {

    public $uses = array('User', 'Organisation');

    private $__currentUrl = false;
    private $__currentAuthKey = false;

    private $__simulate = false;
    private $__config = false;
    private $__report = array();
    private $__verbose = false;

    public function simulate()
    {
        $this->__verbose = $this->params['verbose'];
        $this->__simulate = true;
        $this->setup();
    }

    public function setup()
    {
        $this->__config = file_get_contents(APP . 'Console/Command/training.json');
        if (empty($this->__config)) {
            echo 'No config file found. Make sure that training.json exists and is configured.';
            die();
        }
        $this->__config = json_decode($this->__config, true);
        $this->__report = array();
        for ($i = $this->__config['ID_start']; $i < ($this->__config['ID_start'] + $this->__config['number_of_misps_to_configure']); $i++) {
            $id = $i;
            if ($this->__config['ID_zero_out']) {
                if ($id < 10) {
                    $id = '0' . $id;
                }
            }
            $this->__currentUrl = str_replace('$ID', $id, $this->__config['server_blueprint']);
            if ($this->__verbose) {
                echo 'INFO - Instance to configure' . $this->__currentUrl . PHP_EOL;
            }
            $org = str_replace('$ID', $id, $this->__config['org_blueprint']);
            $org_id = $this->Organisation->createOrgFromName($org, 1, true);
            $org_data = $this->Organisation->find('first', array(
                'recursive' => -1,
                'fields' => array('name', 'uuid', 'local'),
                'conditions' => array('Organisation.id' => $org_id)
            ));
            $remote_org_id = $this->__createOrg($org_data);
            $this->__setSetting('MISP.host_org_id', $remote_org_id, $id, $org);
            $this->__report['servers'][$this->__currentUrl]['host_org_id'] = $remote_org_id;
            $this->__report['remote_orgs'][] = array('id' => $remote_org_id, 'name' => $org);
            $role_id = $this->__createRole($this->__config['role_blueprint']);
            $this->__report['servers'][$this->__currentUrl]['training_role_id'] = $role_id;
            $sync_user = $this->__createSyncUserLocally($remote_org_id, $org);
            $local_host_org = $this->Organisation->find('first', array(
                'recursive' => -1,
                'conditions' => array(
                    'Organisation.id' => Configure::read('MISP.host_org_id')
                ),
                'fields' => array(
                    'name', 'id', 'uuid'
                )
            ));
            $hub_org_id_on_remote = $this->__createOrg($local_host_org);
            $external_baseurl = empty(Configure::read('MISP.external_baseurl')) ? Configure::read('MISP.baseurl') : Configure::read('MISP.external_baseurl');
            $this->__report['servers'][$this->__currentUrl]['sync_connections'][] = $this->__addSyncConnection($external_baseurl, 'Exercise hub', $local_host_org, $hub_org_id_on_remote, $sync_user);
            $this->__report['servers'][$this->__currentUrl]['users'] = $this->__createUsers($remote_org_id, $role_id, $org, $id);
            if (!empty($this->__config['settings'])) {
                foreach ($this->__config['settings'] as $key => $value)
                $this->__setSetting($key, $value, $id, $org);
            }
            if ($this->__config['reset_admin_credentials']) {
                $this->__report['servers'][$this->__currentUrl]['management_account'] = $this->__reset_admin_credentials($this->__report);
            }
        }
        echo 'Setup complete. Please find the modifications below:' . PHP_EOL . PHP_EOL;
        echo json_encode($this->__report, JSON_PRETTY_PRINT);
    }

    private function __addSyncConnection($baseurl, $name, $local_host_org, $hub_org_id_on_remote, $sync_user)
    {
        $server = array(
            'name' => $name,
            'url' => $baseurl,
            'authkey' => $sync_user['User']['authkey'],
            'remote_org_id' => $hub_org_id_on_remote,
            'push' => 1,
            'pull' => 1,
            'self_signed' => 1
        );
        $options = array(
            'url' => $this->__currentUrl . '/servers/add',
            'method' => 'POST',
            'body' => $server
        );
        $response = $this->__queryRemoteMISP($options, true);
        if ($response->code != 200) {
            $this->__responseError($response, $options);
        } else {
            $response_data = json_decode($response->body, true);
            return array(
                'url' => $response_data['Server']['url'],
                'authkey' => $response_data['Server']['authkey']
            );
        }
    }

    private function __createSyncUserLocally($remote_org_id, $org)
    {
        $sync_role = $this->User->Role->find('first', array('recursive' => -1, 'conditions' => array('Role.name' => 'Sync user')));
        $sync_role = $sync_role['Role']['id'];
        $this->User->create();
        $this->User->save(array(
                'external_auth_required' => 0,
                'external_auth_key' => '',
                'server_id' => 0,
                'gpgkey' => '',
                'certif_public' => '',
                'autoalert' => 0,
                'contactalert' => 0,
                'disabled' => 0,
                'newsread' => 0,
                'change_pw' => 1,
                'authkey' => $this->User->generateAuthKey(),
                'termsaccepted' => 0,
                'org_id' => $remote_org_id,
                'role_id' => $sync_role,
                'email' => '<EMAIL>@' . $org . '.test'
        ));
        $user = $this->User->find('first', array('recursive' => -1, 'conditions' => array('User.email' => '<EMAIL>@' . $org . '.<EMAIL>')));
        return $user;
    }

    private function __responseError($response, $options)
    {
        echo sprintf(
            "Received a non-200 response (%s). Aborting.\nQueried URL: %s\n Query type: %s\n Request payload: %s\n\n",
            $response->code,
            $options['url'],
            $options['method'],
            empty($options['body']) ? '' : json_encode($options['body'], JSON_PRETTY_PRINT)
        );
        echo 'Setup failed. Output of what has been created:' . PHP_EOL . PHP_EOL;
        echo json_encode($this->__report, JSON_PRETTY_PRINT);
        die();
    }

    private function __createUsers($remote_org_id, $role_id, $org, $i)
    {
        $summary = array();
        for ($j = 1; $j < (1+$this->__config['user_count']); $j++) {
            $email = $this->__config['user_blueprint'];
            $email = str_replace('$ID', $i, $email);
            $email = str_replace('$ORGNAME', $org, $email);
            $email = str_replace('$USER_ITERATOR', $j, $email);
            $options = array(
                'url' => $this->__currentUrl . '/admin/users/index/searchall:' . $email,
                'method' => 'GET'
            );
            $response = $this->__queryRemoteMISP($options, true);
            if ($response->code != 200) {
                $this->__responseError($response, $options);
            }
            $newKey = $this->User->generateRandomPassword(32);
            if (!empty($response->body)) {
                $user = array(
                    'email' => $email,
                    'password' => $<PASSWORD>,
                    'role_id' => $role_id,
                    'org_id' => $remote_org_id
                );
                $options = array(
                    'url' => $this->__currentUrl . '/admin/users/add',
                    'method' => 'POST',
{'fraction_non_alphanumeric': 0.1709434201355889, 'fraction_numerical': 0.0043717924348983086, 'mean_word_length': 2.0127887001336133, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 273, 'https://': 0, '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': '2348661', 'n_tokens_mistral': 4863, 'n_tokens_neox': 4687, 'n_words': 829}