/**
  * Search for releases by anidb id. Used by API/Sickbeard.
  */
 public function searchbyAnidbId($anidbID, $epno = '', $offset = 0, $limit = 100, $name = '', $maxage = -1)
 {
     $s = new Sites();
     $site = $s->get();
     if ($site->sphinxenabled) {
         $sphinx = new Sphinx();
         $results = $sphinx->searchbyAnidbId($anidbID, $epno, $offset, $limit, $name, $maxage, array(), true);
         if (is_array($results)) {
             return $results;
         }
     }
     $db = new DB();
     $anidbID = $anidbID > -1 ? sprintf(" AND anidbID = %d ", $anidbID) : '';
     $epno = is_numeric($epno) ? sprintf(" AND releases.episode LIKE '%s' ", $db->escapeString('%' . $epno . '%')) : '';
     //
     // 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++;
             }
         }
     }
     $maxage = $maxage > 0 ? sprintf(" and postdate > now() - interval %d day ", $maxage) : '';
     $sql = sprintf("SELECT releases.*, concat(cp.title, ' > ', c.title)\r\n\t\t\tAS category_name, concat(cp.ID, ',', c.ID) AS category_ids, groups.name AS group_name, rn.ID AS nfoID\r\n\t\t\tFROM releases LEFT OUTER JOIN category c ON c.ID = releases.categoryID LEFT OUTER JOIN groups ON groups.ID = releases.groupID\r\n\t\t\tLEFT 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\r\n\t\t\tWHERE releases.passwordstatus <= (select value from site where setting='showpasswordedrelease') %s %s %s %s ORDER BY postdate desc LIMIT %d, %d ", $anidbID, $epno, $searchsql, $maxage, $offset, $limit);
     $orderpos = strpos($sql, "ORDER BY");
     $wherepos = strpos($sql, "WHERE");
     $sqlcount = "SELECT count(releases.ID) AS num FROM releases " . 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;
 }