Subversion Repositories cheapmusic

Rev

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