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
|
|
|
*/
|
|
|
|
|
2012-09-13 17:35:25 +04:00
|
|
|
require_once('../libs/init.php');
|
2008-06-05 01:36:54 +04:00
|
|
|
require_once('../libs/chat.php');
|
2009-02-08 03:52:57 +03:00
|
|
|
require_once('../libs/userinfo.php');
|
2008-06-05 01:36:54 +04:00
|
|
|
require_once('../libs/operator.php');
|
2011-02-26 16:43:30 +03:00
|
|
|
require_once('../libs/groups.php');
|
2011-04-07 12:34:04 +04:00
|
|
|
require_once('../libs/track.php');
|
2012-09-28 18:46:42 +04:00
|
|
|
require_once('../libs/classes/thread.php');
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$operator = get_logged_in();
|
2011-02-26 17:06:19 +03:00
|
|
|
if (!$operator) {
|
2007-10-10 19:15:47 +04:00
|
|
|
start_xml_output();
|
2011-02-26 17:06:19 +03:00
|
|
|
echo "<error><descr>" . myiconv($webim_encoding, "utf-8", escape_with_cdata(getstring("agent.not_logged_in"))) . "</descr></error>";
|
2007-10-10 19:15:47 +04:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$threadstate_to_string = array(
|
2012-09-28 18:46:42 +04:00
|
|
|
Thread::STATE_QUEUE => "wait",
|
|
|
|
Thread::STATE_WAITING => "prio",
|
|
|
|
Thread::STATE_CHATTING => "chat",
|
|
|
|
Thread::STATE_CLOSED => "closed",
|
|
|
|
Thread::STATE_LOADING => "wait",
|
|
|
|
Thread::STATE_LEFT => "closed"
|
2007-10-10 19:15:47 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$threadstate_key = array(
|
2012-09-28 18:46:42 +04:00
|
|
|
Thread::STATE_QUEUE => "chat.thread.state_wait",
|
|
|
|
Thread::STATE_WAITING => "chat.thread.state_wait_for_another_agent",
|
|
|
|
Thread::STATE_CHATTING => "chat.thread.state_chatting_with_agent",
|
|
|
|
Thread::STATE_CLOSED => "chat.thread.state_closed",
|
|
|
|
Thread::STATE_LOADING => "chat.thread.state_loading"
|
2007-10-10 19:15:47 +04:00
|
|
|
);
|
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
function thread_to_xml($thread_info)
|
2011-02-26 17:06:19 +03:00
|
|
|
{
|
2012-09-28 18:46:42 +04:00
|
|
|
global $threadstate_to_string, $threadstate_key,
|
2012-07-16 18:26:53 +04:00
|
|
|
$webim_encoding, $operator, $can_viewthreads, $can_takeover;
|
2012-10-01 15:39:58 +04:00
|
|
|
|
|
|
|
$thread = $thread_info['thread'];
|
|
|
|
|
|
|
|
$state = $threadstate_to_string[$thread->state];
|
|
|
|
$result = "<thread id=\"" . $thread->id . "\" stateid=\"$state\"";
|
2011-02-26 17:06:19 +03:00
|
|
|
if ($state == "closed")
|
|
|
|
return $result . "/>";
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
$state = getstring($threadstate_key[$thread->state]);
|
|
|
|
$nextagent = $thread->nextAgent != 0 ? operator_by_id($thread->nextAgent) : null;
|
2008-10-02 13:43:58 +04:00
|
|
|
$threadoperator = $nextagent ? get_operator_name($nextagent)
|
2012-10-01 15:39:58 +04:00
|
|
|
: ($thread->agentName ? $thread->agentName : "-");
|
2011-02-26 17:06:19 +03:00
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
if ($threadoperator == "-" && $thread_info['groupname']) {
|
|
|
|
$threadoperator = "- " . $thread_info['groupname'] . " -";
|
2009-03-25 03:33:14 +03:00
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
if (!($thread->state == Thread::STATE_CHATTING && $thread->agentId != $operator['operatorid'] && !is_capable($can_takeover, $operator))) {
|
2008-12-09 02:47:40 +03:00
|
|
|
$result .= " canopen=\"true\"";
|
|
|
|
}
|
2012-10-01 15:39:58 +04:00
|
|
|
if ($thread->agentId != $operator['operatorid'] && $thread->nextAgent != $operator['operatorid']
|
2011-02-26 17:06:19 +03:00
|
|
|
&& is_capable($can_viewthreads, $operator)) {
|
2008-10-02 13:43:58 +04:00
|
|
|
$result .= " canview=\"true\"";
|
|
|
|
}
|
2012-07-16 18:26:53 +04:00
|
|
|
if (Settings::get('enableban') == "1") {
|
2008-11-21 18:32:01 +03:00
|
|
|
$result .= " canban=\"true\"";
|
|
|
|
}
|
2007-12-03 00:32:47 +03:00
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
$banForThread = Settings::get('enableban') == "1" ? ban_for_addr($thread->remote) : false;
|
2011-02-26 17:06:19 +03:00
|
|
|
if ($banForThread) {
|
|
|
|
$result .= " ban=\"blocked\" banid=\"" . $banForThread['banid'] . "\"";
|
2008-10-04 03:35:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
$result .= " state=\"$state\" typing=\"" . $thread->userTyping . "\">";
|
2011-02-26 17:06:19 +03:00
|
|
|
$result .= "<name>";
|
|
|
|
if ($banForThread) {
|
2009-03-16 04:34:33 +03:00
|
|
|
$result .= htmlspecialchars(getstring('chat.client.spam.prefix'));
|
2009-03-14 02:46:25 +03:00
|
|
|
}
|
2012-10-01 15:39:58 +04:00
|
|
|
$result .= htmlspecialchars(
|
|
|
|
htmlspecialchars(get_user_name($thread->userName, $thread->remote, $thread->userId))
|
|
|
|
) . "</name>";
|
|
|
|
$result .= "<addr>" . htmlspecialchars(get_user_addr($thread->remote)) . "</addr>";
|
2011-02-26 17:06:19 +03:00
|
|
|
$result .= "<agent>" . htmlspecialchars(htmlspecialchars($threadoperator)) . "</agent>";
|
2012-10-01 15:39:58 +04:00
|
|
|
$result .= "<time>" . $thread->created . "000</time>";
|
|
|
|
$result .= "<modified>" . $thread->modified . "000</modified>";
|
2011-02-26 17:06:19 +03:00
|
|
|
|
|
|
|
if ($banForThread) {
|
|
|
|
$result .= "<reason>" . $banForThread['comment'] . "</reason>";
|
2008-10-04 03:35:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-01 15:39:58 +04:00
|
|
|
$userAgent = get_useragent_version($thread->userAgent);
|
2011-02-26 17:06:19 +03:00
|
|
|
$result .= "<useragent>" . $userAgent . "</useragent>";
|
2012-10-01 15:39:58 +04:00
|
|
|
if ($thread->shownMessageId != 0) {
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
|
|
|
$line = $db->query(
|
|
|
|
"select tmessage from {chatmessage} where messageid = ?",
|
2012-10-01 15:39:58 +04:00
|
|
|
array($thread->shownMessageId),
|
2012-07-13 16:56:50 +04:00
|
|
|
array('return_rows' => Database::RETURN_ONE_ROW)
|
|
|
|
);
|
2011-02-26 17:06:19 +03:00
|
|
|
if ($line) {
|
2008-10-03 19:11:02 +04:00
|
|
|
$message = preg_replace("/[\r\n\t]+/", " ", $line["tmessage"]);
|
2011-02-26 17:06:19 +03:00
|
|
|
$result .= "<message>" . htmlspecialchars(htmlspecialchars($message)) . "</message>";
|
2008-10-03 19:11:02 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
$result .= "</thread>";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:06:19 +03:00
|
|
|
function print_pending_threads($groupids, $since)
|
|
|
|
{
|
2012-09-28 18:46:42 +04:00
|
|
|
global $webim_encoding;
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
2007-10-10 19:15:47 +04:00
|
|
|
|
|
|
|
$revision = $since;
|
2012-10-01 15:39:58 +04:00
|
|
|
$query = "select {chatthread}.*, " .
|
|
|
|
"(select vclocalname from {chatgroup} where {chatgroup}.groupid = {chatthread}.groupid) as groupname " .
|
2012-07-13 16:56:50 +04:00
|
|
|
"from {chatthread} where lrevision > :since " .
|
|
|
|
($since <= 0
|
2012-09-28 18:46:42 +04:00
|
|
|
? "AND istate <> " . Thread::STATE_CLOSED . " AND istate <> " . Thread::STATE_LEFT . " "
|
2012-07-13 16:56:50 +04:00
|
|
|
: "") .
|
2012-07-16 18:26:53 +04:00
|
|
|
(Settings::get('enablegroups') == '1'
|
2012-07-13 16:56:50 +04:00
|
|
|
? "AND (groupid is NULL" . ($groupids
|
|
|
|
? " OR groupid IN ($groupids) OR groupid IN (SELECT parent FROM {chatgroup} WHERE groupid IN ($groupids)) "
|
|
|
|
: "") .
|
|
|
|
") "
|
|
|
|
: "") .
|
|
|
|
"ORDER BY threadid";
|
|
|
|
$rows = $db->query(
|
|
|
|
$query,
|
2012-07-24 20:00:58 +04:00
|
|
|
array(':since' => $since),
|
2012-07-13 16:56:50 +04:00
|
|
|
array('return_rows' => Database::RETURN_ALL_ROWS)
|
|
|
|
);
|
|
|
|
|
|
|
|
$output = array();
|
2008-09-30 02:46:27 +04:00
|
|
|
foreach ($rows as $row) {
|
2012-10-01 15:39:58 +04:00
|
|
|
$thread = Thread::createFromDbInfo($row);
|
|
|
|
$thread_info = array(
|
|
|
|
'thread' => $thread,
|
|
|
|
'groupName' => $row['groupname']
|
|
|
|
);
|
|
|
|
$thread_as_xml = thread_to_xml($thread_info);
|
|
|
|
$output[] = $thread_as_xml;
|
|
|
|
if ($thread->lastRevision > $revision) {
|
|
|
|
$revision = $thread->lastRevision;
|
|
|
|
}
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
|
2011-02-26 17:06:19 +03:00
|
|
|
echo "<threads revision=\"$revision\" time=\"" . time() . "000\">";
|
|
|
|
foreach ($output as $thr) {
|
|
|
|
print myiconv($webim_encoding, "utf-8", $thr);
|
2007-10-10 19:15:47 +04:00
|
|
|
}
|
|
|
|
echo "</threads>";
|
|
|
|
}
|
|
|
|
|
2012-02-25 23:40:05 +04:00
|
|
|
function print_operators($operator)
|
2011-02-26 17:06:19 +03:00
|
|
|
{
|
2012-07-16 18:26:53 +04:00
|
|
|
global $webim_encoding;
|
2011-02-21 03:02:39 +03:00
|
|
|
echo "<operators>";
|
2012-03-16 02:07:09 +04:00
|
|
|
|
|
|
|
$list_options = in_isolation($operator)?array('isolated_operator_id' => $operator['operatorid']):array();
|
|
|
|
$operators = get_operators_list($list_options);
|
2011-02-26 15:09:46 +03:00
|
|
|
|
2011-02-26 17:06:19 +03:00
|
|
|
foreach ($operators as $operator) {
|
2011-02-21 03:02:39 +03:00
|
|
|
if (!operator_is_online($operator))
|
|
|
|
continue;
|
|
|
|
|
2011-04-21 02:44:04 +04:00
|
|
|
$name = myiconv($webim_encoding, "utf-8", htmlspecialchars(htmlspecialchars($operator['vclocalename'])));
|
2011-02-21 03:02:39 +03:00
|
|
|
$away = operator_is_away($operator) ? " away=\"1\"" : "";
|
|
|
|
|
|
|
|
echo "<operator name=\"$name\"$away/>";
|
|
|
|
}
|
2011-02-26 17:06:19 +03:00
|
|
|
echo "</operators>";
|
2011-02-21 03:02:39 +03:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
function visitor_to_xml($visitor)
|
2011-04-07 12:34:04 +04:00
|
|
|
{
|
|
|
|
$result = "<visitor id=\"" . $visitor['visitorid'] . "\">";
|
|
|
|
|
|
|
|
// $result .= "<userid>" . htmlspecialchars($visitor['userid']) . "</userid>";
|
|
|
|
$result .= "<username>" . htmlspecialchars($visitor['username']) . "</username>";
|
|
|
|
|
2012-09-05 16:46:15 +04:00
|
|
|
$result .= "<time>" . $visitor['firsttime'] . "000</time>";
|
|
|
|
$result .= "<modified>" . $visitor['lasttime'] . "000</modified>";
|
2011-04-07 12:34:04 +04:00
|
|
|
// $result .= "<entry>" . htmlspecialchars($visitor['entry']) . "</entry>";
|
|
|
|
|
|
|
|
// $result .= "<path>";
|
|
|
|
// $path = track_retrieve_path($visitor);
|
|
|
|
// ksort($path);
|
|
|
|
// foreach ($path as $k => $v) {
|
|
|
|
// $result .= "<url visited=\"" . $k . "000\">" . htmlspecialchars($v) . "</url>";
|
|
|
|
// }
|
|
|
|
// $result .= "</path>";
|
|
|
|
|
|
|
|
$details = track_retrieve_details($visitor);
|
|
|
|
$userAgent = get_useragent_version($details['user_agent']);
|
|
|
|
$result .= "<useragent>" . $userAgent . "</useragent>";
|
|
|
|
$result .= "<addr>" . htmlspecialchars(get_user_addr($details['remote_host'])) . "</addr>";
|
|
|
|
|
|
|
|
$result .= "<invitations>" . $visitor['invitations'] . "</invitations>";
|
|
|
|
$result .= "<chats>" . $visitor['chats'] . "</chats>";
|
|
|
|
|
|
|
|
$result .= "<invitation>";
|
|
|
|
if ($visitor['invited']) {
|
2012-09-05 16:46:15 +04:00
|
|
|
$result .= "<invitationtime>" . $visitor['invitationtime'] . "000</invitationtime>";
|
2012-07-13 16:56:50 +04:00
|
|
|
$operator = get_operator_name(operator_by_id($visitor['invitedby']));
|
2011-04-07 12:34:04 +04:00
|
|
|
$result .= "<operator>" . htmlspecialchars(htmlspecialchars($operator)) . "</operator>";
|
|
|
|
}
|
|
|
|
$result .= "</invitation>";
|
|
|
|
|
|
|
|
$result .= "</visitor>";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function print_visitors()
|
|
|
|
{
|
2012-09-28 18:46:42 +04:00
|
|
|
global $webim_encoding;
|
2011-04-07 12:34:04 +04:00
|
|
|
|
2012-07-13 16:56:50 +04:00
|
|
|
$db = Database::getInstance();
|
2011-04-07 12:34:04 +04:00
|
|
|
|
2012-02-07 22:10:40 +04:00
|
|
|
// Remove old visitors
|
2012-07-13 16:56:50 +04:00
|
|
|
$db->query(
|
|
|
|
"DELETE FROM {chatsitevisitor} " .
|
2012-09-05 16:46:15 +04:00
|
|
|
"WHERE (:now - lasttime) > :lifetime ".
|
2012-07-13 16:56:50 +04:00
|
|
|
"AND (threadid IS NULL OR " .
|
|
|
|
"(SELECT count(*) FROM {chatthread} WHERE threadid = {chatsitevisitor}.threadid " .
|
2012-09-28 18:46:42 +04:00
|
|
|
"AND istate <> " . Thread::STATE_CLOSED . " AND istate <> " . Thread::STATE_LEFT . ") = 0)",
|
2012-09-05 16:46:15 +04:00
|
|
|
array(
|
|
|
|
':lifetime' => Settings::get('tracking_lifetime'),
|
|
|
|
':now' => time()
|
|
|
|
)
|
2012-07-13 16:56:50 +04:00
|
|
|
);
|
2011-04-07 12:34:04 +04:00
|
|
|
|
|
|
|
// Remove old invitations
|
2012-07-13 16:56:50 +04:00
|
|
|
$db->query(
|
|
|
|
"UPDATE {chatsitevisitor} SET invited = 0, invitationtime = NULL, invitedby = NULL".
|
2012-09-05 16:46:15 +04:00
|
|
|
" WHERE threadid IS NULL AND (:now - invitationtime) > :lifetime",
|
|
|
|
array(
|
|
|
|
':lifetime' => Settings::get('invitation_lifetime'),
|
|
|
|
':now' => time()
|
|
|
|
)
|
2012-07-13 16:56:50 +04:00
|
|
|
);
|
2011-04-07 12:34:04 +04:00
|
|
|
|
|
|
|
// Remove associations of visitors with closed threads
|
2012-07-13 16:56:50 +04:00
|
|
|
$db->query(
|
|
|
|
"UPDATE {chatsitevisitor} SET threadid = NULL WHERE threadid IS NOT NULL AND" .
|
|
|
|
" (SELECT count(*) FROM {chatthread} WHERE threadid = {chatsitevisitor}.threadid" .
|
2012-09-28 18:46:42 +04:00
|
|
|
" AND istate <> " . Thread::STATE_CLOSED . " AND istate <> " . Thread::STATE_LEFT . ") = 0"
|
2012-07-13 16:56:50 +04:00
|
|
|
);
|
2011-04-07 12:34:04 +04:00
|
|
|
|
2012-02-07 22:10:40 +04:00
|
|
|
// Remove old visitors' tracks
|
2012-07-13 16:56:50 +04:00
|
|
|
$db->query(
|
2012-09-05 16:46:15 +04:00
|
|
|
"DELETE FROM {visitedpage} WHERE (:now - visittime) > :lifetime " .
|
2012-07-13 16:56:50 +04:00
|
|
|
" AND visitorid NOT IN (SELECT visitorid FROM {chatsitevisitor})",
|
2012-09-05 16:46:15 +04:00
|
|
|
array(
|
|
|
|
':lifetime' => Settings::get('tracking_lifetime'),
|
|
|
|
':now' => time()
|
|
|
|
)
|
2012-07-13 16:56:50 +04:00
|
|
|
);
|
2011-04-07 12:34:04 +04:00
|
|
|
|
2012-09-05 16:46:15 +04:00
|
|
|
$query = "SELECT visitorid, userid, username, firsttime, lasttime, " .
|
|
|
|
"entry, details, invited, invitationtime, invitedby, invitations, chats " .
|
2012-07-13 16:56:50 +04:00
|
|
|
"FROM {chatsitevisitor} " .
|
2011-04-07 12:34:04 +04:00
|
|
|
"WHERE threadid IS NULL " .
|
|
|
|
"ORDER BY invited, lasttime DESC, invitations";
|
2012-07-16 18:26:53 +04:00
|
|
|
$query .= (Settings::get('visitors_limit') == '0') ? "" : " LIMIT " . Settings::get('visitors_limit');
|
2012-07-13 16:56:50 +04:00
|
|
|
|
|
|
|
$rows = $db->query($query, NULL, array('return_rows' => Database::RETURN_ALL_ROWS));
|
|
|
|
|
|
|
|
$output = array();
|
2011-04-07 12:34:04 +04:00
|
|
|
foreach ($rows as $row) {
|
2012-07-13 16:56:50 +04:00
|
|
|
$visitor = visitor_to_xml($row);
|
2011-04-07 12:34:04 +04:00
|
|
|
$output[] = $visitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "<visitors>";
|
|
|
|
foreach ($output as $thr) {
|
|
|
|
print myiconv($webim_encoding, "utf-8", $thr);
|
|
|
|
}
|
|
|
|
echo "</visitors>";
|
|
|
|
}
|
|
|
|
|
2011-02-26 17:06:19 +03:00
|
|
|
$since = verifyparam("since", "/^\d{1,9}$/", 0);
|
|
|
|
$status = verifyparam("status", "/^\d{1,2}$/", 0);
|
|
|
|
$showonline = verifyparam("showonline", "/^1$/", 0);
|
2011-04-07 12:34:04 +04:00
|
|
|
$showvisitors = verifyparam("showvisitors", "/^1$/", 0);
|
2007-10-10 19:15:47 +04:00
|
|
|
|
2011-04-07 02:31:22 +04:00
|
|
|
if (!isset($_SESSION["${mysqlprefix}operatorgroups"])) {
|
2012-07-13 16:56:50 +04:00
|
|
|
$_SESSION["${mysqlprefix}operatorgroups"] = get_operator_groupslist($operator['operatorid']);
|
2011-02-26 16:43:30 +03:00
|
|
|
}
|
2012-09-28 18:46:42 +04:00
|
|
|
Thread::closeOldThreads();
|
2011-02-26 16:43:30 +03:00
|
|
|
$groupids = $_SESSION["${mysqlprefix}operatorgroups"];
|
2011-02-21 03:02:39 +03:00
|
|
|
|
|
|
|
start_xml_output();
|
|
|
|
echo '<update>';
|
2011-02-26 17:06:19 +03:00
|
|
|
if ($showonline) {
|
2012-02-25 23:40:05 +04:00
|
|
|
print_operators($operator);
|
2011-02-21 03:02:39 +03:00
|
|
|
}
|
2011-02-26 17:06:19 +03:00
|
|
|
print_pending_threads($groupids, $since);
|
2011-04-07 12:34:04 +04:00
|
|
|
if ($showvisitors) {
|
|
|
|
print_visitors();
|
|
|
|
}
|
2011-02-21 03:02:39 +03:00
|
|
|
echo '</update>';
|
2009-07-24 11:37:58 +04:00
|
|
|
notify_operator_alive($operator['operatorid'], $status);
|
2007-10-10 19:15:47 +04:00
|
|
|
exit;
|
|
|
|
|
2013-03-13 01:03:50 +04:00
|
|
|
?>
|