diff --git a/core/database.class.php b/core/database.class.php index 0217de1b..2f16c4aa 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -9,12 +9,13 @@ class Querylet { var $sql; var $variables; - public function querylet($sql, $variables=array()) { + public function Querylet($sql, $variables=array()) { $this->sql = $sql; $this->variables = $variables; } public function append($querylet) { + assert(!is_null($querylet)); $this->sql .= $querylet->sql; $this->variables = array_merge($this->variables, $querylet->variables); } @@ -157,41 +158,10 @@ class Database { $term = $this->resolve_alias($term); - $matches = array(); - if(preg_match("/size(<|>|<=|>=|=)(\d+)x(\d+)/", $term, $matches)) { - $cmp = $matches[1]; - $args = array(int_escape($matches[2]), int_escape($matches[3])); - $img_search->append(new Querylet("AND (width $cmp ? AND height $cmp ?)", $args)); - } - else if(preg_match("/ratio(<|>|<=|>=|=)(\d+):(\d+)/", $term, $matches)) { - $cmp = $matches[1]; - $args = array(int_escape($matches[2]), int_escape($matches[3])); - $img_search->append(new Querylet("AND (width / height $cmp ? / ?)", $args)); - } - else if(preg_match("/(filesize|id)(<|>|<=|>=|=)(\d+[kmg]?b?)/i", $term, $matches)) { - $col = $matches[1]; - $cmp = $matches[2]; - $val = parse_shorthand_int($matches[3]); - $img_search->append(new Querylet("AND (images.$col $cmp $val)")); - } - else if(preg_match("/(poster|user)=(.*)/i", $term, $matches)) { - global $database; - $user = $database->get_user_by_name($matches[2]); - if(!is_null($user)) { - $user_id = $user->id; - } - else { - $user_id = -1; - } - $img_search->append(new Querylet("AND (images.owner_id = $user_id)")); - } - else if(preg_match("/(hash=|md5:)([0-9a-fA-F]*)/i", $term, $matches)) { - $hash = strtolower($matches[2]); - $img_search->append(new Querylet("AND (images.hash = '$hash')")); - } - else if(preg_match("/(filetype|ext)=([a-zA-Z0-9]*)/i", $term, $matches)) { - $ext = strtolower($matches[2]); - $img_search->append(new Querylet("AND (images.ext = '$ext')")); + $stpe = new SearchTermParseEvent($term); + send_event($stpe); + if($stpe->is_querylet_set()) { + $img_search->append($stpe->get_querylet()); } else { $term = str_replace("*", "%", $term); diff --git a/core/event.class.php b/core/event.class.php index 1bd47edb..2715d6d5 100644 --- a/core/event.class.php +++ b/core/event.class.php @@ -244,4 +244,31 @@ class ThumbnailGenerationEvent extends Event { $this->type = $type; } } + + +/* + * SearchTermParseEvent: + * Signal that a search term needs parsing + */ +class SearchTermParseEvent extends Event { + var $term = null; + var $querylet = null; + + public function SearchTermParseEvent($term) { + assert(!is_null($term)); + $this->term = $term; + } + + public function is_querylet_set() { + return !is_null($this->querylet); + } + + public function get_querylet() { + return $this->querylet; + } + + public function set_querylet($q) { + $this->querylet = $q; + } +} ?> diff --git a/ext/index/main.php b/ext/index/main.php index b5c88aef..58834b33 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -79,6 +79,45 @@ class Index extends Extension { $event->panel->add_block($sb); } + + if(is_a($event, 'SearchTermParseEvent')) { + $matches = array(); + if(preg_match("/size(<|>|<=|>=|=)(\d+)x(\d+)/", $event->term, $matches)) { + $cmp = $matches[1]; + $args = array(int_escape($matches[2]), int_escape($matches[3])); + $event->set_querylet(new Querylet("AND (width $cmp ? AND height $cmp ?)", $args)); + } + else if(preg_match("/ratio(<|>|<=|>=|=)(\d+):(\d+)/", $event->term, $matches)) { + $cmp = $matches[1]; + $args = array(int_escape($matches[2]), int_escape($matches[3])); + $event->set_querylet(new Querylet("AND (width / height $cmp ? / ?)", $args)); + } + else if(preg_match("/(filesize|id)(<|>|<=|>=|=)(\d+[kmg]?b?)/i", $event->term, $matches)) { + $col = $matches[1]; + $cmp = $matches[2]; + $val = parse_shorthand_int($matches[3]); + $event->set_querylet(new Querylet("AND (images.$col $cmp $val)")); + } + else if(preg_match("/(poster|user)=(.*)/i", $event->term, $matches)) { + global $database; + $user = $database->get_user_by_name($matches[2]); + if(!is_null($user)) { + $user_id = $user->id; + } + else { + $user_id = -1; + } + $event->set_querylet(new Querylet("AND (images.owner_id = $user_id)")); + } + else if(preg_match("/(hash=|md5:)([0-9a-fA-F]*)/i", $event->term, $matches)) { + $hash = strtolower($matches[2]); + $event->set_querylet(new Querylet("AND (images.hash = '$hash')")); + } + else if(preg_match("/(filetype|ext)=([a-zA-Z0-9]*)/i", $event->term, $matches)) { + $ext = strtolower($matches[2]); + $event->set_querylet(new Querylet("AND (images.ext = '$ext')")); + } + } } } add_event_listener(new Index()); diff --git a/index.php b/index.php index 420de3ca..ed60063c 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@