_meta ) ) { $meta = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); $this->_meta = $meta->Load( 'occurrence_id = %d', array( $occurence->id ) ); } return $this->_meta; } /** * Returns allmeta data related to this event. * * @param object $occurence - Occurrence model instance. * @see \WSAL\MainWPExtension\Adapters\MySQL\ActiveRecord::LoadArray() * @return \WSAL\MainWPExtension\Adapters\MySQL\Meta[] */ public function GetMultiMeta( $occurence ) { if ( ! isset( $this->_meta ) ) { $meta = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); $this->_meta = $meta->LoadArray( 'occurrence_id = %d', array( $occurence->id ) ); } return $this->_meta; } /** * Loads a meta item given its name. * * @param object $occurence - Occurrence model instance. * @param string $name - Meta name. * @see \WSAL\MainWPExtension\Adapters\MySQL\ActiveRecord::Load() * @return \WSAL\MainWPExtension\Adapters\MySQL\Meta The meta item, be sure to checked if it was loaded successfully. */ public function GetNamedMeta( $occurence, $name ) { $meta = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); $this->_meta = $meta->Load( 'occurrence_id = %d AND name = %s', array( $occurence->id, $name ) ); return $this->_meta; } /** * Returns the first meta value from a given set of names. Useful when you have a mix of items that could provide a particular detail. * * @param object $occurence - Occurrence model instance. * @param array $names - List of meta names. * @return \WSAL\MainWPExtension\Adapters\MySQL\Meta The first meta item that exists. */ public function GetFirstNamedMeta( $occurence, $names ) { $meta = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); $query = '(' . str_repeat( 'name = %s OR ', count( $names ) ) . '0)'; $query = 'occurrence_id = %d AND ' . $query . ' ORDER BY name DESC LIMIT 1'; array_unshift( $names, $occurence->id ); // Prepend args with occurrence id. $this->_meta = $meta->Load( $query, $names ); return $meta->getModel()->LoadData( $this->_meta ); } /** * Returns newest unique occurrences. * * @param integer $limit Maximum limit. * @return \WSAL\MainWPExtension\Adapters\MySQL\Occurrence[] */ public static function GetNewestUnique( $limit = PHP_INT_MAX ) { $temp = new self(); return self::LoadMultiQuery(' SELECT *, COUNT(alert_id) as count FROM ( SELECT * FROM ' . $temp->GetTable() . ' ORDER BY created_on DESC ) AS temp_table GROUP BY alert_id LIMIT %d ', array( $limit ) ); } /** * Gets occurences of the same type by IP and Username within specified time frame. * * @param array $args - User arguments. * @return \WSAL\MainWPExtension\Adapters\MySQL\Occurrence[] */ public function CheckKnownUsers( $args = array() ) { $tt2 = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); return self::LoadMultiQuery( 'SELECT occurrence.* FROM `' . $this->GetTable() . '` occurrence INNER JOIN `' . $tt2->GetTable() . '` ipMeta on ipMeta.occurrence_id = occurrence.id and ipMeta.name = "ClientIP" and ipMeta.value = %s INNER JOIN `' . $tt2->GetTable() . '` usernameMeta on usernameMeta.occurrence_id = occurrence.id and usernameMeta.name = "Username" and usernameMeta.value = %s WHERE occurrence.alert_id = %d AND occurrence.site_id = %d AND (created_on BETWEEN %d AND %d) GROUP BY occurrence.id', $args ); } /** * Gets occurences of the same type by IP within specified time frame. * * @param array $args - User arguments. * @return \WSAL\MainWPExtension\Adapters\MySQL\Occurrence[] */ public function CheckUnKnownUsers( $args = array() ) { $tt2 = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); return self::LoadMultiQuery( 'SELECT occurrence.* FROM `' . $this->GetTable() . '` occurrence INNER JOIN `' . $tt2->GetTable() . '` ipMeta on ipMeta.occurrence_id = occurrence.id and ipMeta.name = "ClientIP" and ipMeta.value = %s WHERE occurrence.alert_id = %d AND occurrence.site_id = %d AND (created_on BETWEEN %d AND %d) GROUP BY occurrence.id', $args ); } /** * Gets occurences of the alert 1003. * * @param array $args - User arguments. * @return \WSAL\MainWPExtension\Adapters\MySQL\Occurrence[] */ public function check_alert_1003( $args = array() ) { return self::LoadMultiQuery( 'SELECT occurrence.* FROM `' . $this->GetTable() . '` occurrence WHERE (occurrence.alert_id = %d) AND (occurrence.site_id = %d) AND (occurrence.created_on BETWEEN %d AND %d) GROUP BY occurrence.id', $args ); } /** * Add conditions to the Query * * @param string $query - Query. */ protected function prepareOccurrenceQuery( $query ) { $search_query_parameters = array(); $search_conditions = array(); $conditions = $query->getConditions(); // BUG: not all conditions are occurence related. maybe it's just a field site_id. need seperate arrays. if ( ! empty( $conditions ) ) { $tmp = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); $s_where_clause = ''; foreach ( $conditions as $field => $value ) { if ( ! empty( $s_where_clause ) ) { $s_where_clause .= ' AND '; } $s_where_clause .= 'name = %s AND value = %s'; $search_query_parameters[] = $field; $search_query_parameters[] = $value; } $search_conditions[] = 'id IN ( SELECT DISTINCT occurrence_id FROM ' . $tmp->GetTable() . ' WHERE ' . $s_where_clause . ' )'; } // Do something with search query parameters and search conditions - give them to the query adapter? return $search_conditions; } /** * Gets occurrence by Post_id. * * @param int $post_id - Post ID. * @return \WSAL\MainWPExtension\Adapters\MySQL\Occurrence[] */ public function GetByPostID( $post_id ) { $tt2 = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); return self::LoadMultiQuery( 'SELECT occurrence.* FROM `' . $this->GetTable() . '`AS occurrence INNER JOIN `' . $tt2->GetTable() . '`AS postMeta ON postMeta.occurrence_id = occurrence.id and postMeta.name = "PostID" and postMeta.value = %d GROUP BY occurrence.id ORDER BY created_on DESC', array( $post_id ) ); } /** * Gets occurences of the same type by IP within specified time frame. * * @param array $args - Query Arguments. * @return \WSAL\MainWPExtension\Adapters\MySQL\Occurrence[] */ public function CheckAlert404( $args = array() ) { $tt2 = new \WSAL\MainWPExtension\Adapters\MySQL\Meta( $this->connection ); return self::LoadMultiQuery( 'SELECT occurrence.* FROM `' . $this->GetTable() . '` occurrence INNER JOIN `' . $tt2->GetTable() . '` ipMeta on ipMeta.occurrence_id = occurrence.id and ipMeta.name = "ClientIP" and ipMeta.value = %s INNER JOIN `' . $tt2->GetTable() . '` usernameMeta on usernameMeta.occurrence_id = occurrence.id and usernameMeta.name = "Username" and usernameMeta.value = %s WHERE occurrence.alert_id = %d AND occurrence.site_id = %d AND (created_on BETWEEN %d AND %d) GROUP BY occurrence.id', $args ); } }