/** * Create collections starting from a specific id, and * running until we are at the end * * @var $startingPoint int * @var $cb callable */ public function createCollections($startingPoint, $cb) { $increment = 5000; /* * Create a faked parse search, so we can re-use existing infrastructure */ $parsedSearch = array('sortFields' => array(array('field' => 'id', 'direction' => 'asc')), 'additionalJoins' => array(), 'additionalTables' => array(), 'additionalFields' => array()); /* * Actually fetch the spots */ $svcProvSpotList = new Services_Providers_SpotList($this->_daoFactory->getSpotDao()); while (true) { if ($cb !== null) { $cb('start', $startingPoint, $increment); } // if /** * Get the current list of spots */ $parsedSearch['filter'] = ' (s.id > ' . (int) $startingPoint . ') ' . ' AND (s.collectionid IS NULL) ' . ' AND (s.stamp > 1290578400)' . ' AND (s.category <> 2) ' . ' AND (s.category <> 3) ' . ' AND (NOT ((s.category = 0) AND (s.subcatz = \'z3|\')))' . ' AND (NOT ((s.category = 0) AND (s.subcatz = \'z0|\') AND (s.subcatd LIKE \'%d6|%\')))' . ' AND (NOT ((s.category = 0) AND (s.subcatz = \'z0|\') AND (s.subcatd = \'d13|\')))'; # exclude music videos as well $dbSpotList = $svcProvSpotList->fetchSpotList(0, 0, $increment, $parsedSearch); $startingPoint += $increment; /* * Parse the spots and get an collection id for it */ $dbSpotList['list'] = $this->createCollectionsFromList($dbSpotList['list']); if (empty($dbSpotList['list'])) { if ($cb !== null) { $cb('finish', $startingPoint, $increment); } // if break; } // if /* * now update the database */ $dbConnection = $this->_daoFactory->getConnection(); $dbConnection->beginTransaction(); foreach ($dbSpotList['list'] as $spot) { $dbConnection->exec('UPDATE spots SET collectionid = :collectionid WHERE messageid = :messageid', array(':collectionid' => array($spot['collectionid'], PDO::PARAM_INT), ':messageid' => array($spot['messageid'], PDO::PARAM_INT))); } // foreach $dbConnection->commit(); if ($cb !== null) { $cb('finish', $startingPoint, $increment); } // if } // while }
/** * Search the spotweb database for a specific piece of information * * @param $outputtype */ function search($outputtype) { # Check users' permissions $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_perform_search, ''); $searchParams = array(); /** * Now determine what type of information we are searching for using sabnzbd */ if (($this->_params['t'] == "t" || $this->_params['t'] == "tvsearch") && $this->_params['rid'] != "") { # validate input if (!preg_match('/^[0-9]{1,6}$/', $this->_params['rid'])) { $this->showApiError(201); return; } # if /* * Actually retrieve the information from TVRage, based on the * tvrage passed by the API */ $svcMediaInfoTvrage = new Services_MediaInformation_Tvrage($this->_daoFactory, $this->_settings); $svcMediaInfoTvrage->setSearchid($this->_params['rid']); $tvRageInfo = $svcMediaInfoTvrage->retrieveInfo(); if (!$tvRageInfo->isValid()) { $this->showApiError(300); return; } # if /* * Try to parse the season parameter. This can be either in the form of S1, S01, 1, 2012, etc. * we try to standardize all these types of season definitions into one format. */ $episodeSearch = ''; $seasonSearch = ''; if (preg_match('/^[sS][0-9]{1,2}$/', $this->_params['season']) || preg_match('/^[0-9]{1,4}$/', $this->_params['season'])) { /* * Did we get passed a 4 digit season (most likely a year), or a * two digit season? */ if (strlen($this->_params['season']) < 3) { if (is_numeric($this->_params['season'])) { $seasonSearch = 'S' . str_pad($this->_params['season'], 2, "0", STR_PAD_LEFT); } else { $seasonSearch = $this->_params['season']; } # else } else { $seasonSearch = $this->_params['season'] . ' '; } # else } elseif ($this->_params['season'] != "") { $this->showApiError(201); return; } # if /* * And try to add an episode parameter, basically the same set of rules * as for the season */ if (preg_match('/^[eE][0-9]{1,2}$/', $this->_params['ep']) || preg_match('/^[0-9]{1,2}$/', $this->_params['ep']) || preg_match('/^[0-9]{1,2}\\/[0-9]{1,2}$/', $this->_params['ep'])) { if (is_numeric($this->_params['ep'])) { $episodeSearch .= 'E' . str_pad($this->_params['ep'], 2, "0", STR_PAD_LEFT); } else { $episodeSearch .= $this->_params['ep']; } # else } elseif ($this->_params['ep'] != "") { $this->showApiError(201); return; } else { // Complete season search, add wildcard character to season $seasonSearch .= '*'; // and search for the text 'Season ' ... $searchParams['value'][] = "Titel:=:OR:+\"" . $tvRageInfo->getTitle() . "\" +\"Season " . (int) $this->_params['season'] . "\""; } # else /* * The + operator is supported both by PostgreSQL and MySQL's FTS * * We search both for S04E17 and S04 E17 (with a space) */ $searchParams['value'][] = "Titel:=:OR:+\"" . $tvRageInfo->getTitle() . "\" +" . $seasonSearch . $episodeSearch; if (!empty($episodeSearch)) { $searchParams['value'][] = "Titel:=:OR:+\"" . $tvRageInfo->getTitle() . "\" +" . $seasonSearch . ' +' . $episodeSearch; } # if } elseif ($this->_params['t'] == "music") { if (empty($this->_params['artist']) && empty($this->_params['cat'])) { $this->_params['cat'] = 3000; } else { $searchParams['value'][] = "Titel:=:DEF:\"" . $this->_params['artist'] . "\""; } # if } elseif ($this->_params['t'] == "m" || $this->_params['t'] == "movie") { # validate input if ($this->_params['imdbid'] == "") { $this->showApiError(200); return; } elseif (!preg_match('/^[0-9]{1,8}$/', $this->_params['imdbid'])) { $this->showApiError(201); return; } # if /* * Actually retrieve the information from imdb, based on the * imdbid passed by the API */ $svcMediaInfoImdb = new Services_MediaInformation_Imdb($this->_daoFactory, $this->_settings); $svcMediaInfoImdb->setSearchid($this->_params['imdbid']); $imdbInfo = $svcMediaInfoImdb->retrieveInfo(); if (!$imdbInfo->isValid()) { $this->showApiError(300); return; } # if /* Extract the release date from the IMDB info page */ if ($imdbInfo->getReleaseYear() != null) { $movieReleaseDate = '+(' . $imdbInfo->getReleaseYear() . ')'; } else { $movieReleaseDate = ''; } # else /* * Add movie title to the query */ $searchParams['value'][] = "Titel:=:OR:+\"" . $imdbInfo->getTitle() . "\" " . $movieReleaseDate; // imdb sometimes returns the title translated, if so, pass the original title as well if ($imdbInfo->getAlternateTitle() != null) { $searchParams['value'][] = "Title:=:OR:+\"" . $imdbInfo->getAlternateTitle() . "\" " . $movieReleaseDate; } # if } elseif (!empty($this->_params['q'])) { $searchTerm = str_replace(" ", " +", $this->_params['q']); $searchParams['value'][] = "Titel:=:OR:+" . $searchTerm; } # elseif /* * When a user added a maximum age for queries, convert it to * a Spotweb query as well */ if ($this->_params['maxage'] != "" && is_numeric($this->_params['maxage'])) { $searchParams['value'][] = "date:>:DEF:-" . $this->_params['maxage'] . "days"; } # if /* * We combine the "newznabapi" categories, with a custom extension for * categories so we can filter deeper than the newznab API can per default */ $tmpCat = array(); foreach (explode(",", $this->_params['cat']) as $category) { $tmpCat[] = $this->nabcat2spotcat($category); } # foreach $searchParams['tree'] = implode(",", $tmpCat) . ',' . $this->_params['spotcat']; /* * Do not retrieve spots with a filesize of zero (these are very old spots, * which have no NZB linked to it) as they are useless for a API consumer */ $searchParams['value'][] = "filesize:>:DEF:0"; /* * Gather the preference of the results per page and use it in this * system as well when no value is explicitly provided */ if (!empty($this->_params['limit']) && is_numeric($this->_params['limit']) && $this->_params['limit'] < 500) { $limit = $this->_params['limit']; } else { $limit = $this->_currentSession['user']['prefs']['perpage']; } # else if (!empty($this->_params['offset']) && is_numeric($this->_params['offset'])) { $pageNr = $this->_params['offset']; } else { $pageNr = 0; } # else /* * We get a bunch of query parameters, so now change this to the actual * search query the user requested including the required sorting */ $svcUserFilter = new Services_User_Filters($this->_daoFactory, $this->_settings); $svcSearchQp = new Services_Search_QueryParser($this->_daoFactory->getConnection()); $parsedSearch = $svcSearchQp->filterToQuery($searchParams, array('field' => 'stamp', 'direction' => 'DESC'), $this->_currentSession, $svcUserFilter->getIndexFilter($this->_currentSession['user']['userid'])); /* * Actually fetch the spots, we always perform * this action even when the watchlist is editted */ $svcProvSpotList = new Services_Providers_SpotList($this->_daoFactory->getSpotDao()); $spotsTmp = $svcProvSpotList->fetchSpotList($this->_currentSession['user']['userid'], $pageNr, $limit, $parsedSearch); $this->showResults($spotsTmp, $pageNr * $limit, $outputtype); }
function render() { # Make sure the proper permissions are met $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spotdetail, ''); $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spots_index, ''); $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_rssfeed, ''); $nzbhandling = $this->_currentSession['user']['prefs']['nzbhandling']; # Don't allow the RSS feed to be cached $this->sendExpireHeaders(true); /* * Transform the query parameters to a list of filters, fields, * sortings, etc. */ $svcUserFilter = new Services_User_Filters($this->_daoFactory, $this->_settings); $svcSearchQp = new Services_Search_QueryParser($this->_daoFactory->getConnection()); $parsedSearch = $svcSearchQp->filterToQuery($this->_params['search'], array('field' => $this->_params['sortby'], 'direction' => $this->_params['sortdir']), $this->_currentSession, $svcUserFilter->getIndexFilter($this->_currentSession['user']['userid'])); /* * Actually fetch the spots */ $pageNr = $this->_params['page']; $svcProvSpotList = new Services_Providers_SpotList($this->_daoFactory->getSpotDao()); $spotsTmp = $svcProvSpotList->fetchSpotList($this->_currentSession['user']['userid'], $pageNr, $this->_currentSession['user']['prefs']['perpage'], $parsedSearch); # Create an XML document for RSS $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; $rss = $doc->createElement('rss'); $rss->setAttribute('version', '2.0'); $rss->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom'); $doc->appendChild($rss); $atomSelfLink = $doc->createElementNS('http://www.w3.org/2005/Atom', 'atom10:link'); $atomSelfLink->setAttribute('href', html_entity_decode($this->_tplHelper->makeSelfUrl("full"))); $atomSelfLink->setAttribute('rel', 'self'); $atomSelfLink->setAttribute('type', 'application/rss+xml'); $channel = $doc->createElement('channel'); $channel->appendChild($doc->createElement('generator', 'Spotweb v' . SPOTWEB_VERSION)); $channel->appendChild($doc->createElement('language', 'nl')); $channel->appendChild($doc->createElement('title', 'Spotweb')); $channel->appendChild($doc->createElement('description', 'Spotweb RSS Feed')); $channel->appendChild($doc->createElement('link', $this->_tplHelper->makeBaseUrl("full"))); $channel->appendChild($atomSelfLink); $channel->appendChild($doc->createElement('webMaster', $this->_currentSession['user']['mail'] . ' (' . $this->_currentSession['user']['firstname'] . ' ' . $this->_currentSession['user']['lastname'] . ')')); $channel->appendChild($doc->createElement('pubDate', date('r'))); $rss->appendChild($channel); # Retrieve full spots so we can show images for spots etc. foreach ($spotsTmp['list'] as $spotHeaders) { try { $spot = $this->_tplHelper->getFullSpot($spotHeaders['messageid'], false); /* * We supress the error by using this ugly operator simply because the library * sometimes gives an notice and we cannot be bothered to fix it, but it does * give an incorrect and unusable RSS feed */ $spot = @$this->_tplHelper->formatSpot($spot); $title = str_replace(array('<', '>', '&'), array('<', '>', '&'), $spot['title']); $poster = empty($spot['spotterid']) ? $spot['poster'] : $spot['poster'] . " (" . $spot['spotterid'] . ")"; $guid = $doc->createElement('guid', $spot['messageid']); $guid->setAttribute('isPermaLink', 'false'); $description = $doc->createElement('description'); $descriptionCdata = $doc->createCDATASection($spot['description'] . '<br /><font color="#ca0000">Door: ' . $poster . '</font>'); $description->appendChild($descriptionCdata); $item = $doc->createElement('item'); $item->appendChild($doc->createElement('title', $title)); $item->appendChild($guid); $item->appendChild($doc->createElement('link', $this->_tplHelper->makeBaseUrl("full") . '?page=getspot&messageid=' . urlencode($spot['messageid']) . $this->_tplHelper->makeApiRequestString())); $item->appendChild($description); $item->appendChild($doc->createElement('author', $spot['messageid'] . ' (' . $poster . ')')); $item->appendChild($doc->createElement('pubDate', date('r', $spot['stamp']))); $item->appendChild($doc->createElement('category', SpotCategories::HeadCat2Desc($spot['category']) . ': ' . SpotCategories::Cat2ShortDesc($spot['category'], $spot['subcata']))); $enclosure = $doc->createElement('enclosure'); $enclosure->setAttribute('url', html_entity_decode($this->_tplHelper->makeNzbUrl($spot))); $enclosure->setAttribute('length', $spot['filesize']); switch ($nzbhandling['prepare_action']) { case 'zip': $enclosure->setAttribute('type', 'application/zip'); break; default: $enclosure->setAttribute('type', 'application/x-nzb'); } # switch $item->appendChild($enclosure); $channel->appendChild($item); } catch (Exception $x) { // Article not found. ignore. } # catch } # foreach # Output XML $this->sendContentTypeHeader('rss'); echo $doc->saveXML(); }
function render() { SpotTiming::start(__CLASS__ . '::' . __FUNCTION__); # Give an page title $this->_pageTitle = _("overview"); /* * Make sure the user has the appropriate permissions */ $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_view_spots_index, ''); /* * When the user wants to perform a search, it needs specific search rights * as well */ if (!empty($this->_params['search'])) { $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_perform_search, ''); } # if /* * We get a bunch of query parameters, so now change this to the actual * search query the user requested including the required sorting */ $svcUserFilter = new Services_User_Filters($this->_daoFactory, $this->_settings); $svcSearchQp = new Services_Search_QueryParser($this->_daoFactory->getConnection()); $parsedSearch = $svcSearchQp->filterToQuery($this->_params['search'], array('field' => $this->_params['sortby'], 'direction' => $this->_params['sortdir']), $this->_currentSession, $svcUserFilter->getIndexFilter($this->_currentSession['user']['userid'])); /* * If any specific action was chosen, we perform that as well */ if (isset($parsedSearch['filterValueList'][0]['fieldname']) && $parsedSearch['filterValueList'][0]['fieldname'] == "Watch") { # Make sure the appropriate permissions are set $this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_keep_own_watchlist, ''); $svcSpotStateListDao = new Services_Actions_SpotStateList($this->_daoFactory->getSpotStateListDao()); switch ($this->_action) { case 'remove': $svcSpotStateListDao->removeFromWatchList($this->_params['messageid'], $this->_currentSession['user']['userid']); $spotsNotifications = new SpotNotifications($this->_daoFactory, $this->_settings, $this->_currentSession); $spotsNotifications->sendWatchlistHandled($this->_action, $this->_params['messageid']); break; case 'add': $svcSpotStateListDao->addToWatchList($this->_params['messageid'], $this->_currentSession['user']['userid']); $spotsNotifications = new SpotNotifications($this->_daoFactory, $this->_settings, $this->_currentSession); $spotsNotifications->sendWatchlistHandled($this->_action, $this->_params['messageid']); break; default: } # switch } # if /* * Get the offset from the URL, if none given, we default to zero */ $pageNr = $this->_params['pagenr']; /* * Actually fetch the spots, we always perform * this action even when the watchlist is editted */ $svcProvSpotList = new Services_Providers_SpotList($this->_daoFactory->getSpotDao()); $spotsTmp = $svcProvSpotList->fetchSpotList($this->_currentSession['user']['userid'], $pageNr, $this->_currentSession['user']['prefs']['perpage'], $parsedSearch); /* * If we are on the first page, we want to pass '-1' as the previous page, * so the templates can deduce we are on the first page. * * If there are no more spots, make sure we don't show * the nextpage link */ if ($spotsTmp['hasmore']) { $nextPage = $pageNr + 1; } else { $nextPage = -1; } # else $prevPage = max($pageNr - 1, -1); #- display stuff -# $this->template('spots', array('spots' => $spotsTmp['list'], 'quicklinks' => $this->_settings->get('quicklinks'), 'filters' => $svcUserFilter->getFilterList($this->_currentSession['user']['userid'], 'filter'), 'nextPage' => $nextPage, 'prevPage' => $prevPage, 'parsedsearch' => $parsedSearch, 'data' => $this->_params['data'])); SpotTiming::stop(__CLASS__ . '::' . __FUNCTION__); }