2018-12-20 07:21:32 +01:00
< ? 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 < markjcrane @ fusionpbx . com >
Portions created by the Initial Developer are Copyright ( C ) 2016 - 2018
the Initial Developer . All Rights Reserved .
Contributor ( s ) :
Mark J Crane < markjcrane @ fusionpbx . com >
*/
//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' ]);
2019-03-29 00:58:02 +01:00
$contact_uuid = $_GET [ 'contact_uuid' ];
2018-12-20 07:21:32 +01:00
//set refresh flag
$refresh = $_GET [ 'refresh' ] == 'true' ? true : false ;
2019-03-29 00:58:02 +01:00
//get contact (primary attachment) image, if any
if ( is_uuid ( $contact_uuid ) && ( ! is_array ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ]) || $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ] != $contact_uuid )) {
$sql = " select attachment_filename as filename, attachment_content as image from v_contact_attachments " ;
$sql .= " where domain_uuid = ' " . $_SESSION [ 'domain_uuid' ] . " ' " ;
$sql .= " and contact_uuid = :contact_uuid " ;
$sql .= " and attachment_primary = 1 " ;
$bind [ ':contact_uuid' ] = $contact_uuid ;
$prep_statement = $db -> prepare ( check_sql ( $sql ));
$prep_statement -> execute ( is_array ( $bind ) ? $bind : null );
$_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ] = $prep_statement -> fetch ( PDO :: FETCH_NAMED );
unset ( $sql , $bind , $prep_statement );
}
2018-12-21 05:03:12 +01:00
//get messages
2018-12-29 19:13:18 +01:00
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 " . $array [ 0 ] . " offset 0 " ;
}
else {
$since = " and message_date >= ' " . date ( " Y-m-d H:i:s " , strtotime ( '-' . $_SESSION [ 'message' ][ 'display_last' ][ 'text' ])) . " ' " ;
}
}
}
if ( $limit == '' && $since == '' ) { $limit = " limit 25 offset 0 " ; } //default (message count)
2019-03-29 00:58:02 +01:00
$sql = " select " ;
$sql .= " message_uuid, " ;
$sql .= " domain_uuid, " ;
$sql .= " user_uuid, " ;
$sql .= " contact_uuid, " ;
$sql .= " message_type, " ;
$sql .= " message_direction, " ;
$sql .= " message_date at time zone ' " . $_SESSION [ 'domain' ][ 'time_zone' ][ 'name' ] . " ' as message_date, " ;
$sql .= " message_from, " ;
$sql .= " message_to, " ;
$sql .= " message_text " ;
$sql .= " from v_messages " ;
2018-12-20 07:21:32 +01:00
$sql .= " where user_uuid = ' " . $_SESSION [ 'user_uuid' ] . " ' " ;
$sql .= " and (domain_uuid = ' " . $domain_uuid . " ' or domain_uuid is null) " ;
2018-12-29 19:13:18 +01:00
$sql .= $since ;
2018-12-20 07:21:32 +01:00
$sql .= " and (message_from like '% " . $number . " ' or message_to like '% " . $number . " ') " ;
2018-12-29 19:13:18 +01:00
$sql .= " order by message_date desc " ;
$sql .= $limit ;
2018-12-20 07:21:32 +01:00
$prep_statement = $db -> prepare ( check_sql ( $sql ));
$prep_statement -> execute ();
$messages = $prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
2018-12-29 19:13:18 +01:00
$messages = array_reverse ( $messages );
2018-12-20 07:21:32 +01:00
unset ( $prep_statement , $sql );
2018-12-21 05:03:12 +01:00
//get media (if any)
2019-03-28 19:20:15 +01:00
$sql = " select message_uuid, message_media_uuid, message_media_type, length(decode(message_media_content,'base64')) as message_media_size from v_message_media " ;
2018-12-21 05:03:12 +01:00
$sql .= " where user_uuid = ' " . $_SESSION [ 'user_uuid' ] . " ' " ;
$sql .= " and (domain_uuid = ' " . $domain_uuid . " ' or domain_uuid is null) " ;
$sql .= " and message_uuid in ( " ;
foreach ( $messages as $message ) {
$message_uuids [] = " ' " . $message [ 'message_uuid' ] . " ' " ;
}
$sql .= implode ( ',' , $message_uuids );
$sql .= " ) " ;
$sql .= " and message_media_type <> 'txt' " ;
$prep_statement = $db -> prepare ( check_sql ( $sql ));
$prep_statement -> execute ();
$rows = $prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
unset ( $prep_statement , $sql );
//prep media array
if ( is_array ( $rows ) && sizeof ( $rows ) != 0 ) {
$x = 0 ;
foreach ( $rows as $row ) {
$message_media [ $row [ 'message_uuid' ]][ $x ][ 'uuid' ] = $row [ 'message_media_uuid' ];
$message_media [ $row [ 'message_uuid' ]][ $x ][ 'type' ] = $row [ 'message_media_type' ];
$message_media [ $row [ 'message_uuid' ]][ $x ][ 'size' ] = $row [ 'message_media_size' ];
$x ++ ;
}
}
2018-12-20 07:21:32 +01:00
//css styles
2018-12-21 02:42:10 +01:00
echo " <style> \n " ;
echo " .message-bubble { \n " ;
2019-03-29 00:58:02 +01:00
echo " display: table; \n " ;
2018-12-21 02:42:10 +01:00
echo " padding: 10px; \n " ;
echo " border: 1px solid; \n " ;
echo " margin-bottom: 10px; \n " ;
2019-03-29 00:58:02 +01:00
echo " } \n " ;
2018-12-21 02:42:10 +01:00
echo " .message-bubble-em { \n " ;
2019-03-29 00:58:02 +01:00
echo " margin-right: 30%; \n " ;
echo " border-radius: 0 20px 20px 20px; \n " ;
2018-12-21 02:42:10 +01:00
echo " border-color: #cffec7; \n " ;
echo " background-color: #ecffe9; \n " ;
2019-03-29 00:58:02 +01:00
echo " clear: both; \n " ;
echo " } \n " ;
2018-12-21 02:42:10 +01:00
echo " .message-bubble-me { \n " ;
2019-03-29 00:58:02 +01:00
echo " float: right; \n " ;
echo " margin-left: 30%; \n " ;
echo " border-radius: 20px 20px 0 20px; \n " ;
2018-12-21 02:42:10 +01:00
echo " border-color: #cbf0ff; \n " ;
echo " background-color: #e5f7ff; \n " ;
2019-03-29 00:58:02 +01:00
echo " clear: both; \n " ;
echo " } \n " ;
echo " img.message-bubble-image-em { \n " ;
echo " width: 100px; \n " ;
echo " height: auto; \n " ;
echo " border-radius: 0 11px 11px 11px; \n " ;
2019-03-29 01:15:22 +01:00
echo " border: 1px solid #cffec7; \n " ;
2019-03-29 00:58:02 +01:00
echo " } \n " ;
echo " img.message-bubble-image-me { \n " ;
echo " width: 100px; \n " ;
echo " height: auto; \n " ;
echo " border-radius: 11px 11px 0 11px; \n " ;
2019-03-29 01:15:22 +01:00
echo " border: 1px solid #cbf0ff; \n " ;
2019-03-29 00:58:02 +01:00
echo " } \n " ;
echo " div.message-bubble-image-em { \n " ;
echo " float: left; \n " ;
echo " margin-right: 15px; \n " ;
echo " text-align: left; \n " ;
echo " } \n " ;
echo " div.message-bubble-image-me { \n " ;
echo " float: right; \n " ;
echo " margin-left: 15px; \n " ;
echo " text-align: right; \n " ;
echo " } \n " ;
echo " .message-text { \n " ;
echo " padding-bottom: 5px; \n " ;
echo " font-size: 90%; \n " ;
echo " } \n " ;
2018-12-21 02:42:10 +01:00
echo " .message-bubble-when { \n " ;
2019-03-29 00:58:02 +01:00
echo " font-size: 71%; \n " ;
echo " font-style: italic; \n " ;
echo " } \n " ;
2018-12-21 05:03:12 +01:00
echo " .message-media-link-em { \n " ;
2019-03-29 00:58:02 +01:00
echo " display: inline-block; \n " ;
echo " margin: 5px 10px 5px 0; \n " ;
2018-12-21 05:03:12 +01:00
echo " padding: 8px; \n " ;
echo " background: #cffec7; \n " ;
echo " border-radius: 7px; \n " ;
echo " text-align: center; \n " ;
2019-03-29 00:58:02 +01:00
echo " } \n " ;
2018-12-21 05:03:12 +01:00
echo " .message-media-link-me { \n " ;
2019-03-29 00:58:02 +01:00
echo " display: inline-block; \n " ;
echo " margin: 5px 10px 5px 0; \n " ;
2018-12-21 05:03:12 +01:00
echo " padding: 8px; \n " ;
echo " background: #cbf0ff; \n " ;
echo " border-radius: 7px; \n " ;
echo " text-align: center; \n " ;
2019-03-29 00:58:02 +01:00
echo " } \n " ;
2018-12-21 05:03:12 +01:00
2018-12-21 02:42:10 +01:00
echo " </style> \n " ;
2018-12-20 07:21:32 +01:00
if ( ! $refresh ) {
2019-03-29 00:58:02 +01:00
echo " <div id='thread_messages' style='min-height: 300px; overflow: auto; padding-right: 15px;'> \n " ;
2018-12-20 07:21:32 +01:00
}
//output messages
if ( is_array ( $messages ) && sizeof ( $messages ) != 0 ) {
foreach ( $messages as $message ) {
2018-12-21 05:03:12 +01:00
//parse from message
2018-12-20 07:21:32 +01:00
if ( $message [ 'message_direction' ] == 'inbound' ) {
$message_from = $message [ 'message_to' ];
2018-12-21 05:03:12 +01:00
$media_source = format_phone ( $message [ 'message_from' ]);
}
if ( $message [ 'message_direction' ] == 'outbound' ) {
$media_source = format_phone ( $message [ 'message_to' ]);
2018-12-20 07:21:32 +01:00
}
2018-12-21 05:03:12 +01:00
//message bubble
2019-03-29 00:58:02 +01:00
echo " <span class='message-bubble message-bubble- " . ( $message [ 'message_direction' ] == 'inbound' ? 'em' : 'me' ) . " '> " ;
//contact image
if ( $message [ 'message_direction' ] == 'inbound' ) { //em
if ( is_array ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ]) && sizeof ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ]) != 0 ) {
$attachment_type = strtolower ( pathinfo ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ][ 'filename' ], PATHINFO_EXTENSION ));
echo " <div class='message-bubble-image-em'> \n " ;
echo " <img class='message-bubble-image-em' src='data:image/ " . $attachment_type . " ;base64, " . $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_em' ][ $contact_uuid ][ 'image' ] . " '><br /> \n " ;
echo " </div> \n " ;
}
2018-12-21 06:53:00 +01:00
}
2019-03-29 00:58:02 +01:00
else { //me
if ( is_array ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_me' ]) && sizeof ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_me' ]) != 0 ) {
$attachment_type = strtolower ( pathinfo ( $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_me' ][ 'filename' ], PATHINFO_EXTENSION ));
echo " <div class='message-bubble-image-me'> \n " ;
echo " <img class='message-bubble-image-me' src='data:image/ " . $attachment_type . " ;base64, " . $_SESSION [ 'tmp' ][ 'messages' ][ 'contact_me' ][ 'image' ] . " '><br /> \n " ;
echo " </div> \n " ;
}
2018-12-21 06:53:00 +01:00
}
2019-03-29 00:58:02 +01:00
echo " <div style='display: table;'> \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%;'> " . strtoupper ( $media [ 'type' ]) . ' · ' . strtoupper ( byte_convert ( $media [ 'size' ])) . " </span> " ;
echo " </a> \n " ;
}
}
echo " <br /> \n " ;
}
//message when
2019-03-29 01:09:51 +01:00
echo " <span class='message-bubble-when'> " . ( date ( 'm-d-Y' ) != format_when_local ( $message [ 'message_date' ], 'd' ) ? format_when_local ( $message [ 'message_date' ]) : format_when_local ( $message [ 'message_date' ], 't' )) . " </span> \n " ;
2019-03-29 00:58:02 +01:00
echo " </div> \n " ;
echo " </span> \n " ;
2018-12-20 07:21:32 +01:00
}
2018-12-29 19:13:18 +01:00
echo " <span id='thread_bottom'></span> \n " ;
2018-12-20 07:21:32 +01:00
}
2018-12-30 00:05:59 +01:00
//set current contact
echo " <script> $ ('#contact_current_number').val(' " . $number . " ');</script> \n " ;
2018-12-20 07:21:32 +01:00
if ( ! $refresh ) {
echo " </div> \n " ;
if ( permission_exists ( 'message_add' )) {
//output input form
2018-12-29 19:13:18 +01:00
echo " <form id='message_compose' method='post' enctype='multipart/form-data' action='message_send.php'> \n " ;
2018-12-20 07:21:32 +01:00
echo " <input type='hidden' name='message_from' value=' " . $message_from . " '> \n " ;
echo " <input type='hidden' name='message_to' value=' " . $number . " '> \n " ;
2018-12-29 19:13:18 +01:00
echo " <textarea class='formfld' id='message_text' name='message_text' style='width: 100%; max-width: 100%; min-height: 55px; border: 1px solid #cbcbcb; resize: vertical; padding: 5px 8px; margin-top: 10px; margin-bottom: 5px;' placeholder= \" " . $text [ 'description-enter_response' ] . " \" ></textarea> " ;
echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' style='margin-top: 5px;'> \n " ;
echo " <tr> \n " ;
echo " <td><img src='resources/images/attachment.png' style='min-width: 20px; height: 20px; border: none; padding-right: 5px;'></td> \n " ;
echo " <td width='100%'><input type='file' class='formfld' multiple='multiple' name='message_media[]' id='message_new_media'></td> \n " ;
echo " </td> \n " ;
echo " </table> \n " ;
echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' style='margin-top: 15px;'> \n " ;
echo " <tr> \n " ;
2018-12-30 00:05:59 +01:00
echo " <td align='left' width='50%'><input type='reset' class='btn' value=' " . $text [ 'button-clear' ] . " ' onclick= \" $ ('#message_text').focus(); \" ></td> \n " ;
2019-03-29 00:58:02 +01:00
echo " <td align='center'><span id='thread_refresh_state'><img src='resources/images/refresh_active.gif' style='width: 16px; height: 16px; border: none; cursor: pointer;' onclick= \" refresh_thread_stop(' " . $number . " ',' " . $contact_uuid . " '); \" alt= \" " . $text [ 'label-refresh_pause' ] . " \" title= \" " . $text [ 'label-refresh_pause' ] . " \" ></span></td> \n " ;
2018-12-30 00:05:59 +01:00
echo " <td align='right' width='50%'><input type='submit' class='btn' value=' " . $text [ 'button-send' ] . " ' title= \" " . $text [ 'label-ctrl_enter' ] . " \" ></td> \n " ;
2018-12-29 19:13:18 +01:00
echo " </td> \n " ;
echo " </table> \n " ;
2018-12-20 07:21:32 +01:00
echo " </form> \n " ;
//js to load messages for clicked number
echo " <script> \n " ;
//define form submit function
echo " $ ('#message_compose').submit(function(event) { \n " ;
echo " event.preventDefault(); \n " ;
echo " $ .ajax( { \n " ;
2018-12-29 19:13:18 +01:00
echo " url: $ (this).attr('action'), \n " ;
echo " type: $ (this).attr('method'), \n " ;
echo " data: new FormData(this), \n " ;
echo " processData: false, \n " ;
echo " contentType: false, \n " ;
echo " cache: false, \n " ;
2018-12-20 07:21:32 +01:00
echo " success: function() { \n " ;
echo " document.getElementById('message_compose').reset(); \n " ;
2018-12-27 17:12:24 +01:00
if ( ! http_user_agent ( 'mobile' )) {
echo " if ( $ ('#message_new_layer').is(':hidden')) { \n " ;
echo " $ ('#message_text').focus(); \n " ;
echo " } \n " ;
}
2019-03-29 00:58:02 +01:00
echo " refresh_thread(' " . $number . " ', ' " . $contact_uuid . " ', 'true'); \n " ;
2018-12-20 07:21:32 +01:00
echo " } \n " ;
echo " }); \n " ;
echo " }); \n " ;
//enable ctrl+enter to send
echo " $ ('#message_text').keydown(function (event) { \n " ;
echo " if ((event.keyCode == 10 || event.keyCode == 13) && event.ctrlKey) { \n " ;
echo " $ ('#message_compose').submit(); \n " ;
echo " } \n " ;
echo " }); \n " ;
echo " </script> \n " ;
}
}
?>