Skip to content
12 changes: 6 additions & 6 deletions items/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once("../db.php");
require_once("../util.php");
require_once('../SortHelper.php');
require_once('../FilterHelper.php');
require_once('../util/DB/DataFilter.php');
require_once("../User.php");
require_once("../Assert.php");
require_once("../modules/semver/semver.php");
Expand All @@ -30,7 +30,7 @@
$db_limit = "";
$db_order = '';

$filter = new FilterHelper($db_connection, DB_TABLE_ITEMS);
$filter = new DataFilter($_GET, DB_TABLE_ITEMS, $db_connection);

$filter->add(array('name' => 'type', 'type' => 'custom', 'coerce' => array('ItemType', 'getCode')));
$filter->add(array('name' => 'user', 'type' => 'binary')); # WARN: changes parameter to receive ID instead of name
Expand All @@ -47,7 +47,7 @@ function coerce_regex($value, $db_connection) {
return '"(^|;)' . $db_connection->real_escape_string($value) . '($|;)"';
}

# special filtering (post-MySQL), thus not handled by FilterHelper
# special filtering (post-MySQL), thus not handled by DataFilter
if (isset($_GET["version"]))
{
$version = strtolower($_GET["version"]);
Expand All @@ -74,7 +74,7 @@ function coerce_regex($value, $db_connection) {
}

if ($sort_by_version || count($semver_filters) > 0) {
$db_cond = $filter->evaluate($_GET);
$db_cond = $filter->evaluate();
SortHelper::PrepareSemverSorting(DB_TABLE_ITEMS, 'version', $db_cond, $semver_filters);
$db_join .= ($db_join ? ', ' : 'LEFT JOIN (') . '`semver_index`';
$db_join_on .= ($db_join_on ? ' AND ' : ' ON (') . '`' . DB_TABLE_ITEMS . '`.`version` = `semver_index`.`version`';
Expand All @@ -83,9 +83,9 @@ function coerce_regex($value, $db_connection) {
# These must defined below the call to SortHelper::PrepareSemverSorting() as it can not handle table joins
$filter->add(array('name' => 'version-min', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '>=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex')));
$filter->add(array('name' => 'version-max', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '<=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex')));
$db_cond = $filter->evaluate($_GET); # re-evaluate to include the latest filters
$db_cond = $filter->evaluate(); # re-evaluate to include the latest filters

# enable rating filters if necessary (filter with HAVING instead of WHERE, not currently supported by FilterHelper)
# enable rating filters if necessary (filter with HAVING instead of WHERE, not currently supported by DataFilter)
if ($get_rating = isset($_GET['rating']) || isset($_GET['rating-min']) || isset($_GET['rating-max']) || $sort_by_rating) {
$db_join .= ($db_join ? ', ' : 'LEFT JOIN (') . DB_TABLE_RATINGS;
$db_join_on .= ($db_join_on ? ' AND ' : ' ON (') . 'item = id';
Expand Down
10 changes: 5 additions & 5 deletions stdlib/candidates/Candidate.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once(dirname(__FILE__) . '/../../db.php');
require_once(dirname(__FILE__) . '/../../SortHelper.php');
require_once(dirname(__FILE__) . '/../../FilterHelper.php');
require_once(dirname(__FILE__) . '/../../util/DB/DataFilter.php');
require_once(dirname(__FILE__) . '/../../Assert.php');
require_once(dirname(__FILE__) . '/../../sql2array.php');
require_once(dirname(__FILE__) . '/../../config/stdlib.php');
Expand Down Expand Up @@ -143,7 +143,7 @@ public static function listCandidates($filters = array(), $sort = array()) {
$db_connection = db_ensure_connection();
$db_sort = SortHelper::getOrderClause($sort, array('date' => '`date`', 'approval' => '`approval`'));

$filter = new FilterHelper($db_connection, DB_TABLE_CANDIDATES);
$filter = new DataFilter($filters, DB_TABLE_CANDIDATES, $db_connection);

$filter->add(array('name' => 'item', 'type' => 'binary'));
$filter->add(array('name' => 'user', 'type' => 'binary'));
Expand All @@ -156,7 +156,7 @@ public static function listCandidates($filters = array(), $sort = array()) {

$filter->add(array('name' => 'owner', 'db-name' => 'user', 'type' => 'binary', 'db-table' => DB_TABLE_ITEMS, 'join-ref' => 'item', 'join-key' => 'id'));

$db_cond = $filter->evaluate($filters);
$db_cond = $filter->evaluate();
$db_join = $filter->evaluateJoins();

$db_query = 'SELECT ' . DB_TABLE_CANDIDATES . '.`id`, HEX(' . DB_TABLE_CANDIDATES. '.`item`) AS item FROM ' . DB_TABLE_CANDIDATES . $db_join . $db_cond . ' ' . $db_sort;
Expand All @@ -173,7 +173,7 @@ public static function listVotings($candidate, $filters = array(), $sort = array
}
$db_connection = db_ensure_connection();

$filter = new FilterHelper($db_connection, DB_TABLE_CANDIDATE_VOTING);
$filter = new DataFilter($filters, DB_TABLE_CANDIDATE_VOTING, $db_connection);
$filter->add(array('db-name' => 'candidate', 'value' => $candidate, 'type' => 'int'));

$filter->add(array('name' => 'user', 'type' => 'binary'));
Expand All @@ -184,7 +184,7 @@ public static function listVotings($candidate, $filters = array(), $sort = array
$filter->add(array('name' => 'voted-before', 'db-name' => 'date', 'operator' => '<'));
$filter->add(array('name' => 'voted-after', 'db-name' => 'date', 'operator' => '>'));

$db_cond = $filter->evaluate($filters);
$db_cond = $filter->evaluate();
$db_sort = SortHelper::getOrderClause($sort, array('date' => '`date`'));

$db_query = 'SELECT `candidate`, HEX(`user`) AS user, `accept`, `final`, `reason`, `date` FROM ' . DB_TABLE_CANDIDATE_VOTING . $db_cond . ' ' . $db_sort;
Expand Down
4 changes: 2 additions & 2 deletions stdlib/candidates/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
require_once('../../util.php');
require_once('../../Assert.php');
require_once('../../SortHelper.php');
require_once('../../FilterHelper.php');
require_once('../../util/DB/DataFilter.php');
require_once('Candidate.php');

try {
Assert::RequestMethod(Assert::REQUEST_METHOD_GET);

$content_type = get_preferred_mimetype(array('application/json', 'text/xml', 'application/xml'), 'application/json');

$filters = FilterHelper::FromParams(array('user', 'item', 'created', 'created-after', 'created-before', 'approved', 'owner'));
$filters = DataFilter::FromParams(array('user', 'item', 'created', 'created-after', 'created-before', 'approved', 'owner'));
$sort_list = SortHelper::getListFromParam(isset($_GET['sort']) ? $_GET['sort'] : '');
$candidates = Candidate::listCandidates($filters, $sort_list);

Expand Down
3 changes: 2 additions & 1 deletion stdlib/candidates/voting.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once('../../util.php');
require_once('../../Assert.php');
require_once('../../SortHelper.php');
require_once('../../util/DB/DataFilter.php');
require_once('../../User.php');
require_once('Candidate.php');
require_once('../StdlibPending.php');
Expand Down Expand Up @@ -48,7 +49,7 @@
Assert::GetParameters('id');
$content_type = get_preferred_mimetype(array('application/json', 'text/xml', 'application/xml'), 'application/json');

$filters = FilterHelper::FromParams(array('user', 'final', 'accept', 'voted', 'voted-after', 'voted-before'));
$filters = DataFilter::FromParams(array('user', 'final', 'accept', 'voted', 'voted-after', 'voted-before'));
$sort_list = SortHelper::getListFromParam(isset($_GET['sort']) ? $_GET['sort'] : '');

$votings = Candidate::listVotings($_GET['id'], $filters, $sort_list);
Expand Down
6 changes: 3 additions & 3 deletions stdlib/items.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require_once('../db.php');
require_once('../Assert.php');
require_once('../SortHelper.php');
require_once('../FilterHelper.php');
require_once('../util/DB/DataFilter.php');

try {
Assert::RequestMethod(Assert::REQUEST_METHOD_GET);
Expand All @@ -15,13 +15,13 @@
$db_sort = '';
$db_join = '';

$filter = new FilterHelper($db_connection, DB_TABLE_STDLIB);
$filter = new DataFilter($_GET, DB_TABLE_STDLIB, $db_connection);

$filter->add(array('name' => 'name', 'db-table' => DB_TABLE_ITEMS));
$filter->add(array('name' => 'user', 'type' => 'binary', 'db-table' => DB_TABLE_ITEMS));
$filter->add(array('name' => 'id', 'type' => 'binary', 'db-table' => DB_TABLE_ITEMS));

$db_cond = $filter->evaluate($_GET, ' AND ');
$db_cond = $filter->evaluate(' AND ');

if (isset($_GET['sort'])) {
$sort_list = SortHelper::getListFromParam($_GET['sort']);
Expand Down
6 changes: 3 additions & 3 deletions stdlib/releases/StdlibRelease.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once(dirname(__FILE__) . "/../../db.php");
require_once(dirname(__FILE__) . '/../../SortHelper.php');
require_once(dirname(__FILE__) . '/../../FilterHelper.php');
require_once(dirname(__FILE__) . '/../../util/DB/DataFilter.php');
require_once(dirname(__FILE__) . '/../../Assert.php');
require_once(dirname(__FILE__) . "/../Stdlib.php");
require_once(dirname(__FILE__) . "/../StdlibPending.php");
Expand Down Expand Up @@ -177,7 +177,7 @@ public static function ListReleases($published, $filters = array(), $sort = arra
$db_cond = ($t = self::get_publish_cond($published)) == NULL ? '' : " WHERE $t";
$db_connection = db_ensure_connection();

$filter = new FilterHelper($db_connection, DB_TABLE_STDLIB_RELEASES);
$filter = new DataFilter($filters, DB_TABLE_STDLIB_RELEASES, $db_connection);

$semver_filters = array();
foreach(array('version-min', 'version-max') AS $field) {
Expand All @@ -199,7 +199,7 @@ public static function ListReleases($published, $filters = array(), $sort = arra
# add these below semver preparation as it can not handle table joins
$filter->add(array('name' => 'version-min', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '>=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex')));
$filter->add(array('name' => 'version-max', 'db-name' => 'position', 'db-table' => 'semver_index', 'operator' => '<=', 'type' => 'custom', 'coerce' => array('SortHelper', 'RetrieveSemverIndex')));
$db_cond .= $filter->evaluate($filters, $db_cond ? ' AND ' : ' WHERE ');
$db_cond .= $filter->evaluate($db_cond ? ' AND ' : ' WHERE ');

# get all releases from DB
$db_query = "SELECT `release` FROM " . DB_TABLE_STDLIB_RELEASES . $db_join . $db_cond . $db_sort;
Expand Down
6 changes: 3 additions & 3 deletions users/Suspension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_once(dirname(__FILE__) . '/../db.php');
require_once(dirname(__FILE__) . '/../User.php');
require_once(dirname(__FILE__) . '/../SortHelper.php');
require_once(dirname(__FILE__) . '/../FilterHelper.php');
require_once(dirname(__FILE__) . '/../util/DB/DataFilter.php');
require_once(dirname(__FILE__) . '/../Assert.php');
require_once(dirname(__FILE__) . '/../sql2array.php');
require_once(dirname(__FILE__) . '/../modules/HttpException/HttpException.php');
Expand Down Expand Up @@ -62,7 +62,7 @@ public static function getSuspensionsById($id, $filters = array(), $sort = array
throw new HttpException(500, NULL, 'Must pass a valid array as suspension filter!');
}

$filter = new FilterHelper($db_connection, DB_TABLE_SUSPENSIONS);
$filter = new DataFilter($filters, DB_TABLE_SUSPENSIONS, $db_connection);

$filter->add(array('db-name' => 'user', 'value' => $id, 'type' => 'binary'));

Expand All @@ -88,7 +88,7 @@ public static function getSuspensionsById($id, $filters = array(), $sort = array
)
));

$db_cond = $filter->evaluate($filters);
$db_cond = $filter->evaluate();
$sort = SortHelper::getOrderClause($sort, array('created' => '`created`', 'expires' => '`expires`'));

$db_query = 'SELECT *, HEX(`user`) AS user FROM ' . DB_TABLE_SUSPENSIONS . $db_cond . $sort;
Expand Down
4 changes: 2 additions & 2 deletions users/suspensions/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once('../../modules/HttpException/HttpException.php');
require_once('../../util.php');
require_once('../../SortHelper.php');
require_once('../../FilterHelper.php');
require_once('../../util/DB/DataFilter.php');
require_once('../../User.php');
require_once('../Suspension.php');

Expand All @@ -29,7 +29,7 @@
# validate accept header of request
$content_type = get_preferred_mimetype(array('application/json', 'text/xml', 'application/xml'), 'application/json');

$filters = FilterHelper::FromParams(array('active', 'created', 'created-after', 'created-before', 'expires', 'expires-after', 'expires-before', 'infinite', 'restricted'));
$filters = DataFilter::FromParams(array('active', 'created', 'created-after', 'created-before', 'expires', 'expires-after', 'expires-before', 'infinite', 'restricted'));
$sort_list = SortHelper::getListFromParam(isset($_GET['sort']) ? $_GET['sort'] : '');

$suspensions = Suspension::getSuspensionsById($id, $filters, $sort_list);
Expand Down
62 changes: 53 additions & 9 deletions FilterHelper.php → util/DB/DataFilter.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
<?php
require_once(dirname(__FILE__) . '/modules/HttpException/HttpException.php');
require_once(dirname(__FILE__) . '/../../modules/HttpException/HttpException.php');

class FilterHelper {
class DataFilter {
private $filters = array();
private $source = NULL;
private $table = NULL;
private $connection = NULL;

/*
* Public class instance interface
*/
public function __construct($db_connection, $table) {
public function __construct($source, $table = NULL, $db_connection = NULL) {
$this->SetSource($source);
$this->connection = $db_connection;
$this->table = $table;
$this->setDefaultTable($table);
}

public function add($data) { #$name, $db_name = NULL, $method = 'GET', $op = '=', $default = NULL, $force = NULL) {
$this->filters[] = $data; #array('name' => $name, 'db-name' => $db_name, $method => 'GET', 'operator' => $op, 'default' => $default, 'force-value' => $force);
public function add($data) {
$this->filters[] = $data;
}

public function evaluate($source, $prefix = ' WHERE ') {
$db_cond = '';
public function define($data) {
return $this->add($data);
}

public function SetSource($source) {
$this->source = $source;
}

public function setDefaultTable($table) {
$this->table = $table;
}

public function evaluate($prefix = ' WHERE ', $db_connection = NULL) {
if ($db_connection !== NULL) {
$this->connection = $db_connection;
}
if ($this->connection === NULL) {
throw new HttpException(500, NULL, 'Must specify DB connection for filter!');
}
if ($this->table === NULL) {
throw new HttpException(500, NULL, 'Must specify DB table for filter!');
}

$db_cond = '';

foreach ($this->filters AS $filter) {

Expand Down Expand Up @@ -111,7 +136,9 @@ private function evaluateConditions($conditions, $filter) {
}
$key = '`' . (isset($data['db-table']) ? $data['db-table'] : $this->table) . '`.`' . (isset($data['db-name']) ? $data['db-name'] : $data['name']) . '`'; # the name is also used as column name if no other is specified
if (isset($data['db-function'])) {
$key = $data['db-function'] . '(' . $key . ')';
foreach ((array)$data['db-function'] AS $fn) {
$key = $fn . '(' . $key . ')';
}
}

# Get the value for comparison
Expand Down Expand Up @@ -261,5 +288,22 @@ public static function FromParams($filters, $source = NULL) {
}
return array_intersect_key($source, array_flip($filters));
}

public static function SimpleFilter($filters, $table = NULL) {
$f = new self($table);

foreach ($filters AS $name => $value) {
if (is_array($value)) {
$keys = array_keys($value);
$f->add(array('db-name' => $name, 'value' => $keys[0], 'type' => $value[$keys[0]]));
} else if (is_int($name)) {
$f->add(array('name' => $value));
} else {
$f->add(array('db-name' => $name, 'value' => $value));
}
}

return $f;
}
}
?>