Luigi Santivetti | 7bab494 | 2019-06-16 07:40:53 +0000 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | class 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 | { |
| 23 | /* Got to open a connection here as validation may require one */ |
| 24 | if (!$this->load->database()) |
| 25 | redirect(base_url('index.html')); |
| 26 | |
| 27 | $this->load->helper(array('form', 'url')); |
| 28 | $this->load->library('form_validation'); |
| 29 | |
| 30 | $this->form_validation->set_rules('username', 'Username', |
| 31 | 'required|min_length[5]|max_length[12]|is_unique[users.username]', |
| 32 | array( |
| 33 | 'required' => 'You must provide a %s', |
| 34 | 'min_length' => '%s must be more than 5 chars', |
| 35 | 'max_length' => '%s must be less than 12 chars', |
| 36 | 'is_unique' => 'This %s already exists' |
| 37 | ) |
| 38 | ); |
| 39 | |
| 40 | $this->form_validation->set_rules('password', 'Password', 'required', |
| 41 | array('required' => 'You must provide a %s') |
| 42 | ); |
| 43 | |
| 44 | $this->form_validation->set_rules('passconf', 'Password Confirmation', |
| 45 | 'required|matches[password]', |
| 46 | array( |
| 47 | 'required' => 'You must provide a %s', |
| 48 | 'matches' => 'Passowrd confirmation didn\'t match' |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | $this->form_validation->set_rules('email', 'Email', 'required', |
| 53 | array('required' => 'You must provide a %s') |
| 54 | ); |
| 55 | |
| 56 | if ($this->is_post_invalid()) |
| 57 | { |
| 58 | /* Validation errors already set */ |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | /* Add backticks on ientifiers */ |
| 63 | $this->db->protect_identifiers('users', TRUE); |
| 64 | |
| 65 | /* Always use query bindings as they are automatically escaped */ |
| 66 | $stmt = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)"; |
| 67 | $data = $this->input->post(array('username', 'email')); |
| 68 | $password = $this->input->post('password'); |
| 69 | $hash_password = password_hash($password, PASSWORD_DEFAULT); |
| 70 | $data['password'] = $hash_password; |
| 71 | |
| 72 | if (!$this->db->query($stmt, $data)) |
| 73 | { |
| 74 | /* Debug: |
| 75 | * |
| 76 | * $error = $this->db->error(); |
| 77 | * var_dump($error); |
| 78 | */ |
| 79 | $this->form_validation->set_message('submit_msg', 'Didn\'t work, :|'); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | $this->form_validation->set_string('Invite succesfully sent'); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $this->db->close(); |
| 88 | $this->load->view('page/'.$file_html); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | redirect(base_url('index.html')); |
| 93 | } |
| 94 | } |
| 95 | } |