-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_thread.php
345 lines (307 loc) · 13.2 KB
/
messages_thread.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2016-2019
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
*/
//includes
require_once "root.php";
require_once "resources/require.php";
require_once "resources/check_auth.php";
//check permissions
if (!permission_exists('message_view')) {
echo "access denied";
exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
//get number of messages to load
$number = preg_replace('{[\D]}', '', $_GET['number']);
$contact_uuid = (is_uuid($_GET['contact_uuid'])) ? $_GET['contact_uuid'] : null;
//set refresh flag
$refresh = $_GET['refresh'] == 'true' ? true : false;
//get messages
if (isset($_SESSION['message']['display_last']['text']) && $_SESSION['message']['display_last']['text'] != '') {
$array = explode(' ',$_SESSION['message']['display_last']['text']);
if (is_array($array) && is_numeric($array[0]) && $array[0] > 0) {
if ($array[1] == 'messages') {
$limit = limit_offset($array[0], 0);
}
else {
$since = "and message_date >= :message_date ";
$parameters['message_date'] = date("Y-m-d H:i:s", strtotime('-'.$_SESSION['message']['display_last']['text']));
}
}
}
if ($limit == '' && $since == '') { $limit = limit_offset(25, 0); } //default (message count)
$sql = "SELECT ";
$sql .= "message_uuid, ";
$sql .= "domain_uuid, ";
$sql .= "user_uuid, ";
$sql .= "contact_uuid, ";
$sql .= "message_type, ";
$sql .= "message_direction, ";
if ($_SESSION['domain']['time_zone']['name'] != '') {
$sql .= "message_date AT time zone :time_zone AS message_date, ";
}
else {
$sql .= "message_date, ";
}
$sql .= "message_from, ";
$sql .= "message_to, ";
$sql .= "message_text ";
$sql .= "FROM v_messages ";
$sql .= "WHERE user_uuid = :user_uuid ";
$sql .= "AND (domain_uuid = :domain_uuid OR domain_uuid IS NULL) ";
$sql .= $since;
$sql .= "AND (message_from LIKE :message_number OR message_to LIKE :message_number) ";
$sql .= "ORDER BY message_date DESC ";
$sql .= $limit;
if ($_SESSION['domain']['time_zone']['name'] != '') {
$parameters['time_zone'] = $_SESSION['domain']['time_zone']['name'];
}
$parameters['user_uuid'] = $_SESSION['user_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$parameters['message_number'] = '%'.$number;
$database = new database;
$messages = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
if (is_array($messages) && @sizeof($messages) != 0) {
$messages = array_reverse($messages);
//get media (if any)
$sql = "SELECT ";
$sql .= "message_uuid, ";
$sql .= "message_media_uuid, ";
$sql .= "message_media_type, ";
$sql .= "length(decode(message_media_content,'base64')) AS message_media_size ";
$sql .= "FROM v_message_media ";
$sql .= "WHERE user_uuid = :user_uuid ";
$sql .= "AND (domain_uuid = :domain_uuid OR domain_uuid is null) ";
$sql .= "AND ( ";
foreach ($messages as $index => $message) {
$message_uuids[] = "message_uuid = :message_uuid_".$index;
$parameters['message_uuid_'.$index] = $message['message_uuid'];
}
$sql .= implode(' OR ', $message_uuids);
$sql .= ") ";
$sql .= "AND message_media_type <> 'txt' ";
$parameters['user_uuid'] = $_SESSION['user_uuid'];
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$rows = $database->select($sql, $parameters, 'all');
unset($sql, $parameters, $index);
//prep media array
if (is_array($rows) && @sizeof($rows) != 0) {
foreach ($rows as $index => $row) {
$message_media[$row['message_uuid']][$index]['uuid'] = $row['message_media_uuid'];
$message_media[$row['message_uuid']][$index]['type'] = $row['message_media_type'];
$message_media[$row['message_uuid']][$index]['size'] = $row['message_media_size'];
}
}
}
if (!$refresh) {
echo "<div class='flex_container' id='flex_container'>\n";
echo "<div id='flex_scroll'>\n";
echo "<div id='thread_messages'>\n";
echo " <div class='thread_bottom_anchor'>\n";
echo " <div class='thread_bottom_content'>\n";
}
//output messages
if (is_array($messages) && @sizeof($messages) != 0) {
foreach ($messages as $message) {
//parse from message
if ($message['message_direction'] == 'inbound') {
$message_from = $message['message_to'];
$media_source = format_phone($message['message_from']);
}
if ($message['message_direction'] == 'outbound') {
$media_source = format_phone($message['message_to']);
}
//message when
$message_current_date = strtotime(strstr($message['message_date'], '.', true));
$diff = $message_current_date - $message_previous_date;
if ($diff > 600) {
$age = strtotime("now") - $message_current_date;
//today
if (date('Ymd') == date('Ymd', $message_current_date)) {
echo "<div class='message-time'>".date('g:i a', $message_current_date)."</div>\n";
}
//yesterday
elseif (date('Ymd', strtotime('-1 day')) == date('Ymd', $message_current_date)) {
echo "<div class='message-time'>".$text['label-message-time-yesterday']." " .date('g:i a', $message_current_date)."</div>\n";
}
//last 2 to 6 days
elseif (date('Ymd', strtotime('-6 days')) <= date('Ymd', $message_current_date)) {
echo "<div class='message-time'>".date('l', $message_current_date)." " .$text['label-message-time-at']." " .date('g:i a', $message_current_date)."</div>\n";
}
//7 days or older
else {
echo "<div class='message-time'>".(date('d M. Y', $message_current_date))." ".$text['label-message-time-at']." ".date('g:i a', $message_current_date)."</div>\n";
}
}
$message_previous_date = strtotime(strstr($message['message_date'], '.', true));
//message bubble
echo "<div class='message-bubble message-bubble-".($message['message_direction'] == 'inbound' ? 'em' : 'me')."'>";
//contact image em
if ($message['message_direction'] == 'inbound') {
if (is_array($_SESSION['tmp']['messages']['contact_em'][$contact_uuid]) && @sizeof($_SESSION['tmp']['messages']['contact_em'][$contact_uuid]) != 0) {
echo "<div class='message-bubble-image-em'>\n";
echo " <img class='message-bubble-image-em'><br />\n";
echo "</div>\n";
}
}
//contact image me
else {
if (is_array($_SESSION['tmp']['messages']['contact_me']) && @sizeof($_SESSION['tmp']['messages']['contact_me']) != 0) {
echo "<div class='message-bubble-image-me'>\n";
echo " <img class='message-bubble-image-me'><br />\n";
echo "</div>\n";
}
}
//message
if ($message['message_text'] != '') {
echo "<div class='message-text'>".str_replace("\n",'<br />',escape($message['message_text']))."</div>\n";
}
//attachments
if (is_array($message_media[$message['message_uuid']]) && @sizeof($message_media[$message['message_uuid']]) != 0) {
foreach ($message_media[$message['message_uuid']] as $media) {
if ($media['type'] != 'txt') {
if ($media['type'] == 'jpg' || $media['type'] == 'jpeg' || $media['type'] == 'gif' || $media['type'] == 'png') {
echo "<a href='#' onclick=\"display_media('".$media['uuid']."','".$media_source."');\" class='message-media-link-".($message['message_direction'] == 'inbound' ? 'em' : 'me')."'>";
}
else {
echo "<a href='message_media.php?id=".$media['uuid']."&src=".$media_source."&action=download' class='message-media-link-".($message['message_direction'] == 'inbound' ? 'em' : 'me')."'>";
}
echo "<img src='resources/images/attachment.png' style='width: 16px; height: 16px; border: none; margin-right: 10px;'>";
echo "<span style='font-size: 85%; white-space: nowrap;'>".strtoupper($media['type']).' · '.strtoupper(byte_convert($media['size']))."</span>";
echo "</a>\n";
}
}
}
echo "</div>\n";
}
echo "<span id='thread_bottom'></span>\n";
}
echo "<script>\n";
//set current contact
echo " $('#contact_current_number').val('".$number."');\n";
//set bubble contact images from src images
echo " $('img.message-bubble-image-em').attr('src', $('img#src_message-bubble-image-em_".$contact_uuid."').attr('src'));\n";
echo " $('img.message-bubble-image-me').attr('src', $('img#src_message-bubble-image-me').attr('src'));\n";
echo "</script>\n";
if (!$refresh) {
echo "</div></div></div></div></div></div>\n";
if (permission_exists('message_add')) {
//output input form
echo " <form id='message_compose' method='post' enctype='multipart/form-data' action='message_send.php'>\n";
echo " <input type='hidden' name='message_from' value='".$message_from."'>\n";
echo " <input type='hidden' name='message_to' value='".$number."'>\n";
echo "<div class='compose_container'>\n";
echo "<div class='compose_container_child'>\n";
echo " <div class='compose_text_container'><textarea class='formfld' id='message_text' data-emojiable='true' name='message_text' placeholder=\"".$text['description-enter_response']."\"></textarea></div>";
echo " <div class='submit_container'><input type='submit' class='btn btn_send' value='".$text['button-send']."' title=\"".$text['label-ctrl_enter']."\"></div>\n";
echo "</div>\n";
echo "</div>\n";
echo " <div class='attachment'>\n";
echo " <span id='thread_refresh_state'><img src='resources/images/refresh_active.gif' class='refresh' onclick=\"refresh_thread_stop('".$number."','".$contact_uuid."');\" alt=\"".$text['label-refresh_pause']."\" title=\"".$text['label-refresh_pause']."\"></span>\n";
echo " <img src='resources/images/attachment.png' style='min-width: 20px; height: 20px; border: none; padding-right: 5px;'>\n";
echo " <input type='file' class='formfld' multiple='multiple' name='message_media[]' id='message_new_media'>\n";
echo " </div>\n";
echo " </form>\n";
}
}
?>
<script>
//Load messages for clicked number
//Define form submit function
$('#message_compose').submit(function(event) {
event.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: new FormData(this),
processData: false,
contentType: false,
cache: false,
success: function(){
document.getElementById('message_compose').reset();
if (!http_user_agent('mobile')) {
if ($('#message_new_layer').is(':hidden')) {
$('#message_text').trigger('focus');
}
}
refresh_thread('".$number."', '".$contact_uuid."', 'true');
}
})
})
//Enable ctrl+enter to send
$('#message_text').keydown(function (event) {
if ((event.keyCode == 10 || event.keyCode == 13) && event.ctrlKey) {
$('#message_compose').submit();
}
})
//Set messages height to content height
var threadHeight = $('.thread_bottom_content').height() + 210;
document.getElementById('flex_scroll').style.minHeight = threadHeight + "px";
//Scroll thread to bottom on resize if at bottom
function scrollDown() {
var threadDiv = document.getElementById('flex_container');
//Debugging stuff to delete later
/*
var total = threadHeight - (threadDiv.scrollTop + threadDiv.offsetHeight);
var istrue = 'unset';
if (threadDiv.scrollTop + threadDiv.offsetHeight >= threadHeight - 80) {
istrue = 'yes';
}
else {
istrue = 'false';
}
document.getElementById("contacts").innerHTML = 'threadHeight=' + threadHeight + '<br />scrollTop=' + threadDiv.scrollTop + '<br />offsetHeight=' + threadDiv.offsetHeight + '<br /> total=' + total + '<br />true=' + istrue;
*/
if (threadDiv.scrollTop + threadDiv.offsetHeight >= threadHeight - 800) {
threadDiv.scrollTop = threadDiv.scrollHeight;
setTimeout(function(){ threadDiv.scrollTop = threadHeight; }, 0);
}
}
window.addEventListener("resize", scrollDown);
//Restyle messages which contain only an emoji
var messageTextDiv = document.getElementsByClassName('message-text');
for (var i=0; i < messageTextDiv.length; i++) {
if (messageTextDiv[i].innerHTML.match(/^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])$/)) {
messageTextDiv[i].classList.add('emoji');
}
}
</script>
<!-- Begin emoji-picker JavaScript
<script src="resources/js/config.js"></script>
<script src="resources/js/util.js"></script>
<script src="resources/js/jquery.emojiarea.js"></script>
<script src="resources/js/emoji-picker.js"></script>
<script>
$(function() {
// Initializes and creates emoji set from sprite sheet
window.emojiPicker = new EmojiPicker({
emojiable_selector: '[data-emojiable=true]',
assetsPath: 'resources/images/emoji/',
popupButtonClasses: 'fa fa-smile-o'
});
window.emojiPicker.discover();
});
</script>
-->
<!-- End emoji-picker JavaScript -->