Exemple #1
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^author[=|:](.*)\$/i", $event->term, $matches)) {
         $char = $matches[1];
         $event->add_querylet(new Querylet("Author = :author_char", array("author_char" => $char)));
     }
 }
Exemple #2
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^(.+)tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9]+)\$/i", $event->term, $matches)) {
         global $database;
         $type = $matches[1];
         $cmp = ltrim($matches[2], ":") ?: "=";
         $count = $matches[3];
         $types = $database->get_col('SELECT category FROM image_tag_categories');
         if (in_array($type, $types)) {
             $event->add_querylet(new Querylet("EXISTS (\n\t\t\t\t\t    SELECT 1\n\t\t\t\t\t    FROM image_tags it\n\t\t\t\t\t    LEFT JOIN tags t ON it.tag_id = t.id\n\t\t\t\t\t    WHERE images.id = it.image_id\n\t\t\t\t\t    GROUP BY image_id\n\t\t\t\t\t    HAVING SUM(CASE WHEN t.tag LIKE '{$type}:%' THEN 1 ELSE 0 END) {$cmp} {$count}\n\t\t\t\t\t)"));
         }
     }
 }
Exemple #3
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^parent[=|:]([0-9]+|any|none)\$/", $event->term, $matches)) {
         $parentID = $matches[1];
         if (preg_match("/^(any|none)\$/", $parentID)) {
             $not = $parentID == "any" ? "NOT" : "";
             $event->add_querylet(new Querylet("images.parent_id IS {$not} NULL"));
         } else {
             $event->add_querylet(new Querylet("images.parent_id = :pid", array("pid" => $parentID)));
         }
     } else {
         if (preg_match("/^child[=|:](any|none)\$/", $event->term, $matches)) {
             $not = $matches[1] == "any" ? "=" : "!=";
             $event->add_querylet(new Querylet("images.has_children {$not} TRUE"));
         }
     }
 }
Exemple #4
0
 /**
  * @param SearchTermParseEvent $event
  */
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     global $user;
     $matches = array();
     if (preg_match("/^(poster|user)[=|:](.*)\$/i", $event->term, $matches)) {
         $duser = User::by_name($matches[2]);
         if (!is_null($duser)) {
             $user_id = $duser->id;
         } else {
             $user_id = -1;
         }
         $event->add_querylet(new Querylet("images.owner_id = {$user_id}"));
     } else {
         if (preg_match("/^(poster|user)_id[=|:]([0-9]+)\$/i", $event->term, $matches)) {
             $user_id = int_escape($matches[2]);
             $event->add_querylet(new Querylet("images.owner_id = {$user_id}"));
         } else {
             if ($user->can("view_ip") && preg_match("/^(poster|user)_ip[=|:]([0-9\\.]+)\$/i", $event->term, $matches)) {
                 $user_ip = $matches[2];
                 // FIXME: ip_escape?
                 $event->add_querylet(new Querylet("images.owner_ip = '{$user_ip}'"));
             }
         }
     }
 }
Exemple #5
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^pool[=|:]([0-9]+|any|none)\$/i", $event->term, $matches)) {
         $poolID = $matches[1];
         if (preg_match("/^(any|none)\$/", $poolID)) {
             $not = $poolID == "none" ? "NOT" : "";
             $event->add_querylet(new Querylet("images.id {$not} IN (SELECT DISTINCT image_id FROM pool_images)"));
         } else {
             $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = {$poolID})"));
         }
     } else {
         if (preg_match("/^pool_by_name[=|:](.*)\$/i", $event->term, $matches)) {
             $poolTitle = str_replace("_", " ", $matches[1]);
             $pool = $this->get_single_pool_from_title($poolTitle);
             $poolID = 0;
             if ($pool) {
                 $poolID = $pool['id'];
             }
             $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = {$poolID})"));
         }
     }
 }
Exemple #6
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     global $user;
     $matches = array();
     if (is_null($event->term) && $this->no_rating_query($event->context)) {
         $set = Ratings::privs_to_sql(Ratings::get_user_privs($user));
         $event->add_querylet(new Querylet("rating IN ({$set})"));
     }
     if (preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))\$/D", strtolower($event->term), $matches)) {
         $ratings = $matches[1] ? $matches[1] : $matches[2][0];
         $ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
         $set = "'" . join("', '", $ratings) . "'";
         $event->add_querylet(new Querylet("rating IN ({$set})"));
     }
 }
