Subversion Repositories cheapmusic

Rev

Rev 31 | Rev 36 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 - 1
<?php
2
// Email sending functions
3
include_once 'includes/email_functions.php';
4
include_once 'includes/password.php';
5
 
31 - 6
// Include Session Handling
7
require_once('includes/session.php');
25 - 8
 
9
// Include config file
10
require_once 'includes/config.php';
11
 
12
// Load and initialize user class
13
require_once 'includes/User.class.php';
14
$user = new User();
15
 
16
if(isset($_POST['signupSubmit'])){
17
	$valErr = 0;
18
 
19
	// Store post data into session
20
	$_SESSION['signup_post_data'] = $_POST;
21
 
22
	// Get user inputs
23
	$first_name = $_POST['first_name'];
24
	$last_name = $_POST['last_name'];
25
	$email = $_POST['email'];
26 - 26
	$zip = $_POST['zip'];
25 - 27
	$password = $_POST['password'];
28
	$confirm_password = $_POST['confirm_password'];
29
 
30
	if(empty($first_name)){
31
		$valErr = 1;
32
		$sessData['field_error']['first_name'] = 'Please enter your first name.';
33
	}
31 - 34
/*
25 - 35
	if(empty($last_name)){
36
		$valErr = 1;
37
		$sessData['field_error']['last_name'] = 'Please enter your last name.';
38
	}
31 - 39
*/
25 - 40
	if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)){
41
		$valErr = 1;
42
		$sessData['field_error']['email'] = 'Please enter a valid email.';
43
	}
44
	if(empty($password)){
45
		$valErr = 1;
46
		$sessData['field_error']['password'] = 'Please enter account password.';
47
	}
48
	if(empty($confirm_password)){
49
		$valErr = 1;
50
		$sessData['field_error']['confirm_password'] = 'Please confirm your password.';
51
	}elseif($password !== $confirm_password){
52
		$valErr = 1;
53
		$sessData['field_error']['confirm_password'] = 'Confirm password does not match the password.';
54
	}
55
 
56
	if($valErr == 0){
57
		// Check whether user exists in the database
58
		$cond['where'] = array('email' => $email);
59
		$cond['return_type'] = 'count';
60
		$userCount = $user->getRows($cond);
61
		if($userCount > 0){
62
			$sessData['status']['type'] = 'error';
63
			$sessData['status']['msg'] = 'Email already exists, please use another email.';
64
		}else{
65
			// Email verification code
66
			$uniqidStr = md5(uniqid(mt_rand()));
67
 
68
			// Insert user data in the database
69
			$userData = array(
70
				'first_name' => $first_name,
71
				'last_name' => $last_name,
72
				'email' => $email,
73
				'password' => password_hash($password, PASSWORD_DEFAULT),
26 - 74
				'zip' => $zip,
25 - 75
				'activation_code' => $uniqidStr
76
			);
77
			$insert = $user->insert($userData);
78
 
79
			// Set status based on data insert
80
			if($insert){
81
				// Remove post data from session
82
				unset($_SESSION['signup_post_data']);
83
 
84
				// Send account verification email
85
				@emailVerification($userData);
86
 
87
				$sessData['status']['type'] = 'success';
88
				$sessData['status']['msg'] = 'Your registration was successful. Please check your email inbox to verify and activate your account.';
89
 
90
				// Remove post data from session
91
				unset($_SESSION['signup_post_data']);
92
			}else{
93
				$sessData['status']['type'] = 'error';
94
				$sessData['status']['msg'] = 'Some problem occurred, please try again.';
95
			}
96
		}
97
	}else{
98
        $sessData['status']['type'] = 'error';
99
        $sessData['status']['msg'] = 'Please fill all mandatory fields.';
100
    }
101
 
102
	// Store signup status into the session
103
    $_SESSION['sessData'] = $sessData;
30 - 104
    $redirectURL = ($sessData['status']['type'] == 'success')?'index.php':'registration.php';
25 - 105
 
106
	// Redirect to the home/login page
31 - 107
    MySessionHandler::commit(session_id());
25 - 108
    header("Location:".$redirectURL);
