/
pasta.php
182 lines (156 loc) · 5.22 KB
/
pasta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Script to prevent direct URL access to this file.
* Should include at every beginning of file.
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Pasta main controller, control user login, user logout, and
* handles session variables.
*
* Author: Charles
*/
class Pasta extends MY_Controller {
function Pasta() {
parent::__construct();
/**
* Load models
*/
$this->load->model('User', 'logins_table');
}
public function index() {
/**
* Main site title
*-----------------*/
$data['title'] = "P.A.S.T.A. - Personal Academic Schedule Timetable Arranger";
// check if the user is already logged in
if ($this->user_is_logged_in()) {
// if user is already logged in, do an immediate redirect
redirect('profile', 'refresh');
} else {
// display the main view
$this->put('pasta_main_view', $data);
}
}
// Registration function
public function register() {
// Setup and commit registration login validations criteria.
// The last parameter with `call_back_` begins are custom
// validation functions
$this->form_validation->set_rules('student_id', 'Student ID',
'required|exact_length[7]|numeric|trim|xss_clean|callback_is_unique_student_it');
$this->form_validation->set_rules('password', 'Password',
'required|min_length[6]|alpha_numeric');
$this->form_validation->set_rules('password_confirm', 'Password Confirm',
'required|min_length[6]|alpha_numeric|matches[password]');
$this->form_validation->set_rules('first_name', 'First Name',
'callback_alpha_whitespace');
$this->form_validation->set_rules('last_name', 'Last Name',
'callback_alpha_whitespace');
// validate the form with the registration rules
if ($this->form_validation->run() == FALSE) {
$this->index();
// $this->load->view('/scrape_views/form');
} else {
// query the database to check if there
// is an user with this POST student id
$query = $this->db->get_where('logins',
array('student_id' => $this->input->post('student_id')));
// echo $query->num_rows();
if ($query->result()) {
echo "User is already registered.";
} else {
$this->db->insert('logins', array(
'student_id' => $this->input->post('student_id'),
// store user password in sha1
'password' => $this->encrypt->sha1(
$this->input->post('password')),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'program' => $this->input->post('program')
));
// update session with appropriate information and
// set user to be logged in
$this->session->set_userdata(array(
'student_id' => $this->input->post('student_id'),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'logged_in' => true
));
// redirect user to the first step of the registration process
redirect('coursecompleted', 'location');
}
}
}
public function logout() {
// unassign logged_in boolean
$this->session->set_userdata('logged_in', FALSE);
// redirect to pasta main
redirect('pasta', 'refresh');
}
public function user_login() {
// setup login form validation
$this->form_validation->set_rules(
'login_student_id',
'Student ID',
'required|trim|xss_clean|exact_length[7]|numeric|callback_has_record'
);
$this->form_validation->set_rules(
'login_password',
'Password',
'required|trim|xss_clean|required|min_length[6]|alpha_numeric'
);
if ($this->form_validation->run() == FALSE) {
$this->index();
} else {
// ------------------------------------
// initialize sessions
// ------------------------------------
$user_data = $this->logins_table->find_by_student_id(
$this->input->post('login_student_id'));
$this->session->set_userdata(array(
'id' => $user_data['id'],
'student_id' => $user_data['student_id'],
'first_name' => $user_data['first_name'],
'last_name' => $user_data['last_name'],
'logged_in' => true
));
// redirect to user profile page
redirect('profile', 'redirect');
}
}
/**
* Callback function for an alpha_whitespace form validation
* Allowing white space in family and last name form validation
*/
public function alpha_whitespace($str)
{
$this->form_validation->set_message(
'alpha_whitespace', 'Invalid character.');
return (preg_match("/^([-a-z-\s])+$/i", $str)) ? TRUE : FALSE;
}
/**
* Callback function for an unique student it form validation
* Disallow already taken student it
*/
public function is_unique_student_it($student_id)
{
$this->form_validation->set_message('is_unique_student_it',
'Student ID already taken.');
$query = $this->logins_table->find_by_student_id($student_id);
return ($query == NULL) ? TRUE : FALSE;
}
/**
* Callback function for if an user tries to login with unexisting
* student ID from the record
*/
public function has_record($student_id)
{
$this->form_validation->set_message('has_record',
'User record not found.');
$query = $this->logins_table->find_by_student_id($student_id);
return ($query == NULL) ? FALSE : TRUE;
}
}
/* End of file Pasta.php */
/* Location: ./application/controllers/welcome.php */