public function add_first_manager($attr)
 {
     if ($this->has_manager()) {
         $m = $this->msg->_('firstrun/manager-exists');
         throw new Exception($m);
     }
     $err = array();
     if (mb_strlen($attr['full_name']) == 0) {
         $err['full_name'] = $this->msg->_('/signup/errors/full-name.empty');
     }
     if (!Validator::validate_full_name($attr['full_name'])) {
         $err['full_name'] = $this->msg->_('/signup/errors/full-name.two-words', [255]);
     }
     if (!Validator::validate_email($attr['email'])) {
         $err['email'] = $this->msg->_('/signup/errors/e-mail.invalid');
     }
     if (!Validator::validate_gender($attr['gender'])) {
         $err['gender'] = $this->msg->_('/signup/errors/gender.invalid');
     }
     if (!Validator::validate_birth_date($attr['birth_date'])) {
         $err['birth_date'] = $this->msg->_('/signup/errors/b-date.invalid');
     }
     if (!Validator::validate_username($attr['username'])) {
         $err['username'] = $this->msg->_('/signup/errors/username.fmt', [3, 32]);
     }
     if (!Validator::validate_password($attr['password'])) {
         $err['password'] = $this->msg->_('/signup/errors/password.fmt', [6, 32]);
     }
     if (!empty($err)) {
         throw new Exception(implode("\n", $err));
     }
     // the data was validated...
     // now, add to the database
     $this->conn->beginTransaction();
     $sql = 'INSERT INTO `user`
           (full_name, email, gender, role,
            birth_date, username, password, status)
         VALUES
           (:full_name, :email, :gender, :role,
            :birth_date, :username, :password, :status)';
     $s = $this->conn->prepare($sql);
     if (!$s) {
         throw new DatabaseException($this->conn->errorInfo()[2]);
     }
     $s->bindValue(':full_name', $attr['full_name']);
     $s->bindValue(':email', $attr['email']);
     $s->bindValue(':gender', $attr['gender']);
     $s->bindValue(':role', 'manager');
     $s->bindValue(':birth_date', $attr['birth_date']);
     $s->bindValue(':username', $attr['username']);
     $s->bindValue(':password', password_hash($attr['password'], PASSWORD_BCRYPT));
     $s->bindValue(':status', 'active');
     if (!$s->execute()) {
         throw new DatabaseException($s->errorInfo()[2]);
     }
     $this->conn->commit();
     $file = fopen(get_config_dir() . 'firstmanager.ini', 'w');
     fwrite($file, 'setup = true');
     fclose($file);
 }
Exemple #2
0
<?php

$db_created = file_exists(get_config_dir() . '/dbsettings.ini');
$model = new Model($first_run = !$db_created);
$has_manager = ($db_created and $model->has_manager());
$msg = new Messages($GLOBALS['locale'], 'signup');
if ($has_manager) {
    // database created and already has the first manager
    // there's nothing to set up anymore
    header('Location: /');
    exit;
} else {
    if ($db_created) {
        // database created, but no manager yet
        $fields = array('full_name' => '', 'email' => '', 'gender' => '', 'birth_date' => '', 'username' => '');
        if (req_data('POST', 'action') === 'addfirstmanager') {
            $manager_data = array();
            foreach ($fields as $f => $v) {
                $manager_data[$f] = $fields[$f] = trim(req_data('POST', $f));
            }
            // password: do not trim
            $manager_data['password'] = req_data('POST', 'password');
            $manager_data['password2'] = req_data('POST', 'password2');
            try {
                $model->add_first_manager($manager_data);
            } catch (Exception $e) {
                $GLOBALS['newmanagererror'] = $e->getMessage();
                include 'newmanager.html.php';
                exit;
            }
            exit_with_message($msg->_('first-manager-created'));