Subversion Repositories cheapmusic

Rev

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