From f54acb3ae57f968d5fd1a27e1cbf71cf5970d7b0 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Tue, 24 Jul 2012 16:00:58 +0000 Subject: [PATCH] Fixed bug with placeholders and sql functions --- src/messenger/webim/libs/chat.php | 82 +++++++++++++++++------- src/messenger/webim/libs/operator.php | 39 ++++++----- src/messenger/webim/operator/history.php | 6 +- src/messenger/webim/operator/update.php | 4 +- 4 files changed, 85 insertions(+), 46 deletions(-) diff --git a/src/messenger/webim/libs/chat.php b/src/messenger/webim/libs/chat.php index a5fa9d80..9adf11ad 100644 --- a/src/messenger/webim/libs/chat.php +++ b/src/messenger/webim/libs/chat.php @@ -511,29 +511,27 @@ function setup_chatview_for_operator($thread, $operator) $page['frequency'] = Settings::get('updatefrequency_chat'); } -function update_thread_access($threadid, $params) -{ - $db = Database::getInstance(); - $clause = ""; - $values = array(); - foreach ($params as $k => $v) { - if (strlen($clause) > 0) - $clause .= ", "; - $clause .= $k . "=?"; - $values[] = $v; - } - $values[] = $threadid; - - $db->query( - "update {chatthread} set {$clause} where threadid = ?", - $values - ); -} - +/** + * Pings the chat thread. Updates 'lastpinguser' (or 'lastpingagent') to current timestamp. + * Sends system messages when operator or user was seen more than $connection_timeout seconds ago + * + * @global int $kind_for_agent + * @global int $state_queue + * @global int $state_loading + * @global int $state_chatting + * @global int $state_waiting + * @global int $kind_conn + * @global int $connection_timeout + * @param array $thread Thread's array + * @param boolean $isuser true for user and false for operator + * @param boolean $istyping true if user (or agent) is typing + */ function ping_thread($thread, $isuser, $istyping) { global $kind_for_agent, $state_queue, $state_loading, $state_chatting, $state_waiting, $kind_conn, $connection_timeout; + $db = Database::getInstance(); + $params = array(($isuser ? "lastpinguser" : "lastpingagent") => "CURRENT_TIMESTAMP", ($isuser ? "userTyping" : "agentTyping") => ($istyping ? "1" : "0")); @@ -562,19 +560,48 @@ function ping_thread($thread, $isuser, $istyping) } } - update_thread_access($thread['threadid'], $params); + $clause = ""; + $values = array(); + foreach ($params as $k => $v) { + if (strlen($clause) > 0) { + $clause .= ", "; + } + if (($k == 'lastpinguser' || $k == 'lastpingagent') && $v == 'CURRENT_TIMESTAMP') { + $clause .= $k . " = CURRENT_TIMESTAMP"; + }else{ + $clause .= $k . "=?"; + $values[] = $v; + } + } + $values[] = $thread['threadid']; + + $db->query( + "update {chatthread} set {$clause} where threadid = ?", + $values + ); } function commit_thread($threadid, $params) { $db = Database::getInstance(); + $timestamp_allowed_for = array( + 'dtmcreated', + 'dtmchatstarted', + 'dtmmodified', + 'lastpinguser', + 'lastpingagent' + ); $query = "update {chatthread} t " . "set lrevision = ?, dtmmodified = CURRENT_TIMESTAMP"; $values = array(next_revision()); foreach ($params as $k => $v) { - $query .= ", " . $k . "=?"; - $values[] = $v; + if (in_array($k, $timestamp_allowed_for) && strcasecmp($v,'CURRENT_TIMESTAMP')) { + $query .= ", " . $k . " = CURRENT_TIMESTAMP"; + } else { + $query .= ", " . $k . "=?"; + $values[] = $v; + } } $query .= " where threadid = ?"; $values[] = $threadid; @@ -598,12 +625,21 @@ function close_thread($thread, $isuser) { global $state_closed, $kind_events; + $db = Database::getInstance(); + list($message_count) = $db->query( + "SELECT COUNT(*) FROM {chatmessage} WHERE {chatmessage}.threadid = ? AND ikind = 1", + array($thread['threadid']), + array( + 'return_rows' => Database::RETURN_ONE_ROW, + 'fetch_type' => Database::FETCH_NUM + ) + ); if ($thread['istate'] != $state_closed) { commit_thread( $thread['threadid'], array( 'istate' => $state_closed, - 'messageCount' => "(SELECT COUNT(*) FROM {chatmessage} WHERE {chatmessage}.threadid = t.threadid AND ikind = 1)" + 'messageCount' => $message_count ) ); } diff --git a/src/messenger/webim/libs/operator.php b/src/messenger/webim/libs/operator.php index 97498a82..a6631aa1 100644 --- a/src/messenger/webim/libs/operator.php +++ b/src/messenger/webim/libs/operator.php @@ -183,21 +183,25 @@ function operator_is_disabled($operator) function update_operator($operatorid, $login, $email, $password, $localename, $commonname) { $db = Database::getInstance(); + $values = array( + ':login' => $login, + ':localname' => $localename, + ':commonname' => $commonname, + ':email' => $email, + ':jabbername' => '', + ':operatorid' => $operatorid + ); + if ($password) { + $values[':password'] = md5($password); + } $db->query( "update {chatoperator} set vclogin = :login, " . ($password ? " vcpassword=:password, " : "") . "vclocalename = :localname, vccommonname = :commonname, " . "vcemail = :email, vcjabbername= :jabbername " . "where operatorid = :operatorid", - array( - ':login' => $login, - ':password' => $password, - ':localname' => $localename, - ':commonname' => $commonname, - ':email' => $email, - ':jabbername' => '', - ':operatorid' => $operatorid - ) + $values + ); } @@ -544,6 +548,7 @@ function get_groups_($checkaway, $operator, $order = NULL) $orderby = "iweight, vclocalname"; } + $values = array(); $query = "select {chatgroup}.groupid as groupid, {chatgroup}.parent as parent, vclocalname, vclocaldescription, iweight" . ", (SELECT count(*) from {chatgroupoperator} where {chatgroup}.groupid = " . "{chatgroupoperator}.groupid) as inumofagents" . @@ -558,17 +563,17 @@ function get_groups_($checkaway, $operator, $order = NULL) "and {chatgroupoperator}.operatorid = {chatoperator}.operatorid) as ilastseenaway" : "" ) . - " from {chatgroup} " . - ($operator - ? ", (select distinct parent from {chatgroup}, {chatgroupoperator} " . + " from {chatgroup} "; + if ($operator) { + $query .= ", (select distinct parent from {chatgroup}, {chatgroupoperator} " . "where {chatgroup}.groupid = {chatgroupoperator}.groupid and {chatgroupoperator}.operatorid = ?) i " . - "where {chatgroup}.groupid = i.parent or {chatgroup}.parent = i.parent " - : "" - ) . - " order by " . $orderby; + "where {chatgroup}.groupid = i.parent or {chatgroup}.parent = i.parent "; + $values[] = $operator['operatorid']; + } + $query .= " order by " . $orderby; $groups = $db->query( $query, - array($operator['operatorid']), + $values, array('return_rows' => Database::RETURN_ALL_ROWS) ); return get_sorted_child_groups_($groups); diff --git a/src/messenger/webim/operator/history.php b/src/messenger/webim/operator/history.php index f5652689..3c0a6bb7 100644 --- a/src/messenger/webim/operator/history.php +++ b/src/messenger/webim/operator/history.php @@ -48,9 +48,7 @@ if ($query !== false) { $page['groupName'] = $groupName; $values = array( - ':query' => "%{$escapedQuery}%", - ':kind_user' => $kind_user, - ':kind_agent' => $kind_agent + ':query' => "%{$escapedQuery}%" ); $searchConditions = array(); @@ -58,6 +56,8 @@ if ($query !== false) { $searchConditions[] = "({chatmessage}.tmessage LIKE :query" . ($searchInSystemMessages?'':" AND ({chatmessage}.ikind = :kind_user OR {chatmessage}.ikind = :kind_agent)") . ")"; + $values[':kind_user'] = $kind_user; + $values[':kind_agent'] = $kind_agent; } if ($searchType == 'operator' || $searchType == 'all') { $searchConditions[] = "({chatthread}.agentName LIKE :query)"; diff --git a/src/messenger/webim/operator/update.php b/src/messenger/webim/operator/update.php index 8640ec31..cc71a711 100644 --- a/src/messenger/webim/operator/update.php +++ b/src/messenger/webim/operator/update.php @@ -135,9 +135,7 @@ function print_pending_threads($groupids, $since) "ORDER BY threadid"; $rows = $db->query( $query, - array( - ':since' => $since - ), + array(':since' => $since), array('return_rows' => Database::RETURN_ALL_ROWS) );