TableName = TABLE_PREFIX . 'SpamControl'; } /** * Initializes helper for concrete item * * @param int $resource_id * @param string $data_type * @param int $expiration * @param Array $list_resource_ids * @access public */ public function InitHelper($resource_id, $data_type, $expiration, $list_resource_ids = Array ()) { $this->DataType = $data_type; $this->ResourceId = $resource_id; $this->ListResourceIDs = $list_resource_ids ? $list_resource_ids : Array ($resource_id); if ( preg_match('/(.*):(.*)/', $expiration, $regs) ) { $delay_value = $this->Application->ConfigValue($regs[1]); $delay_interval = $this->Application->ConfigValue($regs[2]); $expiration = $delay_value * $delay_interval; } $this->Expiration = time() + $expiration; } /** * Returns WHERE clause that identified each spam control record * * @param bool $as_array return result as array, not string * * @return string * @access protected */ protected function GetKeyClause($as_array = false) { $user_id = $this->Application->RecallVar('user_id'); if ( $user_id == 0 ) { $user_id = USER_GUEST; } $keys = Array ( 'IPaddress' => $this->Application->getClientIp(), 'PortalUserId' => $user_id, 'DataType' => $this->DataType, ); if ( $as_array ) { $keys['ItemResourceId'] = $this->ResourceId; return $keys; } $ret = ''; foreach ($keys as $field_name => $field_value) { $ret .= '(' . $field_name . ' = ' . $this->Conn->qstr($field_value) . ') AND '; } $ret .= '(ItemResourceId IN (' . implode(',', $this->ListResourceIDs) . '))'; return $ret; } /** * Allows to add current item in spam control * * @return void * @access public */ public function AddToSpamControl() { $fields_hash = $this->GetKeyClause(true); $fields_hash['Expire'] = $this->Expiration; $this->Conn->doInsert($fields_hash, $this->TableName); // create empty expiration cache for given data type in case, when InSpamControl method wasn't called if ( !isset($this->ExpirationCache[$this->DataType]) ) { $this->ExpirationCache[$this->DataType] = Array (); } $this->ExpirationCache[$this->DataType][$this->ResourceId] = $this->Expiration; } /** * Allows to check if current item is in spam control * * @return bool * @access public */ public function InSpamControl() { if ( !array_key_exists($this->DataType, $this->ExpirationCache) ) { $key_clause = $this->GetKeyClause(); $sql = 'SELECT Expire, ItemResourceId FROM ' . $this->TableName . ' WHERE ' . $key_clause; $this->ExpirationCache[$this->DataType] = $this->Conn->GetCol($sql, 'ItemResourceId'); } else { $key_clause = ''; } $cache =& $this->ExpirationCache[$this->DataType]; $expires = array_key_exists($this->ResourceId, $cache) ? $cache[$this->ResourceId] : false; if ( $expires && $expires < time() ) { // spam control record is expired $sql = 'DELETE FROM ' . $this->TableName . ' WHERE ' . $key_clause; $this->Conn->Query($sql); return false; } return $expires ? true : false; } }