-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.php
191 lines (178 loc) · 5.72 KB
/
api.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
require_once "config.php";
$response = array();
if(isset($_GET['apicall'])){
switch($_GET['apicall'])
{
case 'signup':
//this part for registration
if(isTheseParametersAvailable(array('reg_id','username','firstname','lastname','email','password','branch','hostel')))
{
$reg_id = $_POST['reg_id'];
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$branch=$_POST['branch'];
$hostel=$_POST['hostel'];
$stmt = $conn->prepare("SELECT regid FROM users where regid=?");
$stmt->bind_param("s",$reg_id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows>0)
{
$response['error']=true;
$response['message']='User Already Registered';
$stmt->close();
}else{
$stmt=$conn->prepare("INSERT INTO USERS (regid,username,pass,branch,hostel,first_name,last_name,email) VALUES(?,?,?,?,?,?,?,?)");
$stmt->bind_param("ssssssss",$reg_id,$username,$password,$branch,$hostel,$firstname,$lastname,$email);
if($stmt->execute())
{
$stmt = $conn->prepare("SELECT regid,username,first_name,last_name,email,pass,branch,hostel FROM users where regid=?");
$stmt->bind_param("s",$reg_id);
$stmt->execute();
$stmt->bind_result($reg_id,$username,$firstname,$lastname,$email,$password,$branch,$hostel);
$stmt->fetch();
$user=array(
'reg_id'=>$reg_id,
'username'=>$username,
'firstname'=>$firstname,
'lastname'=>$lastname,
'email'=>$email,
'password'=>$password,
'branch'=>$branch,
'hostel'=>$hostel
);
$stmt->close();
$response['error']=false;
$response['message']='User Registered Succesfully';
$response['user']=$user;
}
}
}else{
$response['error']=true;
$response['message']='Requried Parameteres Are Not Available';
}
break;
case 'login':
//this part for login
if(isTheseParametersAvailable(array('reg_id','password')))
{
$reg_id=$_POST['reg_id'];
$password = $_POST['password'];
$stmt=$conn->prepare("SELECT regid,username,pass,first_name,last_name,email,branch,hostel FROM users WHERE regid=? AND pass=?");
$stmt->bind_param("ss",$reg_id,$password);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows>0)
{
$stmt->bind_result($reg_id,$username,$firstname,$lastname,$email,$password,$branch,$hostel);
$stmt->fetch();
$user=array(
'reg_id'=>$reg_id,
'username'=>$username,
'firstname'=>$firstname,
'lastname'=>$lastname,
'email'=>$email,
'password'=>$password,
'branch'=>$branch,
'hostel'=>$hostel
);
$response['error']=false;
$response['message']='Login Succesfull';
$response['user']=$user;
}else{
$response['error']=true;
$response['message']='Invalid Reg ID Or Password';
}
}
break;
case 'addComplaint':
if(isTheseParametersAvailable(array('reg_id','description','type','student_vis','faculty_vis',
'title')))
$reg_id=$_POST['reg_id'];
$description=$_POST['description'];
$type=$_POST['type'];
$title=$_POST['title'];
$student_vis=$_POST['student_vis'];
$faculty_vis=$_POST['faculty_vis'];
$up_vote="0";
$down_vote="0";
$is_resolved ="0";
$complaint_id=NULL;
$created_at=date('Y-m-d');
$stmt = $conn->prepare("INSERT INTO COMPLAINTS (complaint_id,user_id,description,type,student_visibility,faculty_visibility
,up_vote,down_vote,is_resolved,created_at,title) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssss",$complaint_id,
$reg_id,
$description,
$type,
$student_vis,
$faculty_vis,
$up_vote,
$down_vote,
$is_resolved,
$created_at,$title);
if($stmt->execute()){
//echo $type;
$sql = "SELECT complaint_id FROM complaints ORDER BY complaint_id DESC LIMIT 1";
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc())
{
$complaint_id=$row['complaint_id'];
}
}
if($type=="1")
{
$hostel_name="boys";
$stmt1=$conn->prepare("INSERT INTO HOSTEL_COMPLAINTS (hostel_name,complaint_id,user_id) VALUES(?,?,?)");
$hostelSql = "insert into notifications(complaint_id,title,description,is_seen,created_at) values('$complaint_id','$title','$description',0,'$created_at')";
$stmt1->bind_param("sss",$hostel_name,$complaint_id,$reg_id);
mysqli_query($conn,$hostelSql);
$stmt1->execute();
$stmt1->close();
}else if($type=="0")
{
$stmt2=$conn->prepare("INSERT INTO USER_COMPLAINTS (user_id,complaint_id) VALUES(?,?)");
$instituteSql = "insert into notifications(complaint_id,title,description,is_seen,created_at) values('$complaint_id','$title','$description',0,'$created_at')";
$stmt2->bind_param("ss",$reg_id,$complaint_id);
mysqli_query($conn,$instituteSql);
$stmt2->execute();
$stmt2->close();
}else{
$stmt3=$conn->prepare("INSERT INTO INSTITUTE_COMPLAINTS (complaint_id,user_id) VALUES(?,?)");
$stmt3->bind_param("ss",$complaint_id,$reg_id);
$stmt3->execute();
$stmt3->close();
}
$response['error']=false;
$response['message']='Complaint Added';
}else{
$response['error']=true;
$response['message']='Something Went Wrong';
}
$stmt->close();
break;
default :
$response['error']=true;
$response['message']='Invalid Operation Called';
}
}else{
$response['error']=true;
$response['message']='Invalid Api Call';
}
echo json_encode($response);
function isTheseParametersAvailable($params){
foreach($params as $param)
{
if(!isset($_POST[$param])){
return false;
}
}
return true;
}
?>