109
	exit;
110
}elseif(isset($_POST['loginSubmit'])){
111
	// Get user inputs
112
	$email = $_POST['email'];
113
	$password = $_POST['password'];
114
 
115
	// Check whether login details are empty
116
    if(!empty($email) && !empty($password)){
117
		// Get user data from user class
118
        $conditions['where'] = array(
119
            'email' => $email,
120
            'status' => '1'
121
        );
122
        $conditions['return_type'] = 'single';
123
        $userData = $user->getRows($conditions);
124
 
125
		if(!empty($userData) && password_verify($password, $userData['password'])){
126
			// Set user data and status based on login credentials
127
			if($userData['activated'] == '0'){
128
				$sessData['status']['type'] = 'error';
129
				$sessData['status']['msg'] = 'Your account activation is pending, please check your email inbox to verify and activate your account.';
130
			}else{
131
				// If remember me is checked
132
				if (isset($_POST['rememberMe']) && $_POST['rememberMe'] == 1) {
31 - 133
					setcookie('rememberUserId', $userData['id'], time() + (30 * 86400), "/");
134
					setcookie('hash', password_hash($userData['password'] . $userData['id'], PASSWORD_DEFAULT), time() + (30 * 86400), "/");
25 - 135
				}
136
 
35 - 137
				$sessData['userLoggedIn'] = true;
25 - 138
				$sessData['userID'] = $userData['id'];
139
				$sessData['status']['type'] = 'success';
140
				$sessData['status']['msg'] = 'Welcome '.$userData['first_name'].'!';
141
			}
142
		}else{
143
			$sessData['status']['type'] = 'error';
144
            $sessData['status']['msg'] = 'Wrong email or password, please try again.';
145
		}
146
    }else{
147
        $sessData['status']['type'] = 'error';
148
        $sessData['status']['msg'] = 'Enter email and password.';
149
    }
150
 
151
	// Store login status into the session
152
    $_SESSION['sessData'] = $sessData;
153
 
154
	// Redirect to the home page
31 - 155
    MySessionHandler::commit(session_id());
30 - 156
    header("Location:index.php");
25 - 157
	exit;
158
}elseif(isset($_POST['forgotSubmit'])){
159
	$frmDisplay = '';
160
 
161
	// Get user inputs
162
	$email = $_POST['email'];
163
 
164
	// Check whether email is empty
165
    if(!empty($email)){
166
		// Check whether user exists in the database
167
		$cond['where'] = array('email' => $email);
168
		$cond['return_type'] = 'count';
169
		$userCount = $user->getRows($cond);
170
		if($userCount > 0){
171
			// Generat unique string
172
			$uniqidStr = md5(uniqid(mt_rand()));
173
 
174
			// Update data with forgot pass code
175
			$conditions = array(
176
				'email' => $email
177
			);
178
			$data = array(
179
				'forgot_pass_identity' => $uniqidStr
180
			);
181
			$update = $user->update($data, $conditions);
182
 
183
			if($update){
184
				// Get user details
185
				$con['where'] = array('email' => $email);
186
				$con['return_type'] = 'single';
187
				$userDetails = $user->getRows($con);
188
 
189
				// Send reset password email
190
                @forgotPassEmail($userDetails);
191
 
192
				$sessData['status']['type'] = 'success';
193
				$sessData['status']['msg'] = 'Please check your email inbox, we have sent a password reset link to your registered email.';
194
				$frmDisplay = '?frmDis=0';
195
			}else{
196
				$sessData['status']['type'] = 'error';
197
				$sessData['status']['msg'] = 'Some problem occurred, please try again.';
198
			}
199
		}else{
200
			$sessData['status']['type'] = 'error';
201
			$sessData['status']['msg'] = 'Given email is not associated with any account.';
202
		}
203
 
204
    }else{
205
        $sessData['status']['type'] = 'error';
206
        $sessData['status']['msg'] = 'Enter email to create a new password for your account.';
207
    }
208
 
209
	// Store reset password status into the session
210
    $_SESSION['sessData'] = $sessData;
211
 
212
	// Redirect to the forgot pasword page
31 - 213
    MySessionHandler::commit(session_id());
25 - 214
    header("Location:forgotPassword.php".$frmDisplay);
215
}elseif(isset($_POST['resetSubmit'])){
216
	$fp_code = $_POST['fp_code'];
217
 
218
	// Get user inputs
219
	$password = $_POST['password'];
220
	$confirm_password = $_POST['confirm_password'];
221
 
222
	if(!empty($password) && !empty($confirm_password) && !empty($fp_code)){
223
		// Password and confirm password comparison
224
        if($password !== $confirm_password){
225
            $sessData['status']['type'] = 'error';
226
            $sessData['status']['msg'] = 'Confirm password does not match the password.';
227
        }else{
228
			//check whether identity code exists in the database
229
            $cond['where'] = array('forgot_pass_identity' => $fp_code);
230
            $cond['return_type'] = 'count';
231
            $userCount = $user->getRows($cond);
232
            if($userCount > 0){
233
				// Update data with new password
234
				$conditions = array(
235
					'forgot_pass_identity' => $fp_code
236
				);
237
				$data = array(
238
					'password' => password_hash($password, PASSWORD_DEFAULT)
239
				);
240
				$update = $user->update($data, $conditions);
241
				if($update){
242
					$sessData['status']['type'] = 'success';
243
                    $sessData['status']['msg'] = 'Your account password has been reset successfully. Please login with your new password.';
244
				}else{
245
					$sessData['status']['type'] = 'error';
246
					$sessData['status']['msg'] = 'Some problem occurred, please try again.';
247
				}
248
            }else{
249
                $sessData['status']['type'] = 'error';
250
                $sessData['status']['msg'] = 'You are not authorized to reset the password for this account.';
251
            }
252
        }
253
    }else{
254
        $sessData['status']['type'] = 'error';
255
        $sessData['status']['msg'] = 'All fields are mandatory, please fill all the fields.';
256
    }
257
 
258
	// Store reset password status into the session
259
    $_SESSION['sessData'] = $sessData;
30 - 260
    $redirectURL = ($sessData['status']['type'] == 'success')?'index.php':'resetPassword.php?fp_code='.$fp_code;
25 - 261
 
262
	// Redirect to the login/reset pasword page
31 - 263
    MySessionHandler::commit(session_id());
25 - 264
    header("Location:".$redirectURL);
265
	exit;
266
}elseif(isset($_REQUEST['verifyEmail']) && $_REQUEST['verifyEmail'] == 1){
267
	$ac_code = $_REQUEST['ac_code'];
268
 
269
	// Check whether activation code exists in the database
270
	$cond['where'] = array('activation_code' => $ac_code);
271
	$cond['return_type'] = 'count';
272
	$userCount = $user->getRows($cond);
273
	if($userCount > 0){
274
		// Update data with new password
275
		$conditions = array(
276
			'activation_code' => $ac_code
277
		);
278
		$data = array(
279
			'activated' => '1'
280
		);
281
		$update = $user->update($data, $conditions);
282
		if($update){
283
			$sessData['status']['type'] = 'success';
284
			$sessData['status']['msg'] = 'Email verification for your account was successful. Please login to your account.';
285
		}else{
286
			$sessData['status']['type'] = 'error';
287
			$sessData['status']['msg'] = 'Some problem occurred, please try again.';
288
		}
289
	}else{
290
		$sessData['status']['type'] = 'error';
291
		$sessData['status']['msg'] = 'You have used the wrong verification link, please check your email inbox and try again.';
292
	}
293
 
294
	// Store account activation status into the session
295
    $_SESSION['sessData'] = $sessData;
30 - 296
    $redirectURL = 'index.php';
25 - 297
 
31 - 298
	// Redirect to the login page
299
    MySessionHandler::commit(session_id());
25 - 300
    header("Location:".$redirectURL);
301
	exit;
302
}elseif(isset($_POST['updateProfile']) && !empty($_SESSION['sessData']['userID'])){
303
	$valErr = 0;
304
 
305
	$sessData = $_SESSION['sessData'];
306
	$sessUserId = $sessData['userID'];
307
 
308
	// Get user inputs
309
	$first_name = $_POST['first_name'];
310
	$last_name = $_POST['last_name'];
311
	$email = $_POST['email'];
26 - 312
	$zip = $_POST['zip'];
25 - 313
 
314
	if(empty($first_name)){
315
		$valErr = 1;
316
		$sessData['field_error']['first_name'] = 'Please enter your first name.';
317
	}
31 - 318
/*
25 - 319
	if(empty($last_name)){
320
		$valErr = 1;
321
		$sessData['field_error']['last_name'] = 'Please enter your last name.';
322
	}
31 - 323
*/
25 - 324
	if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)){
325
		$valErr = 1;
326
		$sessData['field_error']['email'] = 'Please enter a valid email.';
327
	}
