forked from ninnzz/kumusta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.php
executable file
·161 lines (130 loc) · 4.67 KB
/
notify.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
<?php
include('PHP/src/GlobeApi.php');
$globe = new GlobeApi();
$sms = $globe->sms(7625);
// $test = array(1,2,3,4,5);
// print_r(array_splice($test, 1));
$json = file_get_contents('php://input');
$json = stripslashes($json);
$message = json_decode($json, true);
//if we received a message
if($message) {
if(!isset($message['inboundSMSMessageList']['inboundSMSMessage'])) {
return 'Not Set inboundSMSMessage';
}
$link = mysqli_connect("localhost","root","P@ssw0rd","kumusta") or die("Error " . mysqli_error($link));
foreach($message['inboundSMSMessageList']['inboundSMSMessage'] as $item) {
if(!isset($item['message'], $item['senderAddress'])) {
continue;
}
$result = $link->query('SELECT * FROM users WHERE phoneNumber = \''.str_replace('tel:+63', '', $item['senderAddress']).'\' LIMIT 1;');
$user = $result->fetch_row();
if($user) {
if(strpos(strtoupper($item['message']), 'SEARCH') === 0) {
$name = split(" ", strtoupper($item['message']));
$name = implode(" ", array_splice($name, 1));
//if searching
//logic for pull here
preg_match('/LIMIT ([0-9]+)/', $name, $temp);
if(isset($temp[0])) {
$name = substr($name, strlen($temp[0]));
$limit = $temp[1];
preg_match('/OFFSET ([0-9]+)/', $name, $temp);
if(isset($temp[0])) {
$name = substr($name, strlen($temp[0]));
$offset = $temp[1];
}
}
$fields = array('query' => $name, 'source' => 'mobile', 'limit' => $limit, 'offset' => $offset);
$fields = array_filter($fields);
print_r($fields);
$response = $sms->sendMessage(
$user['2'],
$user['1'],
'You will be receiving the list containing '.$name
);
$fields_string = http_build_query($fields);
$url = 'http://ec2-184-169-205-217.us-west-1.compute.amazonaws.com/search';
$ch = curl_init($url.'?'.$fields_string);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$res = json_decode($response, true);
$data = $res['data'];
$str = "";
foreach($data as $x) {
$str.=$x['message']." | ";
}
if($res['has_more']) {
$str.= ". Please visit http://pprmint.github.io/kumusta to view full list. Thank you :)";
}
if($str == " | ") {
$str = "Sorry, your search didn't match any results";
}
$str = urlencode($str);
$url = "http://ec2-184-169-205-217.us-west-1.compute.amazonaws.com/callback.php";
$fields_string = "message={$str}&phone_number=".$user['1'];
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 2);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
if(strpos(strtoupper($item['message']), 'SUBSCRIBE SEARCH') === 0) {
$name = split(" ", strtoupper($item['message']));
$name = implode(" ", array_splice($name, 2));
//if subscribing to search
$sms->sendMessage(
$user['2'],
$user['1'],
'You will be receiving the list containing details about '.$name.' every 1 hour'
);
$query = 'INSERT INTO search VALUES(%s);';
$data = array('NULL', '\''.$user[0].'\'', '\''.$name.'\'', '\''.date('Y-m-d H:i:s').'\'','NULL');
$query = sprintf($query, implode(',', $data));
$response = $link->query($query);
//logic for adding to cron job here
}
if(strpos(strtoupper($item['message']), 'END SUBSCRIBE SEARCH') === 0) {
$name = split(" ", strtoupper($item['message']));
$name = implode(" ", array_splice($name, 3));
//if ending subscription
$sms->sendMessage(
$user['2'],
$user['1'],
'You have successfully ended your subscription for updates about '.$name
);
$query = 'DELETE FROM search WHERE %s;';
$query = sprintf($query, 'userId = '.$user[0].' AND searchString = \''.$name.'\'');
$response = $link->query($query);
}
if(strpos(strtoupper($item['message']), 'DONATE') === 0) {
$result = $link->query('SELECT * FROM donations ORDER BY id DESC LIMIT 1;');
$donation = $result->fetch_row();
//if donating
$charge = $globe->payment(
$user['2'],
$user['1']
);
$code = '7625'.($donation[0]+1);
$response = $charge->charge(
0,
$code
);
if(!isset($response['error'])) {
$query = 'INSERT INTO donations VALUES(%s);';
$data = array('NULL', $user[0]);
$query = sprintf($query, implode(',', $data));
$response = $link->query($query);
}
}
}
}
mysqli_close($link);
}
?>