/**
  * Search for releases by imdbid/movieinfo. Used by API/Couchpotato.
  */
 public function searchbyImdbId($imdbId, $offset = 0, $limit = 100, $name = "", $cat = array(-1), $genre = "", $maxage = -1)
 {
     $s = new Sites();
     $site = $s->get();
     if ($site->sphinxenabled) {
         $sphinx = new Sphinx();
         $results = $sphinx->searchbyImdbId($imdbId, $offset, $limit, $name, $cat, $genre, $maxage, array(), true);
         if (is_array($results)) {
             return $results;
         }
     }
     $db = new DB();
     if ($imdbId != "-1" && is_numeric($imdbId)) {
         //pad id with zeros just in case
         $imdbId = str_pad($imdbId, 7, "0", STR_PAD_LEFT);
         $imdbId = sprintf(" and releases.imdbID = %d ", $imdbId);
     } else {
         $imdbId = "";
     }
     //
     // if the query starts with a ^ it indicates the search is looking for items which start with the term
     // still do the fulltext match, but mandate that all items returned must start with the provided word
     //
     $words = explode(" ", $name);
     $searchsql = "";
     $intwordcount = 0;
     if (count($words) > 0) {
         foreach ($words as $word) {
             if ($word != "") {
                 //
                 // see if the first word had a caret, which indicates search must start with term
                 //
                 if ($intwordcount == 0 && strpos($word, "^") === 0) {
                     $searchsql .= sprintf(" and releases.searchname like %s", $db->escapeString(substr($word, 1) . "%"));
                 } elseif (substr($word, 0, 2) == '--') {
                     $searchsql .= sprintf(" and releases.searchname not like %s", $db->escapeString("%" . substr($word, 2) . "%"));
                 } else {
                     $searchsql .= sprintf(" and releases.searchname like %s", $db->escapeString("%" . $word . "%"));
                 }
                 $intwordcount++;
             }
         }
     }
     $catsrch = "";
     if (count($cat) > 0 && $cat[0] != -1) {
         $catsrch = " and (";
         foreach ($cat as $category) {
             if ($category != -1) {
                 $categ = new Category();
                 if ($categ->isParent($category)) {
                     $children = $categ->getChildren($category);
                     $chlist = "-99";
                     foreach ($children as $child) {
                         $chlist .= ", " . $child["ID"];
                     }
                     if ($chlist != "-99") {
                         $catsrch .= " releases.categoryID in (" . $chlist . ") or ";
                     }
                 } else {
                     $catsrch .= sprintf(" releases.categoryID = %d or ", $category);
                 }
             }
         }
         $catsrch .= "1=2 )";
     }
     if ($maxage > 0) {
         $maxage = sprintf(" and releases.postdate > now() - interval %d day ", $maxage);
     } else {
         $maxage = "";
     }
     if ($genre != "") {
         $genre = sprintf(" and movieinfo.genre like %s", $db->escapeString("%" . $genre . "%"));
     }
     $sql = sprintf("select releases.*, movieinfo.title as moi_title, movieinfo.tagline as moi_tagline, movieinfo.rating as moi_rating, movieinfo.plot as moi_plot, movieinfo.year as moi_year, movieinfo.genre as moi_genre, movieinfo.director as moi_director, movieinfo.actors as moi_actors, movieinfo.cover as moi_cover, movieinfo.backdrop as moi_backdrop, concat(cp.title, ' > ', c.title) as category_name, concat(cp.ID, ',', c.ID) as category_ids, groups.name as group_name, rn.ID as nfoID from releases left outer join groups on groups.ID = releases.groupID left outer join category c on c.ID = releases.categoryID left outer join releasenfo rn on rn.releaseID = releases.ID and rn.nfo is not null left outer join category cp on cp.ID = c.parentID left outer join movieinfo on releases.imdbID = movieinfo.imdbID where releases.passwordstatus <= (select value from site where setting='showpasswordedrelease') %s %s %s %s %s order by postdate desc limit %d, %d ", $searchsql, $imdbId, $catsrch, $maxage, $genre, $offset, $limit);
     $orderpos = strpos($sql, "order by");
     $wherepos = strpos($sql, "where");
     $sqlcount = "select count(releases.ID) as num from releases left outer join movieinfo on releases.imdbID = movieinfo.imdbID " . substr($sql, $wherepos, $orderpos - $wherepos);
     $countres = $db->queryOneRow($sqlcount, true);
     $res = $db->query($sql, true);
     if (count($res) > 0) {
         $res[0]["_totalrows"] = $countres["num"];
     }
     return $res;
 }