-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.php
228 lines (182 loc) · 5.26 KB
/
pubsub.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
require "/home/sunil/vendor/autoload.php";
$app = new \Slim\Slim();
$app->get('/:topic/:message', function ($topic, $message) {
// echo $topic;
// echo "<br>";
// echo $message;
sendMail($topic, $message);
});
$app->get('/unsub/:topic/:email', function ($topic, $email) {
// echo $topic;
// echo "<br>";
// echo $email;
unsub($topic, $email);
});
$final_ar=array();
/*------------------*/
function sendMail($topic=null, $message=null){
$m=$message;
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "asgn3";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select user_email from subs where topic='$topic'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$email = trim($row['user_email']);
// echo $email;
$msg_un = "to unsubscribe <a href=\"pubsub.php/unsub/$topic/$email\">click</a>";
$message = $message."<br>".$msg_un;
mail($email, $topic, $message);
store_email($topic,$email,$message);
$message=$m;
}
echo "
<script type=\"text/javascript\">
alert(\"success\");
</script>
";
// header("Location: index.php");
}
$conn->close();
}
function unsub($topic=null, $email=null){
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "asgn3";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "delete from subs where topic='$topic' && user_email='$email'";
if ($conn->query($sql) === TRUE) {
echo "Successfully unsubscribed from $topic";
}
else {
echo "Failure. Please try later";
}
$conn->close();
}
function store_email($topic, $email, $message){
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "asgn3";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "insert into message values('$topic','$email','$message','N');";
if ($conn->query($sql) === TRUE) {
// echo "Successfully subscribed to ".$cate[$i]."<br>";
}
else {
// echo "You are already subscribed to ".$cate[$i]."<br>";
}
$conn->close();
}
// function check($operand1=null,$operator=null,$operand2=null){
// $servername = "localhost";
// $username = "root";
// $password = "root";
// $dbname = "DAC";
// // Create connection
// $conn = new mysqli($servername, $username, $password, $dbname);
// // Check connection
// if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
// }
// $sql = "select result from cache where op1=$operand1 && operator='$operator' && op2=$operand2;";
// $result = $conn->query($sql);
// if ($result->num_rows > 0) {
// // output data of each row
// while($row = $result->fetch_assoc()) {
// $tmp=trim($row['result']);
// $final_ar['status']="ok";
// $final_ar['result']=$tmp;
// echo json_encode($final_ar);
// // echo trim($row['result']);
// }
// }
// else {
// insert($operand1,$operator,$operand2);
// }
// $conn->close();
// }
// /*------------------*/
// /*------------------*/
// function insert($operand1=null,$operator=null,$operand2=null){
// $servername = "localhost";
// $username = "root";
// $password = "root";
// $dbname = "DAC";
// // Create connection
// $conn = new mysqli($servername, $username, $password, $dbname);
// // Check connection
// if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
// }
// if($operator == '+'){
// $final = $operand1 + $operand2;
// }
// else if($operator == '-'){
// $final = $operand1 - $operand2;
// }
// else if($operator == '#'){
// if($operand2 == 0) {
// $final_ar['status']="nok";
// $final_ar['result']="cannot divide by 0";
// echo json_encode($final_ar);
// // echo "cannot divide by 0";
// die();
// }
// $operator = '/';
// $final = $operand1 / $operand2;
// }
// else if($operator == 'X'){
// $final = $operand1 * $operand2;
// }
// $final = trim($final);
// if($final>99999999999999){
// $final_ar['status']="nok";
// $final_ar['result']="result out of bound";
// // echo "result out of bound";
// die();
// }
// $sql = "insert into cache values($operand1,'$operator',$operand2,$final);";
// if ($conn->query($sql) === TRUE) {
// $final_ar['status']="ok";
// $final_ar['result']=trim($final);
// echo json_encode($final_ar);
// // echo trim($final);
// }
// else {
// $final_ar['status']="nok";
// $final_ar['result']="please try again";
// echo json_encode($final_ar);
// // echo "Error: ";// . $sql . "<br>" . $conn->error;
// }
// $conn->close();
// }
// /*------------------*/
// // $final_ar['status']="nok";
// // $final_ar['result']="please try again";
// // $app->response()->header('Content-Type', 'application/json');
// // echo json_encode($final_ar);
// // var_dump($final_ar);
$app->run();
?>