2007-10-10 19:15:47 +04:00
< ? php
/*
2009-06-04 02:44:32 +04:00
* This file is part of Mibew Messenger project .
2009-08-04 20:30:39 +04:00
*
2011-02-16 03:22:22 +03:00
* Copyright ( c ) 2005 - 2011 Mibew Messenger Community
2009-08-04 19:03:27 +04:00
* All rights reserved . The contents of this file are subject to the terms of
* the Eclipse Public License v1 . 0 which accompanies this distribution , and
* is available at http :// www . eclipse . org / legal / epl - v10 . html
2009-08-04 20:30:39 +04:00
*
2009-08-04 17:38:37 +04:00
* Alternatively , the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later ( the " GPL " ), in which case
* the provisions of the GPL are applicable instead of those above . If you wish
* to allow use of your version of this file only under the terms of the GPL , and
* not to allow others to use your version of this file under the terms of the
* EPL , indicate your decision by deleting the provisions above and replace them
* with the notice and other provisions required by the GPL .
2009-08-04 20:30:39 +04:00
*
2007-10-10 19:15:47 +04:00
* Contributors :
* Evgeny Gryaznov - initial API and implementation
*/
2008-06-05 01:36:54 +04:00
require_once ( '../libs/common.php' );
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' );
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 (
$state_queue => " wait " ,
$state_waiting => " prio " ,
$state_chatting => " chat " ,
2008-06-05 02:51:46 +04:00
$state_closed => " closed " ,
2009-08-08 02:49:11 +04:00
$state_loading => " wait " ,
$state_left => " closed "
2007-10-10 19:15:47 +04:00
);
$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 " ,
2008-06-05 02:51:46 +04:00
$state_closed => " chat.thread.state_closed " ,
$state_loading => " chat.thread.state_loading "
2007-10-10 19:15:47 +04:00
);
2011-02-26 17:06:19 +03:00
function thread_to_xml ( $thread , $link )
{
2008-12-09 02:47:40 +03:00
global $state_chatting , $threadstate_to_string , $threadstate_key ,
2011-02-26 17:06:19 +03:00
$webim_encoding , $operator , $settings ,
$can_viewthreads , $can_takeover , $mysqlprefix ;
2007-10-10 19:15:47 +04:00
$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 . " /> " ;
2007-10-10 19:15:47 +04:00
2008-05-12 13:23:16 +04:00
$state = getstring ( $threadstate_key [ $thread [ 'istate' ]]);
2011-02-26 17:06:19 +03:00
$nextagent = $thread [ 'nextagent' ] != 0 ? operator_by_id_ ( $thread [ 'nextagent' ], $link ) : null ;
2008-10-02 13:43:58 +04:00
$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' ] . " - " ;
2009-03-25 03:33:14 +03:00
}
2007-10-10 19:15:47 +04:00
2011-02-26 17:06:19 +03:00
if ( ! ( $thread [ 'istate' ] == $state_chatting && $thread [ 'agentId' ] != $operator [ 'operatorid' ] && ! is_capable ( $can_takeover , $operator ))) {
2008-12-09 02:47:40 +03:00
$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 )) {
2008-10-02 13:43:58 +04:00
$result .= " canview= \" true \" " ;
}
2008-11-21 18:32:01 +03:00
if ( $settings [ 'enableban' ] == " 1 " ) {
$result .= " canban= \" true \" " ;
}
2007-12-03 00:32:47 +03:00
2011-02-26 17:06:19 +03:00
$banForThread = $settings [ 'enableban' ] == " 1 " ? ban_for_addr_ ( $thread [ 'remote' ], $link ) : false ;
if ( $banForThread ) {
$result .= " ban= \" blocked \" banid= \" " . $banForThread [ 'banid' ] . " \" " ;
2008-10-04 03:35:17 +04:00
}
2011-02-26 17:06:19 +03:00
$result .= " state= \" $state\ " typing = \ " " . $thread [ 'userTyping' ] . " \" > " ;
$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
}
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> " ;
2008-10-04 03:35:17 +04:00
}
2008-10-03 19:11:02 +04:00
$userAgent = get_useragent_version ( $thread [ 'userAgent' ]);
2011-02-26 17:06:19 +03:00
$result .= " <useragent> " . $userAgent . " </useragent> " ;
if ( $thread [ " shownmessageid " ] != 0 ) {
$query = " select tmessage from ${ mysqlprefix } chatmessage where messageid = " . $thread [ " shownmessageid " ];
2008-10-03 19:11:02 +04:00
$line = select_one_row ( $query , $link );
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 )
{
2011-02-26 16:13:16 +03:00
global $webim_encoding , $settings , $state_closed , $state_left , $mysqlprefix ;
2007-10-10 19:15:47 +04:00
$link = connect ();
$revision = $since ;
$output = array ();
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 ${ mysqlprefix } chatgroup where ${ mysqlprefix } chatgroup.groupid = ${ mysqlprefix } chatthread.groupid) as groupname " .
" from ${ mysqlprefix } chatthread where lrevision > $since " .
( $since <= 0
? " AND istate <> $state_closed AND istate <> $state_left "
: " " ) .
2009-03-25 03:33:14 +03:00
( $settings [ 'enablegroups' ] == '1'
2011-02-26 17:06:19 +03:00
? " AND (groupid is NULL " . ( $groupids
? " OR groupid IN ( $groupids ) "
: " " ) .
" ) "
: " " ) .
2009-03-25 03:33:14 +03:00
" ORDER BY threadid " ;
2008-09-30 02:46:27 +04:00
$rows = select_multi_assoc ( $query , $link );
foreach ( $rows as $row ) {
2011-02-26 17:06:19 +03:00
$thread = thread_to_xml ( $row , $link );
2007-10-10 19:15:47 +04:00
$output [] = $thread ;
2011-02-26 17:06:19 +03:00
if ( $row [ 'lrevision' ] > $revision )
2007-10-10 19:15:47 +04:00
$revision = $row [ 'lrevision' ];
}
mysql_close ( $link );
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> " ;
}
2011-02-26 17:06:19 +03:00
function print_operators ()
{
2011-02-21 03:02:39 +03:00
echo " <operators> " ;
$operators = operator_get_all ();
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 ;
$name = 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> " ;
2011-02-21 03:02:39 +03:00
}
2011-04-07 12:34:04 +04:00
function visitor_to_xml ( $visitor , $link )
{
$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' ], $link ));
$result .= " <operator> " . htmlspecialchars ( htmlspecialchars ( $operator )) . " </operator> " ;
}
$result .= " </invitation> " ;
$result .= " </visitor> " ;
return $result ;
}
function print_visitors ()
{
global $webim_encoding , $settings , $state_closed , $state_left , $mysqlprefix ;
$link = connect ();
// Remove old visitors' tracks
$query = " DELETE FROM ${ mysqlprefix } chatsitevisitor WHERE (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lasttime)) > " . $settings [ 'tracking_lifetime' ] .
" AND (threadid IS NULL OR (SELECT count(*) FROM ${ mysqlprefix } chatthread WHERE threadid = ${ mysqlprefix } chatsitevisitor.threadid " .
" AND istate <> $state_closed AND istate <> $state_left ) = 0) " ;
perform_query ( $query , $link );
// Remove old invitations
$query = " UPDATE ${ mysqlprefix } chatsitevisitor SET invited = 0, invitationtime = NULL, invitedby = NULL " .
" WHERE threadid IS NULL AND (UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(invitationtime)) > " .
$settings [ 'invitation_lifetime' ];
perform_query ( $query , $link );
// Remove associations of visitors with closed threads
$query = " UPDATE ${ mysqlprefix } chatsitevisitor SET threadid = NULL WHERE threadid IS NOT NULL AND " .
" (SELECT count(*) FROM ${ mysqlprefix } chatthread WHERE threadid = ${ mysqlprefix } chatsitevisitor.threadid " .
" AND istate <> $state_closed AND istate <> $state_left ) = 0 " ;
perform_query ( $query , $link );
$output = array ();
$query = " SELECT visitorid, userid, username, unix_timestamp(firsttime), unix_timestamp(lasttime), " .
" entry, path, details, invited, unix_timestamp(invitationtime), invitedby, invitations, chats " .
" FROM ${ mysqlprefix } chatsitevisitor " .
" WHERE threadid IS NULL " .
" ORDER BY invited, lasttime DESC, invitations " ;
$query .= ( $settings [ 'visitors_limit' ] == '0' ) ? " " : " LIMIT " . $settings [ 'visitors_limit' ];
$rows = select_multi_assoc ( $query , $link );
foreach ( $rows as $row ) {
$visitor = visitor_to_xml ( $row , $link );
$output [] = $visitor ;
}
mysql_close ( $link );
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-02-26 16:43:30 +03:00
$link = connect ();
loadsettings_ ( $link );
2011-04-07 02:31:22 +04:00
if ( ! isset ( $_SESSION [ " ${ mysqlprefix } operatorgroups " ])) {
2011-02-26 16:43:30 +03:00
$_SESSION [ " ${ mysqlprefix } operatorgroups " ] = get_operator_groupslist ( $operator [ 'operatorid' ], $link );
}
mysql_close ( $link );
$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 ) {
2011-02-21 03:02:39 +03:00
print_operators ();
}
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 ;
2009-08-04 20:30:39 +04:00
?>