/** * Adds a new user to DB. * Do not add join_date and last_login column, they will be automatically * added. * Provide an $openid with the OpenID as value in order to register users * logging in this way. * * @param array $data corresponds to DB columns */ public function register($data, $openid = NULL) { $this->load->helper('array'); // TODO verify mandatory data existance // Process data. if (isset($data['password'])) { $data['password'] = sha1($data['password']); } if (empty($data['birth_date'])) { $data['birth_date'] = NULL; } $cols = ''; $vals = ''; foreach ($data as $col => $val) { if ($val === NULL) { $cols .= "{$col}, "; $vals .= "NULL, "; continue; } $cols .= "{$col}, "; if (is_int($val)) { $vals .= "{$val}, "; } else { if (is_string($val)) { $vals .= "'{$val}', "; } } } $cols = substr($cols, 0, -2); $vals = substr($vals, 0, -2); $query = $this->db->query("INSERT INTO `users`\n\t\t\t({$cols}, registration_date, last_login)\n\t\t\tVALUES ({$vals}, utc_timestamp(), utc_timestamp())"); if ($query === FALSE) { return FALSE; } // If registered with OpenID insert a row in `users_openid`. if ($openid) { // Find user_id. $query = $this->db->query("SELECT id from `users`\n\t\t\t\tWHERE username = '******'username']}'"); if ($query->num_rows() === 0) { return FALSE; } $user_id = $query->row()->id; // Insert row in `users_openid`. $query = $this->db->query("INSERT INTO `users_openid`\n\t\t\t\t(openid_url, user_id)\n\t\t\t\tVALUES ('{$openid}', {$user_id})"); if (!$query) { return FALSE; } } // If registered with internal authentication it needs to activate // the account. if ($data['auth_src'] == 'internal') { $activation_code = Users_model::gen_activation_code($data['username']); $user_id = $this->get_user_id($data['username']); $query = $this->db->query("INSERT INTO `users_unactivated`\n\t\t\t\t(user_id, activation_code)\n\t\t\t\tVALUES ({$user_id}, '{$activation_code}')"); $this->send_activation_email($user_id, $data['email'], $activation_code, $data['username']); } // TODO exception on failure return $query; }