TableName = TABLE_PREFIX.'SpamControl'; } /** * Initializes helper for concrete item * * @param int $resource_id * @param string $data_type * @param int $expiration */ function InitHelper($resource_id, $data_type, $expiration) { $this->ResourceId = $resource_id; $this->DataType = $data_type; 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 = adodb_mktime() + $expiration; } /** * Returns WHERE clause that identified each spam control record * * @param bool $as_array return result as array, not string * * @return string */ function GetKeyClause($as_array = false) { $user_id = $this->Application->RecallVar('user_id'); if ($user_id == 0) { $user_id = -2; } $keys = Array ( 'ItemResourceId' => $this->ResourceId, 'IPaddress' => $_SERVER['REMOTE_ADDR'], 'PortalUserId' => $user_id, 'DataType' => $this->DataType, ); if ($as_array) { return $keys; } $ret = ''; foreach ($keys as $field_name => $field_value) { $ret .= '('.$field_name.' = '.$this->Conn->qstr($field_value).') AND '; } return preg_replace('/(.*) AND $/', '\\1', $ret); } /** * Allows to add current item in spam control * */ function AddToSpamControl() { $fields_hash = $this->GetKeyClause(true); $fields_hash['Expire'] = $this->Expiration; $this->Conn->doInsert($fields_hash, $this->TableName); } /** * Allows to check if current item is in spam control * * @return bool */ function InSpamControl() { $key_clause = $this->GetKeyClause(); $sql = 'SELECT Expire FROM '.$this->TableName.' WHERE '.$key_clause; $expires = $this->Conn->GetOne($sql); if ($expires && $expires < adodb_mktime()) { // spam control record is expired $sql = 'DELETE FROM '.$this->TableName.' WHERE '.$key_clause; $this->Conn->Query($sql); return false; } return $expires ? true : false; } } ?>