Exemple #7
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/comments(<|>|<=|>=|=)(\\d+)/", $event->term, $matches)) {
         $cmp = $matches[1];
         $comments = $matches[2];
         $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM comments GROUP BY image_id HAVING count(image_id) {$cmp} {$comments})"));
     } else {
         if (preg_match("/commented_by=(.*)/i", $event->term, $matches)) {
             global $database;
             $user = User::by_name($matches[1]);
             if (!is_null($user)) {
                 $user_id = $user->id;
             } else {
                 $user_id = -1;
             }
             $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = {$user_id})"));
         } else {
             if (preg_match("/commented_by_userid=([0-9]+)/i", $event->term, $matches)) {
                 $user_id = int_escape($matches[1]);
                 $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = {$user_id})"));
             }
         }
     }
 }
Exemple #8
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^note[=|:](.*)\$/i", $event->term, $matches)) {
         $notes = int_escape($matches[1]);
         $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE note = {$notes})"));
     } else {
         if (preg_match("/^notes([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+)%/i", $event->term, $matches)) {
             $cmp = ltrim($matches[1], ":") ?: "=";
             $notes = $matches[2];
             $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes {$cmp} {$notes})"));
         } else {
             if (preg_match("/^notes_by[=|:](.*)\$/i", $event->term, $matches)) {
                 $user = User::by_name($matches[1]);
                 if (!is_null($user)) {
                     $user_id = $user->id;
                 } else {
                     $user_id = -1;
                 }
                 $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = {$user_id})"));
             } else {
                 if (preg_match("/^notes_by_userno[=|:](\\d+)\$/i", $event->term, $matches)) {
                     $user_id = int_escape($matches[1]);
                     $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = {$user_id})"));
                 }
             }
         }
     }
 }
Exemple #9
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^comments([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+)\$/i", $event->term, $matches)) {
         $cmp = ltrim($matches[1], ":") ?: "=";
         $comments = $matches[2];
         $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM comments GROUP BY image_id HAVING count(image_id) {$cmp} {$comments})"));
     } else {
         if (preg_match("/^commented_by[=|:](.*)\$/i", $event->term, $matches)) {
             $user = User::by_name($matches[1]);
             if (!is_null($user)) {
                 $user_id = $user->id;
             } else {
                 $user_id = -1;
             }
             $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = {$user_id})"));
         } else {
             if (preg_match("/^commented_by_userno[=|:]([0-9]+)\$/i", $event->term, $matches)) {
                 $user_id = int_escape($matches[1]);
                 $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = {$user_id})"));
             }
         }
     }
 }
