From c5bc5ed2f57d003a493dc33b1c6d042b3e664f61 Mon Sep 17 00:00:00 2001 From: Evgeny Gryaznov Date: Wed, 16 Feb 2011 01:43:19 +0100 Subject: [PATCH] fast history search, fix redirection to groups, extract webim_mail function into notify.php --- src/messenger/webim/leavemessage.php | 5 +++- src/messenger/webim/libs/common.php | 37 +++++++++--------------- src/messenger/webim/libs/notify.php | 37 ++++++++++++++++++++++++ src/messenger/webim/libs/operator.php | 33 ++++++++++++--------- src/messenger/webim/libs/pagination.php | 13 +++++++++ src/messenger/webim/mail.php | 5 +++- src/messenger/webim/operator/history.php | 32 +++++++++----------- src/messenger/webim/operator/restore.php | 8 ++--- 8 files changed, 109 insertions(+), 61 deletions(-) create mode 100644 src/messenger/webim/libs/notify.php diff --git a/src/messenger/webim/leavemessage.php b/src/messenger/webim/leavemessage.php index e891fd17..0ee3f036 100644 --- a/src/messenger/webim/leavemessage.php +++ b/src/messenger/webim/leavemessage.php @@ -24,6 +24,7 @@ require_once('libs/chat.php'); require_once('libs/expand.php'); require_once('libs/groups.php'); require_once('libs/captcha.php'); +require_once('libs/notify.php'); $errors = array(); $page = array(); @@ -110,7 +111,9 @@ $body = getstring2_("leavemail.body", array($visitor_name,$email,$message,$info $inbox_mail = $settings['email']; if($inbox_mail) { - webim_mail($inbox_mail, $email, $subject, $body); + $link = connect(); + webim_mail($inbox_mail, $email, $subject, $body, $link); + mysql_close($link); } setup_logo(); diff --git a/src/messenger/webim/libs/common.php b/src/messenger/webim/libs/common.php index e9d68d00..494b556f 100644 --- a/src/messenger/webim/libs/common.php +++ b/src/messenger/webim/libs/common.php @@ -338,14 +338,6 @@ function perform_query($query,$link) { or die(' Query failed: '.mysql_error()/*.": ".$query*/); } -function rows_count($link,$table,$whereclause="") { - $result = mysql_query("SELECT count(*) FROM $table $whereclause",$link) - or die(' Count query failed: '.mysql_error()); - $line = mysql_fetch_array($result, MYSQL_NUM); - mysql_free_result($result); - return $line[0]; -} - function select_one_row($query,$link) { $result = mysql_query($query,$link) or die(' Query failed: ' . mysql_error().": ".$query); @@ -366,6 +358,20 @@ function select_multi_assoc($query, $link) { return $result; } +function db_build_select($fields, $table, $conditions, $orderandgroup) { + $condition = count($conditions) > 0 ? " where ".implode(" and ", $conditions) : ""; + if($orderandgroup) $orderandgroup = " ".$orderandgroup; + return "select $fields from $table$condition$orderandgroup"; +} + +function db_rows_count($table,$conditions,$countfields, $link) { + $result = mysql_query(db_build_select("count(".($countfields ? $countfields : "*").")", $table, $conditions, ""),$link) + or die(' Count query failed: '.mysql_error()); + $line = mysql_fetch_array($result, MYSQL_NUM); + mysql_free_result($result); + return $line[0]; +} + function start_xml_output() { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); @@ -543,21 +549,6 @@ function date_to_text($unixtime) { return strftime($date_format." ".getlocal("time.timeformat"), $unixtime); } -function webim_mail($toaddr, $reply_to, $subject, $body) { - global $webim_encoding, $webim_mailbox, $mail_encoding; - - $headers = "From: $webim_mailbox\r\n" - ."Reply-To: ".myiconv($webim_encoding, $mail_encoding, $reply_to)."\r\n" - ."Content-Type: text/plain; charset=$mail_encoding\r\n" - .'X-Mailer: PHP/'.phpversion(); - - $real_subject = "=?".$mail_encoding."?B?".base64_encode(myiconv($webim_encoding,$mail_encoding,$subject))."?="; - - $body = preg_replace("/\n/","\r\n", $body); - - @mail($toaddr, $real_subject, wordwrap(myiconv($webim_encoding, $mail_encoding, $body),70), $headers); -} - $dbversion = '1.6.3'; $settings = array( diff --git a/src/messenger/webim/libs/notify.php b/src/messenger/webim/libs/notify.php new file mode 100644 index 00000000..672ba88c --- /dev/null +++ b/src/messenger/webim/libs/notify.php @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/src/messenger/webim/libs/operator.php b/src/messenger/webim/libs/operator.php index 3ebbde7d..9a1cbc67 100644 --- a/src/messenger/webim/libs/operator.php +++ b/src/messenger/webim/libs/operator.php @@ -220,20 +220,30 @@ function setup_redirect_links($threadid,$token) { loadsettings(); $link = connect(); - $operatorscount = rows_count($link, "chatoperator"); - $groupscount = $settings['enablegroups'] == "1" ? rows_count($link, "chatgroup") : 0; - - prepare_pagination(max($operatorscount,$groupscount),8); - $limit = $page['pagination']['limit']; - - $query = "select operatorid, vclogin, vclocalename, vccommonname, istatus, (unix_timestamp(CURRENT_TIMESTAMP)-unix_timestamp(dtmlastvisited)) as time ". - "from chatoperator order by vclogin $limit"; - $operators = select_multi_assoc($query, $link); + $operatorscount = db_rows_count("chatoperator", array(), "", $link); + $groupscount = 0; if($settings['enablegroups'] == "1") { - $groups = get_groups($link, true); + $groups = array(); + foreach(get_groups($link, true) as $group) { + if($group['inumofagents'] == 0) { + continue; + } + $groups[] = $group; + } + $groupscount = count($groups); } + prepare_pagination(max($operatorscount,$groupscount),8); + $p = $page['pagination']; + $limit = $p['limit']; + + $operators = select_multi_assoc(db_build_select( + "operatorid, vclogin, vclocalename, vccommonname, istatus, (unix_timestamp(CURRENT_TIMESTAMP)-unix_timestamp(dtmlastvisited)) as time", + "chatoperator", array(), "order by vclogin $limit"), $link); + + $groups = array_slice($groups, $p['start'], $p['end']-$p['start']); + mysql_close($link); $agent_list = ""; @@ -257,9 +267,6 @@ function setup_redirect_links($threadid,$token) { if($settings['enablegroups'] == "1") { $params = array('thread' => $threadid, 'token' => $token); foreach($groups as $group) { - if($group['inumofagents'] == 0) { - continue; - } $params['nextGroup'] = $group['groupid']; $status = $group['ilastseen'] !== NULL && $group['ilastseen'] < $settings['online_timeout'] ? getlocal("char.redirect.operator.online_suff") diff --git a/src/messenger/webim/libs/pagination.php b/src/messenger/webim/libs/pagination.php index 0382fa1e..19d5a80a 100644 --- a/src/messenger/webim/libs/pagination.php +++ b/src/messenger/webim/libs/pagination.php @@ -72,6 +72,19 @@ function setup_pagination($items,$default_items_per_page=15) { } } +function select_with_pagintation($fields, $table, $conditions, $order, $countfields, $link) { + global $page; + $count = db_rows_count($table, $conditions, $countfields, $link); + prepare_pagination($count); + if($count) { + $p = $page['pagination']; + $limit = $p['limit']; + $page['pagination.items'] = select_multi_assoc(db_build_select($fields, $table, $conditions, $order)." ".$limit, $link); + } else { + $page['pagination.items'] = false; + } +} + function setup_empty_pagination() { global $page; $page['pagination.items'] = false; diff --git a/src/messenger/webim/mail.php b/src/messenger/webim/mail.php index d2d78f69..d79d23a3 100644 --- a/src/messenger/webim/mail.php +++ b/src/messenger/webim/mail.php @@ -22,6 +22,7 @@ require_once('libs/common.php'); require_once('libs/chat.php'); require_once('libs/expand.php'); +require_once('libs/notify.php'); $errors = array(); $page = array(); @@ -63,7 +64,9 @@ foreach( $output as $msg ) { $subject = getstring("mail.user.history.subject"); $body = getstring2("mail.user.history.body", array($thread['userName'],$history) ); -webim_mail($email, $webim_mailbox, $subject, $body); +$link = connect(); +webim_mail($email, $webim_mailbox, $subject, $body, $link); +mysql_close($link); setup_logo(); expand("styles", getchatstyle(), "mailsent.tpl"); diff --git a/src/messenger/webim/operator/history.php b/src/messenger/webim/operator/history.php index 2baa2585..fc26dd2e 100644 --- a/src/messenger/webim/operator/history.php +++ b/src/messenger/webim/operator/history.php @@ -42,31 +42,25 @@ if($query !== false) { while ($group = mysql_fetch_array($result, MYSQL_ASSOC)) { $groupName[$group['groupid']] = $group['vclocalname']; } - $page['groupName'] = $groupName; mysql_free_result($result); - - $result = mysql_query( - "select DISTINCT unix_timestamp(chatthread.dtmcreated) as created, ". + $page['groupName'] = $groupName; + + $escapedQuery = mysql_real_escape_string($query,$link); + select_with_pagintation("DISTINCT unix_timestamp(chatthread.dtmcreated) as created, ". "unix_timestamp(chatthread.dtmmodified) as modified, chatthread.threadid, ". "chatthread.remote, chatthread.agentName, chatthread.userName, groupid, ". - "messageCount as size ". - "from chatthread, chatmessage ". - "where chatmessage.threadid = chatthread.threadid and ". - "((chatthread.userName LIKE '%%$query%%') or ". - " (chatmessage.tmessage LIKE '%%$query%%'))". - "order by created DESC", $link) - or die(' Query failed: ' .mysql_error().": ".$query); - - $foundThreads = array(); - while ($thread = mysql_fetch_array($result, MYSQL_ASSOC)) { - $foundThreads[] = $thread; - } - - mysql_free_result($result); + "messageCount as size", + "chatthread, chatmessage", + array( + "chatmessage.threadid = chatthread.threadid", + "((chatthread.userName LIKE '%%$escapedQuery%%') or (chatmessage.tmessage LIKE '%%$escapedQuery%%'))" + ), + "order by created DESC", + "DISTINCT chatthread.dtmcreated", $link); + mysql_close($link); $page['formq'] = topage($query); - setup_pagination($foundThreads); } else { setup_empty_pagination(); } diff --git a/src/messenger/webim/operator/restore.php b/src/messenger/webim/operator/restore.php index d739035c..30249f04 100644 --- a/src/messenger/webim/operator/restore.php +++ b/src/messenger/webim/operator/restore.php @@ -22,6 +22,7 @@ require_once('../libs/common.php'); require_once('../libs/operator.php'); require_once('../libs/settings.php'); +require_once('../libs/notify.php'); $errors = array(); $page = array('version' => $version); @@ -46,11 +47,10 @@ if (isset($_POST['loginoremail'])) { $link = connect(); $query = "update chatoperator set dtmrestore = CURRENT_TIMESTAMP, vcrestoretoken = '$token' where operatorid = ".$torestore['operatorid']; perform_query($query, $link); + + $href = get_app_location(true,false)."/operator/resetpwd.php?id=".$torestore['operatorid']."&token=$token"; + webim_mail($email, $email, getstring("restore.mailsubj"), getstring2("restore.mailtext",array(get_operator_name($torestore), $href)), $link); mysql_close($link); - - $link = get_app_location(true,false)."/operator/resetpwd.php?id=".$torestore['operatorid']."&token=$token"; - - webim_mail($email, $email, getstring("restore.mailsubj"), getstring2("restore.mailtext",array(get_operator_name($torestore), $link))); $page['isdone'] = true; require('../view/restore.php');