328
 
329
	if($valErr == 0){
330
		// Check whether user exists in the database
331
		$cond['where'] = array('email' => $email);
332
		$cond['where_not'] = array('id' => $sessUserId);
333
		$cond['return_type'] = 'count';
334
		$userCount = $user->getRows($cond);
335
		if($userCount > 0){
336
			$sessData['status']['type'] = 'error';
337
			$sessData['status']['msg'] = 'Email already exists, please use another email.';
338
		}else{
339
			// Get user information
340
			$conditions['where'] = array(
341
				'id' => $sessData['userID'],
342
			);
343
			$conditions['return_type'] = 'single';
344
			$userData = $user->getRows($conditions);
345
			$prevPicture = $userData['picture'];
346
 
347
			// Prepare user data
348
			$userData = array(
349
				'first_name' => $first_name,
350
				'last_name' => $last_name,
351
				'email' => $email,
26 - 352
				'zip' => $zip
25 - 353
			);
354
 
355
			// Profile picture upload
356
			$fileErr = 0;
357
			if(isset($_FILES['picture']['name']) && $_FILES['picture']['name'] != ""){
358
				$targetDir = UPLOAD_PATH.'profile_picture/';
359
				$fileName = time().'_'.basename($_FILES["picture"]["name"]);
360
				$targetFilePath = $targetDir. $fileName;
361
				$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
362
				$allowTypes = array('jpg','png','jpeg','gif');
363
				if(in_array($fileType, $allowTypes)){
364
					if(move_uploaded_file($_FILES["picture"]["tmp_name"], $targetFilePath)){
365
						$userData['picture'] = $fileName;
366
 
367
						// Delete previous profile picture
368
						@unlink(UPLOAD_PATH.'profile_picture/'.$prevPicture);
369
					}
370
				}else{
371
					$fileErr = 1;
372
					$sessData['status']['type'] = 'error';
373
					$sessData['status']['msg'] = 'Please select only jpg/png/gif files.';
374
				}
375
			}
376
 
377
			if($fileErr == 0){
378
				// Update user data in the database
379
				$conditions = array(
380
					'id' => $sessUserId
381
				);
382
				$update = $user->update($userData, $conditions);
383
 
384
				// Set status based on data insert
385
				if($update){
386
					$sessData['status']['type'] = 'success';
26 - 387
					$sessData['status']['msg'] = 'Your profile information has been updated.';
25 - 388
				}else{
389
					$sessData['status']['type'] = 'error';
390
					$sessData['status']['msg'] = 'Some problem occurred, please try again.';
391
				}
392
			}
393
		}
394
    }else{
395
        $sessData['status']['type'] = 'error';
396
        $sessData['status']['msg'] = 'Please fill all mandatory fields.';
397
    }
