-
Notifications
You must be signed in to change notification settings - Fork 4
/
createnotification.php
192 lines (150 loc) · 4.21 KB
/
createnotification.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
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Credentials: true');
error_reporting(0);
$_POST = json_decode(file_get_contents('php://input'), true);
/*******************
0. DOCUMENTATION
********************/
/*
Version: 1.0
Brief: To create a notification by the Teachers or Admin staff
Author: Ameen
First Created: 06-08-2017
Last Modified: 06-08-2017 @Ameen
*/
/**********************************
1.1 AUTHENTICATION STANDARD PART
***********************************/
//Encryption Credentials
define('SECURE_CHECK', true);
require 'secure.php';
//Encryption Validation
if(!isset($_POST['token'])){
$output = array(
"status" => false,
"error" => "Access Token Missing",
"errorCode" => 103,
"response" => ""
);
die(json_encode($output));
}
$token = $_POST['token'];
$decryptedtoken = openssl_decrypt($token, $encryptionMethod, $secretHash);
$tokenid = json_decode($decryptedtoken, true);
//Expiry Validation
date_default_timezone_set('Asia/Calcutta');
$dateStamp = date_create($tokenid['date']);
$today = date_create(date("Y-m-j"));
$interval = date_diff($dateStamp, $today);
$interval = $interval->format('%a');
if($interval > $tokenExpiryDays){
$output = array(
"status" => false,
"error" => "Login Expired",
"errorCode" => 401,
"response" => ""
);
die(json_encode($output));
}
/**********************************
1.2 AUTHENTICATION CUSTOM PART
***********************************/
//Check if the token is valid
if(!($tokenid['schoolID'] == "")){
$schoolID = $tokenid['schoolID'];
$mobile = $tokenid['mobile'];
}
else{
$output = array(
"status" => false,
"error" => "Invalid Token",
"errorCode" => 402,
"response" => ""
);
die(json_encode($output));
}
//Check if the user has permission to access this API
$accessLevel = $tokenid['accessLevel'];
if($accessLevel != "ADMIN" && $accessLevel != "TEACHER"){
$output = array(
"status" => false,
"error" => "Access Restricted",
"errorCode" => 403,
"response" => ""
);
die(json_encode($output));
}
/**********************
2. PARAMETERS CHECK
***********************/
//Params passed or not
if(!isset($_POST['brief'])){
$output = array(
"status" => false,
"error" => "Notification content is not set",
"errorCode" => 102,
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['viewers'])){
$output = array(
"status" => false,
"error" => "Target not set",
"errorCode" => 102,
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['viewersList'])){
$output = array(
"status" => false,
"error" => "Audience not set",
"errorCode" => 102,
"response" => ""
);
die(json_encode($output));
}
//REQUIRED PARAMETERS
date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y h:i:s a', time());
$brief = mysql_real_escape_string($_POST['brief']);
$viewers = $_POST['viewers'];
$viewersList = json_encode($_POST['viewersList']);
$userID = $_POST['userID'];
$photoURL = $_POST['photoURL'];
$photoFlag = isset($_POST['photoURL'])? true : false;
/****************
3. MAIN LOGIC
*****************/
//3.1 CONNECTION TO CUSTOM DATABASE
define('INCLUDE_CHECK', true);
require 'connect_'.$schoolID.'.php';
//3.2 PARAMETER VALIDATIONS
//check if the notification already exists
$clash_check = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `d_notifications` WHERE `brief`='{$brief}' AND `viewers`= '{$viewers}' AND `viewersList`='{$viewersList}'"));
if($clash_check['id'] != ''){
$output = array(
"status" => false,
"error" => "Notification already exists",
"errorCode" => 201,
"response" => ""
);
die(json_encode($output));
}
//3.3 MAIN OPERATION (INSERTION or DELETION or UPDATION)
mysql_query("INSERT INTO `d_notifications`(`id`, `timestamp`, `userID`, `photoFlag`, `photoURL`, `brief`, `viewers`, `viewersList`) VALUES ('','','{$userID}','{$photoFlag}','{$photoURL}','{$brief}','{$viewers}','{$viewersList}')");
$response = array(
"message" => "Notification created sucessfully"
);
$output = array(
"status" => true,
"error" => "",
"errorCode" => "",
"response" => $response
);
die(json_encode($output));
?>