-
Notifications
You must be signed in to change notification settings - Fork 9
/
Auth.php
executable file
·346 lines (275 loc) · 10.9 KB
/
Auth.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Example Auth controller using Authit
*
* @package Authentication
* @category Libraries
* @author Ron Bailey
* @version 1.0
*/
class Auth extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('authit');
$this->load->helper('authit');
$this->config->load('authit');
$this->load->helper('url');
}
public function index()
{
if(!logged_in()) redirect('auth/login');
// Redirect to your logged in landing page here
redirect('auth/dash');
}
/**
* Login page
*/
public function login()
{
if(logged_in()) redirect('auth/dash');
$this->load->library('form_validation');
$this->load->helper('form');
$data['error'] = false;
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run()){
if($this->authit->login(set_value('email'), set_value('password'))){
// Redirect to your logged in landing page here
redirect('auth/dash');
} else {
$data['error'] = 'Your email address and/or password is incorrect.';
}
}
$this->load->view('auth/login', $data);
}
/**
* Signup page
*/
public function signup()
{
// Redirect to your logged in landing page here
if(logged_in()) redirect('auth/dash');
$this->load->library('form_validation');
$this->load->helper('form');
$data['error'] = '';
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique['. $this->config->item('authit_users_table') .'.email]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length['. $this->config->item('authit_password_min_length') .']');
$this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|matches[password]');
if($this->form_validation->run()){
if($this->authit->signup(set_value('email'), set_value('password'))){
$this->authit->login(set_value('email'), set_value('password'));
// Do some post signup stuff like send a welcome email...
// Redirect to your logged in landing page here
redirect('auth/dash');
} else {
$data['error'] = 'Failed to sign up with the given email address and password.';
}
}
$this->load->view('auth/signup', $data);
}
/**
* Logout page
*/
public function logout()
{
if(!logged_in()) redirect('auth/login');
// Redirect to your logged out landing page here
$this->authit->logout('/');
}
/**
* Example dashboard page
*/
public function dash()
{
if(!logged_in()) redirect('auth/login');
echo 'Hi, '. user('email') .'. You have successfully logged in. <a href="'. site_url('auth/logout') .'">Logout</a>';
}
/**
* Forgot password page
*/
public function forgot()
{
// Redirect to your logged in landing page here
if(logged_in()) redirect('auth/dash');
$test_emails = $this->config->item('authit_test_emails');
$this->load->library('form_validation');
$this->load->helper('form');
$data['success'] = false;
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_exists');
if($this->form_validation->run()){
$email = $this->input->post('email');
$this->load->model('authit_model');
$user = $this->authit_model->get_user_by_email($email);
$slug = md5($user->id . $user->email . date('Ymd'));
$this->load->library('email');
$from = "'[email protected]', 'Example App'"; // Change these details
$subject = 'Reset your Password';
$message = 'To reset your password please click the link below and follow the instructions:
'. site_url('auth/reset/'. $user->id .'/'. $slug) .'
If you did not request to reset your password then please just ignore this email and no changes will occur.
Note: This reset code will expire after '. date('j M Y') .'.';
$this->email->from($from);
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
if ($test_emails) {
$this->savemails("Password Reset", $this->email->protocol, $this->email->mailtype, $from, $email, $subject, $message);
} else {
$this->email->send();
}
$data['success'] = true;
}
$this->load->view('auth/forgot_password', $data);
}
public function savemails($origin,$protocol,$mailtype,$from,$email,$subject,$message)
{
$obj = new stdClass();
$obj->origin = $origin;
$obj->protocol = $protocol;
$obj->mailtype = $mailtype;
$obj->curentdate = date('r');
$obj->from = $from;
$obj->email = $email;
$obj->subject = $subject;
$obj->message = $message;
$emailobj = serialize($obj);
$email_db = getcwd().DIRECTORY_SEPARATOR."application".DIRECTORY_SEPARATOR."test_emails".DIRECTORY_SEPARATOR."testemails.db";
if (is_writable($email_db)) {
$fp = fopen($email_db,"w");
fwrite($fp,$emailobj);
fclose($fp);
}
}
public function sentemails()
{
//view emails
$email_db = getcwd().DIRECTORY_SEPARATOR."application".DIRECTORY_SEPARATOR."test_emails".DIRECTORY_SEPARATOR."testemails.db";
if (file_exists($email_db)){
$emailobj = file_get_contents($email_db);
$obj = unserialize($emailobj);
if (!empty($obj)) { ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Emails Sent</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Email Preview</h1>
<div id="body">
<h3>Details</h3>
<ul>
<li><strong>Origin:</strong> <?php echo (isset($obj->origin) ? $obj->origin : ""); ?></li>
<li><strong>Protocol:</strong> <?php echo (isset($obj->protocol) ? $obj->protocol : ""); ?></li>
<li><strong>Mail type:</strong> <?php echo (isset($obj->mailtype) ? $obj->mailtype : ""); ?></li>
<li><strong>Sent At:</strong> <?php echo (isset($obj->curentdate) ? $obj->curentdate : ""); ?></li>
</ul>
<hr />
<p><strong>To:</strong> <?php echo (isset($obj->email) ? $obj->email : ""); ?></p>
<p><strong>From:</strong> <?php echo (isset($obj->from) ? $obj->from : ""); ?></p>
<p><strong>Subject:</strong> <?php echo (isset($obj->subject) ? $obj->subject : ""); ?></p>
<p><strong>Message Body:</strong> <?php echo (isset($obj->message) ? $obj->message : ""); ?></p>
</div>
</div>
</body>
</html>
<?php
}
}
}
/**
* CI Form Validation callback that checks a given email exists in the db
*
* @param string $email the submitted email
* @return boolean returns false on error
*/
public function email_exists($email)
{
$this->load->model('authit_model');
if($this->authit_model->get_user_by_email($email)){
return true;
} else {
$this->form_validation->set_message('email_exists', 'We couldn\'t find that email address in our system.');
return false;
}
}
/**
* Reset password page
*/
public function reset()
{
// Redirect to your logged in landing page here
if(logged_in()) redirect('auth/dash');
$this->load->library('form_validation');
$this->load->helper('form');
$data['success'] = false;
$user_id = $this->uri->segment(3);
if(!$user_id) show_error('Invalid reset code.');
$hash = $this->uri->segment(4);
if(!$hash) show_error('Invalid reset code.');
$this->load->model('authit_model');
$user = $this->authit_model->get_user($user_id);
if(!$user) show_error('Invalid reset code.');
$slug = md5($user->id . $user->email . date('Ymd'));
if($hash != $slug) show_error('Invalid reset code.');
$this->form_validation->set_rules('password', 'Password', 'required|min_length['. $this->config->item('authit_password_min_length') .']');
$this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|matches[password]');
if($this->form_validation->run()){
$this->authit->reset_password($user->id, $this->input->post('password'));
$data['success'] = true;
}
$this->load->view('auth/reset_password', $data);
}
}