Exemple #10
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     // check for tags first as tag based searches are more common.
     if (preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+)\$/i", $event->term, $matches)) {
         $cmp = ltrim($matches[1], ":") ?: "=";
         $tags = $matches[2];
         $event->add_querylet(new Querylet('images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) ' . $cmp . ' ' . $tags . ')'));
     } else {
         if (preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+):(\\d+)\$/i", $event->term, $matches)) {
             $cmp = preg_replace('/^:/', '=', $matches[1]);
             $args = array("width{$this->stpen}" => int_escape($matches[2]), "height{$this->stpen}" => int_escape($matches[3]));
             $event->add_querylet(new Querylet("width / height {$cmp} :width{$this->stpen} / :height{$this->stpen}", $args));
         } else {
             if (preg_match("/^(filesize|id)([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+[kmg]?b?)\$/i", $event->term, $matches)) {
                 $col = $matches[1];
                 $cmp = ltrim($matches[2], ":") ?: "=";
                 $val = parse_shorthand_int($matches[3]);
                 $event->add_querylet(new Querylet("images.{$col} {$cmp} :val{$this->stpen}", array("val{$this->stpen}" => $val)));
             } else {
                 if (preg_match("/^(hash|md5)[=|:]([0-9a-fA-F]*)\$/i", $event->term, $matches)) {
                     $hash = strtolower($matches[2]);
                     $event->add_querylet(new Querylet('images.hash = :hash', array("hash" => $hash)));
                 } else {
                     if (preg_match("/^(filetype|ext)[=|:]([a-zA-Z0-9]*)\$/i", $event->term, $matches)) {
                         $ext = strtolower($matches[2]);
                         $event->add_querylet(new Querylet('images.ext = :ext', array("ext" => $ext)));
                     } else {
                         if (preg_match("/^(filename|name)[=|:]([a-zA-Z0-9]*)\$/i", $event->term, $matches)) {
                             $filename = strtolower($matches[2]);
                             $event->add_querylet(new Querylet("images.filename LIKE :filename{$this->stpen}", array("filename{$this->stpen}" => "%{$filename}%")));
                         } else {
                             if (preg_match("/^(source)[=|:](.*)\$/i", $event->term, $matches)) {
                                 $source = strtolower($matches[2]);
                                 if (preg_match("/^(any|none)\$/i", $source)) {
                                     $not = $source == "any" ? "NOT" : "";
                                     $event->add_querylet(new Querylet("images.source IS {$not} NULL"));
                                 } else {
                                     $event->add_querylet(new Querylet('images.source LIKE :src', array("src" => "%{$source}%")));
                                 }
                             } else {
                                 if (preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)\$/i", $event->term, $matches)) {
                                     $cmp = ltrim($matches[1], ":") ?: "=";
                                     $val = $matches[2];
                                     $event->add_querylet(new Querylet("images.posted {$cmp} :posted{$this->stpen}", array("posted{$this->stpen}" => $val)));
                                 } else {
                                     if (preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+)x(\\d+)\$/i", $event->term, $matches)) {
                                         $cmp = ltrim($matches[1], ":") ?: "=";
                                         $args = array("width{$this->stpen}" => int_escape($matches[2]), "height{$this->stpen}" => int_escape($matches[3]));
                                         $event->add_querylet(new Querylet("width {$cmp} :width{$this->stpen} AND height {$cmp} :height{$this->stpen}", $args));
                                     } else {
                                         if (preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+)\$/i", $event->term, $matches)) {
                                             $cmp = ltrim($matches[1], ":") ?: "=";
                                             $event->add_querylet(new Querylet("width {$cmp} :width{$this->stpen}", array("width{$this->stpen}" => int_escape($matches[2]))));
                                         } else {
                                             if (preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\\d+)\$/i", $event->term, $matches)) {
                                                 $cmp = ltrim($matches[1], ":") ?: "=";
                                                 $event->add_querylet(new Querylet("height {$cmp} :height{$this->stpen}", array("height{$this->stpen}" => int_escape($matches[2]))));
                                             } else {
                                                 if (preg_match("/^order[=|:](id|width|height|filesize|filename)[_]?(desc|asc)?\$/i", $event->term, $matches)) {
                                                     global $order_sql;
                                                     $ord = strtolower($matches[1]);
                                                     $default_order_for_column = preg_match("/^(id|filename)\$/", $matches[1]) ? "ASC" : "DESC";
                                                     $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column;
                                                     $order_sql = "images.{$ord} {$sort}";
                                                     $event->add_querylet(new Querylet("1=1"));
                                                     //small hack to avoid metatag being treated as normal tag
                                                 } else {
                                                     if (preg_match("/^order[=|:]random[_]([0-9]{1,4})\$/i", $event->term, $matches)) {
                                                         global $order_sql;
                                                         //order[=|:]random requires a seed to avoid duplicates
                                                         //since the tag can't be changed during the parseevent, we instead generate the seed during submit using js
                                                         $seed = $matches[1];
                                                         $order_sql = "RAND({$seed})";
                                                         $event->add_querylet(new Querylet("1=1"));
                                                         //small hack to avoid metatag being treated as normal tag
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->stpen++;
 }
Exemple #11
0
 public function onSearchTermParse(SearchTermParseEvent $event)
 {
     $matches = array();
     if (preg_match("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\\d+)\$/i", $event->term, $matches)) {
         $cmp = ltrim($matches[1], ":") ?: "=";
         $score = $matches[2];
         $event->add_querylet(new Querylet("numeric_score {$cmp} {$score}"));
     } else {
         if (preg_match("/^upvoted_by[=|:](.*)\$/i", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)", array("ns_user_id" => $duser->id)));
         } else {
             if (preg_match("/^downvoted_by[=|:](.*)\$/i", $event->term, $matches)) {
                 $duser = User::by_name($matches[1]);
                 if (is_null($duser)) {
                     throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
                 }
                 $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", array("ns_user_id" => $duser->id)));
             } else {
                 if (preg_match("/^upvoted_by_id[=|:](\\d+)\$/i", $event->term, $matches)) {
                     $iid = int_escape($matches[1]);
                     $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)", array("ns_user_id" => $iid)));
                 } else {
                     if (preg_match("/^downvoted_by_id[=|:](\\d+)\$/i", $event->term, $matches)) {
                         $iid = int_escape($matches[1]);
                         $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", array("ns_user_id" => $iid)));
                     } else {
                         if (preg_match("/^order[=|:](numeric_)?(score)[_]?(desc|asc)?\$/i", $event->term, $matches)) {
                             $default_order_for_column = "DESC";
                             $sort = isset($matches[3]) ? strtoupper($matches[3]) : $default_order_for_column;
                             Image::$order_sql = "images.numeric_score {$sort}";
                             $event->add_querylet(new Querylet("1=1"));
                             //small hack to avoid metatag being treated as normal tag
                         }
                     }
                 }
             }
         }
     }
 }