public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); if (!isset($qd['fields']) || !is_array($qd['fields'])) { throw new Exception($this->msg->_('/new-question/errors/invalid-format')); } $this->n_answers = count($this->answers); if (!empty($extra)) { if (!is_array($extra) || count($extra) != count($qd['fields'])) { $k = '/new-question/errors/invalid-format'; throw new Exception($this->msg->_($k)); } } else { $extra = false; } $this->n_real_answers = preg_match_all(':<\\s*f\\s*/>:', $this->body); $err = array(); foreach ($qd['fields'] as $i => $f) { if (!is_string($f)) { $err['fields'] = $this->msg->_('invalid-format', [$f]); } else { if (!$extra) { $hex_strings = []; do { $hex_id = '#' . random_hex_string(8); } while (in_array($hex_id, $hex_strings)); // just in case $hex_strings[] = $hex_id; } else { $hex_id = $extra[$i]; } $real = $i < $this->n_real_answers; $this->answers[] = array('text' => $f, 'hex_id' => $hex_id, 'real' => $real); $this->answers_by_hex_id[$hex_id] = array('text' => $f, 'real' => $real); $this->n_answers++; } } if ($err) { throw new Exception(my_json_encode($err)); } if ($this->n_real_answers > $this->n_answers) { throw new Exception($this->msg->_('/new-question/errors/ft-num-fields')); } }
public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); $err = []; if (!isset($qd['shuffle'])) { $qd['shuffle'] = ''; } switch ($qd['shuffle']) { case 'left': $this->shuffle_left = true; $this->shuffle_right = false; break; case 'right': $this->shuffle_left = false; $this->shuffle_right = true; break; case 'both': $this->shuffle_left = true; $this->shuffle_right = true; break; default: $err['shuffle'] = $this->msg->_('invalid-format'); } if (!isset($qd['items'])) { $qd['items'] = array(); } if (!is_array($qd['items'])) { $err['items'] = $this->msg->_('invalid-format'); } $this->n_rows = count($qd['items']); if (!empty($extra)) { if (!is_array($extra) || count($extra) != 2 || !is_array($extra[0]) || !is_array($extra[1]) || count($extra[0]) != $this->n_rows || count($extra[1]) != $this->n_rows) { $k = '/new-question/errors/invalid-format'; throw new Exception($this->msg->_($k)); } } else { $extra = false; } foreach ($qd['items'] as $i => $a) { if (!is_array($a) || !array_key_exists('left', $a) || !is_string($a['left']) || !array_key_exists('right', $a) || !is_string($a['right'])) { $err['items'] = $this->msg->_('invalid-format'); break; } if (!$extra) { $hex_strings = []; do { $left_hex_id = '#' . random_hex_string(8); $right_hex_id = '#' . random_hex_string(8); } while (in_array($left_hex_id, $hex_strings) || in_array($right_hex_id, $hex_strings)); // just in case $hex_strings[] = $left_hex_id; $hex_strings[] = $right_hex_id; } else { if (!is_string($extra[0][$i]) || !is_string($extra[1][$i])) { $err['items'] = $this->msg->_('invalid-format'); break; } $left_hex_id = $extra[0][$i]; $right_hex_id = $extra[1][$i]; } $this->left_column[] = array('text' => $a['left'], 'hex_id' => $left_hex_id); $this->right_column[] = array('text' => $a['right'], 'hex_id' => $right_hex_id); $this->left_column_by_hex_id[$left_hex_id] = $a['left']; $this->right_column_by_hex_id[$right_hex_id] = $a['right']; } if ($err) { throw new Exception(my_json_encode($err)); } }
public function resend_confirmation_email($email) { $sql = 'SELECT user_id, status FROM `user` WHERE email = :email;'; $s = $this->conn->prepare($sql); if (!$s) { throw new DatabaseException($this->conn->errorInfo()[2]); } $s->bindValue(':email', $email); if (!$s->execute()) { throw new DatabaseException($s->errorInfo()[2]); } $user_data = $s->fetch(PDO::FETCH_ASSOC); if (empty($user_data)) { $key = '/signup/confirmation-ui/new-code/unregistered'; throw new Exception($this->msg->_($key)); } if ($user_data['status'] !== 'pending-activation') { $key = '/signup/confirmation-ui/new-code/already-active'; throw new Exception($this->msg->_($key)); } // delete old confirmation codes from this user $sql = 'DELETE FROM `confirmation_code` WHERE user_id = :user_id'; $s = $this->conn->prepare($sql); if (!$s) { throw new DatabaseException($this->conn->errorInfo()[2]); } $s->bindValue(':user_id', $user_data['user_id']); if (!$s->execute()) { throw new DatabaseException($s->errorInfo()[2]); } // generate new code and send it $confirmation_code = random_hex_string(32); $sql = 'INSERT INTO `confirmation_code` (code, user_id, expires_at) VALUES (:code, :user_id, :expires_at)'; $s = $this->conn->prepare($sql); if (!$s) { throw new DatabaseException($this->conn->errorInfo()[2]); } $s->bindValue(':user_id', $user_data['user_id']); $s->bindValue(':code', $confirmation_code); $s->bindValue(':expires_at', date('Y-m-d H:i:s', strtotime('+24 hours'))); if (!$s->execute()) { throw new DatabaseException($s->errorInfo()[2]); } MailSender::send_confirmation_mail($this->msg, $email, $confirmation_code); }
public function from_user($qd, $extra = array()) { parent::from_user($qd, $extra); $err = []; if (!isset($qd['answers'])) { $qd['answers'] = array(); } if (!is_array($qd['answers'])) { $err['answers'] = $msg->_('invalid-format'); } if (!empty($extra)) { if (!is_array($extra) || count($extra) != count($qd['answers'])) { $k = '/new-question/errors/invalid-format'; throw new Exception($this->msg->_($k)); } } else { $extra = false; } foreach ($qd['answers'] as $i => $a) { if (!is_array($a) || !(!empty($a['text']) && isset($a['fixed']) && isset($a['correct'])) || !is_string($a['text']) || !Validator::validate_boolean($a['fixed']) || !Validator::validate_boolean($a['correct'])) { $err['answers'] = $this->msg->_('invalid-format'); break; } if (!$extra) { $hex_strings = []; do { $hex_id = '#' . random_hex_string(8); } while (in_array($hex_id, $hex_strings)); // just in case $hex_strings[] = $hex_id; } else { $hex_id = $extra[$i]; } $this->answers[] = array('text' => $a['text'], 'correct' => $a['correct'], 'fixed' => $a['fixed'], 'hex_id' => $hex_id); $this->answers_by_hex_id[$hex_id] = array('text' => $a['text'], 'correct' => $a['correct'], 'fixed' => $a['fixed']); if ($a['correct']) { $this->correct_answers++; } $this->total_answers++; } if ($this->total_answers == 0) { $err['answers'] = $this->msg->_('mc-at-least-one'); } if ($err) { throw new Exception(my_json_encode($err)); } set_empty_if_undefined($qd['min_answers']); set_empty_if_undefined($qd['max_answers']); if (!is_int($qd['min_answers'])) { $err['min_answers'] = $this->msg->_('invalid-format'); } elseif (!($qd['min_answers'] >= 0) || !($qd['min_answers'] <= $this->correct_answers)) { $err['min_answers'] = $this->msg->_('mc-min'); } elseif ($qd['min_answers'] == $this->total_answers) { $err['min-answers'] = $this->msg->_('mc-min-lt-total'); } else { $this->min_answers = $qd['min_answers']; } if (!is_int($qd['max_answers'])) { $err['max_answers'] = $this->msg->_('invalid-format'); } elseif (!($qd['max_answers'] >= $this->correct_answers) || !($qd['max_answers'] <= $this->total_answers)) { $err['max_answers'] = $this->msg->_('mc-max'); } elseif ($qd['max_answers'] == 0) { $err['max-answers'] = $this->msg->_('mc-max-gt-zero'); } else { $this->max_answers = $qd['max_answers']; } if ($err) { throw new Exception(my_json_encode($err)); } }