i18n/src/messenger/webim/operator/update.php

301 lines
10 KiB
PHP
Raw Normal View History

<?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.
*/
require_once('../libs/common.php');
require_once('../libs/chat.php');
require_once('../libs/userinfo.php');
require_once('../libs/operator.php');
require_once('../libs/groups.php');
2011-04-07 12:34:04 +04:00
require_once('../libs/track.php');
$operator = get_logged_in();
2011-02-26 17:06:19 +03:00
if (!$operator) {
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>";
exit;
}
$threadstate_to_string = array(
$state_queue => "wait",
$state_waiting => "prio",
$state_chatting => "chat",
$state_closed => "closed",
$state_loading => "wait",
$state_left => "closed"
);
$threadstate_key = array(
$state_queue => "chat.thread.state_wait",
$state_waiting => "chat.thread.state_wait_for_another_agent",
$state_chatting => "chat.thread.state_chatting_with_agent",
$state_closed => "chat.thread.state_closed",
$state_loading => "chat.thread.state_loading"
);
function thread_to_xml($thread)
2011-02-26 17:06:19 +03:00
{
global $state_chatting, $threadstate_to_string, $threadstate_key,
$webim_encoding, $operator, $can_viewthreads, $can_takeover;
$state = $threadstate_to_string[$thread['istate']];
2011-02-26 17:06:19 +03:00
$result = "<thread id=\"" . $thread['threadid'] . "\" stateid=\"$state\"";
if ($state == "closed")
return $result . "/>";
$state = getstring($threadstate_key[$thread['istate']]);
$nextagent = $thread['nextagent'] != 0 ? operator_by_id($thread['nextagent']) : null;
$threadoperator = $nextagent ? get_operator_name($nextagent)
2011-02-26 17:06:19 +03:00
: ($thread['agentName'] ? $thread['agentName'] : "-");
if ($threadoperator == "-" && $thread['groupname']) {
$threadoperator = "- " . $thread['groupname'] . " -";
}
2011-02-26 17:06:19 +03:00
if (!($thread['istate'] == $state_chatting && $thread['agentId'] != $operator['operatorid'] && !is_capable($can_takeover, $operator))) {
$result .= " canopen=\"true\"";
}
if ($thread['agentId'] != $operator['operatorid'] && $thread['nextagent'] != $operator['operatorid']
2011-02-26 17:06:19 +03:00
&& is_capable($can_viewthreads, $operator)) {
$result .= " canview=\"true\"";
}
if (Settings::get('enableban') == "1") {
$result .= " canban=\"true\"";
}
$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'] . "\"";
}
2011-02-26 17:06:19 +03:00
$result .= " state=\"$state\" typing=\"" . $thread['userTyping'] . "\">";
$result .= "<name>";
if ($banForThread) {
$result .= htmlspecialchars(getstring('chat.client.spam.prefix'));
}
2011-02-26 17:06:19 +03:00
$result .= htmlspecialchars(htmlspecialchars(get_user_name($thread['userName'], $thread['remote'], $thread['userid']))) . "</name>";
$result .= "<addr>" . htmlspecialchars(get_user_addr($thread['remote'])) . "</addr>";
$result .= "<agent>" . htmlspecialchars(htmlspecialchars($threadoperator)) . "</agent>";
$result .= "<time>" . $thread['unix_timestamp(dtmcreated)'] . "000</time>";
$result .= "<modified>" . $thread['unix_timestamp(dtmmodified)'] . "000</modified>";
if ($banForThread) {
$result .= "<reason>" . $banForThread['comment'] . "</reason>";
}
$userAgent = get_useragent_version($thread['userAgent']);
2011-02-26 17:06:19 +03:00
$result .= "<useragent>" . $userAgent . "</useragent>";
if ($thread["shownmessageid"] != 0) {
$db = Database::getInstance();
$line = $db->query(
"select tmessage from {chatmessage} where messageid = ?",
array($thread["shownmessageid"]),
array('return_rows' => Database::RETURN_ONE_ROW)
);
2011-02-26 17:06:19 +03:00
if ($line) {
$message = preg_replace("/[\r\n\t]+/", " ", $line["tmessage"]);
2011-02-26 17:06:19 +03:00
$result .= "<message>" . htmlspecialchars(htmlspecialchars($message)) . "</message>";
}
}
$result .= "</thread>";
return $result;
}
2011-02-26 17:06:19 +03:00
function print_pending_threads($groupids, $since)
{
global $webim_encoding, $state_closed, $state_left;
$db = Database::getInstance();
$revision = $since;
2011-02-26 17:06:19 +03:00
$query = "select threadid, userName, agentName, unix_timestamp(dtmcreated), userTyping, " .
"unix_timestamp(dtmmodified), lrevision, istate, remote, nextagent, agentId, " .
"userid, shownmessageid, userAgent, (select vclocalname from {chatgroup} where {chatgroup}.groupid = {chatthread}.groupid) as groupname " .
"from {chatthread} where lrevision > :since " .
($since <= 0
2012-07-24 17:07:22 +04:00
? "AND istate <> {$state_closed} AND istate <> {$state_left} "
: "") .
(Settings::get('enablegroups') == '1'
? "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,
array(
2012-07-24 17:07:22 +04:00
':since' => $since
),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
$output = array();
foreach ($rows as $row) {
$thread = thread_to_xml($row);
$output[] = $thread;
2011-02-26 17:06:19 +03:00
if ($row['lrevision'] > $revision)
$revision = $row['lrevision'];
}
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);
}
echo "</threads>";
}
2012-02-25 23:40:05 +04:00
function print_operators($operator)
2011-02-26 17:06:19 +03:00
{
global $webim_encoding;
echo "<operators>";
$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) {
if (!operator_is_online($operator))
continue;
2011-04-21 02:44:04 +04:00
$name = myiconv($webim_encoding, "utf-8", htmlspecialchars(htmlspecialchars($operator['vclocalename'])));
$away = operator_is_away($operator) ? " away=\"1\"" : "";
echo "<operator name=\"$name\"$away/>";
}
2011-02-26 17:06:19 +03:00
echo "</operators>";
}
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>";
$result .= "<time>" . $visitor['unix_timestamp(firsttime)'] . "000</time>";
$result .= "<modified>" . $visitor['unix_timestamp(lasttime)'] . "000</modified>";
// $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']) {
$result .= "<invitationtime>" . $visitor['unix_timestamp(invitationtime)'] . "000</invitationtime>";
$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()
{
global $webim_encoding, $state_closed, $state_left;
2011-04-07 12:34:04 +04:00
$db = Database::getInstance();
2011-04-07 12:34:04 +04:00
// Remove old visitors
$db->query(
"DELETE FROM {chatsitevisitor} " .
"WHERE (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lasttime)) > ? ".
"AND (threadid IS NULL OR " .
"(SELECT count(*) FROM {chatthread} WHERE threadid = {chatsitevisitor}.threadid " .
"AND istate <> {$state_closed} AND istate <> {$state_left}) = 0)",
array(Settings::get('tracking_lifetime'))
);
2011-04-07 12:34:04 +04:00
// Remove old invitations
$db->query(
"UPDATE {chatsitevisitor} SET invited = 0, invitationtime = NULL, invitedby = NULL".
" WHERE threadid IS NULL AND (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(invitationtime)) > ?",
array(Settings::get('invitation_lifetime'))
);
2011-04-07 12:34:04 +04:00
// Remove associations of visitors with closed threads
$db->query(
"UPDATE {chatsitevisitor} SET threadid = NULL WHERE threadid IS NOT NULL AND" .
" (SELECT count(*) FROM {chatthread} WHERE threadid = {chatsitevisitor}.threadid" .
" AND istate <> {$state_closed} AND istate <> {$state_left}) = 0"
);
2011-04-07 12:34:04 +04:00
// Remove old visitors' tracks
$db->query(
"DELETE FROM {visitedpage} WHERE (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(visittime)) > ? " .
" AND visitorid NOT IN (SELECT visitorid FROM {chatsitevisitor})",
array(Settings::get('tracking_lifetime'))
);
2011-04-07 12:34:04 +04:00
$query = "SELECT visitorid, userid, username, unix_timestamp(firsttime), unix_timestamp(lasttime), " .
2012-01-24 16:42:15 +04:00
"entry, details, invited, unix_timestamp(invitationtime), invitedby, invitations, chats " .
"FROM {chatsitevisitor} " .
2011-04-07 12:34:04 +04:00
"WHERE threadid IS NULL " .
"ORDER BY invited, lasttime DESC, invitations";
$query .= (Settings::get('visitors_limit') == '0') ? "" : " LIMIT " . Settings::get('visitors_limit');
$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) {
$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);
if (!isset($_SESSION["${mysqlprefix}operatorgroups"])) {
$_SESSION["${mysqlprefix}operatorgroups"] = get_operator_groupslist($operator['operatorid']);
}
close_old_threads();
$groupids = $_SESSION["${mysqlprefix}operatorgroups"];
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-26 17:06:19 +03:00
print_pending_threads($groupids, $since);
2011-04-07 12:34:04 +04:00
if ($showvisitors) {
print_visitors();
}
echo '</update>';
notify_operator_alive($operator['operatorid'], $status);
exit;
2013-03-13 01:03:50 +04:00
?>