Subversion Repositories cheapmusic

Rev

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