blob: 064bd45545791e4f535f843006a726f1164f8b9e [file] [log] [blame]
Luigi Santivetti7bab4942019-06-16 07:40:53 +00001<?php
2
3class Invite extends CI_Controller {
4 private function is_valid_file($rel_path = '/', $file_html = 'home.html')
5 {
6 $abs_path = APPPATH.$rel_path.$file_html;
7 if (file_exists($abs_path))
8 return true;
9
10 return false;
11 }
12
13 private function is_post_invalid()
14 {
15 return !$this->form_validation->run();
16 }
17
18 public function view($file_name = 'invite')
19 {
20 $file_html = $file_name.'.html';
21 if ($this->is_valid_file('views/page/', $file_html))
22 {
Luigi Santivetti7bab4942019-06-16 07:40:53 +000023 $this->form_validation->set_rules('username', 'Username',
24 'required|min_length[5]|max_length[12]|is_unique[users.username]',
25 array(
26 'required' => 'You must provide a %s',
27 'min_length' => '%s must be more than 5 chars',
28 'max_length' => '%s must be less than 12 chars',
29 'is_unique' => 'This %s already exists'
30 )
31 );
32
33 $this->form_validation->set_rules('password', 'Password', 'required',
34 array('required' => 'You must provide a %s')
35 );
36
37 $this->form_validation->set_rules('passconf', 'Password Confirmation',
38 'required|matches[password]',
39 array(
40 'required' => 'You must provide a %s',
41 'matches' => 'Passowrd confirmation didn\'t match'
42 )
43 );
44
45 $this->form_validation->set_rules('email', 'Email', 'required',
46 array('required' => 'You must provide a %s')
47 );
48
49 if ($this->is_post_invalid())
50 {
51 /* Validation errors already set */
52 }
53 else
54 {
55 /* Add backticks on ientifiers */
56 $this->db->protect_identifiers('users', TRUE);
57
58 /* Always use query bindings as they are automatically escaped */
59 $stmt = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
60 $data = $this->input->post(array('username', 'email'));
61 $password = $this->input->post('password');
Luigi Santivetti57a98ca2020-10-13 22:55:51 +010062
63 $pepp = getenv('HOST_PEPPER');
64 $pepp_password = hash_hmac("sha256", $password, $pepp);
65 $hash_password = password_hash($pepp_password, PASSWORD_BCRYPT);
66
Luigi Santivetti7bab4942019-06-16 07:40:53 +000067 $data['password'] = $hash_password;
68
69 if (!$this->db->query($stmt, $data))
70 {
71 /* Debug:
72 *
73 * $error = $this->db->error();
74 * var_dump($error);
75 */
76 $this->form_validation->set_message('submit_msg', 'Didn\'t work, :|');
77 }
78 else
79 {
80 $this->form_validation->set_string('Invite succesfully sent');
81 }
82 }
83
Luigi Santivetti7bab4942019-06-16 07:40:53 +000084 $this->load->view('page/'.$file_html);
85 }
86 else
87 {
88 redirect(base_url('index.html'));
89 }
90 }
91}