/** * Close old invitations. * * Triggers {@link \Mibew\EventDispatcher\Events::INVITATION_IGNORE} event. */ function invitation_close_old() { // Run only one instance of cleaning process. $lock = new ProcessLock('invitations_close_old'); if ($lock->get()) { // Freeze the time for the whole cleaning process. $now = time(); $db = Database::getInstance(); // Remove links between visitors and invitations that will be closed. $db->query("UPDATE {sitevisitor} v, {thread} t SET " . "v.threadid = NULL " . "WHERE t.istate = :state_invited " . "AND t.invitationstate = :invitation_wait " . "AND (:now - t.dtmcreated) > :lifetime", array(':invitation_wait' => Thread::INVITATION_WAIT, ':state_invited' => Thread::STATE_INVITED, ':lifetime' => Settings::get('invitation_lifetime'), ':now' => $now)); // Get all invitations to close $threads = $db->query("SELECT * FROM {thread} " . "WHERE istate = :state_invited " . "AND invitationstate = :invitation_wait " . "AND (:now - dtmcreated) > :lifetime", array(':invitation_wait' => Thread::INVITATION_WAIT, ':state_invited' => Thread::STATE_INVITED, ':lifetime' => Settings::get('invitation_lifetime'), ':now' => $now), array('return_rows' => Database::RETURN_ALL_ROWS)); // Close the invitations foreach ($threads as $thread_info) { $thread = Thread::createFromDbInfo($thread_info); $thread->invitationState = Thread::INVITATION_IGNORED; $thread->state = Thread::STATE_CLOSED; $thread->closed = $now; $thread->save(); // Notify the operator about autoclosing $thread->postMessage(Thread::KIND_FOR_AGENT, getlocal('Visitor ignored invitation and it was closed automatically', null, $thread->locale, true)); $args = array('invitation' => $thread); EventDispatcher::getInstance()->triggerEvent(Events::INVITATION_IGNORE, $args); unset($thread); } // Release the lock $lock->release(); } }
/** * Return updated threads list. API function * * Triggers * {@link \Mibew\EventDispatcher\Events::USERS_UPDATE_THREADS_ALTER} event. * * @param array $args Associative array of arguments. It must contains the * following keys: * - 'agentId': Id of the agent related to users window * - 'revision': last revision number at client side * @return array Array of results. It contains the following keys: * - 'threads': array of threads changes */ protected function apiUpdateThreads($args) { $operator = $this->checkOperator($args['agentId']); $since = $args['revision']; // Get operator groups if (!isset($_SESSION[SESSION_PREFIX . "operatorgroups"])) { $_SESSION[SESSION_PREFIX . "operatorgroups"] = get_operator_groups_list($operator['operatorid']); } $group_ids = $_SESSION[SESSION_PREFIX . "operatorgroups"]; $db = Database::getInstance(); $query = "SELECT t.*, " . " g.vclocalname AS group_localname, " . " g.vccommonname AS group_commonname " . " FROM {thread} t LEFT OUTER JOIN {opgroup} g ON " . " t.groupid = g.groupid " . " WHERE t.lrevision > :since " . " AND t.istate <> " . Thread::STATE_INVITED . ($since == 0 ? " AND t.istate <> " . Thread::STATE_CLOSED . " AND t.istate <> " . Thread::STATE_LEFT : "") . (Settings::get('enablegroups') == '1' ? " AND (g.groupid is NULL" . ($group_ids ? " OR g.groupid IN ({$group_ids}) OR g.groupid IN " . "(SELECT parent FROM {opgroup} " . "WHERE groupid IN ({$group_ids})) " : "") . ") " : "") . " ORDER BY t.threadid"; $rows = $db->query($query, array(':since' => $since), array('return_rows' => Database::RETURN_ALL_ROWS)); $revision = $since; $threads = array(); foreach ($rows as $row) { // Create thread instance $thread = Thread::createFromDbInfo($row); // Calculate agent permissions $can_open = !($thread->state == Thread::STATE_CHATTING && $thread->agentId != $operator['operatorid'] && !is_capable(CAN_TAKEOVER, $operator)); $can_view = $thread->agentId != $operator['operatorid'] && $thread->nextAgent != $operator['operatorid'] && is_capable(CAN_VIEWTHREADS, $operator); $can_ban = Settings::get('enableban') == "1"; // Get ban info $ban = Settings::get('enableban') == "1" ? Ban::loadByAddress($thread->remote) : false; if ($ban !== false && !$ban->isExpired()) { $ban_info = array('id' => $ban->id, 'reason' => $ban->comment); } else { $ban_info = false; } // Get user name $user_name = get_user_name($thread->userName, $thread->remote, $thread->userId); // Get user ip if (preg_match("/(\\d+\\.\\d+\\.\\d+\\.\\d+)/", $thread->remote, $matches) != 0) { $user_ip = $matches[1]; } else { $user_ip = false; } // Get thread operartor name $next_agent = $thread->nextAgent != 0 ? operator_by_id($thread->nextAgent) : false; if ($next_agent) { $agent_name = get_operator_name($next_agent); } else { if ($thread->agentName) { $agent_name = $thread->agentName; } else { $group_name = get_group_name(array('vccommonname' => $row['group_commonname'], 'vclocalname' => $row['group_localname'])); if ($group_name) { $agent_name = '-' . $group_name . '-'; } else { $agent_name = '-'; } } } // Get first message $first_message = null; if ($thread->shownMessageId != 0) { $line = $db->query("SELECT tmessage FROM {message} WHERE messageid = ? LIMIT 1", array($thread->shownMessageId), array('return_rows' => Database::RETURN_ONE_ROW)); if ($line) { $first_message = preg_replace("/[\r\n\t]+/", " ", $line["tmessage"]); } } $threads[] = array('id' => $thread->id, 'token' => $thread->lastToken, 'userId' => $thread->userId, 'userName' => $user_name, 'userIp' => $user_ip, 'remote' => $thread->remote, 'userAgent' => get_user_agent_version($thread->userAgent), 'agentId' => $thread->agentId, 'agentName' => $agent_name, 'canOpen' => $can_open, 'canView' => $can_view, 'canBan' => $can_ban, 'ban' => $ban_info, 'state' => $thread->state, 'totalTime' => $thread->created, 'waitingTime' => $thread->modified, 'firstMessage' => $first_message); // Get max revision if ($thread->lastRevision > $revision) { $revision = $thread->lastRevision; } // Clean up unset($thread); } // Provide an ability to alter threads list $arguments = array('threads' => $threads); $dispatcher = EventDispatcher::getInstance(); $dispatcher->triggerEvent(Events::USERS_UPDATE_THREADS_ALTER, $arguments); // Send results back to the client. "array_values" function should be // used to avoid problems with JSON conversion. If there will be gaps in // keys (the keys are not serial) JSON Object will be produced instead // of an Array. return array('threads' => array_values($arguments['threads']), 'lastRevision' => $revision); }
/** * Generates a page with a user history. * * @param Request $request * @return string Rendered page content */ public function userAction(Request $request) { $operator = $this->getOperator(); $user_id = $request->attributes->get('user_id', ''); $page = array(); if (!empty($user_id)) { $db = Database::getInstance(); $query = "SELECT {thread}.* " . "FROM {thread} " . "WHERE userid=:user_id " . "AND (invitationstate = :invitation_accepted " . "OR invitationstate = :invitation_not_invited) " . "ORDER BY dtmcreated DESC"; $found = $db->query($query, array(':user_id' => $user_id, ':invitation_accepted' => Thread::INVITATION_ACCEPTED, ':invitation_not_invited' => Thread::INVITATION_NOT_INVITED), array('return_rows' => Database::RETURN_ALL_ROWS)); } else { $found = null; } $page = array_merge($page, prepare_menu($operator)); // Setup pagination $pagination = setup_pagination($found, 6); $page['pagination'] = $pagination['info']; $page['pagination.items'] = $pagination['items']; if (!empty($page['pagination.items'])) { foreach ($page['pagination.items'] as $key => $item) { $thread = Thread::createFromDbInfo($item); $page['pagination.items'][$key] = array('threadId' => $thread->id, 'userName' => $thread->userName, 'userAddress' => get_user_addr($thread->remote), 'agentName' => $thread->agentName, 'chatTime' => $thread->modified - $thread->created, 'chatCreated' => $thread->created); } } $page['title'] = getlocal("Visit history"); $page['menuid'] = "history"; return $this->render('history_user', $page); }
/** * Close all old threads that were not closed by some reasons. */ public static function closeOldThreads() { if (Settings::get('thread_lifetime') == 0) { // Threads live forever. return; } // We need to run only one instance of cleaning process. $lock = new ProcessLock('threads_close_old'); if ($lock->get()) { $query = "SELECT * FROM {thread} " . "WHERE istate <> :state_closed " . "AND istate <> :state_left " . "AND ABS(:now - dtmcreated) > :thread_lifetime " . "AND ( " . "( " . "lastpingagent <> 0 " . "AND lastpinguser <> 0 " . "AND ABS(:now - lastpinguser) > :thread_lifetime " . "AND ABS(:now - lastpingagent) > :thread_lifetime " . ") OR ( " . "lastpingagent = 0 " . "AND lastpinguser <> 0 " . "AND ABS(:now - lastpinguser) > :thread_lifetime " . ") OR ( " . "lastpinguser = 0 " . "AND lastpingagent <> 0 " . "AND ABS(:now - lastpingagent) > :thread_lifetime " . ") OR ( " . "lastpinguser = 0 " . "AND lastpingagent = 0 " . ") " . ")"; // Get appropriate threads $now = time(); $rows = Database::getInstance()->query($query, array(':now' => $now, ':state_closed' => self::STATE_CLOSED, ':state_left' => self::STATE_LEFT, ':thread_lifetime' => Settings::get('thread_lifetime')), array('return_rows' => Database::RETURN_ALL_ROWS)); // Perform the cleaning $revision = self::nextRevision(); foreach ($rows as $row) { $thread = Thread::createFromDbInfo($row); $thread->lastRevision = $revision; $thread->modified = $now; $thread->closed = $now; $thread->state = self::STATE_CLOSED; $thread->save(); unset($thread); } // Release the lock $lock->release(); } }