-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_actions.php
110 lines (97 loc) · 3.47 KB
/
user_actions.php
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
<?php
session_start();
include_once('sql/connection.php');
include_once('sql/user.php');
include_once('sql/utilities.php');
function actionLogin ($obj){
$obj->userName = strip_tags(trim($obj->userName));
$obj->passWord = strip_tags(trim($obj->passWord));
if (empty($obj->userName)) {
return generateResponse("You didn't enter the username!", "Denied");
}
if (empty($obj->passWord)) {
return generateResponse("You didn't enter the password!", "Denied");
}
if (verifyUserAccount($obj->userName, $obj->passWord)){
$_SESSION['username'] = $obj->userName;
// $_SESSION['userid'] = getUser($obj->userName)['user_id'];
return generateResponse("Login successfully!", "Successfully");
}
else{
return generateResponse("User or password incorrect!", "Denied");
}
}
function actionLogout($obj){
if (isset($_SESSION['username'])){
session_unset();
session_destroy();
return generateResponse("Logout successfully!", "Successfully");
}else {
return generateResponse("No session to logout!", "Denied");
}
}
function actionRegister($obj){
$obj->userName = strip_tags(trim($obj->userName));
$obj->passWord = strip_tags(trim($obj->passWord));
$obj->name = strip_tags(trim($obj->name));
$obj->email = strip_tags(trim($obj->email));
$obj->location = strip_tags(trim($obj->location));
$obj->nationality = strip_tags(trim($obj->nationality));
if (empty($obj->userName)) {
return generateResponse("You didn't enter the userName!", "Denied");
}
if (empty($obj->passWord)) {
return generateResponse("You didn't enter the passWord!", "Denied");
}
if (empty($obj->name)) {
return generateResponse("You didn't enter the name!", "Denied");
}
if (empty($obj->email)) {
return generateResponse("You didn't enter the email!", "Denied");
}
if (empty($obj->location)) {
return generateResponse("You didn't enter the location!", "Denied");
}
if (empty($obj->nationality)) {
return generateResponse("You didn't enter the nationality!", "Denied");
}
if (userExists($obj->userName)){
return generateResponse("This username already exists!", "Denied");
}
else if (insertUser($obj->userName, $obj->isOwner, $obj->isReviewer, $obj->passWord, $obj->name, $obj->email, $obj->location, $obj->nationality) == 0){
return generateResponse("Inserted with success!", "Successfully");
}
}
function actionUpdate($obj){
$obj->userName = strip_tags(trim($obj->userName));
$obj->passWord = strip_tags(trim($obj->passWord));
$obj->name = strip_tags(trim($obj->name));
$obj->email = strip_tags(trim($obj->email));
$obj->location = strip_tags(trim($obj->location));
$obj->nationality = strip_tags(trim($obj->nationality));
if(updateUser($obj->userName, $obj->isOwner, $obj->isReviewer, $obj->passWord, $obj->name, $obj->email, $obj->location, $obj->nationality) == 0){
return generateResponse("Update with success!", "Successfully");
}else {
return generateResponse("Error updating user", "Denied");
}
}
$data = file_get_contents('php://input');// serve para ler todo o post gerado por uma pagina que chamou o arquivo php atual
if(isset($data)){
$obj = json_decode($data); //parsing json data to php object
switch($obj->type){
case 'login':
$result = actionLogin($obj);
break;
case 'logout':
$result = actionLogout($obj);
break;
case 'register':
$result = actionRegister($obj);
break;
case 'update':
$result = actionUpdate($obj);
break;
}
}
echo json_encode($result);
?>