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
* Pavel Petroshenko - history search
*/
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
$kind_to_string = array ( $kind_user => " user " , $kind_agent => " agent " , $kind_for_agent => " hidden " ,
2008-10-06 02:48:36 +04:00
$kind_info => " inf " , $kind_conn => " conn " , $kind_events => " event " , $kind_avatar => " avatar " );
2007-10-10 19:15:47 +04:00
2008-10-03 19:11:02 +04:00
function get_user_id () {
return ( time () + microtime ()) . rand ( 0 , 99999999 );
}
2007-10-10 19:15:47 +04:00
function next_token () {
return rand ( 99999 , 99999999 );
}
function next_revision ( $link ) {
perform_query ( " update chatrevision set id=LAST_INSERT_ID(id+1) " , $link );
$val = mysql_insert_id ( $link );
return $val ;
}
2008-05-06 01:08:57 +04:00
function post_message_ ( $threadid , $kind , $message , $link , $from = null , $utime = null , $opid = null ) {
2007-10-10 19:15:47 +04:00
$query = sprintf (
2008-09-30 02:35:08 +04:00
" insert into chatmessage (threadid,ikind,tmessage,tname,agentId,dtmcreated) values (%s, %s,'%s',%s,%s,%s) " ,
2007-10-10 19:15:47 +04:00
$threadid ,
$kind ,
2009-04-05 03:51:16 +04:00
mysql_real_escape_string ( $message , $link ),
$from ? " ' " . mysql_real_escape_string ( $from , $link ) . " ' " : " null " ,
2007-10-10 19:15:47 +04:00
$opid ? $opid : " 0 " ,
2008-05-06 01:08:57 +04:00
$utime ? " FROM_UNIXTIME( $utime ) " : " CURRENT_TIMESTAMP " );
2007-10-10 19:15:47 +04:00
perform_query ( $query , $link );
2007-12-03 00:32:47 +03:00
return mysql_insert_id ( $link );
2007-10-10 19:15:47 +04:00
}
function post_message ( $threadid , $kind , $message , $from = null , $agentid = null ) {
$link = connect ();
2007-12-03 00:32:47 +03:00
$id = post_message_ ( $threadid , $kind , $message , $link , $from , null , $agentid );
2007-10-10 19:15:47 +04:00
mysql_close ( $link );
2007-12-03 00:32:47 +03:00
return $id ;
2007-10-10 19:15:47 +04:00
}
function prepare_html_message ( $text ) {
$escaped_text = htmlspecialchars ( $text );
$text_w_links = preg_replace ( '/(http|ftp):\/\/\S*/' , '<a href="$0" target="_blank">$0</a>' , $escaped_text );
$multiline = str_replace ( " \n " , " <br/> " , $text_w_links );
return $multiline ;
}
function message_to_html ( $msg ) {
2008-10-06 02:48:36 +04:00
global $kind_to_string , $kind_avatar ;
if ( $msg [ 'ikind' ] == $kind_avatar ) return " " ;
2007-10-10 19:15:47 +04:00
$message = " <span> " . date ( " H:i:s " , $msg [ 'created' ]) . " </span> " ;
$kind = $kind_to_string { $msg [ 'ikind' ]};
if ( $msg [ 'tname' ] )
$message .= " <span class='n $kind '> " . htmlspecialchars ( $msg [ 'tname' ]) . " </span>: " ;
$message .= " <span class='m $kind '> " . prepare_html_message ( $msg [ 'tmessage' ]) . " </span><br/> " ;
return $message ;
}
function message_to_text ( $msg ) {
2008-10-06 02:48:36 +04:00
global $kind_user , $kind_agent , $kind_info , $kind_avatar ;
if ( $msg [ 'ikind' ] == $kind_avatar ) return " " ;
2008-05-06 01:08:57 +04:00
$message_time = date ( " H:i:s " , $msg [ 'created' ]);
2007-10-10 19:15:47 +04:00
if ( $msg [ 'ikind' ] == $kind_user || $msg [ 'ikind' ] == $kind_agent ) {
if ( $msg [ 'tname' ] )
2008-05-06 01:08:57 +04:00
return $message_time . $msg [ 'tname' ] . " : " . $msg [ 'tmessage' ] . " \n " ;
2007-10-10 19:15:47 +04:00
else
2008-05-06 01:08:57 +04:00
return $message_time . $msg [ 'tmessage' ] . " \n " ;
2007-10-10 19:15:47 +04:00
} else if ( $msg [ 'ikind' ] == $kind_info ) {
2008-05-06 01:08:57 +04:00
return $message_time . $msg [ 'tmessage' ] . " \n " ;
2007-10-10 19:15:47 +04:00
} else {
2008-05-06 01:08:57 +04:00
return $message_time . " [ " . $msg [ 'tmessage' ] . " ] \n " ;
2007-10-10 19:15:47 +04:00
}
}
function get_messages ( $threadid , $meth , $isuser , & $lastid ) {
2008-10-06 02:48:36 +04:00
global $kind_for_agent , $kind_avatar , $webim_encoding ;
2007-10-10 19:15:47 +04:00
$link = connect ();
$query = sprintf (
2008-09-30 02:35:08 +04:00
" select messageid,ikind,unix_timestamp(dtmcreated) as created,tname,tmessage from chatmessage " .
" where threadid = %s and messageid > %s %s order by messageid " ,
$threadid , $lastid , $isuser ? " and ikind <> $kind_for_agent " : " " );
2007-10-10 19:15:47 +04:00
$messages = array ();
2008-09-30 02:35:08 +04:00
$msgs = select_multi_assoc ( $query , $link );
foreach ( $msgs as $msg ) {
$message = " " ;
if ( $meth == 'xml' ) {
2008-10-06 02:48:36 +04:00
switch ( $msg [ 'ikind' ]) {
case $kind_avatar :
$message = " <avatar> " . myiconv ( $webim_encoding , " utf-8 " , escape_with_cdata ( $msg [ 'tmessage' ])) . " </avatar> " ;
break ;
default :
$message = " <message> " . myiconv ( $webim_encoding , " utf-8 " , escape_with_cdata ( message_to_html ( $msg ))) . " </message> \n " ;
}
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 ;
2007-12-03 00:32:47 +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
}
mysql_close ( $link );
return $messages ;
2008-09-30 02:35:08 +04:00
}
2007-10-10 19:15:47 +04:00
2008-10-03 02:41:35 +04:00
function print_thread_messages ( $thread , $token , $lastid , $isuser , $format , $agentid = null ) {
2009-06-09 03:19:21 +04:00
global $webim_encoding , $webimroot , $connection_timeout , $settings ;
2008-05-11 02:35:16 +04:00
$threadid = $thread [ 'threadid' ];
2008-09-30 02:35:08 +04: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
if ( $format == " xml " ) {
2008-09-30 02:35:08 +04:00
$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 ();
2008-10-03 02:41:35 +04:00
print ( " <thread lastid= \" $lastid\ " typing = \ " " . $istyping . " \" canpost= \" " . (( $isuser || $agentid != null && $agentid == $thread [ 'agentId' ]) ? 1 : 0 ) . " \" > " );
2007-10-10 19:15:47 +04:00
foreach ( $output as $msg ) {
2008-03-05 01:22:48 +03:00
print $msg ;
2007-10-10 19:15:47 +04:00
}
print ( " </thread> " );
} else if ( $format == " html " ) {
2009-06-09 03:19:21 +04:00
loadsettings ();
2008-09-30 02:35:08 +04:00
$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 ();
2009-06-05 19:49:51 +04: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 (
" <!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 " .
2009-06-09 03:19:21 +04:00
" <meta http-equiv= \" Refresh \" content= \" " . $settings [ 'updatefrequency_oldchat' ] . " ; URL= $url &sn=11 \" > \n " .
2008-09-30 02:35:08 +04:00
" <meta http-equiv= \" Pragma \" content= \" no-cache \" > \n " .
2009-06-05 19:49:51 +04:00
" <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';} \" > " .
2008-09-30 02:35:08 +04:00
" <table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td valign='top' class='message'> " );
2009-06-05 19:49:51 +04:00
2007-10-10 19:15:47 +04:00
foreach ( $output as $msg ) {
print $msg ;
}
print (
2009-06-05 19:49:51 +04:00
" </td></tr></table><a name='aend'></a> " .
2008-09-30 02:35:08 +04:00
" </body></html> " );
2007-10-10 19:15:47 +04:00
}
}
2008-10-03 19:11:02 +04:00
function get_user_name ( $username , $addr , $id ) {
2008-10-12 05:21:37 +04:00
global $settings ;
loadsettings ();
2008-09-30 02:35:08 +04:00
return str_replace ( " { addr} " , $addr ,
2008-10-03 19:11:02 +04:00
str_replace ( " { id} " , $id ,
2008-10-12 05:21:37 +04:00
str_replace ( " { name} " , $username , $settings [ 'usernamepattern' ])));
2007-10-10 19:15:47 +04:00
}
2008-05-06 01:08:57 +04:00
function is_ajax_browser ( $browserid , $ver , $useragent ) {
if ( $browserid == " opera " )
2007-10-10 19:15:47 +04:00
return $ver >= 8.02 ;
2008-05-06 01:08:57 +04:00
if ( $browserid == " safari " )
2007-10-10 19:15:47 +04:00
return $ver >= 125 ;
2008-05-06 01:08:57 +04:00
if ( $browserid == " msie " )
2007-10-10 19:15:47 +04:00
return $ver >= 5.5 && ! strstr ( $useragent , " powerpc " );
2008-05-06 01:08:57 +04:00
if ( $browserid == " netscape " )
2007-10-10 19:15:47 +04:00
return $ver >= 7.1 ;
2008-05-06 01:08:57 +04:00
if ( $browserid == " mozilla " )
2007-10-10 19:15:47 +04:00
return $ver >= 1.4 ;
2008-05-06 01:08:57 +04:00
if ( $browserid == " firefox " )
2007-10-10 19:15:47 +04:00
return $ver >= 1.0 ;
2008-12-18 18:09:44 +03:00
if ( $browserid == " chrome " )
return true ;
2007-10-10 19:15:47 +04:00
return false ;
}
2008-05-06 01:08:57 +04:00
function is_old_browser ( $browserid , $ver ) {
if ( $browserid == " opera " )
2007-10-10 19:15:47 +04:00
return $ver < 7.0 ;
2008-05-06 01:08:57 +04: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
}
2008-12-18 18:09:44 +03:00
$knownAgents = array ( " opera " , " msie " , " chrome " , " safari " , " firefox " , " netscape " , " mozilla " );
2007-10-10 19:15:47 +04:00
function get_remote_level ( $useragent ) {
global $knownAgents ;
$useragent = strtolower ( $useragent );
foreach ( $knownAgents as $agent ) {
if ( strstr ( $useragent , $agent ) ) {
if ( preg_match ( " / " . $agent . " [ \\ s \ /]?( \\ d+( \\ . \\ d+)?)/ " , $useragent , $matches ) ) {
$ver = $matches [ 1 ];
if ( is_ajax_browser ( $agent , $ver , $useragent ) )
return " ajaxed " ;
else if ( is_old_browser ( $agent , $ver ) )
return " old " ;
return " simple " ;
}
}
}
return " simple " ;
}
2008-09-30 02:35:08 +04:00
function is_agent_opera95 () {
$useragent = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]);
if ( strstr ( $useragent , " opera " ) ) {
if ( preg_match ( " /opera[ \\ s \ /]?( \\ d+( \\ . \\ d+)?)/ " , $useragent , $matches ) ) {
$ver = $matches [ 1 ];
if ( $ver >= " 9.5 " )
return true ;
}
}
return false ;
}
2009-04-24 19:52:17 +04:00
function is_mac_opera () {
$useragent = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]);
return strstr ( $useragent , " opera " ) && strstr ( $useragent , " mac " );
}
2008-09-30 02:35:08 +04:00
function needsFramesrc () {
$useragent = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]);
return strstr ( $useragent , " safari/ " );
}
2008-11-08 02:24:45 +03:00
function setup_logo () {
global $page , $settings ;
loadsettings ();
$page [ 'ct.company.name' ] = topage ( $settings [ 'title' ]);
$page [ 'ct.company.chatLogoURL' ] = topage ( $settings [ 'logo' ]);
$page [ 'webimHost' ] = topage ( $settings [ 'hosturl' ]);
}
2009-08-11 19:06:46 +04:00
function setup_leavemessage ( $name , $email , $message , $groupid , $groupname , $info , $referrer , $canshowcaptcha ) {
global $settings , $page ;
$page [ 'formname' ] = topage ( $name );
$page [ 'formemail' ] = topage ( $email );
$page [ 'formmessage' ] = $message ? topage ( $message ) : " " ;
$page [ 'showcaptcha' ] = $settings [ " enablecaptcha " ] == " 1 " && $canshowcaptcha ? " 1 " : " " ;
$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 ));
}
2009-04-05 03:51:16 +04:00
function setup_survey ( $name , $email , $groupid , $info , $referrer ) {
global $settings , $page ;
$page [ 'formname' ] = topage ( $name );
$page [ 'formemail' ] = topage ( $email );
$page [ 'formgroupid' ] = $groupid ;
$page [ 'forminfo' ] = topage ( $info );
$page [ 'referrer' ] = urlencode ( topage ( $referrer ));
if ( $settings [ 'enablegroups' ] == '1' && $settings [ " surveyaskgroup " ] == " 1 " ) {
2009-07-19 04:07:34 +04:00
$link = connect ();
2009-07-24 11:37:58 +04:00
$allgroups = get_groups ( $link , false );
2009-07-19 04:07:34 +04:00
mysql_close ( $link );
2009-04-05 03:51:16 +04:00
$val = " " ;
2009-07-19 04:07:34 +04:00
foreach ( $allgroups as $k ) {
$groupname = $k [ 'vclocalname' ];
if ( $k [ 'inumofagents' ] == 0 ) {
continue ;
}
if ( $k [ 'ilastseen' ] !== NULL && $k [ 'ilastseen' ] < $settings [ 'online_timeout' ]) {
if ( ! $groupid ) {
2009-07-24 11:37:58 +04:00
$groupid = $k [ 'groupid' ]; // select first online group
2009-07-19 04:07:34 +04:00
}
2009-08-11 19:06:46 +04:00
} else {
$groupname .= " (offline) " ;
2009-07-19 04:07:34 +04:00
}
$isselected = $k [ 'groupid' ] == $groupid ;
$val .= " <option value= \" " . $k [ 'groupid' ] . " \" " . ( $isselected ? " selected= \" selected \" " : " " ) . " > $groupname </option> " ;
2009-04-05 03:51:16 +04:00
}
$page [ 'groups' ] = $val ;
}
$page [ 'showemail' ] = $settings [ " surveyaskmail " ] == " 1 " ? " 1 " : " " ;
$page [ 'showmessage' ] = $settings [ " surveyaskmessage " ] == " 1 " ? " 1 " : " " ;
$page [ 'showname' ] = $settings [ 'usercanchangename' ] == " 1 " ? " 1 " : " " ;
}
2008-09-30 02:35:08 +04:00
function setup_chatview_for_user ( $thread , $level ) {
2008-10-12 05:21:37 +04:00
global $page , $webimroot , $settings ;
2008-09-30 02:35:08 +04:00
loadsettings ();
$page = array ();
$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' ]));
2008-10-12 05:21:37 +04:00
$page [ 'canChangeName' ] = $settings [ 'usercanchangename' ] == " 1 " ;
2008-11-08 03:44:20 +03:00
$page [ 'chat.title' ] = topage ( $settings [ 'chattitle' ]);
2008-09-30 02:35:08 +04:00
2008-11-08 02:24:45 +03:00
setup_logo ();
2009-04-24 19:09:46 +04:00
if ( $settings [ 'sendmessagekey' ] == 'enter' ) {
$page [ 'send_shortcut' ] = " Enter " ;
$page [ 'ignorectrl' ] = 1 ;
} else {
2009-04-24 19:52:17 +04:00
$page [ 'send_shortcut' ] = is_mac_opera () ? " ⌘-Enter " : " Ctrl-Enter " ;
2009-04-24 19:09:46 +04:00
$page [ 'ignorectrl' ] = 0 ;
}
2008-09-30 02:35:08 +04:00
2009-06-05 19:49:51 +04: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
if ( $settings [ 'enablessl' ] == " 1 " && ! is_secure_request ()) {
2009-06-05 19:49:51 +04: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 ();
2009-06-09 03:19:21 +04:00
$page [ 'frequency' ] = $settings [ 'updatefrequency_chat' ];
2008-09-30 02:35:08 +04:00
}
2009-04-10 03:22:12 +04:00
function load_canned_messages ( $locale , $groupid ) {
$link = connect ();
$result = select_multi_assoc (
" select vcvalue from chatresponses where locale = ' " . $locale . " ' " .
" AND (groupid is NULL OR groupid = 0) order by vcvalue " , $link );
if ( count ( $result ) == 0 ) {
foreach ( explode ( " \n " , getstring_ ( 'chat.predefined_answers' , $locale )) as $answer ) {
$result [] = array ( 'vcvalue' => $answer );
}
}
if ( $groupid ) {
$result2 = select_multi_assoc (
" select vcvalue from chatresponses where locale = ' " . $locale . " ' " .
" AND groupid = $groupid order by vcvalue " , $link );
foreach ( $result as $r ) {
$result2 [] = $r ;
}
$result = $result2 ;
}
mysql_close ( $link );
return $result ;
}
2008-09-30 02:35:08 +04:00
function setup_chatview_for_operator ( $thread , $operator ) {
global $page , $webimroot , $company_logo_link , $company_name , $settings ;
loadsettings ();
$page = array ();
$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' ];
2009-04-05 03:51:16 +04:00
$page [ 'ct.user.name' ] = htmlspecialchars ( topage ( get_user_name ( $thread [ 'userName' ], $thread [ 'remote' ], $thread [ 'userid' ])));
2008-11-08 03:44:20 +03:00
$page [ 'chat.title' ] = topage ( $settings [ 'chattitle' ]);
2008-09-30 02:35:08 +04:00
2008-11-08 02:24:45 +03:00
setup_logo ();
2009-04-24 19:09:46 +04:00
if ( $settings [ 'sendmessagekey' ] == 'enter' ) {
$page [ 'send_shortcut' ] = " Enter " ;
$page [ 'ignorectrl' ] = 1 ;
} else {
2009-04-24 19:52:17 +04:00
$page [ 'send_shortcut' ] = is_mac_opera () ? " ⌘-Enter " : " Ctrl-Enter " ;
2009-04-24 19:09:46 +04:00
$page [ 'ignorectrl' ] = 0 ;
}
2009-03-08 23:53:16 +03:00
if ( $settings [ 'enablessl' ] == " 1 " && ! is_secure_request ()) {
2009-06-05 19:49:51 +04: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 ();
2008-10-03 19:11:02 +04:00
$page [ 'historyParams' ] = array ( " userid " => " " . $thread [ 'userid' ]);
2008-10-11 02:26:14 +04:00
$page [ 'historyParamsLink' ] = add_params ( $webimroot . " /operator/userhistory.php " , $page [ 'historyParams' ]);
2008-10-11 04:06:15 +04:00
$predefinedres = " " ;
2009-04-10 03:22:12 +04:00
$canned_messages = load_canned_messages ( $thread [ 'locale' ], $thread [ 'groupid' ]);
foreach ( $canned_messages as $answer ) {
$predefinedres .= " <option> " . htmlspecialchars ( topage ( $answer [ 'vcvalue' ])) . " </option> " ;
2008-10-11 04:06:15 +04:00
}
2008-10-12 04:16:44 +04:00
$page [ 'predefinedAnswers' ] = $predefinedres ;
2009-06-05 19:49:51 +04: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' ] = " " ;
2009-06-09 03:19:21 +04:00
$page [ 'frequency' ] = $settings [ 'updatefrequency_chat' ];
2008-09-30 02:35:08 +04:00
}
2007-10-10 19:15:47 +04:00
function update_thread_access ( $threadid , $params , $link ) {
$clause = " " ;
foreach ( $params as $k => $v ) {
if ( strlen ( $clause ) > 0 )
$clause .= " , " ;
2008-09-30 02:35:08 +04:00
$clause .= $k . " = " . $v ;
2007-10-10 19:15:47 +04:00
}
perform_query (
" update chatthread set $clause " .
" where threadid = " . $threadid , $link );
}
2008-05-06 01:08:57 +04: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 ;
2007-10-10 19:15:47 +04:00
$link = connect ();
2008-05-11 02:35:16 +04:00
$params = array (( $isuser ? " lastpinguser " : " lastpingagent " ) => " CURRENT_TIMESTAMP " ,
( $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
if ( $thread [ 'istate' ] == $state_loading && $isuser ) {
$params [ 'istate' ] = $state_queue ;
commit_thread ( $thread [ 'threadid' ], $params , $link );
mysql_close ( $link );
return ;
}
2008-05-11 02:35:16 +04:00
if ( $lastping > 0 && abs ( $current - $lastping ) > $connection_timeout ) {
2007-10-10 19:15:47 +04:00
$params [ $isuser ? " lastpingagent " : " lastpinguser " ] = " 0 " ;
if ( ! $isuser ) {
$message_to_post = getstring_ ( " chat.status.user.dead " , $thread [ 'locale' ]);
2008-05-11 02:35:16 +04:00
post_message_ ( $thread [ 'threadid' ], $kind_for_agent , $message_to_post , $link , null , $lastping + $connection_timeout );
2007-10-10 19:15:47 +04:00
} else if ( $thread [ 'istate' ] == $state_chatting ) {
$message_to_post = getstring_ ( " chat.status.operator.dead " , $thread [ 'locale' ]);
2008-05-11 02:35:16 +04:00
post_message_ ( $thread [ 'threadid' ], $kind_conn , $message_to_post , $link , 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 ;
2007-10-10 19:15:47 +04:00
commit_thread ( $thread [ 'threadid' ], $params , $link );
mysql_close ( $link );
return ;
}
}
update_thread_access ( $thread [ 'threadid' ], $params , $link );
mysql_close ( $link );
}
function commit_thread ( $threadid , $params , $link ) {
2008-06-05 02:51:46 +04:00
$query = " update chatthread t set lrevision = " . next_revision ( $link ) . " , dtmmodified = CURRENT_TIMESTAMP " ;
2007-10-10 19:15:47 +04:00
foreach ( $params as $k => $v ) {
2008-09-30 02:35:08 +04:00
$query .= " , " . $k . " = " . $v ;
2007-10-10 19:15:47 +04:00
}
$query .= " where threadid = " . $threadid ;
perform_query ( $query , $link );
}
function rename_user ( $thread , $newname ) {
global $kind_events ;
$link = connect ();
2009-04-05 18:20:34 +04:00
commit_thread ( $thread [ 'threadid' ], array ( 'userName' => " ' " . mysql_real_escape_string ( $newname , $link ) . " ' " ), $link );
2007-10-10 19:15:47 +04:00
if ( $thread [ 'userName' ] != $newname ) {
2009-04-05 18:20:34 +04:00
post_message_ ( $thread [ 'threadid' ], $kind_events ,
getstring2_ ( " chat.status.user.changedname " , array ( $thread [ 'userName' ], $newname ), $thread [ 'locale' ]), $link );
2007-10-10 19:15:47 +04:00
}
2009-04-05 18:20:34 +04:00
mysql_close ( $link );
2007-10-10 19:15:47 +04:00
}
function close_thread ( $thread , $isuser ) {
global $state_closed , $kind_events ;
2008-09-30 02:35:08 +04:00
2009-04-05 18:20:34 +04:00
$link = connect ();
2007-10-10 19:15:47 +04:00
if ( $thread [ 'istate' ] != $state_closed ) {
2008-09-30 02:35:08 +04:00
commit_thread ( $thread [ 'threadid' ], array ( 'istate' => $state_closed ,
2008-06-05 02:51:46 +04:00
'messageCount' => '(SELECT COUNT(*) FROM chatmessage WHERE chatmessage.threadid = t.threadid AND ikind = 1)' ), $link );
2007-10-10 19:15:47 +04:00
}
$message = $isuser ? getstring2_ ( " chat.status.user.left " , array ( $thread [ 'userName' ]), $thread [ 'locale' ])
: getstring2_ ( " chat.status.operator.left " , array ( $thread [ 'agentName' ]), $thread [ 'locale' ]);
2009-04-05 18:20:34 +04:00
post_message_ ( $thread [ 'threadid' ], $kind_events , $message , $link );
mysql_close ( $link );
2007-10-10 19:15:47 +04:00
}
2008-05-11 02:35:16 +04:00
function thread_by_id_ ( $id , $link ) {
return select_one_row ( " select threadid,userName,agentName,agentId,lrevision,istate,ltoken,userTyping,agentTyping " .
2009-03-13 03:52:37 +03:00
" ,unix_timestamp(dtmmodified) as modified, unix_timestamp(dtmcreated) as created " .
2009-04-10 03:22:12 +04:00
" ,remote,referer,locale,unix_timestamp(lastpinguser) as lpuser,unix_timestamp(lastpingagent) as lpagent, unix_timestamp(CURRENT_TIMESTAMP) as current,nextagent,shownmessageid,userid,userAgent,groupid " .
2008-05-11 02:35:16 +04:00
" from chatthread where threadid = " . $id , $link );
}
2008-10-04 03:35:17 +04:00
function ban_for_addr_ ( $addr , $link ) {
2009-04-05 03:51:16 +04:00
return select_one_row ( " select banid,comment from chatban where unix_timestamp(dtmtill) > unix_timestamp(CURRENT_TIMESTAMP) AND address = ' " . mysql_real_escape_string ( $addr , $link ) . " ' " , $link );
2008-10-04 03:35:17 +04:00
}
2008-05-11 02:35:16 +04:00
function thread_by_id ( $id ) {
$link = connect ();
$thread = thread_by_id_ ( $id , $link );
mysql_close ( $link );
return $thread ;
}
2009-08-08 02:49:11 +04:00
function create_thread ( $groupid , $username , $remoteHost , $referer , $lang , $userid , $userbrowser , $initialState , $link ) {
2007-10-10 19:15:47 +04:00
$query = sprintf (
2009-03-25 02:50:38 +03:00
" insert into chatthread (userName,userid,ltoken,remote,referer,lrevision,locale,userAgent,dtmcreated,dtmmodified,istate " . ( $groupid ? " ,groupid " : " " ) . " ) values " .
2009-08-08 02:49:11 +04:00
" ('%s','%s',%s,'%s','%s',%s,'%s','%s',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP, $initialState " . ( $groupid ? " , $groupid " : " " ) . " ) " ,
2009-04-05 18:20:34 +04:00
mysql_real_escape_string ( $username , $link ),
mysql_real_escape_string ( $userid , $link ),
2007-10-10 19:15:47 +04:00
next_token (),
2009-04-05 18:20:34 +04:00
mysql_real_escape_string ( $remoteHost , $link ),
mysql_real_escape_string ( $referer , $link ),
2007-10-10 19:15:47 +04:00
next_revision ( $link ),
2009-04-05 18:20:34 +04:00
mysql_real_escape_string ( $lang , $link ),
mysql_real_escape_string ( $userbrowser , $link ));
2007-10-10 19:15:47 +04:00
perform_query ( $query , $link );
$id = mysql_insert_id ( $link );
2008-05-11 02:35:16 +04:00
$newthread = thread_by_id_ ( $id , $link );
2007-10-10 19:15:47 +04:00
return $newthread ;
}
function do_take_thread ( $threadid , $operatorId , $operatorName ) {
global $state_chatting ;
$link = connect ();
2008-09-30 02:35:08 +04:00
commit_thread ( $threadid ,
2007-10-10 19:15:47 +04:00
array ( " istate " => $state_chatting ,
2008-10-02 13:43:58 +04:00
" nextagent " => 0 ,
2007-10-10 19:15:47 +04:00
" agentId " => $operatorId ,
2009-04-05 18:20:34 +04:00
" agentName " => " ' " . mysql_real_escape_string ( $operatorName , $link ) . " ' " ), $link );
2007-10-10 19:15:47 +04:00
mysql_close ( $link );
}
function reopen_thread ( $threadid ) {
2009-08-08 02:49:11 +04:00
global $state_queue , $state_loading , $state_waiting , $state_chatting , $state_closed , $state_left , $kind_events ;
2009-04-05 18:20:34 +04:00
$link = connect ();
$thread = thread_by_id_ ( $threadid , $link );
2007-10-10 19:15:47 +04:00
if ( ! $thread )
return FALSE ;
2009-08-08 02:49:11 +04:00
if ( $thread [ 'istate' ] == $state_closed || $thread [ 'istate' ] == $state_left )
2007-10-10 19:15:47 +04:00
return FALSE ;
2008-06-05 02:51:46 +04:00
if ( $thread [ 'istate' ] != $state_chatting && $thread [ 'istate' ] != $state_queue && $thread [ 'istate' ] != $state_loading ) {
2008-09-30 02:35:08 +04:00
commit_thread ( $threadid ,
2008-10-02 13:43:58 +04:00
array ( " istate " => $state_waiting , " nextagent " => 0 ), $link );
2007-10-10 19:15:47 +04:00
}
2009-04-05 18:20:34 +04:00
post_message_ ( $thread [ 'threadid' ], $kind_events , getstring_ ( " chat.status.user.reopenedthread " , $thread [ 'locale' ]), $link );
mysql_close ( $link );
2007-10-10 19:15:47 +04:00
return $thread ;
}
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 = " " ;
$operatorName = ( $thread [ 'locale' ] == $home_locale ) ? $operator [ 'vclocalename' ] : $operator [ 'vccommonname' ];
2008-06-05 02:51:46 +04:00
if ( $state == $state_queue || $state == $state_waiting || $state == $state_loading ) {
2007-10-10 19:15:47 +04:00
do_take_thread ( $threadid , $operator [ 'operatorid' ], $operatorName );
2008-10-03 03:21:56 +04:00
if ( $state == $state_waiting ) {
2008-05-18 02:14:29 +04:00
if ( $operatorName != $thread [ 'agentName' ] ) {
$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' ]);
}
} else if ( $state == $state_chatting ) {
if ( $operator [ 'operatorid' ] != $thread [ 'agentId' ] ) {
2008-09-30 02:35:08 +04:00
do_take_thread ( $threadid , $operator [ 'operatorid' ], $operatorName );
2007-10-10 19:15:47 +04:00
$message_to_post = getstring2_ ( " chat.status.operator.changed " , array ( $operatorName , $thread [ 'agentName' ]), $thread [ 'locale' ]);
}
} else {
die ( " cannot take thread " );
}
if ( $message_to_post ) {
post_message ( $threadid , $kind_events , $message_to_post );
2008-10-06 02:48:36 +04:00
post_message ( $threadid , $kind_avatar , $operator [ 'vcavatar' ] ? $operator [ 'vcavatar' ] : " " );
2007-10-10 19:15:47 +04: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' ];
2008-09-30 02:35:08 +04:00
if ( $thread [ 'istate' ] == $state_waiting &&
2008-10-02 13:43:58 +04:00
( $thread [ 'nextagent' ] == $operator [ 'operatorid' ]
|| $thread [ 'agentId' ] == $operator [ 'operatorid' ] )) {
2007-10-10 19:15:47 +04:00
do_take_thread ( $thread [ 'threadid' ], $operator [ 'operatorid' ], $operatorName );
2008-05-18 02:14:29 +04:00
if ( $operatorName != $thread [ 'agentName' ] ) {
$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
2007-10-10 19:15:47 +04:00
post_message ( $thread [ 'threadid' ], $kind_events , $message_to_post );
2008-10-06 04:55:43 +04:00
post_message ( $thread [ 'threadid' ], $kind_avatar , $operator [ 'vcavatar' ] ? $operator [ 'vcavatar' ] : " " );
2007-10-10 19:15:47 +04:00
}
}
2009-04-05 18:20:34 +04:00
function check_connections_from_remote ( $remote , $link ) {
2009-08-08 02:49:11 +04:00
global $settings , $state_closed , $state_left ;
2009-04-05 18:20:34 +04:00
if ( $settings [ 'max_connections_from_one_host' ] == 0 ) {
return true ;
}
$result = select_one_row (
" select count(*) as opened from chatthread " .
2009-08-08 02:49:11 +04:00
" where remote = ' " . mysql_real_escape_string ( $remote , $link ) . " ' AND istate <> $state_closed AND istate <> $state_left " , $link );
2009-04-05 18:20:34 +04:00
if ( $result && isset ( $result [ 'opened' ])) {
return $result [ 'opened' ] < $settings [ 'max_connections_from_one_host' ];
}
return true ;
}
2007-10-10 19:15:47 +04: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 ;
2008-05-12 13:23:16 +04:00
if ( isset ( $_COOKIE [ $namecookie ]) ) {
$data = base64_decode ( strtr ( $_COOKIE [ $namecookie ], '-_,' , '+/=' ));
if ( strlen ( $data ) > 0 ) {
$userName = myiconv ( " utf-8 " , $webim_encoding , $data );
}
2009-03-08 23:53:16 +03:00
}
if ( $userName == $defaultName ) {
$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
$userId = " " ;
if ( isset ( $_COOKIE [ $usercookie ])) {
$userId = $_COOKIE [ $usercookie ];
} else {
$userId = get_user_id ();
setcookie ( $usercookie , $userId , time () + 60 * 60 * 24 * 365 );
}
return array ( 'id' => $userId , 'name' => $userName );
2007-10-10 19:15:47 +04:00
}
2009-08-08 02:49:11 +04:00
function get_remote_host () {
$extAddr = $_SERVER [ 'REMOTE_ADDR' ];
if ( isset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ]) &&
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] != $_SERVER [ 'REMOTE_ADDR' ]) {
$extAddr = $_SERVER [ 'REMOTE_ADDR' ] . ' (' . $_SERVER [ 'HTTP_X_FORWARDED_FOR' ] . ')' ;
}
return isset ( $_SERVER [ 'REMOTE_HOST' ]) ? $_SERVER [ 'REMOTE_HOST' ] : $extAddr ;
}
2009-04-10 18:12:57 +04:00
?>