| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286 |
- <?php
- declare(strict_types=1);
- /**
- * Controller to handle every feed actions.
- */
- class FreshRSS_feed_Controller extends FreshRSS_ActionController {
- /**
- * This action is called before every other action in that class. It is
- * the common boilerplate for every action. It is triggered by the
- * underlying framework.
- */
- #[\Override]
- public function firstAction(): void {
- if (!FreshRSS_Auth::hasAccess()) {
- $action = Minz_Request::actionName();
- $allow_anonymous_refresh = FreshRSS_Context::systemConf()->allow_anonymous_refresh;
- // Likely coming from bookmarklet, redirect to the login page
- if ($action === 'add') {
- Minz_Request::forward(['c' => 'auth', 'a' => 'login']);
- return;
- }
- if ($action !== 'actualize' || (!$allow_anonymous_refresh && !Minz_Request::tokenIsOk())) {
- Minz_Error::error(403);
- }
- }
- }
- /**
- * @param array<string,mixed> $attributes
- * @throws FreshRSS_AlreadySubscribed_Exception
- * @throws FreshRSS_BadUrl_Exception
- * @throws FreshRSS_Feed_Exception
- * @throws FreshRSS_FeedNotAdded_Exception
- * @throws Minz_FileNotExistException
- */
- public static function addFeed(string $url, string $title = '', int $cat_id = 0, string $new_cat_name = '',
- string $http_auth = '', array $attributes = [], int $kind = FreshRSS_Feed::KIND_RSS): FreshRSS_Feed {
- FreshRSS_UserDAO::touch();
- if (function_exists('set_time_limit')) {
- @set_time_limit(300);
- }
- $catDAO = FreshRSS_Factory::createCategoryDao();
- $url = trim($url);
- /** @var string|null $urlHooked */
- $urlHooked = Minz_ExtensionManager::callHook(Minz_HookType::CheckUrlBeforeAdd, $url);
- if ($urlHooked === null) {
- throw new FreshRSS_FeedNotAdded_Exception($url);
- }
- $url = $urlHooked;
- $cat = null;
- if ($cat_id > 0) {
- $cat = $catDAO->searchById($cat_id);
- }
- if ($cat === null && $new_cat_name != '') {
- $new_cat_id = $catDAO->addCategory(['name' => $new_cat_name]);
- $cat_id = $new_cat_id > 0 ? $new_cat_id : $cat_id;
- $cat = $catDAO->searchById($cat_id);
- }
- if ($cat === null) {
- $catDAO->checkDefault();
- }
- $feed = new FreshRSS_Feed($url); //Throws FreshRSS_BadUrl_Exception
- $title = trim($title);
- if ($title !== '') {
- $feed->_name($title);
- }
- $feed->_kind($kind);
- $feed->_attributes($attributes);
- $feed->_httpAuth($http_auth);
- if ($cat === null) {
- $feed->_categoryId(FreshRSS_CategoryDAO::DEFAULTCATEGORYID);
- } else {
- $feed->_category($cat);
- }
- switch ($kind) {
- case FreshRSS_Feed::KIND_RSS:
- case FreshRSS_Feed::KIND_RSS_FORCED:
- if ($feed->load(loadDetails: true) === null) { // Throws FreshRSS_Feed_Exception, Minz_FileNotExistException
- throw new FreshRSS_FeedNotAdded_Exception($url);
- }
- break;
- case FreshRSS_Feed::KIND_HTML_XPATH:
- case FreshRSS_Feed::KIND_XML_XPATH:
- $feed->_website($url);
- break;
- }
- $feedDAO = FreshRSS_Factory::createFeedDao();
- if ($feedDAO->searchByUrl($feed->url()) !== null) {
- throw new FreshRSS_AlreadySubscribed_Exception($url, $feed->name());
- }
- /** @var FreshRSS_Feed|null $feed */
- $feed = Minz_ExtensionManager::callHook(Minz_HookType::FeedBeforeInsert, $feed);
- if ($feed === null) {
- throw new FreshRSS_FeedNotAdded_Exception($url);
- }
- $id = $feedDAO->addFeedObject($feed);
- if (!$id) {
- // There was an error in database… we cannot say what here.
- throw new FreshRSS_FeedNotAdded_Exception($url);
- }
- $feed->_id($id);
- // Ok, feed has been added in database. Now we have to refresh entries.
- self::actualizeFeedsAndCommit($id, $url);
- return $feed;
- }
- /**
- * This action subscribes to a feed.
- *
- * It can be reached by both GET and POST requests.
- *
- * GET request displays a form to add and configure a feed.
- * Request parameter is:
- * - url_rss (default: false)
- *
- * POST request adds a feed in database.
- * Parameters are:
- * - url_rss (default: false)
- * - category (default: false)
- * - http_user (default: false)
- * - http_pass (default: false)
- * It tries to get website information from RSS feed.
- * If no category is given, feed is added to the default one.
- *
- * If url_rss is false, nothing happened.
- */
- public function addAction(): void {
- $url = Minz_Request::paramString('url_rss');
- if ($url === '') {
- // No url, do nothing
- Minz_Request::forward([
- 'c' => 'subscription',
- 'a' => 'index',
- ], true);
- }
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $url_redirect = [
- 'c' => 'subscription',
- 'a' => 'add',
- 'params' => [],
- ];
- $limits = FreshRSS_Context::systemConf()->limits;
- $this->view->feeds = $feedDAO->listFeeds();
- if (count($this->view->feeds) >= $limits['max_feeds']) {
- Minz_Request::bad(_t('feedback.sub.feed.over_max', $limits['max_feeds']), $url_redirect);
- }
- if (Minz_Request::isPost()) {
- $cat = Minz_Request::paramInt('category');
- // HTTP information are useful if feed is protected behind a
- // HTTP authentication
- $user = Minz_Request::paramString('http_user');
- $pass = Minz_Request::paramString('http_pass');
- $http_auth = '';
- if ($user != '' && $pass != '') { //TODO: Sanitize
- $http_auth = $user . ':' . $pass;
- }
- $cookie = Minz_Request::paramString('curl_params_cookie', plaintext: true);
- $cookie_file = Minz_Request::paramBoolean('curl_params_cookiefile');
- $max_redirs = Minz_Request::paramInt('curl_params_redirects');
- $useragent = Minz_Request::paramString('curl_params_useragent', plaintext: true);
- $proxy_address = Minz_Request::paramString('curl_params', plaintext: true);
- $proxy_type = Minz_Request::paramIntNull('proxy_type');
- $request_method = Minz_Request::paramString('curl_method', plaintext: true);
- $request_fields = Minz_Request::paramString('curl_fields', plaintext: true);
- $headers = Minz_Request::paramTextToArray('http_headers', plaintext: true);
- $opts = [];
- if ($proxy_type !== null) {
- $opts[CURLOPT_PROXYTYPE] = $proxy_type;
- }
- if ($proxy_address !== '') {
- $opts[CURLOPT_PROXY] = $proxy_address;
- }
- if ($cookie !== '') {
- $opts[CURLOPT_COOKIE] = $cookie;
- }
- if ($cookie_file) {
- // Pass empty cookie file name to enable the libcurl cookie engine
- // without reading any existing cookie data.
- $opts[CURLOPT_COOKIEFILE] = '';
- }
- if ($max_redirs !== 0) {
- $opts[CURLOPT_MAXREDIRS] = $max_redirs;
- $opts[CURLOPT_FOLLOWLOCATION] = 1;
- }
- if ($useragent !== '') {
- $opts[CURLOPT_USERAGENT] = $useragent;
- }
- if ($request_method === 'POST') {
- $opts[CURLOPT_POST] = true;
- if ($request_fields !== '') {
- $opts[CURLOPT_POSTFIELDS] = $request_fields;
- if (json_decode($request_fields, true) !== null) {
- $opts[CURLOPT_HTTPHEADER] = ['Content-Type: application/json'];
- }
- }
- }
- $headers = array_filter($headers, fn(string $header): bool => trim($header) !== '');
- if (!empty($headers)) {
- $opts[CURLOPT_HTTPHEADER] = array_merge($headers, $opts[CURLOPT_HTTPHEADER] ?? []);
- $opts[CURLOPT_HTTPHEADER] = array_unique($opts[CURLOPT_HTTPHEADER]);
- }
- $attributes = [
- 'curl_params' => empty($opts) ? null : $opts,
- ];
- $attributes['ssl_verify'] = Minz_Request::paramTernary('ssl_verify');
- $timeout = Minz_Request::paramInt('timeout');
- $attributes['timeout'] = $timeout > 0 ? $timeout : null;
- $feed_kind = Minz_Request::paramInt('feed_kind') ?: FreshRSS_Feed::KIND_RSS;
- if ($feed_kind === FreshRSS_Feed::KIND_HTML_XPATH || $feed_kind === FreshRSS_Feed::KIND_XML_XPATH) {
- $xPathSettings = [];
- if (Minz_Request::paramString('xPathFeedTitle') !== '') {
- $xPathSettings['feedTitle'] = Minz_Request::paramString('xPathFeedTitle', true);
- }
- if (Minz_Request::paramString('xPathItem') !== '') {
- $xPathSettings['item'] = Minz_Request::paramString('xPathItem', true);
- }
- if (Minz_Request::paramString('xPathItemTitle') !== '') {
- $xPathSettings['itemTitle'] = Minz_Request::paramString('xPathItemTitle', true);
- }
- if (Minz_Request::paramString('xPathItemContent') !== '') {
- $xPathSettings['itemContent'] = Minz_Request::paramString('xPathItemContent', true);
- }
- if (Minz_Request::paramString('xPathItemUri') !== '') {
- $xPathSettings['itemUri'] = Minz_Request::paramString('xPathItemUri', true);
- }
- if (Minz_Request::paramString('xPathItemAuthor') !== '') {
- $xPathSettings['itemAuthor'] = Minz_Request::paramString('xPathItemAuthor', true);
- }
- if (Minz_Request::paramString('xPathItemTimestamp') !== '') {
- $xPathSettings['itemTimestamp'] = Minz_Request::paramString('xPathItemTimestamp', true);
- }
- if (Minz_Request::paramString('xPathItemTimeFormat') !== '') {
- $xPathSettings['itemTimeFormat'] = Minz_Request::paramString('xPathItemTimeFormat', true);
- }
- if (Minz_Request::paramString('xPathItemThumbnail') !== '') {
- $xPathSettings['itemThumbnail'] = Minz_Request::paramString('xPathItemThumbnail', true);
- }
- if (Minz_Request::paramString('xPathItemCategories') !== '') {
- $xPathSettings['itemCategories'] = Minz_Request::paramString('xPathItemCategories', true);
- }
- if (Minz_Request::paramString('xPathItemUid') !== '') {
- $xPathSettings['itemUid'] = Minz_Request::paramString('xPathItemUid', true);
- }
- if (!empty($xPathSettings)) {
- $attributes['xpath'] = $xPathSettings;
- }
- } elseif ($feed_kind === FreshRSS_Feed::KIND_JSON_DOTNOTATION || $feed_kind === FreshRSS_Feed::KIND_HTML_XPATH_JSON_DOTNOTATION) {
- $jsonSettings = [];
- if (Minz_Request::paramString('jsonFeedTitle') !== '') {
- $jsonSettings['feedTitle'] = Minz_Request::paramString('jsonFeedTitle', true);
- }
- if (Minz_Request::paramString('jsonItem') !== '') {
- $jsonSettings['item'] = Minz_Request::paramString('jsonItem', true);
- }
- if (Minz_Request::paramString('jsonItemTitle') !== '') {
- $jsonSettings['itemTitle'] = Minz_Request::paramString('jsonItemTitle', true);
- }
- if (Minz_Request::paramString('jsonItemContent') !== '') {
- $jsonSettings['itemContent'] = Minz_Request::paramString('jsonItemContent', true);
- }
- if (Minz_Request::paramString('jsonItemUri') !== '') {
- $jsonSettings['itemUri'] = Minz_Request::paramString('jsonItemUri', true);
- }
- if (Minz_Request::paramString('jsonItemAuthor') !== '') {
- $jsonSettings['itemAuthor'] = Minz_Request::paramString('jsonItemAuthor', true);
- }
- if (Minz_Request::paramString('jsonItemTimestamp') !== '') {
- $jsonSettings['itemTimestamp'] = Minz_Request::paramString('jsonItemTimestamp', true);
- }
- if (Minz_Request::paramString('jsonItemTimeFormat') !== '') {
- $jsonSettings['itemTimeFormat'] = Minz_Request::paramString('jsonItemTimeFormat', true);
- }
- if (Minz_Request::paramString('jsonItemThumbnail') !== '') {
- $jsonSettings['itemThumbnail'] = Minz_Request::paramString('jsonItemThumbnail', true);
- }
- if (Minz_Request::paramString('jsonItemCategories') !== '') {
- $jsonSettings['itemCategories'] = Minz_Request::paramString('jsonItemCategories', true);
- }
- if (Minz_Request::paramString('jsonItemUid') !== '') {
- $jsonSettings['itemUid'] = Minz_Request::paramString('jsonItemUid', true);
- }
- if (!empty($jsonSettings)) {
- $attributes['json_dotnotation'] = $jsonSettings;
- }
- if (Minz_Request::paramString('xPathToJson', plaintext: true) !== '') {
- $attributes['xPathToJson'] = Minz_Request::paramString('xPathToJson', plaintext: true);
- }
- }
- try {
- $feed = self::addFeed($url, '', $cat, '', $http_auth, $attributes, $feed_kind);
- } catch (FreshRSS_BadUrl_Exception $e) {
- // Given url was not a valid url!
- Minz_Log::warning($e->getMessage());
- Minz_Request::bad(_t('feedback.sub.feed.invalid_url', $url), $url_redirect);
- return;
- } catch (FreshRSS_Feed_Exception $e) {
- // Something went bad (timeout, server not found, etc.)
- Minz_Log::warning($e->getMessage());
- Minz_Request::bad(_t('feedback.sub.feed.internal_problem', _url('index', 'logs')), $url_redirect);
- return;
- } catch (Minz_FileNotExistException $e) {
- // Cache directory doesn’t exist!
- Minz_Log::error($e->getMessage());
- Minz_Request::bad(_t('feedback.sub.feed.internal_problem', _url('index', 'logs')), $url_redirect);
- return;
- } catch (FreshRSS_AlreadySubscribed_Exception $e) {
- Minz_Request::bad(_t('feedback.sub.feed.already_subscribed', $e->feedName()), $url_redirect);
- return;
- } catch (FreshRSS_FeedNotAdded_Exception $e) {
- Minz_Request::bad(_t('feedback.sub.feed.not_added', $e->url()), $url_redirect);
- return;
- }
- // Entries are in DB, we redirect to feed configuration page.
- $url_redirect['a'] = 'feed';
- $url_redirect['params']['id'] = '' . $feed->id();
- Minz_Request::good(
- _t('feedback.sub.feed.added', $feed->name()),
- $url_redirect,
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- } else {
- // GET request: we must ask confirmation to user before adding feed.
- FreshRSS_View::prependTitle(_t('sub.feed.title_add') . ' · ');
- $catDAO = FreshRSS_Factory::createCategoryDao();
- $this->view->categories = $catDAO->listCategories(prePopulateFeeds: false);
- $this->view->feed = new FreshRSS_Feed($url);
- try {
- // We try to get more information about the feed.
- $this->view->feed->load(loadDetails: true);
- $this->view->load_ok = true;
- } catch (Exception) {
- $this->view->load_ok = false;
- }
- $feed = $feedDAO->searchByUrl($this->view->feed->url());
- if ($feed !== null) {
- // Already subscribe so we redirect to the feed configuration page.
- $url_redirect['a'] = 'feed';
- $url_redirect['params']['id'] = $feed->id();
- Minz_Request::good(
- _t('feedback.sub.feed.already_subscribed', $feed->name()),
- $url_redirect,
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- }
- }
- }
- /**
- * This action remove entries from a given feed.
- *
- * It should be reached by a POST action.
- *
- * Parameter is:
- * - id (default: false)
- */
- public function truncateAction(): void {
- if (!Minz_Request::isPost()) {
- Minz_Request::forward(['c' => 'subscription'], true);
- }
- $id = Minz_Request::paramInt('id');
- $url_redirect = [
- 'c' => 'subscription',
- 'a' => 'index',
- 'params' => ['id' => $id],
- ];
- if (!Minz_Request::isPost()) {
- Minz_Request::forward($url_redirect, true);
- }
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $n = $feedDAO->truncate($id);
- invalidateHttpCache();
- if ($n === false) {
- Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
- } else {
- Minz_Request::good(
- _t('feedback.sub.feed.n_entries_deleted', $n),
- $url_redirect,
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- }
- }
- /**
- * @param FreshRSS_SimplePieCustom|null $simplePiePush Used by WebSub (PubSubHubbub) to push updates
- * @param string $selfUrl Used by WebSub (PubSubHubbub) to override the feed URL
- * @return array{0:int,1:FreshRSS_Feed|null,2:int,3:array<FreshRSS_Feed>} Number of updated feeds, first feed or null, number of new articles,
- * list of feeds for which a cache refresh is needed
- * @throws FreshRSS_BadUrl_Exception
- */
- public static function actualizeFeeds(?int $feed_id = null, ?string $feed_url = null, ?int $maxFeeds = null,
- ?FreshRSS_SimplePieCustom $simplePiePush = null, string $selfUrl = ''): array {
- if (function_exists('set_time_limit')) {
- @set_time_limit(300);
- }
- if (!is_int($feed_id) || $feed_id <= 0) {
- $feed_id = null;
- }
- if (!is_string($feed_url) || trim($feed_url) === '') {
- $feed_url = null;
- }
- if (!is_int($maxFeeds) || $maxFeeds <= 0) {
- $maxFeeds = PHP_INT_MAX;
- }
- $catDAO = FreshRSS_Factory::createCategoryDao();
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $entryDAO = FreshRSS_Factory::createEntryDao();
- // Create a list of feeds to actualize.
- $feeds = [];
- if ($feed_id !== null || $feed_url !== null) {
- $feed = $feed_id !== null ? $feedDAO->searchById($feed_id) : $feedDAO->searchByUrl($feed_url);
- if ($feed !== null && $feed->id() > 0) {
- if ($selfUrl !== '') {
- $feed->_selfUrl($selfUrl);
- }
- $feeds[] = $feed;
- $feed_id = $feed->id();
- }
- } else {
- $feeds = $feedDAO->listFeedsOrderUpdate(-1);
- // Hydrate category for each feed to avoid that each feed has to make an SQL request
- $categories = $catDAO->listCategories(prePopulateFeeds: false, details: false);
- foreach ($feeds as $feed) {
- $category = $categories[$feed->categoryId()] ?? null;
- if ($category !== null) {
- $feed->_category($category);
- }
- }
- }
- // WebSub (PubSubHubbub) support
- $pubsubhubbubEnabledGeneral = FreshRSS_Context::systemConf()->pubsubhubbub_enabled;
- $pshbMinAge = time() - (3600 * 24); //TODO: Make a configuration.
- $nbUpdatedFeeds = 0;
- $nbNewArticles = 0;
- $feedsCacheToRefresh = [];
- /** @var array<int,array<string,true>> */
- $categoriesEntriesTitle = [];
- foreach ($feeds as $feed) {
- $feed = Minz_ExtensionManager::callHook(Minz_HookType::FeedBeforeActualize, $feed);
- if (!($feed instanceof FreshRSS_Feed)) {
- continue;
- }
- $url = $feed->url(); //For detection of HTTP 301
- $oldSimplePieHash = $feed->attributeString('SimplePieHash');
- $pubSubHubbubEnabled = $pubsubhubbubEnabledGeneral && $feed->pubSubHubbubEnabled();
- if ($simplePiePush === null && $feed_id === null && $pubSubHubbubEnabled && ($feed->lastUpdate() > $pshbMinAge)) {
- //$text = 'Skip pull of feed using PubSubHubbub: ' . $url;
- //Minz_Log::debug($text);
- //Minz_Log::debug($text, PSHB_LOG);
- continue; //When PubSubHubbub is used, do not pull refresh so often
- }
- if ($feed->mute() && ($feed_id === null || $simplePiePush !== null)) {
- continue; // If the feed is disabled, only allow refresh if manually requested for that specific feed
- }
- $mtime = $feed->cacheModifiedTime() ?: 0;
- $ttl = $feed->ttl();
- if ($ttl === FreshRSS_Feed::TTL_DEFAULT) {
- $ttl = FreshRSS_Context::userConf()->ttl_default;
- }
- if ($simplePiePush === null && $feed_id === null && (time() <= $feed->lastUpdate() + $ttl)) {
- //Too early to refresh from source, but check whether the feed was updated by another user
- $ε = 10; // negligible offset errors in seconds
- if ($mtime <= 0 ||
- $feed->lastUpdate() + $ε >= $mtime ||
- time() + $ε >= $mtime + FreshRSS_Context::systemConf()->limits['cache_duration']) { // is cache still valid?
- continue; //Nothing newer from other users
- }
- Minz_Log::debug('Feed ' . $feed->url(false) . ' was updated at ' . date('c', $feed->lastUpdate()) .
- ', and at ' . date('c', $mtime) . ' by another user; take advantage of newer cache.');
- }
- if (!$feed->lock()) {
- Minz_Log::notice('Feed already being actualized: ' . $feed->url(false));
- continue;
- }
- $feedIsNew = $feed->lastUpdate() <= 0;
- try {
- if ($simplePiePush !== null) {
- $simplePie = $simplePiePush; //Used by WebSub
- } elseif ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH) {
- $simplePie = $feed->loadHtmlXpath();
- if ($simplePie === null) {
- throw new FreshRSS_Feed_Exception('HTML+XPath Web scraping failed for [' . $feed->url(false) . ']');
- }
- } elseif ($feed->kind() === FreshRSS_Feed::KIND_XML_XPATH) {
- $simplePie = $feed->loadHtmlXpath();
- if ($simplePie === null) {
- throw new FreshRSS_Feed_Exception('XML+XPath parsing failed for [' . $feed->url(false) . ']');
- }
- } elseif ($feed->kind() === FreshRSS_Feed::KIND_JSON_DOTNOTATION) {
- $simplePie = $feed->loadJson();
- if ($simplePie === null) {
- throw new FreshRSS_Feed_Exception('JSON dot notation parsing failed for [' . $feed->url(false) . ']');
- }
- } elseif ($feed->kind() === FreshRSS_Feed::KIND_JSONFEED) {
- $simplePie = $feed->loadJson();
- if ($simplePie === null) {
- throw new FreshRSS_Feed_Exception('JSON Feed parsing failed for [' . $feed->url(false) . ']');
- }
- } elseif ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH_JSON_DOTNOTATION) {
- $simplePie = $feed->loadJson();
- if ($simplePie === null) {
- throw new FreshRSS_Feed_Exception('HTML+XPath+JSON parsing failed for [' . $feed->url(false) . ']');
- }
- } else {
- $simplePie = $feed->load(false, $feedIsNew);
- }
- if ($simplePie === null) {
- // Feed is cached and unchanged
- $newGuids = [];
- $entries = [];
- $feedIsEmpty = false; // We do not know
- $feedIsUnchanged = true;
- } else {
- $newGuids = $feed->loadGuids($simplePie);
- $entries = $feed->loadEntries($simplePie);
- $feedIsEmpty = $simplePiePush === null && empty($newGuids);
- $feedIsUnchanged = false;
- }
- $mtime = $feed->cacheModifiedTime() ?: time();
- } catch (FreshRSS_Feed_Exception $e) {
- Minz_Log::warning($e->getMessage());
- $feedDAO->updateLastUpdate($feed->id(), true);
- if ($e->getCode() === 410) {
- // HTTP 410 Gone
- Minz_Log::warning('Muting gone feed: ' . $feed->url(false));
- $feedDAO->mute($feed->id(), true);
- }
- $feed->unlock();
- continue;
- }
- $needFeedCacheRefresh = false;
- $nbMarkedUnread = 0;
- if (count($newGuids) > 0) {
- if (!$feed->hasAttribute('read_when_same_title_in_feed')) {
- $readWhenSameTitleInFeed = (int)FreshRSS_Context::userConf()->mark_when['same_title_in_feed'];
- } elseif ($feed->attributeBoolean('read_when_same_title_in_feed') === false) {
- $readWhenSameTitleInFeed = 0;
- } else {
- $readWhenSameTitleInFeed = $feed->attributeInt('read_when_same_title_in_feed') ?? 0;
- }
- if ($readWhenSameTitleInFeed > 0) {
- $titlesAsRead = array_fill_keys($feedDAO->listTitles($feed->id(), $readWhenSameTitleInFeed), true);
- } else {
- $titlesAsRead = [];
- }
- $category = $feed->category();
- if (!isset($categoriesEntriesTitle[$feed->categoryId()]) && $category !== null && $category->hasAttribute('read_when_same_title_in_category')) {
- $categoriesEntriesTitle[$feed->categoryId()] = array_fill_keys(
- $catDAO->listTitles($feed->categoryId(), $category->attributeInt('read_when_same_title_in_category') ?? 0),
- true
- );
- }
- $mark_updated_article_unread = $feed->attributeBoolean('mark_updated_article_unread') ?? FreshRSS_Context::userConf()->mark_updated_article_unread;
- // For this feed, check existing GUIDs already in database.
- $existingHashForGuids = $entryDAO->listHashForFeedGuids($feed->id(), $newGuids);
- /** @var array<string,bool> $newGuids */
- $newGuids = [];
- // Add entries in database if possible.
- /** @var FreshRSS_Entry $entry */
- foreach ($entries as $entry) {
- if (isset($newGuids[$entry->guid()])) {
- continue; //Skip subsequent articles with same GUID
- }
- $newGuids[$entry->guid()] = true;
- $entry->_lastSeen($mtime);
- if (isset($existingHashForGuids[$entry->guid()])) {
- $existingHash = $existingHashForGuids[$entry->guid()];
- if (strcasecmp($existingHash, $entry->hash()) !== 0) {
- //This entry already exists but has been updated
- $entry->_isUpdated(true);
- $entry->_lastModified($mtime);
- //Minz_Log::debug('Entry with GUID `' . $entry->guid() . '` updated in feed ' . $feed->url(false) .
- //', old hash ' . $existingHash . ', new hash ' . $entry->hash());
- $entry->_isFavorite(null); // Do not change favourite state
- $entry->_isRead($mark_updated_article_unread ? false : null); //Change is_read according to policy.
- if ($mark_updated_article_unread) {
- Minz_ExtensionManager::callHook(Minz_HookType::EntryAutoUnread, $entry, 'updated_article');
- }
- $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeInsert, $entry);
- if (!($entry instanceof FreshRSS_Entry)) {
- // An extension has returned a null value, there is nothing to insert.
- continue;
- }
- // NB: Do not mark updated articles as read based on their title, as the duplicate title maybe be from the same article.
- $entry->applyFilterActions([]);
- if ($readWhenSameTitleInFeed > 0) {
- $titlesAsRead[$entry->title()] = true;
- }
- if (isset($categoriesEntriesTitle[$feed->categoryId()])) {
- $categoriesEntriesTitle[$feed->categoryId()][$entry->title()] = true;
- }
- if (!$entry->isRead()) {
- $needFeedCacheRefresh = true; //Maybe
- $nbMarkedUnread++;
- }
- // If the entry has changed, there is a good chance for the full content to have changed as well.
- $entry->loadCompleteContent(true);
- $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeUpdate, $entry);
- if (!($entry instanceof FreshRSS_Entry)) {
- // An extension has returned a null value, there is nothing to insert.
- continue;
- }
- $entryDAO->updateEntry($entry->toArray());
- }
- } else {
- $entry->_isUpdated(false);
- $id = uTimeString();
- $entry->_id($id);
- $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeInsert, $entry);
- if (!($entry instanceof FreshRSS_Entry)) {
- // An extension has returned a null value, there is nothing to insert.
- continue;
- }
- $entry->applyFilterActions(array_merge($titlesAsRead, $categoriesEntriesTitle[$feed->categoryId()] ?? []));
- if ($readWhenSameTitleInFeed > 0) {
- $titlesAsRead[$entry->title()] = true;
- }
- if (isset($categoriesEntriesTitle[$feed->categoryId()])) {
- $categoriesEntriesTitle[$feed->categoryId()][$entry->title()] = true;
- }
- $needFeedCacheRefresh = true;
- if ($pubSubHubbubEnabled && $simplePiePush === null) { //We use push, but have discovered an article by pull!
- $text = 'An article was discovered by pull although we use PubSubHubbub!: Feed ' .
- \SimplePie\Misc::url_remove_credentials($url) .
- ' GUID ' . $entry->guid();
- Minz_Log::warning($text, PSHB_LOG);
- Minz_Log::warning($text);
- $pubSubHubbubEnabled = false;
- $feed->pubSubHubbubError(true);
- }
- $entry = Minz_ExtensionManager::callHook(Minz_HookType::EntryBeforeAdd, $entry);
- if (!($entry instanceof FreshRSS_Entry)) {
- // An extension has returned a null value, there is nothing to insert.
- continue;
- }
- if ($entryDAO->addEntry($entry->toArray(), true)) {
- $nbNewArticles++;
- }
- }
- }
- // N.B.: Applies to _entry table and not _entrytmp:
- $entryDAO->updateLastSeen($feed->id(), array_keys($newGuids), $mtime);
- } elseif ($feedIsUnchanged) {
- // Feed cache was unchanged, so mark as seen the same entries as last time
- $entryDAO->updateLastSeenUnchanged($feed->id(), $mtime);
- }
- unset($entries);
- if (rand(0, 30) === 1) { // Remove old entries once in 30.
- $nb = $feed->cleanOldEntries();
- if ($nb > 0) {
- $needFeedCacheRefresh = true;
- }
- }
- $feedDAO->updateLastUpdate($feed->id(), false, $mtime);
- if ($simplePiePush === null) {
- // Do not call for WebSub events, as we do not know the list of articles still on the upstream feed.
- $needFeedCacheRefresh |= ($feed->markAsReadUponGone($feedIsEmpty, $mtime) != false);
- }
- if ($needFeedCacheRefresh) {
- $feedsCacheToRefresh[] = $feed;
- }
- $feedProperties = [];
- if ($oldSimplePieHash !== $feed->attributeString('SimplePieHash')) {
- $feedProperties['attributes'] = $feed->attributes();
- }
- if ($feed->url() !== $url) { // HTTP 301 Moved Permanently
- Minz_Log::warning('Feed ' . \SimplePie\Misc::url_remove_credentials($url) .
- ' moved permanently to ' . $feed->url(includeCredentials: false));
- $feedProperties['url'] = $feed->url();
- } elseif ($simplePiePush !== null && $selfUrl !== '' && $selfUrl !== $feed->url()) { // selfUrl has priority for WebSub
- // https://github.com/pubsubhubbub/PubSubHubbub/wiki/Moving-Feeds-or-changing-Hubs
- Minz_Log::debug('WebSub unsubscribe ' . $feed->url(includeCredentials: false));
- if (!$feed->pubSubHubbubSubscribe(false)) { //Unsubscribe
- Minz_Log::warning('Error while WebSub unsubscribing from ' . $feed->url(includeCredentials: false));
- }
- $feed->_url($selfUrl);
- Minz_Log::warning('Feed ' . \SimplePie\Misc::url_remove_credentials($url) .
- ' canonical address moved to ' . $feed->url(includeCredentials: false));
- $feedProperties['url'] = $feed->url();
- }
- if ($simplePie != null) {
- if ($feed->name(true) === '') {
- //HTML to HTML-PRE //ENT_COMPAT except '&'
- $name = strtr(html_only_entity_decode($simplePie->get_title()), ['<' => '<', '>' => '>', '"' => '"']);
- $feed->_name($name);
- $feedProperties['name'] = $feed->name(false);
- }
- if ($feed->website() === '' || $feed->website() === $feed->url()) {
- $website = html_only_entity_decode($simplePie->get_link());
- if ($website !== $feed->website()) {
- $feed->_website($website);
- $feedProperties['website'] = $feed->website();
- $feed->faviconPrepare();
- }
- }
- if (trim($feed->description()) === '') {
- $description = html_only_entity_decode($simplePie->get_description());
- if ($description !== '') {
- $feed->_description($description);
- $feedProperties['description'] = $feed->description();
- }
- }
- }
- if (!empty($feedProperties) || $feedIsNew) {
- $feedProperties['attributes'] = $feed->attributes();
- $ok = $feedDAO->updateFeed($feed->id(), $feedProperties);
- if (!$ok && $feedIsNew) {
- //Cancel adding new feed in case of database error at first actualize
- $feedDAO->deleteFeed($feed->id());
- $feed->unlock();
- break;
- }
- }
- $feed->faviconPrepare();
- if ($pubsubhubbubEnabledGeneral && $feed->pubSubHubbubPrepare() != false) {
- Minz_Log::notice('WebSub subscribe ' . $feed->url(false));
- if (!$feed->pubSubHubbubSubscribe(true)) { //Subscribe
- Minz_Log::warning('Error while WebSub subscribing to ' . $feed->url(false));
- }
- }
- $feed->unlock();
- $nbUpdatedFeeds++;
- unset($feed);
- gc_collect_cycles();
- if ($nbUpdatedFeeds >= $maxFeeds) {
- break;
- }
- }
- return [$nbUpdatedFeeds, reset($feeds) ?: null, $nbNewArticles, $feedsCacheToRefresh];
- }
- /**
- * Feeds on which to apply a the keep max unreads policy, or all feeds if none specified.
- * @return int The number of articles marked as read
- */
- private static function keepMaxUnreads(FreshRSS_Feed ...$feeds): int {
- $affected = 0;
- if (empty($feeds)) {
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feeds = $feedDAO->listFeedsOrderUpdate(-1);
- }
- foreach ($feeds as $feed) {
- $n = $feed->markAsReadMaxUnread();
- if ($n !== false && $n > 0) {
- Minz_Log::debug($n . ' unread entries exceeding max number of ' . $feed->keepMaxUnread() . ' for [' . $feed->url(false) . ']');
- $affected += $n;
- }
- }
- return $affected;
- }
- /**
- * Auto-add labels to new articles.
- * @param int $nbNewEntries The number of top recent entries to process.
- * @return int|false The number of new labels added, or false in case of error.
- */
- private static function applyLabelActions(int $nbNewEntries): int|false {
- $tagDAO = FreshRSS_Factory::createTagDao();
- $labels = FreshRSS_Context::labels();
- $labels = array_filter($labels, static fn(FreshRSS_Tag $label) => !empty($label->filtersAction('label')));
- if (count($labels) <= 0) {
- return 0;
- }
- $entryDAO = FreshRSS_Factory::createEntryDao();
- $applyLabels = [];
- foreach (FreshRSS_Entry::fromTraversable($entryDAO->selectAll(order: 'DESC', limit: $nbNewEntries)) as $entry) {
- foreach ($labels as $label) {
- $label->applyFilterActions($entry, $applyLabel);
- if ($applyLabel) {
- $applyLabels[] = [
- 'id_tag' => $label->id(),
- 'id_entry' => $entry->id(),
- ];
- }
- }
- }
- return $tagDAO->tagEntries($applyLabels);
- }
- public static function commitNewEntries(): int {
- $entryDAO = FreshRSS_Factory::createEntryDao();
- $nbNewEntries = $entryDAO->countNewEntries();
- if ($nbNewEntries > 0) {
- if ($entryDAO->commitNewEntries()) {
- self::applyLabelActions($nbNewEntries);
- }
- }
- return $nbNewEntries;
- }
- /**
- * @param FreshRSS_SimplePieCustom|null $simplePiePush Used by WebSub (PubSubHubbub) to push updates
- * @param string $selfUrl Used by WebSub (PubSubHubbub) to override the feed URL
- * @return array{0:int,1:FreshRSS_Feed|null,2:int,3:array<FreshRSS_Feed>} Number of updated feeds, first feed or null, number of new articles,
- * list of feeds for which a cache refresh is needed
- * @throws FreshRSS_BadUrl_Exception
- */
- public static function actualizeFeedsAndCommit(?int $feed_id = null, ?string $feed_url = null, ?int $maxFeeds = null,
- ?FreshRSS_SimplePieCustom $simplePiePush = null, string $selfUrl = ''): array {
- $entryDAO = FreshRSS_Factory::createEntryDao();
- [$nbUpdatedFeeds, $feed, $nbNewArticles, $feedsCacheToRefresh] =
- FreshRSS_feed_Controller::actualizeFeeds($feed_id, $feed_url, $maxFeeds, $simplePiePush, $selfUrl);
- if ($nbNewArticles > 0) {
- $entryDAO->beginTransaction();
- FreshRSS_feed_Controller::commitNewEntries();
- }
- if (count($feedsCacheToRefresh) > 0) {
- $feedDAO = FreshRSS_Factory::createFeedDao();
- self::keepMaxUnreads(...$feedsCacheToRefresh);
- $feedDAO->updateCachedValues(...array_map(fn(FreshRSS_Feed $f) => $f->id(), $feedsCacheToRefresh));
- }
- if ($entryDAO->inTransaction()) {
- $entryDAO->commit();
- }
- if (rand(0, 30) === 1) { // Remove old cache once in a while
- cleanCache(CLEANCACHE_HOURS);
- }
- return [$nbUpdatedFeeds, $feed, $nbNewArticles, $feedsCacheToRefresh];
- }
- /**
- * This action actualizes entries from one or several feeds.
- *
- * Parameters are:
- * - id (default: null): Feed ID, or set to -1 to commit new articles to the main database
- * - url (default: null): Feed URL (instead of feed ID)
- * - maxFeeds (default: 10): Max number of feeds to refresh
- * - noCommit (default: 0): Set to 1 to prevent committing the new articles to the main database
- * If id and url are not specified, all the feeds are actualized, within the limits of maxFeeds.
- */
- public function actualizeAction(): int {
- Minz_Session::_param('actualize_feeds', false);
- $id = Minz_Request::paramInt('id');
- $url = Minz_Request::paramString('url');
- $maxFeeds = Minz_Request::paramInt('maxFeeds') ?: 10;
- $noCommit = ($_POST['noCommit'] ?? 0) == 1;
- if ($id === -1 && !$noCommit) { //Special request only to commit & refresh DB cache
- $nbUpdatedFeeds = 0;
- $feed = null;
- FreshRSS_feed_Controller::commitNewEntries();
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feedDAO->updateCachedValues();
- } else {
- if ($id === 0 && $url === '') {
- // Case of a batch refresh (e.g. cron)
- $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
- $databaseDAO->minorDbMaintenance();
- Minz_ExtensionManager::callHookVoid(Minz_HookType::FreshrssUserMaintenance);
- FreshRSS_feed_Controller::commitNewEntries();
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feedDAO->updateCachedValues();
- FreshRSS_category_Controller::refreshDynamicOpmls();
- }
- $entryDAO = FreshRSS_Factory::createEntryDao();
- [$nbUpdatedFeeds, $feed, $nbNewArticles, $feedsCacheToRefresh] = self::actualizeFeeds($id, $url, $maxFeeds);
- if (!$noCommit) {
- if ($nbNewArticles > 0) {
- $entryDAO->beginTransaction();
- FreshRSS_feed_Controller::commitNewEntries();
- }
- $feedDAO = FreshRSS_Factory::createFeedDao();
- if ($id !== 0 && $id !== -1) {
- if ($feed instanceof FreshRSS_Feed) {
- self::keepMaxUnreads($feed);
- }
- // Case of single feed refreshed, always update its cache
- $feedDAO->updateCachedValues($id);
- } elseif (count($feedsCacheToRefresh) > 0) {
- self::keepMaxUnreads(...$feedsCacheToRefresh);
- // Case of multiple feeds refreshed, only update cache of affected feeds
- $feedDAO->updateCachedValues(...array_map(fn(FreshRSS_Feed $f) => $f->id(), $feedsCacheToRefresh));
- }
- }
- if ($entryDAO->inTransaction()) {
- $entryDAO->commit();
- }
- }
- if (Minz_Request::paramBoolean('ajax')) {
- // Most of the time, ajax request is for only one feed. But since
- // there are several parallel requests, we should return that there
- // are several updated feeds.
- Minz_Request::setGoodNotification(_t('feedback.sub.feed.actualizeds'));
- // No layout in ajax request.
- $this->view->_layout(null);
- } elseif ($feed instanceof FreshRSS_Feed && $id > 0) {
- // Redirect to the main page with correct notification.
- Minz_Request::good(
- _t('feedback.sub.feed.actualized', $feed->name()),
- ['params' => ['get' => 'f_' . $id]],
- notificationName: 'actualizeAction',
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0);
- } elseif ($nbUpdatedFeeds >= 1) {
- Minz_Request::good(
- _t('feedback.sub.feed.n_actualized', $nbUpdatedFeeds),
- [],
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- } else {
- Minz_Request::good(
- _t('feedback.sub.feed.no_refresh'),
- [],
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- }
- return $nbUpdatedFeeds;
- }
- /**
- * @throws Minz_ConfigurationNamespaceException
- * @throws Minz_PDOConnectionException
- */
- public static function renameFeed(int $feed_id, string $feed_name): bool {
- if ($feed_id <= 0 || $feed_name === '') {
- return false;
- }
- FreshRSS_UserDAO::touch();
- $feedDAO = FreshRSS_Factory::createFeedDao();
- return $feedDAO->updateFeed($feed_id, ['name' => $feed_name]);
- }
- public static function moveFeed(int $feed_id, int $cat_id, string $new_cat_name = ''): bool {
- if ($feed_id <= 0 || ($cat_id <= 0 && $new_cat_name === '')) {
- return false;
- }
- FreshRSS_UserDAO::touch();
- $catDAO = FreshRSS_Factory::createCategoryDao();
- if ($cat_id > 0) {
- $cat = $catDAO->searchById($cat_id);
- $cat_id = $cat === null ? 0 : $cat->id();
- }
- if ($cat_id <= 1 && $new_cat_name != '') {
- $cat_id = $catDAO->addCategory(['name' => $new_cat_name]);
- }
- if ($cat_id <= 1) {
- $catDAO->checkDefault();
- $cat_id = FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
- }
- $feedDAO = FreshRSS_Factory::createFeedDao();
- return $feedDAO->updateFeed($feed_id, ['category' => $cat_id]);
- }
- /**
- * This action changes the category of a feed.
- *
- * This page must be reached by a POST request.
- *
- * Parameters are:
- * - f_id (default: false)
- * - c_id (default: false)
- * If c_id is false, default category is used.
- *
- * @todo should handle order of the feed inside the category.
- */
- public function moveAction(): void {
- if (!Minz_Request::isPost()) {
- Minz_Request::forward(['c' => 'subscription'], true);
- }
- $feed_id = Minz_Request::paramInt('f_id');
- $cat_id = Minz_Request::paramInt('c_id');
- if (self::moveFeed($feed_id, $cat_id)) {
- // TODO: return something useful
- // Log a notice to prevent "Empty IF statement" warning in PHP_CodeSniffer
- Minz_Log::notice('Moved feed `' . $feed_id . '` in the category `' . $cat_id . '`');
- } else {
- Minz_Log::warning('Cannot move feed `' . $feed_id . '` in the category `' . $cat_id . '`');
- Minz_Error::error(404);
- }
- }
- public static function deleteFeed(int $feed_id): bool {
- FreshRSS_UserDAO::touch();
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feed = $feedDAO->searchById($feed_id);
- if ($feed === null) {
- return false;
- }
- if ($feedDAO->deleteFeed($feed_id)) {
- // TODO: Delete old favicon (non-custom)
- if ($feed->customFavicon() && !$feed->attributeBoolean('customFaviconDisallowDel')) {
- FreshRSS_Feed::faviconDelete($feed->hashFavicon());
- }
- // Remove related queries
- $queries = FreshRSS_UserQuery::remove_query_by_get('f_' . $feed_id, FreshRSS_Context::userConf()->queries);
- FreshRSS_Context::userConf()->queries = $queries;
- FreshRSS_Context::userConf()->save();
- return true;
- }
- return false;
- }
- /**
- * This action deletes a feed.
- *
- * This page must be reached by a POST request.
- * If there are related queries, they are deleted too.
- *
- * Parameters are:
- * - id (default: false)
- */
- public function deleteAction(): void {
- if (!Minz_Request::isPost()) {
- Minz_Request::forward(['c' => 'subscription'], true);
- }
- $from = Minz_Request::paramString('from');
- $id = Minz_Request::paramInt('id');
- switch ($from) {
- case 'stats':
- $redirect_url = ['c' => 'stats', 'a' => 'idle'];
- break;
- case 'normal':
- $get = Minz_Request::paramString('get');
- if ($get !== '') {
- $redirect_url = ['c' => 'index', 'a' => 'normal', 'params' => ['get' => $get]];
- } else {
- $redirect_url = ['c' => 'index', 'a' => 'normal'];
- }
- break;
- default:
- $redirect_url = ['c' => 'subscription', 'a' => 'index'];
- if (!Minz_Request::isPost()) {
- Minz_Request::forward($redirect_url, true);
- }
- }
- if (self::deleteFeed($id)) {
- Minz_Request::good(
- _t('feedback.sub.feed.deleted'),
- $redirect_url,
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- } else {
- Minz_Request::bad(_t('feedback.sub.feed.error'), $redirect_url);
- }
- }
- /**
- * This action force clears the cache of a feed.
- *
- * Parameters are:
- * - id (mandatory - no default): Feed ID
- *
- */
- public function clearCacheAction(): void {
- if (!Minz_Request::isPost()) {
- Minz_Request::forward(['c' => 'subscription'], true);
- }
- //Get Feed.
- $id = Minz_Request::paramInt('id');
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feed = $feedDAO->searchById($id);
- if ($feed === null) {
- Minz_Request::bad(_t('feedback.sub.feed.not_found'), []);
- return;
- }
- $feed->clearCache();
- Minz_Request::good(
- _t('feedback.sub.feed.cache_cleared', $feed->name()),
- ['params' => ['get' => 'f_' . $feed->id()]],
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- }
- /**
- * This action forces reloading the articles of a feed.
- *
- * Parameters are:
- * - id (mandatory - no default): Feed ID
- *
- * @throws FreshRSS_BadUrl_Exception
- */
- public function reloadAction(): void {
- if (!Minz_Request::isPost()) {
- Minz_Request::forward(['c' => 'subscription'], true);
- }
- if (function_exists('set_time_limit')) {
- @set_time_limit(300);
- }
- //Get Feed ID.
- $feed_id = Minz_Request::paramInt('id');
- $limit = Minz_Request::paramInt('reload_limit') ?: 10;
- $feedDAO = FreshRSS_Factory::createFeedDao();
- $feed = $feedDAO->searchById($feed_id);
- if ($feed === null) {
- Minz_Request::bad(_t('feedback.sub.feed.not_found'), []);
- return;
- }
- //Re-fetch articles as if the feed was new.
- $feedDAO->updateFeed($feed->id(), [ 'lastUpdate' => 0 ]);
- self::actualizeFeedsAndCommit($feed_id);
- //Extract all feed entries from database, load complete content and store them back in database.
- $entryDAO = FreshRSS_Factory::createEntryDao();
- $entries = $entryDAO->listWhere('f', $feed_id, FreshRSS_Entry::STATE_ALL, order: 'DESC', limit: $limit);
- //We need another DB connection in parallel for unbuffered streaming
- Minz_ModelPdo::$usesSharedPdo = false;
- if (FreshRSS_Context::systemConf()->db['type'] === 'mysql') {
- // Second parallel connection for unbuffered streaming: MySQL
- $entryDAO2 = FreshRSS_Factory::createEntryDao();
- } else {
- // Single connection for buffered queries (in memory): SQLite, PostgreSQL
- //TODO: Consider an unbuffered query for PostgreSQL
- $entryDAO2 = $entryDAO;
- }
- foreach ($entries as $entry) {
- $oldContent = $entry->content(withEnclosures: false);
- if ($entry->loadCompleteContent(true)) {
- $entry->_lastModified(time());
- if ($entry->content(withEnclosures: false) !== $oldContent) {
- $entryDAO2->updateEntry($entry->toArray());
- }
- }
- }
- Minz_ModelPdo::$usesSharedPdo = true;
- //Give feedback to user.
- Minz_Request::good(
- _t('feedback.sub.feed.reloaded', $feed->name()),
- ['params' => ['get' => 'f_' . $feed->id()]],
- showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
- );
- }
- /**
- * This action creates a preview of a content-selector.
- *
- * Parameters are:
- * - id (mandatory - no default): Feed ID
- * - selector (mandatory - no default): Selector to preview
- *
- */
- public function contentSelectorPreviewAction(): void {
- //Configure.
- $this->view->fatalError = '';
- $this->view->selectorSuccess = false;
- $this->view->htmlContent = '';
- $this->view->_layout(null);
- $this->_csp([
- 'default-src' => "'self'",
- 'frame-ancestors' => "'self'",
- 'frame-src' => '*',
- 'img-src' => '* data:',
- 'media-src' => '*',
- ]);
- //Get parameters.
- $feed_id = Minz_Request::paramInt('id');
- $content_selector = Minz_Request::paramString('selector');
- if ($content_selector === '') {
- $this->view->fatalError = _t('feedback.sub.feed.selector_preview.selector_empty');
- return;
- }
- //Check Feed ID validity.
- $entryDAO = FreshRSS_Factory::createEntryDao();
- $entries = $entryDAO->listWhere('f', $feed_id);
- $entry = null;
- //Get first entry (syntax robust for Generator or Array)
- foreach ($entries as $myEntry) {
- $entry = $myEntry;
- }
- if ($entry == null) {
- $this->view->fatalError = _t('feedback.sub.feed.selector_preview.no_entries');
- return;
- }
- //Get feed.
- $feed = $entry->feed();
- if ($feed === null) {
- $this->view->fatalError = _t('feedback.sub.feed.selector_preview.no_feed');
- return;
- }
- $feed->_pathEntries($content_selector);
- $feed->_attribute('path_entries_filter', Minz_Request::paramString('selector_filter', true));
- //Fetch & select content.
- try {
- $fullContent = $entry->getContentByParsing();
- if ($fullContent != '') {
- $this->view->selectorSuccess = true;
- $this->view->htmlContent = $fullContent;
- } else {
- $this->view->selectorSuccess = false;
- $this->view->htmlContent = $entry->content(false);
- }
- } catch (Exception) {
- $this->view->fatalError = _t('feedback.sub.feed.selector_preview.http_error');
- }
- }
- }
|