messages_contacts.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (!permission_exists('message_view')) {
  26. echo "access denied";
  27. exit;
  28. }
  29. //add multi-lingual support
  30. $language = new text;
  31. $text = $language->get();
  32. //connect to the database
  33. //connect to the database
  34. $database = database::new();
  35. //get selected number/contact
  36. if (isset($_GET['number']) && !empty($_GET['number'])) {
  37. $_SESSION['user']['contact_number'] = $_GET['number'];
  38. }
  39. //build a list of groups the user is a member of to be used in a SQL in
  40. foreach($_SESSION['user']['groups'] as $group) {
  41. if (is_uuid($group['group_uuid'])) {
  42. $group_uuids[] = $group['group_uuid'];
  43. }
  44. }
  45. $group_uuids_in = "'".implode("','", $group_uuids)."'";
  46. //get the list of contacts
  47. $sql = "select *, \n";
  48. $sql .= "(\n";
  49. $sql .= " (select effective_caller_id_name as name from v_extensions where n.number = extension limit 1)\n";
  50. $sql .= " union\n";
  51. $sql .= " (select concat_ws(' ', contact_name_given, contact_name_family) as name from v_contacts where contact_uuid in (select contact_uuid from v_contact_phones where n.number = phone_number) limit 1)\n";
  52. $sql .= ") as name,\n";
  53. $sql .= "(\n";
  54. $sql .= " select contact_uuid from v_contact_phones where n.number = phone_number limit 1\n";
  55. $sql .= ") as contact_uuid,\n";
  56. $sql .= "(\n";
  57. $sql .= " select attachment_filename from v_contact_attachments where contact_uuid in (select contact_uuid from v_contact_phones where n.number = phone_number)\n";
  58. $sql .= ") as contact_image_filename,\n";
  59. $sql .= "(\n";
  60. $sql .= " select attachment_content from v_contact_attachments where contact_uuid in (select contact_uuid from v_contact_phones where n.number = phone_number)\n";
  61. $sql .= ") as contact_image_content,\n";
  62. $sql .= "(\n";
  63. $sql .= " select count(*) as count from v_messages where message_read is not true and message_direction = 'inbound' and message_from = n.number\n";
  64. $sql .= ") as count,\n";
  65. $sql .= "(\n";
  66. $sql .= " select message_text from v_messages\n";
  67. $sql .= " where \n";
  68. $sql .= " (\n";
  69. $sql .= " (message_direction = 'inbound' and message_from = n.number)\n";
  70. $sql .= " or \n";
  71. $sql .= " (message_direction = 'outbound' and message_to = n.number)\n";
  72. $sql .= " )\n";
  73. $sql .= " and message_text is not null\n";
  74. $sql .= " order by message_date desc limit 1\n";
  75. $sql .= ") as message,\n";
  76. $sql .= "(\n";
  77. $sql .= " select message_date from v_messages \n";
  78. $sql .= " where (\n";
  79. $sql .= " (message_direction = 'inbound' and message_from = n.number)\n";
  80. $sql .= " or \n";
  81. $sql .= " (message_direction = 'outbound' and message_to = n.number)\n";
  82. $sql .= " )\n";
  83. $sql .= " and message_text is not null\n";
  84. $sql .= " order by message_date desc limit 1\n";
  85. $sql .= ") as date\n";
  86. $sql .= "from (\n";
  87. $sql .= " select number from \n";
  88. $sql .= " (\n";
  89. $sql .= " select distinct(message_from) as number from v_messages \n";
  90. //$sql .= " select distinct(REPLACE(message_from, '+', '')) as number from v_messages \n";
  91. $sql .= " where domain_uuid = :domain_uuid \n";
  92. $sql .= " and message_direction = 'inbound' and message_from is not null \n";
  93. //$sql .= " and user_uuid = :user_uuid \n";
  94. $sql .= " and ( \n";
  95. $sql .= " user_uuid = :user_uuid \n";
  96. $sql .= " or \n";
  97. $sql .= " group_uuid in (\n";
  98. $sql .= " select group_uuid from v_destinations \n";
  99. $sql .= " where group_uuid in (".$group_uuids_in.") \n";
  100. $sql .= " and domain_uuid = :domain_uuid \n";
  101. $sql .= " ) \n";
  102. $sql .= " )\n";
  103. $sql .= " and message_from ~'^\+?([0-9]+\.?[0-9]*|\.[0-9]+)$' \n";
  104. $sql .= " union \n";
  105. $sql .= " select distinct(message_to) as number from v_messages \n";
  106. //$sql .= " select distinct(REPLACE(message_to, '+', '')) as number from v_messages \n";
  107. $sql .= " where domain_uuid = :domain_uuid \n";
  108. $sql .= " and message_direction = 'outbound' and message_from is not null \n";
  109. //$sql .= " and user_uuid = :user_uuid ";
  110. $sql .= " and ( \n";
  111. $sql .= " user_uuid = :user_uuid \n";
  112. $sql .= " or \n";
  113. $sql .= " group_uuid in (\n";
  114. $sql .= " select group_uuid from v_destinations \n";
  115. $sql .= " where group_uuid in (".$group_uuids_in.") \n";
  116. $sql .= " and domain_uuid = :domain_uuid \n";
  117. $sql .= " ) \n";
  118. $sql .= " ) \n";
  119. $sql .= " and message_to ~'^\+?([0-9]+\.?[0-9]*|\.[0-9]+)$' \n";
  120. $sql .= " ) as nested \n";
  121. $sql .= " where number not in \n";
  122. $sql .= " ( \n";
  123. $sql .= " select destination_number as number \n";
  124. $sql .= " from v_destinations \n";
  125. $sql .= " where destination_type = 'inbound' \n";
  126. $sql .= " and domain_uuid = :domain_uuid \n";
  127. $sql .= " union \n";
  128. $sql .= " select (concat(destination_prefix, destination_number)) as number \n";
  129. $sql .= " from v_destinations \n";
  130. $sql .= " where destination_type = 'inbound' \n";
  131. $sql .= " and domain_uuid = :domain_uuid \n";
  132. $sql .= " ) \n";
  133. $sql .= " order by number asc\n";
  134. $sql .= ") as n\n";
  135. $sql .= "order by \n";
  136. //uncomment below to have the selected message pop up to the top of the list
  137. //$sql .= "case when (number = :number) then 0 end asc,\n";
  138. $sql .= "date desc\n";
  139. $parameters['domain_uuid'] = $domain_uuid;
  140. $parameters['user_uuid'] = $_SESSION['user']['user_uuid'];
  141. //uncomment below to have the selected message pop up to the top of the list
  142. //$parameters['number'] = $_SESSION['user']['contact_number'] ?? null;
  143. //echo "<pre>\n";
  144. //echo $sql;
  145. //echo "</pre>\n";
  146. //view_array($parameters);
  147. $contacts = $database->select($sql, $parameters, 'all');
  148. //view_array($contacts);
  149. unset($sql, $parameters);
  150. //show the content
  151. echo "<!DOCTYPE html>\n";
  152. echo "<html>\n";
  153. echo "<head>\n";
  154. //include icons
  155. echo "<link rel='stylesheet' type='text/css' href='/resources/fontawesome/css/all.min.css.php'>\n";
  156. // echo "<script language='JavaScript' type='text/javascript' src='/resources/fontawesome/js/solid.min.js.php' defer></script>\n";
  157. //js to load messages for clicked number
  158. echo "<script>\n";
  159. //scroll to the bottom
  160. echo " function scroll_to_bottom(id) {\n";
  161. echo " parent.document.getElementById(id).contentWindow.scrollTo(0, 999999);\n";
  162. echo " }\n";
  163. echo "\n";
  164. //update the url
  165. echo " function update_url(id, url) {\n";
  166. //echo " alert('from: '+parent.document.getElementById('message_from').value);\n";
  167. //echo " alert('to: '+parent.document.getElementById('message_to').value);\n";
  168. //echo " alert('to: '+parent.document.getElementById('message_text').value);\n";
  169. echo " parent.document.getElementById(id).onload = function() {\n";
  170. // echo " scroll_to_bottom(id);\n";
  171. echo " this.contentWindow.scrollTo(0, 999999);\n";
  172. echo " }\n";
  173. echo " parent.document.getElementById(id).src = url;\n";
  174. //echo " scroll_to_bottom(id);\n";
  175. echo " }\n";
  176. echo "</script>\n";
  177. //styles
  178. echo "<style>\n";
  179. echo "\n";
  180. echo " body {\n";
  181. echo " margin: 0 14px 0 0;\n";
  182. echo " }\n";
  183. echo " #message_new_layer {\n";
  184. echo " z-index: 999999;\n";
  185. echo " position: absolute;\n";
  186. echo " left: 0px;\n";
  187. echo " top: 0px;\n";
  188. echo " right: 0px;\n";
  189. echo " bottom: 0px;\n";
  190. echo " text-align: center;\n";
  191. echo " vertical-align: middle;\n";
  192. echo " }\n";
  193. echo "\n";
  194. echo " #message_new_container {\n";
  195. echo " display: block;\n";
  196. echo " background-color: #fff;\n";
  197. echo " padding: 20px 30px;\n";
  198. if (http_user_agent('mobile')) {
  199. echo " margin: 0;\n";
  200. }
  201. else {
  202. echo " margin: auto 30%;\n";
  203. }
  204. echo " text-align: left;\n";
  205. echo " -webkit-box-shadow: 0px 1px 20px #888;\n";
  206. echo " -moz-box-shadow: 0px 1px 20px #888;\n";
  207. echo " box-shadow: 0px 1px 20px #888;\n";
  208. echo " }\n";
  209. echo "\n";
  210. echo " #message_media_layer {\n";
  211. echo " z-index: 999999;\n";
  212. echo " position: absolute;\n";
  213. echo " left: 0px;\n";
  214. echo " top: 0px;\n";
  215. echo " right: 0px;\n";
  216. echo " bottom: 0px;\n";
  217. echo " text-align: center;\n";
  218. echo " vertical-align: middle;\n";
  219. echo " }\n";
  220. echo "\n";
  221. echo " td.contact_selected {\n";
  222. echo " border-right: 5px solid ".($_SESSION['theme']['message_bubble_em_border_color']['text'] ?? '#abefa0').";\n";
  223. echo " }\n";
  224. echo "\n";
  225. echo " .contact_list_image {\n";
  226. echo " float: left;\n";
  227. echo " width: 75px;\n";
  228. echo " height: 75px;\n";
  229. echo " margin: 3px 8px 3px 2px;\n";
  230. echo " border: 1px solid ".($_SESSION['theme']['table_row_border_color']['text'] ?? '#c5d1e5').";\n";
  231. echo " background-repeat: no-repeat;\n";
  232. echo " background-size: cover;\n";
  233. echo " background-position: center center;\n";
  234. echo " border-radius: 11px;\n";
  235. echo " }\n";
  236. echo "\n";
  237. echo " .row_style0 {\n";
  238. echo " border-top: 1px solid ".($_SESSION['theme']['message_bubble_em_border_color']['text'] ?? '#abefa0').";\n";
  239. echo " border-bottom: 1px solid ".($_SESSION['theme']['message_bubble_em_border_color']['text'] ?? '#abefa0').";\n";
  240. echo " border-radius: 4px;\n";
  241. echo " background: ".($_SESSION['theme']['message_bubble_em_background_color']['text'] ?? '#daffd4').";\n";
  242. echo " color: ".($_SESSION['theme']['body_text_color']['text'] ?? '#5f5f5f').";\n";
  243. echo " font-family: ".($_SESSION['theme']['body_text_font']['text'] ?? 'arial').";\n";
  244. echo " font-size: 12px;\n";
  245. echo " text-align: left;\n";
  246. echo " padding: 4px 7px;\n";
  247. echo " padding-top: 8px;\n";
  248. echo " cursor: pointer;\n";
  249. echo " }\n";
  250. echo "\n";
  251. echo " .row_style1 {\n";
  252. echo " border-bottom: 1px solid ".($_SESSION['theme']['table_row_border_color']['text'] ?? '#c5d1e5').";\n";
  253. echo " border-radius: 4px;\n";
  254. echo " color: ".($_SESSION['theme']['body_text_color']['text'] ?? '#5f5f5f').";\n";
  255. echo " font-family: ".($_SESSION['theme']['body_text_font']['text'] ?? 'arial').";\n";
  256. echo " font-size: 12px;\n";
  257. echo " text-align: left;\n";
  258. echo " padding: 4px 7px;\n";
  259. echo " padding-top: 8px;\n";
  260. echo " cursor: pointer;\n";
  261. echo " }\n";
  262. echo "\n";
  263. echo " .contact_message {\n";
  264. echo " margin-top: 5px;\n";
  265. echo " padding-left: 5px;\n";
  266. echo " }\n";
  267. echo " @media (max-width: 121px) {\n";
  268. echo " .contact_message {\n";
  269. echo " display: none;\n";
  270. echo " }\n";
  271. echo " .contact_image {\n";
  272. echo " float: none;\n";
  273. echo " margin-left: 20.5%;\n";
  274. echo " }\n";
  275. echo " .row_style0, .row_style1 {\n";
  276. echo " padding-bottom: 0px;\n";
  277. echo " text-align: center;\n";
  278. echo " }\n";
  279. echo " }\n";
  280. echo "</style>\n";
  281. //end the header and start the body
  282. echo "</head>\n";
  283. echo "<body>\n";
  284. //contacts list
  285. if (!empty($contacts) && @sizeof($contacts) != 0) {
  286. echo "<table class='tr_hover' width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  287. foreach ($contacts as $row) {
  288. $number = $row['number'];
  289. $name = $row['name'];
  290. $count = $row['count'];
  291. $message = $row['message'];
  292. $date = $row['date'];
  293. // $row['contact_image_content'];
  294. // $row['contact_image_filename'];
  295. // $row['contact_uuid'];
  296. //get the image file extension
  297. if (!empty($row['contact_image_filename'])) {
  298. $contact_image_extension = pathinfo($row['contact_image_filename'], PATHINFO_EXTENSION);
  299. }
  300. //set the count label
  301. if ($count == 0) {
  302. $count = '';
  303. }
  304. else {
  305. $count = ' ('.$count.')';
  306. }
  307. //$contact_name = format_phone($row['number']);
  308. $contact_name = $row['number'];
  309. if (!empty($row['name'])) {
  310. $contact_name = escape($row['name']);
  311. }
  312. if (!empty($_SESSION['user']['contact_number']) && $_SESSION['user']['contact_number'] == $number) {
  313. echo "<tr onclick=\"parent.document.getElementById('message_to').value=".escape($number)."; parent.document.getElementById('contacts_frame').src='messages_contacts.php?number=".urlencode($number)."'; update_url('messages_frame', 'messages_thread.php?number=".urlencode($number)."'); ".(permission_exists('contact_view') && !empty($_SESSION['message']['contact_details']['boolean']) && $_SESSION['message']['contact_details']['boolean'] == 'true' ? "parent.document.getElementById('contact_frame').src='message_contact.php?id=".$row['contact_uuid']."';" : null)."\"><td valign='top' class='row_style0 contact_selected'>\n";
  314. if (permission_exists('contact_view') && !empty($_SESSION['message']['contact_details']['boolean']) && $_SESSION['message']['contact_details']['boolean'] == 'true') {
  315. echo "<script>parent.document.getElementById('contact_frame').src='message_contact.php?destination=".urlencode($number)."&id=".$row['contact_uuid']."';</script>";
  316. }
  317. $selected = true;
  318. }
  319. else {
  320. echo "<tr onclick=\"parent.document.getElementById('message_to').value=".escape($number)."; parent.document.getElementById('contacts_frame').src='messages_contacts.php?number=".urlencode($number)."'; update_url('messages_frame', 'messages_thread.php?number=".urlencode($number)."'); ".(permission_exists('contact_view') && !empty($_SESSION['message']['contact_details']['boolean']) && $_SESSION['message']['contact_details']['boolean'] == 'true' ? "parent.document.getElementById('contact_frame').src='message_contact.php';" : null)."\"><td valign='top' class='row_style1'>\n"; // onclick=\"load_thread('".urlencode($number)."', '".$contact[$number]['contact_uuid']."');\"
  321. $selected = false;
  322. }
  323. if (!empty($row['contact_image_filename'])) {
  324. //echo "<img id='src_message-bubble-image-em_".$row['contact_uuid']."' style='display: none;' src='data:image/".$contact_image_extension.";base64,".$row['contact_image_content']."'>\n";
  325. echo "<div class='contact_image' style='width: 50px; height: 50px; float: left; padding-right: 3px;'>\n";
  326. echo " <img id='src_message-bubble-image-em_".$row['contact_uuid']."' src=\"data:image/png;base64,".$row['contact_image_content']."\" style=\"width: 50px;\">\n";
  327. echo "</div>\n";
  328. //echo "<img id='contact_image_".$row['contact_uuid']."' class='contact_list_image' src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'>\n";
  329. }
  330. else {
  331. echo "<div class='contact_image' style='width: 50px; height: 50px; float: left; padding-right: 3px;'>\n";
  332. echo " <i class='fas fa-".(!empty($contact_name) && !is_numeric($contact_name) ? 'user-tie' : 'user')." fa-fw fa-3x' style='margin-top: 4px; color: ".($selected ? ($_SESSION['theme']['message_bubble_em_when_color']['text'] ?? '#52b342') : '#ccc').";'></i>\n";
  333. echo "</div>\n";
  334. }
  335. echo "<div style='padding-left: 3px; margin-bottom: 4px;'>\n";
  336. echo " <a href='view:messages' onclick='event.preventDefault();' style='text-decoration: none; color: ".($_SESSION['theme']['text_link_color']['text'] ?? '#004083').";'>\n";
  337. echo " <strong>".(is_numeric($contact_name) ? format_phone($contact_name) : escape($contact_name))."</strong>".$count."<br />\n";
  338. echo " </a>\n";
  339. echo " <div class='contact_message'>\n";
  340. echo " ".(!empty($message) && strlen($message) <= 100 ? escape($message) : substr($message,0,100).'...')."<br />\n";
  341. echo " </div>\n";
  342. echo "</div>\n";
  343. //if ($selected) {
  344. // echo "<script>$('#contact_current_name').html(\"<a href='callto:".escape($number)."'>".escape(format_phone($number))."</a>\");</script>\n";
  345. //}
  346. echo "</td></tr>\n";
  347. }
  348. echo "</table>\n";
  349. //echo "<script>\n";
  350. //foreach ($numbers as $number) {
  351. // if (is_array($_SESSION['tmp']['messages']['contact_em'][$contact[$number]['contact_uuid']]) && @sizeof($_SESSION['tmp']['messages']['contact_em'][$contact[$number]['contact_uuid']]) != 0) {
  352. // echo "$('img#contact_image_".$contact[$number]['contact_uuid']."').css('backgroundImage', 'url(' + $('img#src_message-bubble-image-em_".$contact[$number]['contact_uuid']."').attr('src') + ')');\n";
  353. // }
  354. //}
  355. //echo "</script>\n";
  356. }
  357. else {
  358. echo "<div style='padding: 15px;'><center>&middot;&middot;&middot;</center>";
  359. }
  360. echo "</body>\n";
  361. //echo "<center>\n";
  362. //echo " <span id='contacts_refresh_state'><img src='resources/images/refresh_active.gif' style='width: 16px; height: 16px; border: none; margin-top: 3px; cursor: pointer;' onclick=\"refresh_contacts_stop();\" alt=\"".$text['label-refresh_pause']."\" title=\"".$text['label-refresh_pause']."\"></span> ";
  363. //echo "</center>\n";
  364. echo "</html>\n";
  365. ?>