/** * Save the ban to the database. * * Triggers {@link \Mibew\EventDispatcher\Events::BAN_CREATE} event. */ public function save() { $db = Database::getInstance(); if (!$this->id) { // This ban is new. $db->query("INSERT INTO {ban} (dtmcreated, dtmtill, address, comment) " . "VALUES (:created, :till, :address, :comment)", array(':created' => (int) $this->created, ':till' => (int) $this->till, ':address' => $this->address, ':comment' => $this->comment)); $this->id = $db->insertedId(); $args = array('ban' => $this); EventDispatcher::getInstance()->triggerEvent(Events::BAN_CREATE, $args); } else { // Get the original state of the ban for "update" event. $original_ban = Ban::load($this->id); // Update existing ban $db->query("UPDATE {ban} SET dtmtill = :till, address = :address, " . "comment = :comment WHERE banid = :id", array(':id' => $this->id, ':till' => (int) $this->till, ':address' => $this->address, ':comment' => $this->comment)); $args = array('ban' => $this, 'original_ban' => $original_ban); EventDispatcher::getInstance()->triggerEvent(Events::BAN_UPDATE, $args); } }
/** * Processes submitting of the form which is generated in * {@link \Mibew\Controller\BanController::showEditFormAction()} method. * * @param Request $request Incoming request. * @return string Rendered page content. * @throws NotFoundException If the ban with specified ID is not found in * the system. */ public function submitEditFormAction(Request $request) { csrf_check_token($request); $operator = $this->getOperator(); $errors = array(); $page = array('banId' => '', 'saved' => false); // Get form fields and validate them $ban_id = $request->attributes->getInt('ban_id'); $address = $request->request->get('address'); $days = $request->request->get('days'); $comment = $request->request->get('comment'); if (!$address) { $errors[] = no_field('Visitor\'s Address'); } if (!preg_match("/^\\d+\$/", $days)) { $errors[] = wrong_field('Days'); } if (!$comment) { $errors[] = no_field('Comment'); } // Check if the ban already exists in the database $existing_ban = Ban::loadByAddress($address); $ban_duplicate = !$ban_id && $existing_ban || $ban_id && $existing_ban && $ban_id != $existing_ban->id; if ($ban_duplicate) { $ban_url = $this->generateUrl('ban_edit', array('ban_id' => $existing_ban->id)); $errors[] = getlocal('The specified address is already in use. Click <a href="{1}">here</a> if you want to edit it.', array($address, $ban_url)); } if (count($errors) != 0) { $request->attributes->set('errors', $errors); // The form should be rebuild. Invoke appropriate action. return $this->showEditFormAction($request); } // Save ban into the database if (!$ban_id) { $ban = new Ban(); $ban->created = time(); } else { $ban = Ban::load($ban_id); if (!$ban) { throw new NotFoundException('The ban is not found.'); } } $ban->till = time() + $days * 24 * 60 * 60; $ban->address = $address; $ban->comment = $comment; $ban->save(); // Rerender the form page $page['saved'] = true; $page['address'] = $address; $page['title'] = getlocal('Block address'); $page = array_merge($page, prepare_menu($operator, false)); return $this->render('ban', $page); }
/** * 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); }