LICENSE: AGPLv2
#
# CHANGES
# 151212-0828 - First Build for customer chat
# 151213-1106 - Added variable filtering
# 151217-1016 - Allow for carrying of group_id through to new chat
# 151219-0850 - Added translation code
# 160108-1710 - Added available_agents variable
# 160120-1943 - Added show_email variable
# 160203-1051 - Added display of chat message after ending it
# 160303-0055 - Added code for chat transfers
# 160725-1711 - Fixed nested iframe issue
# 160805-2315 - Added coding to show logos in customer display
# 161026-2230 - Added translation QXZ to untranslated text
# 220220-1940 - Added allow_web_debug system setting
# 240801-1130 - Code updates for PHP8 compatibility
#
require("dbconnect_mysqli.php");
require("functions.php");
$style_array=array("", "italics", "bold italics");
if (isset($_GET["action"])) {$action=$_GET["action"];}
elseif (isset($_POST["action"])) {$action=$_POST["action"];}
if (isset($_GET["DB"])) {$DB=$_GET["DB"];}
elseif (isset($_POST["DB"])) {$DB=$_POST["DB"];}
if (isset($_GET["chat_id"])) {$chat_id=$_GET["chat_id"];}
elseif (isset($_POST["chat_id"])) {$chat_id=$_POST["chat_id"];}
if (isset($_GET["chat_level"])) {$chat_level=$_GET["chat_level"];}
elseif (isset($_POST["chat_level"])) {$chat_level=$_POST["chat_level"];}
if (isset($_GET["chat_member_name"])) {$chat_member_name=$_GET["chat_member_name"];}
elseif (isset($_POST["chat_member_name"])) {$chat_member_name=$_POST["chat_member_name"];}
if (isset($_GET["chat_message"])) {$chat_message=$_GET["chat_message"];}
elseif (isset($_POST["chat_message"])) {$chat_message=$_POST["chat_message"];}
if (isset($_GET["lead_id"])) {$lead_id=$_GET["lead_id"];}
elseif (isset($_POST["lead_id"])) {$lead_id=$_POST["lead_id"];}
if (isset($_GET["group_id"])) {$group_id=$_GET["group_id"];}
elseif (isset($_POST["group_id"])) {$group_id=$_POST["group_id"];}
if (isset($_GET["user"])) {$user=$_GET["user"];}
elseif (isset($_POST["user"])) {$user=$_POST["user"];}
if (isset($_GET["user_level"])) {$user_level=$_GET["user_level"];}
elseif (isset($_POST["user_level"])) {$user_level=$_POST["user_level"];}
if (isset($_GET["keepalive"])) {$keepalive=$_GET["keepalive"];}
elseif (isset($_POST["keepalive"])) {$keepalive=$_POST["keepalive"];}
if (isset($_GET["current_message_count"])) {$current_message_count=$_GET["current_message_count"];}
elseif (isset($_POST["current_message_count"])) {$current_message_count=$_POST["current_message_count"];}
if (isset($_GET["language"])) {$language=$_GET["language"];}
elseif (isset($_POST["language"])) {$language=$_POST["language"];}
if (isset($_GET["available_agents"])) {$available_agents=$_GET["available_agents"];}
elseif (isset($_POST["available_agents"])) {$available_agents=$_POST["available_agents"];}
if (isset($_GET["show_email"])) {$show_email=$_GET["show_email"];}
elseif (isset($_POST["show_email"])) {$show_email=$_POST["show_email"];}
if(isset($chat_member_name)) {$chat_member_name = preg_replace('/[^- \.\,\_0-9a-zA-Z]/',"",$chat_member_name);}
if (!$user) {echo "No user, no using."; exit;}
if(isset($DB)) {$DB = preg_replace("/[^0-9a-zA-Z]/","",$DB);}
if(isset($lead_id)) {$lead_id = preg_replace("/[^0-9]/","",$lead_id);}
if(isset($chat_id)) {$chat_id = preg_replace('/[^- \_\.0-9a-zA-Z]/','',$chat_id);}
if(isset($chat_level)) {$chat_level = preg_replace('/[^- \_\.0-9a-zA-Z]/','',$chat_level);}
if(isset($group_id)) {$group_id = preg_replace('/[^- \_0-9a-zA-Z]/','',$group_id);}
if(isset($language)) {$language = preg_replace('/[^-\_0-9a-zA-Z]/','',$language);}
if(isset($user)) {$user = preg_replace("/\'|\"|\\\\|;/","",$user);}
if(isset($chat_member_name)) {$chat_member_name = preg_replace("/\'|\"|\\\\|;/","",$chat_member_name);}
if(isset($available_agents)) {$available_agents = preg_replace('/[^-\_0-9a-zA-Z]/','',$available_agents);}
if(isset($show_email)) {$show_email = preg_replace('/[^-\_0-9a-zA-Z]/','',$show_email);}
if(isset($chat_message)) {$chat_message = preg_replace('/\|/', '|', $chat_message);}
if(isset($action)) {$action = preg_replace('/[^-\_0-9a-zA-Z]/','',$action);}
if(isset($user_level)) {$user_level = preg_replace('/[^-\_0-9a-zA-Z]/','',$user_level);}
if(isset($keepalive)) {$keepalive = preg_replace('/[^-\_0-9a-zA-Z]/','',$keepalive);}
if(isset($current_message_count)) {$current_message_count = preg_replace('/[^-\_0-9a-zA-Z]/','',$current_message_count);}
$use_agent_colors=1;
if (file_exists('options.php'))
{require('options.php');}
#############################################
##### START SYSTEM_SETTINGS LOOKUP #####
$VUselected_language='';
$stmt = "SELECT use_non_latin,enable_languages,language_method,default_language,chat_url,allow_chats,allow_web_debug FROM system_settings;";
$rslt=mysql_to_mysqli($stmt, $link);
if ($mel > 0) {mysql_error_logging($NOW_TIME,$link,$mel,$stmt,'00XXX',$user,$server_ip,$session_name,$one_mysql_log);}
if ($DB) {echo "$stmt\n";}
$qm_conf_ct = mysqli_num_rows($rslt);
if ($qm_conf_ct > 0)
{
$row=mysqli_fetch_row($rslt);
$non_latin = $row[0];
$SSenable_languages = $row[1];
$SSlanguage_method = $row[2];
$SSdefault_language = $row[3];
$chat_url = $row[4];
$SSallow_chats = $row[5];
$SSallow_web_debug = $row[6];
}
$VUselected_language = $SSdefault_language;
if ($SSallow_web_debug < 1) {$DB=0;}
##### END SETTINGS LOOKUP #####
###########################################
if ($non_latin < 1)
{
$user = preg_replace('/[^- \+\_\.0-9a-zA-Z]/','',$user);
}
else
{
$user = preg_replace("/\'|\"|\\\\|;/","",$user);
}
if (strlen($language) > 1)
{
$stmt = "SELECT language_code,language_description FROM vicidial_languages where language_id='$language' and active='Y';";
$rslt=mysql_to_mysqli($stmt, $link);
if ($mel > 0) {mysql_error_logging($NOW_TIME,$link,$mel,$stmt,'00XXX',$user,$server_ip,$session_name,$one_mysql_log);}
if ($DB) {echo "$stmt\n";}
$lang_good_ct = mysqli_num_rows($rslt);
if ($lang_good_ct > 0)
{
$row=mysqli_fetch_row($rslt);
$language_code = $row[0];
$language_description = $row[1];
$VUselected_language = $language;
}
}
if ($SSallow_chats < 1)
{
header ("Content-type: text/html; charset=utf-8");
echo _QXZ("Error, chat disabled on this system");
exit;
}
if ($action=="send_message" && $chat_id) {
$live_stmt="SELECT status from vicidial_live_chats where chat_id='$chat_id'";
$live_rslt=mysql_to_mysqli($live_stmt, $link);
if ($user && $chat_message && $chat_id && mysqli_num_rows($live_rslt)>0) {
$live_row=mysqli_fetch_row($live_rslt);
$status=$live_row[0];
if ($status=="WAITING") {
echo _QXZ("Chat is waiting for an agent").": $chat_id";
} else {
if ($status=="LIVE") {
# Check that the customer is still in the chat.
$active_stmt="SELECT * from vicidial_chat_participants where chat_id='$chat_id' and chat_member='$user'";
$active_rslt=mysql_to_mysqli($active_stmt, $link);
if (mysqli_num_rows($active_rslt)>0) {
$ins_stmt="INSERT IGNORE INTO vicidial_chat_log(chat_id, message, poster, chat_member_name, chat_level) VALUES('$chat_id', '".mysqli_real_escape_string($link, $chat_message)."', '$user', '".mysqli_real_escape_string($link, $chat_member_name)."', '$chat_level')";
$ins_rslt=mysql_to_mysqli($ins_stmt, $link);
if (mysqli_affected_rows($link)<1) {
echo ""._QXZ("SYSTEM ERROR")."
\n";
}
}
} else {
echo ""._QXZ("SYSTEM ERROR")."
\n";
}
}
} else if (mysqli_num_rows($rslt)==0) {
echo _QXZ("Chat has been closed").": $chat_id";
}
}
if ($action=="leave_chat" && $user && $chat_id) {
if (!$chat_member_name) {$chat_member_name="Customer";}
$del_stmt2="DELETE from vicidial_chat_participants where chat_id='$chat_id' and chat_member='$user'";
$del_rslt2=mysql_to_mysqli($del_stmt2, $link);
$deleted_participants=mysqli_affected_rows($link);
if ($deleted_participants>0) {
# ERASE THE CHAT IF THE PERSON NEVER GOT PICKED UP - I DON'T REMEMBER WHY I MOVED customer_leave_chat TO leave_chat
$stmt="SELECT lead_id, status, chat_creator from vicidial_live_chats where chat_id='$chat_id'"; # and status='WAITING' and chat_creator='NONE'
$rslt=mysql_to_mysqli($stmt, $link);
if (mysqli_num_rows($rslt)>0) {
$row=mysqli_fetch_row($rslt);
$lead_id=$row[0];
$chat_status=$row[1];
$chat_creator=$row[2];
if ($chat_status=="WAITING" && $chat_creator=="NONE") {
# USE SPECIAL DROP STATUS 'CDROP' FOR DROPPED CHATS WHERE NO AGENT RESPONDED TO CHAT
$upd_stmt="UPDATE vicidial_list set status='CDROP' where lead_id='$lead_id'";
$upd_rslt=mysql_to_mysqli($upd_stmt, $link);
$ins_stmt="INSERT IGNORE INTO vicidial_chat_archive SELECT chat_id, chat_start_time, 'DROP', chat_creator, group_id, lead_id, transferring_agent, user_direct, user_direct_group_id From vicidial_live_chats where chat_id='$chat_id'";
$ins_rslt=mysql_to_mysqli($ins_stmt, $link);
$del_stmt="DELETE from vicidial_live_chats where chat_id='$chat_id'";
$del_rslt=mysql_to_mysqli($del_stmt, $link);
# ARCHIVE CHAT IN CASE CUSTOMER IS LEAVING WHILE BEING TRANSFERRED
$archive_log_stmt="insert ignore into vicidial_chat_log_archive select * from vicidial_chat_log where chat_id='$chat_id'";
$archive_log_rslt=mysql_to_mysqli($archive_log_stmt, $link);
$del_log_stmt="delete from vicidial_chat_log where chat_id='$chat_id'";
$del_log_rslt=mysql_to_mysqli($del_log_stmt, $link);
} else {
# CHAT IS LIVE/AGENT NEEDS NOTIFICATION
$ins_alert_stmt="INSERT INTO vicidial_chat_log(poster, chat_member_name, message_time, message, chat_id, chat_level) SELECT '$chat_creator', full_name, now(), '$chat_member_name has left chat', '$chat_id', '1' from vicidial_users where user='$chat_creator'";
$ins_alert_rslt=mysql_to_mysqli($ins_alert_stmt, $link);
}
}
}
}
if ($action=="update_chat_window" && $chat_id) {
$status_stmt="SELECT status, chat_creator, transferring_agent from vicidial_live_chats where chat_id='$chat_id'";
$status_rslt=mysql_to_mysqli($status_stmt, $link);
if (mysqli_num_rows($status_rslt)==0) {
# JCJ - comment out to display in Javascript function that calls this in parent page.
echo "Error|";
# Create color-coding for archived chat
$stmt="SELECT * from vicidial_chat_log_archive where chat_id='$chat_id' order by message_time asc";
$rslt=mysql_to_mysqli($stmt, $link);
$chat_members=array();
while ($row=mysqli_fetch_row($rslt)) {
if (!in_array("$row[4]", $chat_members)) {
array_push($chat_members, "$row[4]");
}
}
## DISPLAY LOGO ON SCREEN EVEN AFTER CHAT IS ENDED
$chat_color_stmt="select menu_background, frame_background, std_row1_background, std_row2_background, std_row3_background, std_row4_background, std_row5_background, web_logo from vicidial_inbound_groups vig, vicidial_screen_colors v where vig.group_id='$group_id' and vig.customer_chat_screen_colors=v.colors_id and length(frame_background)=6 and length(menu_background)=6 limit 1;";
$color_rslt=mysql_to_mysqli($chat_color_stmt, $link);
$web_logo=""; $filepath="vicidial/images";
if(mysqli_num_rows($color_rslt)>0 && $use_agent_colors>0) {
$color_row=mysqli_fetch_array($color_rslt);
$color_array=array("#000000", "#000000", "#000000", "#000000", "#000000", "#000000", "#000000");
$chat_background_array=array("#$color_row[std_row1_background]", "#$color_row[std_row2_background]", "#$color_row[std_row3_background]", "#$color_row[std_row4_background]", "#$color_row[std_row5_background]", "#$color_row[frame_background]", "#$color_row[menu_background]");
# switch ($color_row["web_logo"]) {
# case "default_new":
# $color_row["web_logo"]=".png";
# break;
# case "default_old";
# $color_row["web_logo"]=".gif";
# $filepath="vicidial";
# break;
# }
# $web_logo=$color_row["web_logo"];
} else {
# if(mysqli_num_rows($color_rslt)>0) {
# $color_row=mysqli_fetch_array($color_rslt);
# switch ($color_row["web_logo"]) {
# case "default_new":
# $color_row["web_logo"]=".png";
# break;
# case "default_old";
# $color_row["web_logo"]=".gif";
# $filepath="vicidial";
# break;
# }
# $web_logo=$color_row["web_logo"];
# }
$color_array=array("#FF0000", "#0000FF", "#009900", "#990099", "#009999", "#666600", "#999999");
$chat_background_array=array("#FFCCCC", "#CCCCFF", "#CCFFCC", "#FFCCFF", "#CCFFFF", "#CCCC99", "#CCCCCC");
}
# if (!preg_match("/\.(jpg|gif|png|bmp)$/", $web_logo)) {$web_logo.=".png";}
$chat_status="INACTIVE";
echo "$chat_status|";
# echo "
| \n"; # echo ""._QXZ("Current chat").": INACTIVE\n"; # echo " | \n"; # echo "\n";
# if (file_exists("../$filepath/vicidial_admin_web_logo$web_logo"))
# {
# echo " | \n";
# echo "
| \n";
# echo ""._QXZ("Waiting for next available agent...")." \n"; $chat_status=""._QXZ("WAITING").""; echo "$chat_status|"; $chat_count_stmt="SELECT chat_id from vicidial_live_chats vlc, vicidial_inbound_groups vig where vlc.status='WAITING' and (vlc.group_id='$group_id' or (vlc.group_id='AGENTDIRECT_CHAT')) and (transferring_agent is null or transferring_agent!='$user') and vlc.group_id=vig.group_id order by queue_priority desc, chat_id asc"; $chat_count_rslt=mysql_to_mysqli($chat_count_stmt, $link); $people_ahead_of_you=0; while($chat_count_row=mysqli_fetch_row($chat_count_rslt)) { if ($chat_count_row[0]!=$chat_id) { $people_ahead_of_you++; } else { break; } } if ($people_ahead_of_you>0) { echo ""._QXZ("There are")." $people_ahead_of_you "._QXZ("customer(s) in chat queue ahead of you").""; } else { echo ""._QXZ("You are the next customer in line").""; } # echo " | \n";
# echo "\n";
# if (file_exists("../$filepath/vicidial_admin_web_logo$web_logo")) {
# echo " | \n";
# echo "