Menu

[r1]: / app / controllers / LogInCT.php  Maximize  Restore  History

Download this file

421 lines (354 with data), 10.7 kB

  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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* Controller class used for handing login access and authentication.
*
* @author Dana Murad
* @file LogInCT.php
*
* @todo
*/
require_once("../config/main.php");
class LogInCT extends CT
{
//__________________________________ M E M B E R S _____________________________________
/**
* @desc Constants for session name of logged-in flag.
* @const
*/
const LOGGED_IN_FLAG = "logged_in";
//__________________________________ C O N S T R U C T O R ____________________________
/**
* @desc Constructor
*/
function __construct
(
)
{
parent::__construct();
}
//__________________________________ M A I N __________________________________________
/**
* @desc Logs a user in and forwards to a page, mybooks page by default.
* @param string $gotoPage
*/
public function loginUser($gotoPage = "mylistings.php")
{
$_SESSION[LogInCT::LOGGED_IN_FLAG] = TRUE;
header("location: " . BASE_URL . $gotoPage);
exit();
}
/**
* @desc Registers a user and sends validation email.
* @param string $firstName
* @param string $lastName
* @param string $eduEmail
* @param string $phone
* @param string $username
* @param string $password
* @param string $state
* @param string $city
* @param string $secretQuestion
* @param string $secretAnswer
* @return array
*/
public function registerUser
(
$firstName,
$lastName,
$eduEmail,
$phone,
$username,
$password,
$state,
$city,
$secretQuestion,
$secretAnswer
)
{
try
{
$verStr = getRandomString(15);
$password = SHA1($password);
$userVO = new UserVO(INVALID,0,$username,$password,$firstName,$lastName,$city,$state,$eduEmail,$phone,$verStr,$secretQuestion,$secretAnswer,0,0,0,date("Y-m-d"));
$userDAO = new DAO($userVO);
$userDAO->begin();
if(!$userDAO->insert())
{
throw new Exception("Could not insert new user into database.");
}
$mailer = new Mailer();
$actURL = BASE_URL . "login.php?pageAction=activate&verStr=$verStr&username=$username";
$subject = SITE_NAME . "Registration: Activation Required";
$body = "Hi $firstName,\n\nThanks for signing up to " . SITE_NAME . ". To complete your registration, you must go to the following link within 10 days and activate your account." .
" You can click or copy and paste it into your internet browser. <br/><br/> <a href='$actURL'>$actURL</a> <br/><br/> Regards, <br/><br/><br/><br/><b> Administrator </b>";
if(!$mailer->send($subject,$body,$eduEmail,NOREPLY_EMAIL,SITE_NAME))
{
$userDAO->rollback();
throw new Exception("Could not send registration activation email.");
}
$userDAO->commit();
}
catch(Exception $e)
{
Error::report($e);
}
}
/**
* @desc Activates a user via activation string and the username. If one user is found, the varStr is cleared, status set to on.
* @param string $verStr
* @param string $username
* @return array
*/
public function activateUser
(
$verStr,
$username
)
{
try
{
$userVO = new UserVO();
$userDAO = new DAO($userVO);
$username = mysql_real_escape_string($username);
$voList = $userDAO->findWhere($userVO,"`username` = '$username'");
if(count($voList) == 1)
{
if($voList[0]->status == 1) // already active
{
return true;
}
if($voList[0]->verStr == $verStr) //verStr matches
{
$voList[0]->verStr = "";
$voList[0]->status = 1;
$voList[0]->dateRegistered = Date::getCurrentMySQLDate();
$userDAO->update($voList[0]);
return true;
}
}
return false;
}
catch(Exception $e)
{
Error::report($e);
}
}
/**
* @desc Authenticate user
* @param $userid
* @param $password
* @return bool
*/
public function resetUserPassword
(
$email
)
{
if(checkEmailAddress($email))
{
$userVO = new CustomerVO();
$customerDAO = new DAO($userVO);
$voList = $customerDAO->findWhere("email = '$email'");
if(isset($voList[0]))
{
$vo = $voList[0];
$mailer = new Mailer();
$newPass = getRandomString(7);
$vo->passwd = MD5($newPass);
$vo->status = CustomerVO::INACTIVE;
$customerDAO = new DAO($vo);
$customerDAO->update();
# Send Message
$msg = "Dear " . $vo->firstName . "<br /><br />You recently requested to reset your password at myforeclosuresearch.com. Your username is still " . $vo->username .
" and your temporary password is <b>" . $newPass . "</b>. <u>You must login and reactivate your account within 48 hours</u>.<br/><br/>Login is located at <a href='" . BASE_URL . "' >" . BASE_URL . "</a>. If you have any questions, please don't hesitate to contact us.
<br /><br /><br /><br />Management";
if ($mailer->send("Your Password Has Been Reset",$msg,$email))
{
return true;
}
else
{
header("Location: /confirmation.php?sec=6"); // Error occured page.
}
}
else
{
$this->addMessage("E-mail address is incorrect and does not exist. Please confirm it is correct or try another.");
}
}
else
{
$this->addMessage("Invalid or empty email address. Must be in the form example@host.com.");
}
return false;
}
/**
* @desc Update the password a user
*
*/
public function resetPassword
(
$newPassword
)
{
$userVO = new CustomerVO();
$customerDAO = new DAO($userVO);
$voList = $customerDAO->findWhere("username = ''");
$voList[0]->passwd = $newPassword;
$customerDAO = new DAO($voList[0]);
$customerDAO->update();
}
//__________________________________ V A L I D A T I O N S ____________________________
/**
* @desc Authenticate user, sets userID and userName session.
* @param string $userid
* @param string $password
* @return bool
*/
public function isValidUser
(
$username,
$password
)
{
$userVO = new UserVO();
$userDAO = new DAO($userVO);
$username = mysql_real_escape_string($username);
$password = encryptPassword($password);
$voList = $userDAO->findWhere($userVO, "username = '$username' AND password = '$password' AND status = 1");
if(isset($voList[0]))
{
$userVO = $voList[0];
$userVO->failedLoginCount = 0;
$userDAO->update($userVO);
$_SESSION['userID'] = $voList[0]->id;
$_SESSION['username'] = $voList[0]->username;
$_SESSION['displayName'] = $voList[0]->firstName;
return TRUE;
}
$voList = $userDAO->findWhere($userVO, "username = '$username'");
if(isset($voList[0]))
{
$userVO = $voList[0];
if($userVO->failedLoginCount >= FAILED_LOGIN_COUNT)
{
$this->addMessage("You have reached the maximum number of times you can attempt to login. Your account has been locked and you must attempt recovery.");
$userVO->status = UserVO::INACTIVE;
$userDAO->update($userVO);
return FALSE;
}
else
{
$userVO->failedLoginCount++;
$userDAO->update($userVO);
return FALSE;
}
}
return FALSE;
}
/**
* @desc checks fields of the registration process.
* @param string $firstName
* @param string $lastName
* @param string $eduEmail
* @param string $phone
* @param string $username
* @param string $password
* @param string $confirmPassword
* @param string $state
* @param string $city
* @param string $secretQuestion
* @param string $secretAnswer
* @param string $agreeTerms
* @return array
*/
public function isValidRegistration
(
$firstName,
$lastName,
$eduEmail,
$phone,
$username,
$password,
$confirmPassword,
$state,
$city,
$secretQuestion,
$secretAnswer,
$agreeTerms
)
{
$noError = TRUE;
if(empty($firstName) || empty($lastName))
{
$this->addMessage("Both first name and last name fields are required.");
$noError = FALSE;
}
if(empty($eduEmail))
{
$this->addMessage("Your college or educational email address is required. (Must end in .edu extension). ");
$noError = FALSE;
}
else
{
$pattern = '#^[A-Za-z0-9._%-]+@[a-zA-Z0-9.-]+\.edu$#';
if(DEBUG)
{
$pattern = '#^[A-Za-z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}#';
}
if (preg_match($pattern,$eduEmail) == 0)
{
$this->addMessage("You must provide an email address that ends in .edu such as \"student@university.edu\". Please read our FAQ if any questions.");
$noError = FALSE;
}
}
if(empty($username))
{
$this->addMessage("Please type a desired username that you'll use for logging in.");
$noError = FALSE;
}
else if (preg_match("/\s+/",$username))
{
$this->addMessage("Usernames cannot contain spaces. You can use letters, numbers and the common printable special characters on your keyboard");
$noError = FALSE;
}
else
{
$userVO = new UserVO();
$userDAO = new DAO($userVO);
$voList = $userDAO->findWhere("","`username` = '$username'","","","","LIMIT 1");
if(!empty($voList))
{
$this->addMessage("We're sorry, the username $username has already been taken, please try another.");
$noError = FALSE;
}
}
if(empty($password))
{
$this->addMessage("You must provide a password to authenticate you when you log in. Choose a password that is atleast 6 characters and contains letters and numbers.");
$noError = FALSE;
}
elseif($password != $confirmPassword)
{
$this->addMessage("Passwords do not match. Please type your password exactly the same twice to ensure correct capture.");
$noError = FALSE;
}
if(empty($state) || empty($city))
{
$this->addMessage("Please choose a state then a city closest to you.");
$noError = FALSE;
}
if(empty($secretQuestion) || empty($secretAnswer))
{
$this->addMessage("Please provide a question to be asked of you when you lose your password. Then provide the answer that only you would know.");
$noError = FALSE;
}
if(empty($agreeTerms))
{
$this->addMessage("Please read and agree to our terms and conditions before registering. You are legally bound by it.");
$noError = FALSE;
}
return $noError;
}
//__________________________________ U T I L I T Y ____________________________________
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.