2007-10-10 19:15:47 +04:00
|
|
|
<?php
|
|
|
|
/*
|
2013-03-07 01:22:53 +04:00
|
|
|
* Copyright 2005-2013 the original author or authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2007-10-10 19:15:47 +04:00
|
|
|
*/
|
|
|
|
|
2011-04-07 12:34:04 +04:00
|
|
|
require_once(dirname(__FILE__).'/track.php');
|
|
|
|
|
2008-05-06 01:08:57 +04:00
|
|
|
$connection_timeout = 30; // sec
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2009-03-08 23:53:16 +03:00
|
|
|
$namecookie = "WEBIM_Data";
|
2008-10-03 19:11:02 +04:00
|
|
|
$usercookie = "WEBIM_UserID";
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$state_queue = 0;
|
|
|
|
$state_waiting = 1;
|
|
|
|
$state_chatting = 2;
|
|
|
|
$state_closed = 3;
|
2008-06-05 02:51:46 +04:00
|
|
|
$state_loading = 4;
|
2009-08-08 02:49:11 +04:00
|
|
|
$state_left = 5;
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$kind_user = 1;
|
|
|
|
$kind_agent = 2;
|
|
|
|
$kind_for_agent = 3;
|
|
|
|
$kind_info = 4;
|
|
|
|
$kind_conn = 5;
|
|
|
|
$kind_events = 6;
|
2008-10-06 02:48:36 +04:00
|
|
|
$kind_avatar = 7;
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
$kind_to_string = array($kind_user => "user", $kind_agent => "agent", $kind_for_agent => "hidden",
|
|
|
|
$kind_info => "inf", $kind_conn => "conn", $kind_events => "event", $kind_avatar => "avatar");
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function get_user_id()
|
|
|
|
{
|
|
|
|
return (time() + microtime()) . rand(0, 99999999);
|
2008-10-03 19:11:02 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function next_token()
|
|
|
|
{
|
|
|
|
return rand(99999, 99999999);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function next_revision()
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
$db->query("update {chatrevision} set id=LAST_INSERT_ID(id+1)");
|
|
|
|
$val = $db->insertedId();
|
2007-10-10 19:15:47 +04:00
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
/**
|
|
|
|
* @todo Think about post_message_ and post_message diffrence
|
|
|
|
*/
|
|
|
|
function post_message_($threadid, $kind, $message, $from = null, $utime = null, $opid = null)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
$query = "insert into {chatmessage} " .
|
|
|
|
"(threadid,ikind,tmessage,tname,agentId,dtmcreated) " .
|
|
|
|
"values (?,?,?,?,?,".($utime?"FROM_UNIXTIME(?)":"CURRENT_TIMESTAMP").")";
|
|
|
|
$values = array(
|
2011-02-26 17:04:12 +03:00
|
|
|
$threadid,
|
|
|
|
$kind,
|
2012-07-13 16:56:50 +04:00
|
|
|
$message,
|
|
|
|
($from ? $from : "null"),
|
|
|
|
($opid ? $opid : 0)
|
|
|
|
);
|
|
|
|
if ($utime) {
|
|
|
|
$values[] = $utime;
|
|
|
|
}
|
|
|
|
$db->query($query, $values);
|
|
|
|
return $db->insertedId();
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function post_message($threadid, $kind, $message, $from = null, $agentid = null)
|
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
return post_message_($threadid, $kind, $message, $from, null, $agentid);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2012-02-11 01:39:46 +04:00
|
|
|
function prepare_html_message($text, $allow_formating)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2007-10-10 19:15:47 +04:00
|
|
|
$escaped_text = htmlspecialchars($text);
|
2011-04-07 12:34:04 +04:00
|
|
|
$text_w_links = preg_replace('/(https?|ftp):\/\/\S*/', '<a href="$0" target="_blank">$0</a>', $escaped_text);
|
2011-02-26 17:04:12 +03:00
|
|
|
$multiline = str_replace("\n", "<br/>", $text_w_links);
|
2012-02-11 01:39:46 +04:00
|
|
|
if (! $allow_formating) {
|
|
|
|
return $multiline;
|
|
|
|
}
|
|
|
|
$formated = preg_replace('/<(span|strong)>(.*)<\/\1>/U', '<$1>$2</$1>', $multiline);
|
|
|
|
$formated = preg_replace('/<span class="(.*)">(.*)<\/span>/U', '<span class="$1">$2</span>', $formated);
|
|
|
|
return $formated;
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function message_to_html($msg)
|
|
|
|
{
|
2012-02-11 01:39:46 +04:00
|
|
|
global $kind_to_string, $kind_user, $kind_agent, $kind_avatar;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($msg['ikind'] == $kind_avatar) return "";
|
|
|
|
$message = "<span>" . date("H:i:s", $msg['created']) . "</span> ";
|
2007-10-10 19:15:47 +04:00
|
|
|
$kind = $kind_to_string{$msg['ikind']};
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($msg['tname'])
|
|
|
|
$message .= "<span class='n$kind'>" . htmlspecialchars($msg['tname']) . "</span>: ";
|
2012-02-11 01:39:46 +04:00
|
|
|
$allow_formating = ($msg['ikind'] != $kind_user && $msg['ikind'] != $kind_agent);
|
|
|
|
$message .= "<span class='m$kind'>" . prepare_html_message($msg['tmessage'], $allow_formating) . "</span><br/>";
|
2007-10-10 19:15:47 +04:00
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function message_to_text($msg)
|
|
|
|
{
|
2008-10-06 02:48:36 +04:00
|
|
|
global $kind_user, $kind_agent, $kind_info, $kind_avatar;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($msg['ikind'] == $kind_avatar) return "";
|
|
|
|
$message_time = date("H:i:s ", $msg['created']);
|
|
|
|
if ($msg['ikind'] == $kind_user || $msg['ikind'] == $kind_agent) {
|
|
|
|
if ($msg['tname'])
|
|
|
|
return $message_time . $msg['tname'] . ": " . $msg['tmessage'] . "\n";
|
2007-10-10 19:15:47 +04:00
|
|
|
else
|
2011-02-26 17:04:12 +03:00
|
|
|
return $message_time . $msg['tmessage'] . "\n";
|
|
|
|
} else if ($msg['ikind'] == $kind_info) {
|
|
|
|
return $message_time . $msg['tmessage'] . "\n";
|
2007-10-10 19:15:47 +04:00
|
|
|
} else {
|
2011-02-26 17:04:12 +03:00
|
|
|
return $message_time . "[" . $msg['tmessage'] . "]\n";
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function get_messages($threadid, $meth, $isuser, &$lastid)
|
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
global $kind_for_agent, $kind_avatar, $webim_encoding;
|
|
|
|
$db = Database::getInstance();
|
|
|
|
|
|
|
|
$msgs = $db->query(
|
|
|
|
"select messageid,ikind,unix_timestamp(dtmcreated) as created,tname,tmessage from {chatmessage} " .
|
|
|
|
"where threadid = :threadid and messageid > :lastid " .
|
|
|
|
($isuser ? "and ikind <> {$kind_for_agent} " : "") .
|
|
|
|
"order by messageid",
|
|
|
|
array(
|
|
|
|
':threadid' => $threadid,
|
|
|
|
':lastid' => $lastid,
|
|
|
|
),
|
|
|
|
array('return_rows' => Database::RETURN_ALL_ROWS)
|
|
|
|
|
|
|
|
);
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$messages = array();
|
2008-09-30 02:35:08 +04:00
|
|
|
foreach ($msgs as $msg) {
|
|
|
|
$message = "";
|
|
|
|
if ($meth == 'xml') {
|
2008-10-06 02:48:36 +04:00
|
|
|
switch ($msg['ikind']) {
|
|
|
|
case $kind_avatar:
|
2011-02-26 17:04:12 +03:00
|
|
|
$message = "<avatar>" . myiconv($webim_encoding, "utf-8", escape_with_cdata($msg['tmessage'])) . "</avatar>";
|
2008-10-06 02:48:36 +04:00
|
|
|
break;
|
|
|
|
default:
|
2011-02-26 17:04:12 +03:00
|
|
|
$message = "<message>" . myiconv($webim_encoding, "utf-8", escape_with_cdata(message_to_html($msg))) . "</message>\n";
|
2008-10-06 02:48:36 +04:00
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
} else {
|
2008-10-06 02:48:36 +04:00
|
|
|
if ($msg['ikind'] != $kind_avatar) {
|
|
|
|
$message = (($meth == 'text') ? message_to_text($msg) : topage(message_to_html($msg)));
|
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
}
|
2008-03-05 01:22:48 +03:00
|
|
|
|
2007-10-10 19:15:47 +04:00
|
|
|
$messages[] = $message;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($msg['messageid'] > $lastid) {
|
2007-10-10 19:15:47 +04:00
|
|
|
$lastid = $msg['messageid'];
|
2007-12-03 00:32:47 +03:00
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
2008-09-30 02:35:08 +04:00
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function print_thread_messages($thread, $token, $lastid, $isuser, $format, $agentid = null)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $webim_encoding, $webimroot, $connection_timeout;
|
2008-05-11 02:35:16 +04:00
|
|
|
$threadid = $thread['threadid'];
|
2011-02-26 17:04:12 +03:00
|
|
|
$istyping = abs($thread['current'] - $thread[$isuser ? "lpagent" : "lpuser"]) < $connection_timeout
|
|
|
|
&& $thread[$isuser ? "agentTyping" : "userTyping"] == "1" ? "1" : "0";
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($format == "xml") {
|
|
|
|
$output = get_messages($threadid, "xml", $isuser, $lastid);
|
2008-03-05 01:22:48 +03:00
|
|
|
|
2007-10-10 19:15:47 +04:00
|
|
|
start_xml_output();
|
2011-02-26 17:04:12 +03:00
|
|
|
print("<thread lastid=\"$lastid\" typing=\"" . $istyping . "\" canpost=\"" . (($isuser || $agentid != null && $agentid == $thread['agentId']) ? 1 : 0) . "\">");
|
|
|
|
foreach ($output as $msg) {
|
2008-03-05 01:22:48 +03:00
|
|
|
print $msg;
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
print("</thread>");
|
2011-02-26 17:04:12 +03:00
|
|
|
} else if ($format == "html") {
|
|
|
|
$output = get_messages($threadid, "html", $isuser, $lastid);
|
2008-03-05 01:22:48 +03:00
|
|
|
|
2007-10-10 19:15:47 +04:00
|
|
|
start_html_output();
|
2011-02-26 17:04:12 +03:00
|
|
|
$url = "$webimroot/thread.php?act=refresh&thread=$threadid&token=$token&html=on&user=" . ($isuser ? "true" : "false");
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2009-06-05 19:49:51 +04:00
|
|
|
print(
|
2011-02-26 17:04:12 +03:00
|
|
|
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" .
|
|
|
|
"<html>\n<head>\n" .
|
|
|
|
"<link href=\"$webimroot/styles/default/chat.css\" rel=\"stylesheet\" type=\"text/css\">\n" .
|
2012-07-16 18:26:53 +04:00
|
|
|
"<meta http-equiv=\"Refresh\" content=\"" . Settings::get('updatefrequency_oldchat') . "; URL=$url&sn=11\">\n" .
|
2011-02-26 17:04:12 +03:00
|
|
|
"<meta http-equiv=\"Pragma\" content=\"no-cache\">\n" .
|
|
|
|
"<title>chat</title>\n" .
|
|
|
|
"</head>\n" .
|
|
|
|
"<body bgcolor='#FFFFFF' text='#000000' link='#C28400' vlink='#C28400' alink='#C28400' onload=\"if( location.hash != '#aend' ){location.hash='#aend';}\">" .
|
|
|
|
"<table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td valign='top' class='message'>");
|
|
|
|
|
|
|
|
foreach ($output as $msg) {
|
2007-10-10 19:15:47 +04:00
|
|
|
print $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
print(
|
2011-02-26 17:04:12 +03:00
|
|
|
"</td></tr></table><a name='aend'></a>" .
|
|
|
|
"</body></html>");
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function get_user_name($username, $addr, $id)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
return str_replace(
|
|
|
|
"{addr}", $addr,
|
|
|
|
str_replace(
|
|
|
|
"{id}", $id,
|
|
|
|
str_replace("{name}", $username, Settings::get('usernamepattern'))
|
|
|
|
)
|
|
|
|
);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function is_ajax_browser($browserid, $ver, $useragent)
|
|
|
|
{
|
|
|
|
if ($browserid == "opera")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver >= 8.02;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "safari")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver >= 125;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "msie")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver >= 5.5 && !strstr($useragent, "powerpc");
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "netscape")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver >= 7.1;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "mozilla")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver >= 1.4;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "firefox")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver >= 1.0;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "chrome")
|
2008-12-18 18:09:44 +03:00
|
|
|
return true;
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function is_old_browser($browserid, $ver)
|
|
|
|
{
|
|
|
|
if ($browserid == "opera")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver < 7.0;
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($browserid == "msie")
|
2007-10-10 19:15:47 +04:00
|
|
|
return $ver < 5.0;
|
2008-09-30 02:35:08 +04:00
|
|
|
return false;
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
$knownAgents = array("opera", "msie", "chrome", "safari", "firefox", "netscape", "mozilla");
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function get_remote_level($useragent)
|
|
|
|
{
|
2007-10-10 19:15:47 +04:00
|
|
|
global $knownAgents;
|
|
|
|
$useragent = strtolower($useragent);
|
2011-02-26 17:04:12 +03:00
|
|
|
foreach ($knownAgents as $agent) {
|
|
|
|
if (strstr($useragent, $agent)) {
|
|
|
|
if (preg_match("/" . $agent . "[\\s\/]?(\\d+(\\.\\d+)?)/", $useragent, $matches)) {
|
2007-10-10 19:15:47 +04:00
|
|
|
$ver = $matches[1];
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if (is_ajax_browser($agent, $ver, $useragent))
|
2007-10-10 19:15:47 +04:00
|
|
|
return "ajaxed";
|
2011-02-26 17:04:12 +03:00
|
|
|
else if (is_old_browser($agent, $ver))
|
2007-10-10 19:15:47 +04:00
|
|
|
return "old";
|
|
|
|
|
|
|
|
return "simple";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "simple";
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function is_agent_opera95()
|
|
|
|
{
|
2008-09-30 02:35:08 +04:00
|
|
|
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
|
2011-02-26 17:04:12 +03:00
|
|
|
if (strstr($useragent, "opera")) {
|
|
|
|
if (preg_match("/opera[\\s\/]?(\\d+(\\.\\d+)?)/", $useragent, $matches)) {
|
2008-09-30 02:35:08 +04:00
|
|
|
$ver = $matches[1];
|
|
|
|
|
|
|
|
if ($ver >= "9.5")
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function is_mac_opera()
|
|
|
|
{
|
2009-04-24 19:52:17 +04:00
|
|
|
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
|
2011-02-26 17:04:12 +03:00
|
|
|
return strstr($useragent, "opera") && strstr($useragent, "mac");
|
2009-04-24 19:52:17 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function needsFramesrc()
|
|
|
|
{
|
2008-09-30 02:35:08 +04:00
|
|
|
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
|
2011-02-26 17:04:12 +03:00
|
|
|
return strstr($useragent, "safari/");
|
2008-09-30 02:35:08 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 21:26:18 +04:00
|
|
|
function setup_logo($group = NULL)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $page;
|
2012-03-13 21:26:18 +04:00
|
|
|
$toplevelgroup = (!$group)?array():get_top_level_group($group);
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['ct.company.name'] = topage(empty($toplevelgroup['vctitle'])?Settings::get('title'):$toplevelgroup['vctitle']);
|
|
|
|
$page['ct.company.chatLogoURL'] = topage(empty($toplevelgroup['vclogo'])?Settings::get('logo'):$toplevelgroup['vclogo']);
|
|
|
|
$page['webimHost'] = topage(empty($toplevelgroup['vchosturl'])?Settings::get('hosturl'):$toplevelgroup['vchosturl']);
|
2008-11-08 02:24:45 +03:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function setup_leavemessage($name, $email, $message, $groupid, $groupname, $info, $referrer, $canshowcaptcha)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $page;
|
2009-08-11 19:06:46 +04:00
|
|
|
$page['formname'] = topage($name);
|
|
|
|
$page['formemail'] = topage($email);
|
|
|
|
$page['formmessage'] = $message ? topage($message) : "";
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['showcaptcha'] = Settings::get("enablecaptcha") == "1" && $canshowcaptcha ? "1" : "";
|
2009-08-11 19:06:46 +04:00
|
|
|
$page['formgroupid'] = $groupid;
|
|
|
|
$page['formgroupname'] = $groupname;
|
2011-02-16 04:17:30 +03:00
|
|
|
$page['forminfo'] = topage($info);
|
2009-08-11 19:06:46 +04:00
|
|
|
$page['referrer'] = urlencode(topage($referrer));
|
2012-03-09 23:40:07 +04:00
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('enablegroups') == '1') {
|
2012-03-09 23:40:07 +04:00
|
|
|
$groups = setup_groups_select($groupid, false);
|
|
|
|
if ($groups) {
|
|
|
|
$page['groups'] = $groups['select'];
|
|
|
|
$page['group.descriptions'] = json_encode($groups['descriptions']);
|
|
|
|
$page['default.department.description'] = $groups['defaultdescription'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-11 19:06:46 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function setup_survey($name, $email, $groupid, $info, $referrer)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $page;
|
2011-02-26 17:04:12 +03:00
|
|
|
|
2009-04-05 03:51:16 +04:00
|
|
|
$page['formname'] = topage($name);
|
|
|
|
$page['formemail'] = topage($email);
|
|
|
|
$page['formgroupid'] = $groupid;
|
|
|
|
$page['forminfo'] = topage($info);
|
|
|
|
$page['referrer'] = urlencode(topage($referrer));
|
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('enablegroups') == '1' && Settings::get('surveyaskgroup') == '1') {
|
2012-03-09 23:40:07 +04:00
|
|
|
$groups = setup_groups_select($groupid, true);
|
|
|
|
if ($groups) {
|
|
|
|
$page['groups'] = $groups['select'];
|
|
|
|
$page['group.descriptions'] = json_encode($groups['descriptions']);
|
|
|
|
$page['default.department.description'] = $groups['defaultdescription'];
|
2009-04-05 03:51:16 +04:00
|
|
|
}
|
|
|
|
}
|
2011-02-26 17:04:12 +03:00
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['showemail'] = Settings::get("surveyaskmail") == "1" ? "1" : "";
|
|
|
|
$page['showmessage'] = Settings::get("surveyaskmessage") == "1" ? "1" : "";
|
|
|
|
$page['showname'] = Settings::get('usercanchangename') == "1" ? "1" : "";
|
2009-04-05 03:51:16 +04:00
|
|
|
}
|
|
|
|
|
2012-03-09 23:40:07 +04:00
|
|
|
function setup_groups_select($groupid, $markoffline)
|
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$showgroups = ($groupid == '')?true:group_has_children($groupid);
|
2012-03-09 23:40:07 +04:00
|
|
|
if (!$showgroups) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$allgroups = get_groups(false);
|
2012-03-09 23:40:07 +04:00
|
|
|
|
|
|
|
if (empty($allgroups)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$val = "";
|
|
|
|
$selectedgroupid = $groupid;
|
|
|
|
$groupdescriptions = array();
|
|
|
|
foreach ($allgroups as $k) {
|
|
|
|
$groupname = $k['vclocalname'];
|
|
|
|
if ($k['inumofagents'] == 0 || ($groupid && $k['parent'] != $groupid && $k['groupid'] != $groupid )) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-16 18:26:53 +04:00
|
|
|
if ($k['ilastseen'] !== NULL && $k['ilastseen'] < Settings::get('online_timeout')) {
|
2012-03-09 23:40:07 +04:00
|
|
|
if (!$selectedgroupid) {
|
|
|
|
$selectedgroupid = $k['groupid']; // select first online group
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$groupname .= $markoffline?" (offline)":"";
|
|
|
|
}
|
|
|
|
$isselected = $k['groupid'] == $selectedgroupid;
|
|
|
|
if ($isselected) {
|
|
|
|
$defaultdescription = $k['vclocaldescription'];
|
|
|
|
}
|
|
|
|
$val .= "<option value=\"" . $k['groupid'] . "\"" . ($isselected ? " selected=\"selected\"" : "") . ">$groupname</option>";
|
|
|
|
$groupdescriptions[] = $k['vclocaldescription'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'select' => $val,
|
|
|
|
'descriptions' => $groupdescriptions,
|
|
|
|
'defaultdescription' => $defaultdescription
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function setup_chatview_for_user($thread, $level)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $page, $webimroot;
|
2008-09-30 02:35:08 +04:00
|
|
|
$page = array();
|
2012-07-13 16:56:50 +04:00
|
|
|
if (! empty($thread['groupid'])) {
|
2012-03-13 21:26:18 +04:00
|
|
|
$group = group_by_id($thread['groupid']);
|
|
|
|
$group = get_top_level_group($group);
|
|
|
|
} else {
|
|
|
|
$group = array();
|
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
$page['agent'] = false;
|
|
|
|
$page['user'] = true;
|
|
|
|
$page['canpost'] = true;
|
|
|
|
$nameisset = getstring("chat.default.username") != $thread['userName'];
|
|
|
|
$page['displ1'] = $nameisset ? "none" : "inline";
|
|
|
|
$page['displ2'] = $nameisset ? "inline" : "none";
|
|
|
|
$page['level'] = $level;
|
|
|
|
$page['ct.chatThreadId'] = $thread['threadid'];
|
|
|
|
$page['ct.token'] = $thread['ltoken'];
|
2009-04-05 03:51:16 +04:00
|
|
|
$page['ct.user.name'] = htmlspecialchars(topage($thread['userName']));
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['canChangeName'] = Settings::get('usercanchangename') == "1";
|
|
|
|
$page['chat.title'] = topage(empty($group['vcchattitle'])?Settings::get('chattitle'):$group['vcchattitle']);
|
2011-12-26 22:39:27 +04:00
|
|
|
$page['chat.close.confirmation'] = getlocal('chat.close.confirmation');
|
2008-09-30 02:35:08 +04:00
|
|
|
|
2012-03-13 21:26:18 +04:00
|
|
|
setup_logo($group);
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('sendmessagekey') == 'enter') {
|
2009-04-24 19:09:46 +04:00
|
|
|
$page['send_shortcut'] = "Enter";
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['ignorectrl'] = 1;
|
2009-04-24 19:09:46 +04:00
|
|
|
} else {
|
2009-04-24 19:52:17 +04:00
|
|
|
$page['send_shortcut'] = is_mac_opera() ? "⌘-Enter" : "Ctrl-Enter";
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['ignorectrl'] = 0;
|
2009-04-24 19:09:46 +04:00
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
$params = "thread=" . $thread['threadid'] . "&token=" . $thread['ltoken'];
|
|
|
|
$page['mailLink'] = "$webimroot/client.php?" . $params . "&level=$level&act=mailthread";
|
2009-03-08 23:53:16 +03:00
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('enablessl') == "1" && !is_secure_request()) {
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['sslLink'] = get_app_location(true, true) . "/client.php?" . $params . "&level=$level";
|
2009-03-08 23:53:16 +03:00
|
|
|
}
|
|
|
|
|
2008-09-30 02:35:08 +04:00
|
|
|
$page['isOpera95'] = is_agent_opera95();
|
|
|
|
$page['neediframesrc'] = needsFramesrc();
|
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['frequency'] = Settings::get('updatefrequency_chat');
|
2008-09-30 02:35:08 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function setup_chatview_for_operator($thread, $operator)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $page, $webimroot, $company_logo_link, $webim_encoding, $company_name;
|
2008-09-30 02:35:08 +04:00
|
|
|
$page = array();
|
2012-03-13 21:26:18 +04:00
|
|
|
if (! is_null($thread['groupid'])) {
|
|
|
|
$group = group_by_id($thread['groupid']);
|
|
|
|
$group = get_top_level_group($group);
|
|
|
|
} else {
|
|
|
|
$group = array();
|
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
$page['agent'] = true;
|
|
|
|
$page['user'] = false;
|
2008-10-02 13:43:58 +04:00
|
|
|
$page['canpost'] = $thread['agentId'] == $operator['operatorid'];
|
2008-09-30 02:35:08 +04:00
|
|
|
$page['ct.chatThreadId'] = $thread['threadid'];
|
|
|
|
$page['ct.token'] = $thread['ltoken'];
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['ct.user.name'] = htmlspecialchars(topage(get_user_name($thread['userName'], $thread['remote'], $thread['userid'])));
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['chat.title'] = topage(empty($group['vcchattitle'])?Settings::get('chattitle'):$group['vcchattitle']);
|
2012-01-10 20:35:31 +04:00
|
|
|
$page['chat.close.confirmation'] = getlocal('chat.close.confirmation');
|
2008-09-30 02:35:08 +04:00
|
|
|
|
2012-03-13 21:26:18 +04:00
|
|
|
setup_logo($group);
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('sendmessagekey') == 'enter') {
|
2009-04-24 19:09:46 +04:00
|
|
|
$page['send_shortcut'] = "Enter";
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['ignorectrl'] = 1;
|
2009-04-24 19:09:46 +04:00
|
|
|
} else {
|
2009-04-24 19:52:17 +04:00
|
|
|
$page['send_shortcut'] = is_mac_opera() ? "⌘-Enter" : "Ctrl-Enter";
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['ignorectrl'] = 0;
|
2009-04-24 19:09:46 +04:00
|
|
|
}
|
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('enablessl') == "1" && !is_secure_request()) {
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['sslLink'] = get_app_location(true, true) . "/operator/agent.php?thread=" . $thread['threadid'] . "&token=" . $thread['ltoken'];
|
2009-03-08 23:53:16 +03:00
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
$page['isOpera95'] = is_agent_opera95();
|
|
|
|
$page['neediframesrc'] = needsFramesrc();
|
2011-02-26 17:04:12 +03:00
|
|
|
$page['historyParams'] = array("userid" => "" . $thread['userid']);
|
|
|
|
$page['historyParamsLink'] = add_params($webimroot . "/operator/userhistory.php", $page['historyParams']);
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('enabletracking')) {
|
2012-07-13 16:56:50 +04:00
|
|
|
$visitor = track_get_visitor_by_threadid($thread['threadid']);
|
2011-04-07 12:34:04 +04:00
|
|
|
$page['trackedParams'] = array("visitor" => "" . $visitor['visitorid']);
|
|
|
|
$page['trackedParamsLink'] = add_params($webimroot . "/operator/tracked.php", $page['trackedParams']);
|
|
|
|
}
|
2008-10-11 04:06:15 +04:00
|
|
|
$predefinedres = "";
|
2012-01-26 20:00:30 +04:00
|
|
|
$canned_messages = load_canned_messages($thread['locale'], 0);
|
|
|
|
if ($thread['groupid']) {
|
|
|
|
$canned_messages = array_merge(
|
|
|
|
load_canned_messages($thread['locale'], $thread['groupid']),
|
|
|
|
$canned_messages
|
|
|
|
);
|
|
|
|
};
|
2011-02-26 17:04:12 +03:00
|
|
|
foreach ($canned_messages as $answer) {
|
2012-01-29 01:52:44 +04:00
|
|
|
$predefinedres .= "<option>" . htmlspecialchars(topage($answer['vctitle']?$answer['vctitle']:cutstring($answer['vcvalue'], 97, '...'))) . "</option>";
|
|
|
|
$fullAnswers[] = myiconv($webim_encoding, getoutputenc(), $answer['vcvalue']);
|
2008-10-11 04:06:15 +04:00
|
|
|
}
|
2008-10-12 04:16:44 +04:00
|
|
|
$page['predefinedAnswers'] = $predefinedres;
|
2012-01-29 01:52:44 +04:00
|
|
|
$page['fullPredefinedAnswers'] = json_encode($fullAnswers);
|
2011-02-26 17:04:12 +03:00
|
|
|
$params = "thread=" . $thread['threadid'] . "&token=" . $thread['ltoken'];
|
|
|
|
$page['redirectLink'] = "$webimroot/operator/agent.php?" . $params . "&act=redirect";
|
2008-10-02 13:43:58 +04:00
|
|
|
|
2008-09-30 02:35:08 +04:00
|
|
|
$page['namePostfix'] = "";
|
2012-07-16 18:26:53 +04:00
|
|
|
$page['frequency'] = Settings::get('updatefrequency_chat');
|
2008-09-30 02:35:08 +04:00
|
|
|
}
|
|
|
|
|
2012-07-24 20:00:58 +04:00
|
|
|
/**
|
|
|
|
* Pings the chat thread. Updates 'lastpinguser' (or 'lastpingagent') to current timestamp.
|
|
|
|
* Sends system messages when operator or user was seen more than $connection_timeout seconds ago
|
|
|
|
*
|
|
|
|
* @global int $kind_for_agent
|
|
|
|
* @global int $state_queue
|
|
|
|
* @global int $state_loading
|
|
|
|
* @global int $state_chatting
|
|
|
|
* @global int $state_waiting
|
|
|
|
* @global int $kind_conn
|
|
|
|
* @global int $connection_timeout
|
|
|
|
* @param array $thread Thread's array
|
|
|
|
* @param boolean $isuser true for user and false for operator
|
|
|
|
* @param boolean $istyping true if user (or agent) is typing
|
|
|
|
*/
|
2011-02-26 17:04:12 +03:00
|
|
|
function ping_thread($thread, $isuser, $istyping)
|
|
|
|
{
|
2008-06-05 02:51:46 +04:00
|
|
|
global $kind_for_agent, $state_queue, $state_loading, $state_chatting, $state_waiting, $kind_conn, $connection_timeout;
|
2012-07-13 16:56:50 +04:00
|
|
|
|
2012-07-24 20:00:58 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
|
2008-05-11 02:35:16 +04:00
|
|
|
$params = array(($isuser ? "lastpinguser" : "lastpingagent") => "CURRENT_TIMESTAMP",
|
2011-02-26 17:04:12 +03:00
|
|
|
($isuser ? "userTyping" : "agentTyping") => ($istyping ? "1" : "0"));
|
2008-09-30 02:35:08 +04:00
|
|
|
|
2008-05-11 02:35:16 +04:00
|
|
|
$lastping = $thread[$isuser ? "lpagent" : "lpuser"];
|
|
|
|
$current = $thread['current'];
|
2008-06-05 02:51:46 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($thread['istate'] == $state_loading && $isuser) {
|
2008-06-05 02:51:46 +04:00
|
|
|
$params['istate'] = $state_queue;
|
2012-07-13 16:56:50 +04:00
|
|
|
commit_thread($thread['threadid'], $params);
|
2008-06-05 02:51:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($lastping > 0 && abs($current - $lastping) > $connection_timeout) {
|
2007-10-10 19:15:47 +04:00
|
|
|
$params[$isuser ? "lastpingagent" : "lastpinguser"] = "0";
|
2011-02-26 17:04:12 +03:00
|
|
|
if (!$isuser) {
|
2007-10-10 19:15:47 +04:00
|
|
|
$message_to_post = getstring_("chat.status.user.dead", $thread['locale']);
|
2012-07-13 16:56:50 +04:00
|
|
|
post_message_($thread['threadid'], $kind_for_agent, $message_to_post, null, $lastping + $connection_timeout);
|
2011-02-26 17:04:12 +03:00
|
|
|
} else if ($thread['istate'] == $state_chatting) {
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$message_to_post = getstring_("chat.status.operator.dead", $thread['locale']);
|
2012-07-13 16:56:50 +04:00
|
|
|
post_message_($thread['threadid'], $kind_conn, $message_to_post, null, $lastping + $connection_timeout);
|
2007-10-10 19:15:47 +04:00
|
|
|
$params['istate'] = $state_waiting;
|
2008-10-02 13:43:58 +04:00
|
|
|
$params['nextagent'] = 0;
|
2012-07-13 16:56:50 +04:00
|
|
|
commit_thread($thread['threadid'], $params);
|
2007-10-10 19:15:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 20:00:58 +04:00
|
|
|
$clause = "";
|
|
|
|
$values = array();
|
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
if (strlen($clause) > 0) {
|
|
|
|
$clause .= ", ";
|
|
|
|
}
|
|
|
|
if (($k == 'lastpinguser' || $k == 'lastpingagent') && $v == 'CURRENT_TIMESTAMP') {
|
|
|
|
$clause .= $k . " = CURRENT_TIMESTAMP";
|
|
|
|
}else{
|
|
|
|
$clause .= $k . "=?";
|
|
|
|
$values[] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$values[] = $thread['threadid'];
|
|
|
|
|
|
|
|
$db->query(
|
|
|
|
"update {chatthread} set {$clause} where threadid = ?",
|
|
|
|
$values
|
|
|
|
);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function commit_thread($threadid, $params)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
|
2012-07-24 20:00:58 +04:00
|
|
|
$timestamp_allowed_for = array(
|
|
|
|
'dtmcreated',
|
|
|
|
'dtmchatstarted',
|
|
|
|
'dtmmodified',
|
|
|
|
'lastpinguser',
|
|
|
|
'lastpingagent'
|
|
|
|
);
|
2012-07-13 16:56:50 +04:00
|
|
|
$query = "update {chatthread} t " .
|
|
|
|
"set lrevision = ?, dtmmodified = CURRENT_TIMESTAMP";
|
|
|
|
$values = array(next_revision());
|
2011-02-26 17:04:12 +03:00
|
|
|
foreach ($params as $k => $v) {
|
2012-07-24 20:00:58 +04:00
|
|
|
if (in_array($k, $timestamp_allowed_for) && strcasecmp($v,'CURRENT_TIMESTAMP')) {
|
|
|
|
$query .= ", " . $k . " = CURRENT_TIMESTAMP";
|
|
|
|
} else {
|
|
|
|
$query .= ", " . $k . "=?";
|
|
|
|
$values[] = $v;
|
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
2012-07-13 16:56:50 +04:00
|
|
|
$query .= " where threadid = ?";
|
|
|
|
$values[] = $threadid;
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$db->query($query, $values);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function rename_user($thread, $newname)
|
|
|
|
{
|
2007-10-10 19:15:47 +04:00
|
|
|
global $kind_events;
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
commit_thread($thread['threadid'], array('userName' => $newname));
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($thread['userName'] != $newname) {
|
|
|
|
post_message_($thread['threadid'], $kind_events,
|
2012-07-13 16:56:50 +04:00
|
|
|
getstring2_("chat.status.user.changedname", array($thread['userName'], $newname), $thread['locale']));
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function close_thread($thread, $isuser)
|
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
global $state_closed, $kind_events;
|
2008-09-30 02:35:08 +04:00
|
|
|
|
2012-07-24 20:00:58 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
list($message_count) = $db->query(
|
|
|
|
"SELECT COUNT(*) FROM {chatmessage} WHERE {chatmessage}.threadid = ? AND ikind = 1",
|
|
|
|
array($thread['threadid']),
|
|
|
|
array(
|
|
|
|
'return_rows' => Database::RETURN_ONE_ROW,
|
|
|
|
'fetch_type' => Database::FETCH_NUM
|
|
|
|
)
|
|
|
|
);
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($thread['istate'] != $state_closed) {
|
2012-07-13 16:56:50 +04:00
|
|
|
commit_thread(
|
|
|
|
$thread['threadid'],
|
|
|
|
array(
|
|
|
|
'istate' => $state_closed,
|
2012-07-24 20:00:58 +04:00
|
|
|
'messageCount' => $message_count
|
2012-07-13 16:56:50 +04:00
|
|
|
)
|
|
|
|
);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
$message = $isuser ? getstring2_("chat.status.user.left", array($thread['userName']), $thread['locale'])
|
|
|
|
: getstring2_("chat.status.operator.left", array($thread['agentName']), $thread['locale']);
|
2012-07-13 16:56:50 +04:00
|
|
|
post_message_($thread['threadid'], $kind_events, $message);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function close_old_threads()
|
2011-11-26 01:00:22 +04:00
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $state_closed, $state_left, $state_chatting;
|
|
|
|
if (Settings::get('thread_lifetime') == 0) {
|
2011-11-26 01:00:22 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
|
|
|
|
$query = "update {chatthread} set lrevision = :next_revision, " .
|
|
|
|
"dtmmodified = CURRENT_TIMESTAMP, istate = :state_closed " .
|
|
|
|
"where istate <> :state_closed and istate <> :state_left " .
|
2012-07-24 20:11:52 +04:00
|
|
|
"and ((lastpingagent <> 0 and lastpinguser <> 0 and " .
|
2012-07-13 16:56:50 +04:00
|
|
|
"(ABS(UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lastpinguser)) > ".
|
|
|
|
":thread_lifetime and " .
|
|
|
|
"ABS(UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lastpingagent)) > ".
|
2012-07-24 20:11:52 +04:00
|
|
|
":thread_lifetime)) or " .
|
|
|
|
"lastpingagent = 0 and lastpinguser <> 0 and " .
|
|
|
|
"ABS(UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lastpinguser)) > ".
|
2012-07-13 16:56:50 +04:00
|
|
|
":thread_lifetime)";
|
|
|
|
|
|
|
|
$db->query(
|
|
|
|
$query,
|
|
|
|
array(
|
|
|
|
':next_revision' => next_revision(),
|
|
|
|
':state_closed' => $state_closed,
|
|
|
|
':state_left' => $state_left,
|
2012-07-16 18:26:53 +04:00
|
|
|
':thread_lifetime' => Settings::get('thread_lifetime')
|
2012-07-13 16:56:50 +04:00
|
|
|
)
|
|
|
|
);
|
2011-11-26 01:00:22 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function thread_by_id($id)
|
2011-11-26 01:00:22 +04:00
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
return $db->query(
|
|
|
|
"select threadid,userName,agentName,agentId,lrevision,istate,ltoken,userTyping, " .
|
|
|
|
"agentTyping,unix_timestamp(dtmmodified) as modified, " .
|
|
|
|
"unix_timestamp(dtmcreated) as created, " .
|
|
|
|
"unix_timestamp(dtmchatstarted) as chatstarted,remote,referer,locale," .
|
|
|
|
"unix_timestamp(lastpinguser) as lpuser,unix_timestamp(lastpingagent) as lpagent," .
|
|
|
|
"unix_timestamp(CURRENT_TIMESTAMP) as current,nextagent,shownmessageid,userid, " .
|
|
|
|
"userAgent,groupid from {chatthread} where threadid = ?",
|
|
|
|
array($id),
|
|
|
|
array('return_rows' => Database::RETURN_ONE_ROW)
|
|
|
|
);
|
2011-11-26 01:00:22 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function ban_for_addr($addr)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
return $db->query(
|
|
|
|
"select banid,comment from {chatban} " .
|
|
|
|
"where unix_timestamp(dtmtill) > unix_timestamp(CURRENT_TIMESTAMP) AND address = ?",
|
|
|
|
array($addr),
|
|
|
|
array('return_rows' => Database::RETURN_ONE_ROW)
|
|
|
|
);
|
2008-05-11 02:35:16 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function create_thread($groupid, $username, $remoteHost, $referer, $lang, $userid, $userbrowser, $initialState)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
2008-10-04 03:35:17 +04:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$query = "insert into {chatthread} (userName,userid,ltoken,remote,referer, " .
|
|
|
|
"lrevision,locale,userAgent,dtmcreated,dtmmodified,istate" .
|
|
|
|
($groupid ? ",groupid" : "") . ") values " .
|
|
|
|
"(?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,?" .
|
|
|
|
($groupid ? ", ?" : "") . ")";
|
2008-05-11 02:35:16 +04:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$values = array(
|
|
|
|
$username,
|
|
|
|
$userid,
|
2011-02-26 17:04:12 +03:00
|
|
|
next_token(),
|
2012-07-13 16:56:50 +04:00
|
|
|
$remoteHost,
|
|
|
|
$referer,
|
|
|
|
next_revision(),
|
|
|
|
$lang,
|
|
|
|
$userbrowser,
|
|
|
|
$initialState
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($groupid) {
|
|
|
|
$values[] = $groupid;
|
|
|
|
}
|
2011-02-26 17:04:12 +03:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$db->query($query, $values);
|
|
|
|
$id = $db->insertedId();
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$newthread = thread_by_id($id);
|
2007-10-10 19:15:47 +04:00
|
|
|
return $newthread;
|
|
|
|
}
|
|
|
|
|
2011-11-19 18:23:04 +04:00
|
|
|
function do_take_thread($threadid, $operatorId, $operatorName, $chatstart = false)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2007-10-10 19:15:47 +04:00
|
|
|
global $state_chatting;
|
2011-11-19 18:23:04 +04:00
|
|
|
$params = array("istate" => $state_chatting,
|
|
|
|
"nextagent" => 0,
|
|
|
|
"agentId" => $operatorId,
|
2012-07-13 16:56:50 +04:00
|
|
|
"agentName" => $operatorName);
|
2011-11-19 18:23:04 +04:00
|
|
|
if ($chatstart){
|
|
|
|
$params['dtmchatstarted'] = "CURRENT_TIMESTAMP";
|
|
|
|
}
|
2012-07-13 16:56:50 +04:00
|
|
|
commit_thread($threadid, $params);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function reopen_thread($threadid)
|
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $state_queue, $state_loading, $state_waiting, $state_chatting, $state_closed, $state_left, $kind_events;
|
2011-11-26 01:00:22 +04:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$thread = thread_by_id($threadid);
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if (!$thread)
|
2007-10-10 19:15:47 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('thread_lifetime') != 0 && abs($thread['lpuser'] - time()) > Settings::get('thread_lifetime') && abs($thread['lpagent'] - time()) > Settings::get('thread_lifetime')) {
|
2011-11-26 01:00:22 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($thread['istate'] == $state_closed || $thread['istate'] == $state_left)
|
2007-10-10 19:15:47 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($thread['istate'] != $state_chatting && $thread['istate'] != $state_queue && $thread['istate'] != $state_loading) {
|
2012-07-13 16:56:50 +04:00
|
|
|
commit_thread(
|
|
|
|
$threadid,
|
|
|
|
array("istate" => $state_waiting, "nextagent" => 0)
|
|
|
|
);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
post_message_($thread['threadid'], $kind_events, getstring_("chat.status.user.reopenedthread", $thread['locale']));
|
2007-10-10 19:15:47 +04:00
|
|
|
return $thread;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function take_thread($thread, $operator)
|
|
|
|
{
|
2008-10-06 02:48:36 +04:00
|
|
|
global $state_queue, $state_loading, $state_waiting, $state_chatting, $kind_events, $kind_avatar, $home_locale;
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$state = $thread['istate'];
|
|
|
|
$threadid = $thread['threadid'];
|
|
|
|
$message_to_post = "";
|
2011-11-19 18:23:04 +04:00
|
|
|
$chatstart = $thread['chatstarted'] == 0;
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$operatorName = ($thread['locale'] == $home_locale) ? $operator['vclocalename'] : $operator['vccommonname'];
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($state == $state_queue || $state == $state_waiting || $state == $state_loading) {
|
2011-11-19 18:23:04 +04:00
|
|
|
do_take_thread($threadid, $operator['operatorid'], $operatorName, $chatstart);
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($state == $state_waiting) {
|
|
|
|
if ($operatorName != $thread['agentName']) {
|
2008-05-18 02:14:29 +04:00
|
|
|
$message_to_post = getstring2_("chat.status.operator.changed", array($operatorName, $thread['agentName']), $thread['locale']);
|
|
|
|
} else {
|
|
|
|
$message_to_post = getstring2_("chat.status.operator.returned", array($operatorName), $thread['locale']);
|
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
} else {
|
|
|
|
$message_to_post = getstring2_("chat.status.operator.joined", array($operatorName), $thread['locale']);
|
|
|
|
}
|
2011-02-26 17:04:12 +03:00
|
|
|
} else if ($state == $state_chatting) {
|
|
|
|
if ($operator['operatorid'] != $thread['agentId']) {
|
2011-11-19 18:23:04 +04:00
|
|
|
do_take_thread($threadid, $operator['operatorid'], $operatorName, $chatstart);
|
2007-10-10 19:15:47 +04:00
|
|
|
$message_to_post = getstring2_("chat.status.operator.changed", array($operatorName, $thread['agentName']), $thread['locale']);
|
|
|
|
}
|
|
|
|
} else {
|
2012-02-06 23:58:36 +04:00
|
|
|
return false;
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($message_to_post) {
|
|
|
|
post_message($threadid, $kind_events, $message_to_post);
|
|
|
|
post_message($threadid, $kind_avatar, $operator['vcavatar'] ? $operator['vcavatar'] : "");
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
2012-02-06 23:58:36 +04:00
|
|
|
return true;
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function check_for_reassign($thread, $operator)
|
|
|
|
{
|
2008-10-06 04:55:43 +04:00
|
|
|
global $state_waiting, $home_locale, $kind_events, $kind_avatar;
|
2007-10-10 19:15:47 +04:00
|
|
|
$operatorName = ($thread['locale'] == $home_locale) ? $operator['vclocalename'] : $operator['vccommonname'];
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($thread['istate'] == $state_waiting &&
|
|
|
|
($thread['nextagent'] == $operator['operatorid']
|
|
|
|
|| $thread['agentId'] == $operator['operatorid'])) {
|
2007-10-10 19:15:47 +04:00
|
|
|
do_take_thread($thread['threadid'], $operator['operatorid'], $operatorName);
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($operatorName != $thread['agentName']) {
|
2008-05-18 02:14:29 +04:00
|
|
|
$message_to_post = getstring2_("chat.status.operator.changed", array($operatorName, $thread['agentName']), $thread['locale']);
|
|
|
|
} else {
|
|
|
|
$message_to_post = getstring2_("chat.status.operator.returned", array($operatorName), $thread['locale']);
|
|
|
|
}
|
2008-09-30 02:35:08 +04:00
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
post_message($thread['threadid'], $kind_events, $message_to_post);
|
|
|
|
post_message($thread['threadid'], $kind_avatar, $operator['vcavatar'] ? $operator['vcavatar'] : "");
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function check_connections_from_remote($remote)
|
2011-02-26 17:04:12 +03:00
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $state_closed, $state_left;
|
|
|
|
if (Settings::get('max_connections_from_one_host') == 0) {
|
2009-04-05 18:20:34 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-13 16:56:50 +04:00
|
|
|
|
|
|
|
$db = Database::getInstance();
|
|
|
|
$result = $db->query(
|
|
|
|
"select count(*) as opened from {chatthread} " .
|
|
|
|
"where remote = ? AND istate <> ? AND istate <> ?",
|
|
|
|
array($remote, $state_closed, $state_left),
|
|
|
|
array('return_rows' => Database::RETURN_ONE_ROW)
|
|
|
|
);
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($result && isset($result['opened'])) {
|
2012-07-16 18:26:53 +04:00
|
|
|
return $result['opened'] < Settings::get('max_connections_from_one_host');
|
2009-04-05 18:20:34 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function visitor_from_request()
|
|
|
|
{
|
2009-03-08 23:53:16 +03:00
|
|
|
global $namecookie, $webim_encoding, $usercookie;
|
|
|
|
$defaultName = getstring("chat.default.username");
|
|
|
|
$userName = $defaultName;
|
2011-02-26 17:04:12 +03:00
|
|
|
if (isset($_COOKIE[$namecookie])) {
|
|
|
|
$data = base64_decode(strtr($_COOKIE[$namecookie], '-_,', '+/='));
|
|
|
|
if (strlen($data) > 0) {
|
|
|
|
$userName = myiconv("utf-8", $webim_encoding, $data);
|
2008-05-12 13:23:16 +04:00
|
|
|
}
|
2009-03-08 23:53:16 +03:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
if ($userName == $defaultName) {
|
2009-03-08 23:53:16 +03:00
|
|
|
$userName = getgetparam('name', $userName);
|
2008-05-12 13:23:16 +04:00
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2008-10-03 19:11:02 +04:00
|
|
|
if (isset($_COOKIE[$usercookie])) {
|
|
|
|
$userId = $_COOKIE[$usercookie];
|
|
|
|
} else {
|
|
|
|
$userId = get_user_id();
|
2011-02-26 17:04:12 +03:00
|
|
|
setcookie($usercookie, $userId, time() + 60 * 60 * 24 * 365);
|
2008-10-03 19:11:02 +04:00
|
|
|
}
|
2011-02-26 17:04:12 +03:00
|
|
|
return array('id' => $userId, 'name' => $userName);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:04:12 +03:00
|
|
|
function get_remote_host()
|
|
|
|
{
|
2009-08-08 02:49:11 +04:00
|
|
|
$extAddr = $_SERVER['REMOTE_ADDR'];
|
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
|
2011-02-26 17:04:12 +03:00
|
|
|
$_SERVER['HTTP_X_FORWARDED_FOR'] != $_SERVER['REMOTE_ADDR']) {
|
|
|
|
$extAddr = $_SERVER['REMOTE_ADDR'] . ' (' . $_SERVER['HTTP_X_FORWARDED_FOR'] . ')';
|
2009-08-08 02:49:11 +04:00
|
|
|
}
|
|
|
|
return isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : $extAddr;
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:03:50 +04:00
|
|
|
?>
|