-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchpreviews.php
299 lines (274 loc) · 13.4 KB
/
searchpreviews.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
require_once __DIR__."/vendor/autoload.php";
require_once "root.php";
require_once "resources/require.php";
require_once "resources/check_auth.php";
header("Content-Type: application/json");
foreach ($_SESSION['user']['extension'] as $ext) {
if ($ext['extension_uuid'] == $_GET['extension_uuid']) {
$extension = $ext;
break;
}
}
if (!$extension) {
http_response_code(400);
echo json_encode(array("error" => "invalid or unauthorized extension"));
die();
}
$database = new database;
// get user's number
$sql = "SELECT phone_number FROM webtexting_destinations WHERE domain_uuid = :domain_uuid AND extension_uuid = :extension_uuid";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['extension_uuid'] = $extension['extension_uuid'];
$ownNumber = $database->select($sql, $parameters, 'column');
unset($parameters);
if($ownNumber[0]=='+'){
$ownNumber = subStr($ownNumber,1);
}
if (!$ownNumber) {
echo "To use Web Texting purchase the service at <a href='https://acceleratenetworks.com/services/'>https://acceleratenetworks.com/services/</a> then <a href='mailto:https://acceleratenetworks.com/support/'>Contact Support</a>";
echo "<br> No SMS-enabled number for this extension.";
include_once "footer.php";
die();
}
//this is for Contact Search
if($_GET['query_string'] != null){
$query_string = $_GET['query_string'];
if(strlen($query_string)>2){
}
}
else{
$query_string = '';
}
$sql = "SELECT *
FROM webtexting_threads,v_contacts , v_contact_phones
WHERE webtexting_threads.domain_uuid = :domain_uuid
AND webtexting_threads.domain_uuid = v_contacts.domain_uuid
AND v_contact_phones.contact_uuid = v_contacts.contact_uuid
AND webtexting_threads.remote_number = v_contact_phones.phone_number
AND (LOWER(CONCAT(v_contacts.contact_organization , ' ' ,v_contacts.contact_name_given , ' ' , v_contacts.contact_name_middle ,
' ' , v_contacts.contact_name_family , ' ' , v_contacts.contact_nickname, ' ', v_contacts.contact_title, ' ',
v_contacts.contact_role) ) LIKE LOWER('%'||:query_string||'%' ) or v_contact_phones.phone_number LIKE '%'||:query_string||'%') ;";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['query_string'] = $query_string;
$solo_conversations = $database->select($sql, $parameters, 'all');
unset($parameters);
$z = 0;
if($solo_conversations){
foreach ($solo_conversations as $solo) {
$usedNumbers[$solo['phone_number']] = true;
$threadPreviews[$z]['remoteNumber'] = $solo['phone_number'];
$threadPreviews[$z]['displayName']= $solo['phone_number'];
$threadPreviews[$z]['contactEditLink'] = "/app/contacts/contact_edit.php?id=".$solo['contact_uuid'];
$threadPreviews[$z]['threadUUID'] = $solo['thread_uuid'];
$threadPreviews[$z]['link'] = "thread.php?extension_uuid=".$extension['extension_uuid']."&number=".$solo['phone_number'];
$threadPreviews[$z]['ownNumber'] = $ownNumber;
$threadPreviews[$z]['timestamp'] = $solo['last_message'];
$name_parts = array();
if ($solo['contact_organization']) {
$name_parts[] = $solo['contact_organization'];
}
if ($solo['contact_title']) {
$name_parts[] = $solo['contact_title'];
}
if ($solo['contact_name_prefix']) {
$name_parts[] = $solo['contact_name_prefix'];
}
if ($solo['contact_name_given']) {
$name_parts[] = $solo['contact_name_given'];
}
if ($solo['contact_name_middle']) {
$name_parts[] = $solo['contact_name_middle'];
}
if ($solo['contact_name_family']) {
$name_parts[] = $solo['contact_name_family'];
}
if ($solo['contact_nickname']) {
$name_parts[] = $solo['contact_nickname'];
}
if ($solo['contact_role']) {
$name_parts[] = $solo['contact_role'];
}
if (sizeof($name_parts) > 0) {
$threadPreviews[$z]['displayName'] = implode(" ", $name_parts);
$contacts[$solo['phone_number']] = $threadPreviews[$z]['displayName'] ;
}
$z++;
}
}
//this is for group search
//TODO add group member contact names to member array
$sql = "SELECT *
from webtexting_groups,webtexting_threads
where webtexting_groups.domain_uuid = :domain_uuid
AND webtexting_threads.domain_uuid = :domain_uuid
AND webtexting_threads.domain_uuid = webtexting_groups.domain_uuid
AND extension_uuid = :extension_uuid
AND webtexting_threads.group_uuid = webtexting_groups.group_uuid
AND LOWER(webtexting_groups.name) LIKE LOWER('%'||:query_string||'%' ) ";
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['query_string'] = $query_string;
$groups = $database->select($sql, $parameters, 'all');
unset($parameters);
if($groups){
foreach ($groups as $group) {
$threadPreviews[$z]['groupUUID'] = $group['group_uuid'];
$threadPreviews[$z]['groupMembers'] = $group['members'];
$threadPreviews[$z]['threadUUID'] = $group['thread_uuid'];
if ($group['name'] != null) {
$display_name = $group['name'];
} else {
$display_name = $group['members'];
}
$group_members = explode(",",$group['members']);
$threadPreviews[$z]['groupMembers'] = explode(",",$group['members']);
$member_index =0;
foreach( $group_members as $member ){
if($contacts[$member] != null){ //we already got the contact name for most numbers might as well cut back on queries
$threadPreviews[$z]['groupMembers'][$member_index] = $contacts[$member];
}
else if($member == $ownNumber){
$threadPreviews[$z]['groupMembers'][$member_index] = 'Me';
}
else{
$sql = "SELECT v_contacts.contact_uuid, v_contacts.contact_organization, v_contacts.contact_name_given, v_contacts.contact_name_middle, v_contacts.contact_name_family, v_contacts.contact_nickname, v_contacts.contact_title, v_contacts.contact_role FROM v_contact_phones, v_contacts WHERE v_contact_phones.phone_number = :number AND v_contact_phones.domain_uuid = :domain_uuid AND v_contacts.contact_uuid = v_contact_phones.contact_uuid LIMIT 1;";
$parameters['number'] = $member;
$parameters['domain_uuid'] = $domain_uuid;
$contact = $database->select($sql, $parameters, 'row');
unset($parameters);
if ($contact) {
$name_parts = array();
if ($contact['contact_organization']) {
$name_parts[] = $contact['contact_organization'];
}
if ($contact['contact_title']) {
$name_parts[] = $contact['contact_title'];
}
if ($contact['contact_name_prefix']) {
$name_parts[] = $contact['contact_name_prefix'];
}
if ($contact['contact_name_given']) {
$name_parts[] = $contact['contact_name_given'];
}
if ($contact['contact_name_middle']) {
$name_parts[] = $contact['contact_name_middle'];
}
if ($contact['contact_name_family']) {
$name_parts[] = $contact['contact_name_family'];
}
if ($contact['contact_nickname']) {
$name_parts[] = $contact['contact_nickname'];
}
if ($contact['contact_role']) {
$name_parts[] = $contact['contact_role'];
}
if (sizeof($name_parts) > 0) {
$threadPreviews[$z]['groupMembers'][$member_index] = implode(" ", $name_parts);
}
}
else{
$threadPreviews[$z]['groupMembers'][$member_index] = $member;
}
}
$member_index++;
}
$threadPreviews[$z]['link'] = "thread.php?extension_uuid=".$extension['extension_uuid']."&group=".$group['group_uuid'];
$threadPreviews[$z]['ownNumber'] = $ownNumber;
$threadPreviews[$z]['timestamp'] = $group['last_message'];
$threadPreviews[$z]['displayName'] = $display_name;
$z++;
}
}
//if no group and no contact check if a non-contact non-group thread exists
else{
//if(!$solo_conversations){
$sql = "SELECT *
FROM webtexting_threads
WHERE webtexting_threads.domain_uuid = :domain_uuid
AND webtexting_threads.local_number = :own_number
AND webtexting_threads.remote_number LIKE LOWER('%'||:query_string||'%' ) ;";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['own_number'] = $ownNumber;
$parameters['query_string'] = $query_string;
$local_to_remote_conversations = $database->select($sql, $parameters, 'all');
unset($parameters);
if($local_to_remote_conversations){
foreach ($local_to_remote_conversations as $local_to_remote) {
$usedNumbers[$local_to_remote['remote_number']] = true;
$threadPreviews[$z]['remoteNumber'] = $local_to_remote['remote_number'];
$threadPreviews[$z]['displayName']= $local_to_remote['remote_number'];
$threadPreviews[$z]['threadUUID'] = $local_to_remote['thread_uuid'];
$threadPreviews[$z]['contactEditLink'] = "/app/contacts/contact_edit.php?id=".$local_to_remote['contact_uuid'];
$threadPreviews[$z]['link'] = "thread.php?extension_uuid=".$extension['extension_uuid']."&number=".$local_to_remote['remote_number'];
$threadPreviews[$z]['ownNumber'] = $ownNumber;
$threadPreviews[$z]['timestamp'] = $local_to_remote['last_message'];
$z++;
}
}
//}
}
//this is where we fetch and attach the bodyPreviews
//we could modify this to provide our initial unread message count as well
$z=0;
foreach($threadPreviews as $preview){
$sql = "SELECT * FROM webtexting_messages WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND ";
if ($preview['groupUUID'] != null) {
$sql .= "group_uuid = :group_uuid";
$parameters['group_uuid'] = $preview['groupUUID'];
} else {
$sql .= "(from_number = :number OR to_number = :number) AND group_uuid IS NULL";
$parameters['number'] = $preview['remoteNumber'];
}
$sql .= " ORDER BY start_stamp DESC LIMIT 1";
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$last_message = $database->select($sql, $parameters, 'all');
unset($parameters);
if($last_message[0]['content_type'] == 'message/cpim'){
$threadPreviews[$z]['bodyPreview'] = 'MMS Message.';
}
else{
$threadPreviews[$z]['bodyPreview'] = $last_message[0]['message'];
}
$last_message = false;
//this is where we find out when the user last accessed each thread
//If they've opened a thread before they have a thread_uuid
//else they haven't and we don't need to calculate unreads
if($threadPreviews[$z]['threadUUID']){
$sql = "SELECT timestamp from webtexting_threads_last_seen WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND thread_uuid = :thread_uuid;";
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['thread_uuid'] = $threadPreviews[$z]['threadUUID'];
$last_seen_stamp = $database->select($sql, $parameters, 'row');
unset($parameters);
if($last_seen_stamp['timestamp']){
//here is where we would want to prepare and execute the count since last seen query
$sql = "SELECT COUNT(*) from webtexting_messages WHERE extension_uuid = :extension_uuid AND domain_uuid = :domain_uuid AND
(start_stamp BETWEEN :last_seen_stamp AND NOW()) AND ";
if ($preview['groupUUID'] != null) {
$sql .= "group_uuid = :group_uuid AND (to_number = :own_number AND NOT (from_number = to_number));";
$parameters['own_number'] = $ownNumber;
$parameters['group_uuid'] = $preview['groupUUID'];
} else {
$sql .= "(to_number = :own_number AND from_number = :remote_number AND group_uuid IS NULL)";
$parameters['own_number'] = $ownNumber;
$parameters['remote_number'] = $preview['remoteNumber'];
}
$parameters['extension_uuid'] = $extension['extension_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['last_seen_stamp'] = $last_seen_stamp['timestamp'];
$count_since_last_seen = $database->select($sql, $parameters, 'row');
unset($parameters);
if($count_since_last_seen['count']){
$threadPreviews[$z]['newMessages'] = $count_since_last_seen['count'];
}
$count_since_last_seen = 0;
}
$last_seen_stamp = false;
}
$z++;
}
echo(json_encode($threadPreviews));
return json_encode($threadPreviews);
//This handles group and contact search jsut need to add number search