398
 
399
	// Store signup status into the session
400
    $_SESSION['sessData'] = $sessData;
26 - 401
	$redirectURL = 'editAccount.php';
25 - 402
 
31 - 403
	// Redirect to the profile page
404
    MySessionHandler::commit(session_id());
25 - 405
    header("Location:".$redirectURL);
406
	exit;
407
}elseif(isset($_POST['updatePassword']) && !empty($_SESSION['sessData']['userID'])){
408
	$sessData = $_SESSION['sessData'];
409
	$sessUserId = $sessData['userID'];
410
 
411
	// Get user inputs
412
	$old_password = $_POST['old_password'];
413
	$password = $_POST['password'];
414
	$confirm_password = $_POST['confirm_password'];
415
 
416
	if(!empty($password) && !empty($confirm_password)){
417
		// Password and confirm password comparison
418
        if($password !== $confirm_password){
419
            $sessData['status']['type'] = 'error';
420
            $sessData['status']['msg'] = 'Confirm password does not match the password.';
421
        }else{
422
			// Check whether identity code exists in the database
423
			$cond['where'] = array('id' => $sessUserId);
424
            $cond['return_type'] = 'single';
425
            $userData = $user->getRows($cond);
426
 
427
			if((!empty($userData) && !empty($sessData['loginType']) && $sessData['loginType'] == 'social') || (!empty($userData) && password_verify($old_password, $userData['password']))){
428
				// Update data with new password
429
				$conditions = array(
430
					'id' => $sessUserId
431
				);
31 - 432
				$passwordHash = password_hash($password, PASSWORD_DEFAULT);
25 - 433
				$data = array(
31 - 434
					'password' => $passwordHash
25 - 435
				);
436
				$update = $user->update($data, $conditions);
437
				if($update){
31 - 438
                    if (!empty($_COOKIE['rememberUserId'])){
439
    					setcookie('hash', password_hash($passwordHash . $sessUserId, PASSWORD_DEFAULT), time() + (30 * 86400), "/");
440
    				}
25 - 441
					$sessData['status']['type'] = 'success';
442
                    $sessData['status']['msg'] = 'Your account password has been updated successfully.';
443
				}else{
444
					$sessData['status']['type'] = 'error';
445
					$sessData['status']['msg'] = 'Some problem occurred, please try again.';
446
				}
447
            }else{
448
                $sessData['status']['type'] = 'error';
449
                $sessData['status']['msg'] = 'The given old password does not match your current account password.';
450
            }
451
        }
452
    }else{
453
        $sessData['status']['type'] = 'error';
454
        $sessData['status']['msg'] = 'Please fill all mandatory fields.';
455
    }
456
 
457
	// Store reset password status into the session
458
    $_SESSION['sessData'] = $sessData;
26 - 459
    $redirectURL = 'changePassword.php';
25 - 460
 
461
	// Redirect to the pasword settings page
31 - 462
    MySessionHandler::commit(session_id());
25 - 463
    header("Location:".$redirectURL);
464
	exit;
465
}elseif(!empty($_REQUEST['logoutSubmit'])){
466
	// Include social login handler
467
	if(!empty($_SESSION['sessData']['loginType']) && ($_SESSION['sessData']['loginType'] == 'social') && !empty($_SESSION['google_access_token'])){
468
		require_once 'includes/socialLogin.php';
469
	}
470
 
471
	// Remove cookie data
31 - 472
	setcookie("rememberUserId", "", time() - 3600, "/");
473
	setcookie("hash", "", time() - 3600, "/");
474
    unset($_COOKIE['rememberUserId']);
475
    unset($_COOKIE['hash']);
25 - 476
 
477
	// Remove session data
478
	unset($_SESSION['facebook_access_token']);
479
	unset($_SESSION['FBRLH_state']);
480
	if(isset($_SESSION['google_access_token'])){
481
		// Reset OAuth access token
482
		$gClient->revokeToken();
483
	}
484
	unset($_SESSION['google_access_token']);
485
	unset($_SESSION['twitter_access_token']);
486
	unset($_SESSION['twitter_token_secret']);
487
    unset($_SESSION['sessData']);
488
    session_destroy();
489
 
31 - 490
	// Store logout status into the session
25 - 491
    $sessData['status']['type'] = 'success';
492
    $sessData['status']['msg'] = 'You have logged off your account.';
493
    $_SESSION['sessData'] = $sessData;
494
 
495
	// Redirect to the home page
31 - 496
    MySessionHandler::commit(session_id());
497
    header("Location:../index.php");
25 - 498
	exit;
499
}else{
500
	// Redirect to the home page
31 - 501
    MySessionHandler::commit(session_id());
502
    header("Location:../index.php");
25 - 503
	exit;
504
}