Context.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * The context object handles the current configuration file and different
  5. * useful functions associated to the current view state.
  6. */
  7. final class FreshRSS_Context {
  8. /** @var array<int,FreshRSS_Category> where the key is the category ID */
  9. private static array $categories = [];
  10. /** @var array<int,FreshRSS_Tag> where the key is the label ID */
  11. private static array $tags = [];
  12. public static string $name = '';
  13. public static string $description = '';
  14. public static int $total_unread = 0;
  15. public static int $total_important_unread = 0;
  16. /** @var array{all:int,read:int,unread:int} */
  17. public static array $total_starred = [
  18. 'all' => 0,
  19. 'read' => 0,
  20. 'unread' => 0,
  21. ];
  22. public static int $get_unread = 0;
  23. /** @var array{all:bool,A:bool,starred:bool,important:bool,feed:int|false,category:int|false,tag:int|false,tags:bool,Z:bool} */
  24. public static array $current_get = [
  25. 'all' => false,
  26. 'A' => false,
  27. 'starred' => false,
  28. 'important' => false,
  29. 'feed' => false,
  30. 'category' => false,
  31. 'tag' => false,
  32. 'tags' => false,
  33. 'Z' => false,
  34. ];
  35. public static string $next_get = 'a';
  36. public static int $state = 0;
  37. /** @var 'ASC'|'DESC' */
  38. public static string $order = 'DESC';
  39. /** @var 'id'|'c.name'|'date'|'f.name'|'lastUserModified'|'length'|'link'|'rand'|'title' */
  40. public static string $sort = 'id';
  41. /** @var 'ASC'|'DESC' */
  42. public static string $secondary_sort_order = 'DESC';
  43. /** @var 'id'|'date'|'link'|'title' */
  44. public static string $secondary_sort = 'id';
  45. public static int $number = 0;
  46. public static int $offset = 0;
  47. public static FreshRSS_BooleanSearch $search;
  48. /** @var numeric-string */
  49. public static string $continuation_id = '0';
  50. /** @var numeric-string */
  51. public static string $id_max = '0';
  52. public static int $sinceHours = 0;
  53. public static bool $isCli = false;
  54. /**
  55. * @access private
  56. * @deprecated Will be made `private`; use `FreshRSS_Context::systemConf()` instead.
  57. * @internal
  58. */
  59. public static ?FreshRSS_SystemConfiguration $system_conf = null;
  60. /**
  61. * @access private
  62. * @deprecated Will be made `private`; use `FreshRSS_Context::userConf()` instead.
  63. * @internal
  64. */
  65. public static ?FreshRSS_UserConfiguration $user_conf = null;
  66. /**
  67. * Initialize the context for the global system.
  68. */
  69. public static function initSystem(bool $reload = false): void {
  70. if ($reload || FreshRSS_Context::$system_conf === null) {
  71. //TODO: Keep in session what we need instead of always reloading from disk
  72. FreshRSS_Context::$system_conf = FreshRSS_SystemConfiguration::init(DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php');
  73. }
  74. }
  75. /**
  76. * @throws FreshRSS_Context_Exception
  77. */
  78. public static function &systemConf(): FreshRSS_SystemConfiguration {
  79. if (FreshRSS_Context::$system_conf === null) {
  80. throw new FreshRSS_Context_Exception('System configuration not initialised!');
  81. }
  82. return FreshRSS_Context::$system_conf;
  83. }
  84. public static function hasSystemConf(): bool {
  85. return FreshRSS_Context::$system_conf !== null;
  86. }
  87. /**
  88. * Initialize the context for the current user.
  89. */
  90. public static function initUser(string $username = '', bool $userMustExist = true): void {
  91. FreshRSS_Context::$user_conf = null;
  92. if (!isset($_SESSION)) {
  93. Minz_Session::init('FreshRSS');
  94. }
  95. Minz_Session::lock();
  96. if ($username == '') {
  97. $username = Minz_User::name() ?? '';
  98. }
  99. if (($username === Minz_User::INTERNAL_USER || FreshRSS_user_Controller::checkUsername($username)) &&
  100. (!$userMustExist || FreshRSS_user_Controller::userExists($username))) {
  101. try {
  102. //TODO: Keep in session what we need instead of always reloading from disk
  103. FreshRSS_Context::$user_conf = FreshRSS_UserConfiguration::init(
  104. USERS_PATH . '/' . $username . '/config.php',
  105. FRESHRSS_PATH . '/config-user.default.php');
  106. Minz_User::change($username);
  107. } catch (Exception $ex) {
  108. Minz_Log::warning($ex->getMessage(), USERS_PATH . '/_/' . LOG_FILENAME);
  109. }
  110. }
  111. if (FreshRSS_Context::$user_conf == null) {
  112. Minz_Session::_params([
  113. 'loginOk' => false,
  114. Minz_User::CURRENT_USER => false,
  115. ]);
  116. }
  117. Minz_Session::unlock();
  118. if (FreshRSS_Context::$user_conf == null) {
  119. return;
  120. }
  121. FreshRSS_Context::$search = new FreshRSS_BooleanSearch('');
  122. //Legacy
  123. $oldEntries = FreshRSS_Context::$user_conf->attributeInt('old_entries') ?? 0;
  124. $keepMin = FreshRSS_Context::$user_conf->attributeInt('keep_history_default') ?? -5;
  125. if ($oldEntries > 0 || $keepMin > -5) { //Freshrss < 1.15
  126. $archiving = FreshRSS_Context::$user_conf->archiving;
  127. $archiving['keep_max'] = false;
  128. if ($oldEntries > 0) {
  129. $archiving['keep_period'] = 'P' . $oldEntries . 'M';
  130. }
  131. if ($keepMin > 0) {
  132. $archiving['keep_min'] = $keepMin;
  133. } elseif ($keepMin == -1) { //Infinite
  134. $archiving['keep_period'] = false;
  135. $archiving['keep_min'] = false;
  136. }
  137. FreshRSS_Context::$user_conf->archiving = $archiving;
  138. }
  139. //Legacy < 1.16.1
  140. if (!in_array(FreshRSS_Context::$user_conf->display_categories, [ 'active', 'remember', 'all', 'none' ], true)) {
  141. FreshRSS_Context::$user_conf->display_categories = FreshRSS_Context::$user_conf->display_categories === true ? 'all' : 'active';
  142. }
  143. // FreshRSS 1.27.1+
  144. if (isset(FreshRSS_Context::$user_conf->shortcuts['close_dropdown'])) {
  145. $shortcuts = FreshRSS_Context::$user_conf->shortcuts;
  146. $shortcuts['close_menus'] = $shortcuts['close_dropdown'];
  147. unset($shortcuts['close_dropdown']);
  148. FreshRSS_Context::$user_conf->shortcuts = $shortcuts;
  149. FreshRSS_Context::$user_conf->save();
  150. }
  151. FreshRSS_Context::$user_conf->language = preg_replace_callback(
  152. '/-(\\w{2})$/',
  153. static fn (array $matches): string => strtoupper($matches[0]),
  154. FreshRSS_Context::$user_conf->language ?? Minz_Translate::DEFAULT_LANGUAGE
  155. ) ?? Minz_Translate::DEFAULT_LANGUAGE;
  156. }
  157. /**
  158. * @throws FreshRSS_Context_Exception
  159. */
  160. public static function &userConf(): FreshRSS_UserConfiguration {
  161. if (FreshRSS_Context::$user_conf === null) {
  162. throw new FreshRSS_Context_Exception('User configuration not initialised!');
  163. }
  164. return FreshRSS_Context::$user_conf;
  165. }
  166. public static function hasUserConf(): bool {
  167. return FreshRSS_Context::$user_conf !== null;
  168. }
  169. public static function clearUserConf(): void {
  170. FreshRSS_Context::$user_conf = null;
  171. }
  172. /**
  173. * @internal
  174. */
  175. public static function setUserConf(?FreshRSS_UserConfiguration $user_conf): void {
  176. FreshRSS_Context::$user_conf = $user_conf;
  177. }
  178. /** @return array<int,FreshRSS_Category> where the key is the category ID */
  179. public static function categories(): array {
  180. if (empty(self::$categories)) {
  181. $catDAO = FreshRSS_Factory::createCategoryDao();
  182. self::$categories = $catDAO->listSortedCategories(prePopulateFeeds: true, details: false);
  183. }
  184. return self::$categories;
  185. }
  186. /** @return array<int,FreshRSS_Feed> where the key is the feed ID */
  187. public static function feeds(): array {
  188. return FreshRSS_Category::findFeeds(self::categories());
  189. }
  190. /** @return array<int,FreshRSS_Tag> where the key is the label ID */
  191. public static function labels(bool $precounts = false): array {
  192. if (empty(self::$tags) || $precounts) {
  193. $tagDAO = FreshRSS_Factory::createTagDao();
  194. self::$tags = $tagDAO->listTags($precounts);
  195. }
  196. return self::$tags;
  197. }
  198. /**
  199. * This action updates the Context object by using request parameters.
  200. *
  201. * HTTP GET request parameters are:
  202. * - state (default: conf->default_view)
  203. * - search (default: empty string)
  204. * - order (default: conf->sort_order)
  205. * - nb (default: conf->posts_per_page)
  206. * - next (default: empty string)
  207. * - hours (default: 0)
  208. * @throws FreshRSS_Context_Exception
  209. * @throws Minz_ConfigurationNamespaceException
  210. * @throws Minz_PDOConnectionException
  211. */
  212. public static function updateUsingRequest(bool $computeStatistics): void {
  213. if ($computeStatistics && self::$total_unread === 0) {
  214. // Update number of read / unread variables.
  215. $entryDAO = FreshRSS_Factory::createEntryDao();
  216. self::$total_starred = $entryDAO->countUnreadReadFavorites();
  217. self::$total_unread = FreshRSS_Category::countUnread(self::categories(), FreshRSS_Feed::PRIORITY_MAIN_STREAM);
  218. self::$total_important_unread = FreshRSS_Category::countUnread(self::categories(), FreshRSS_Feed::PRIORITY_IMPORTANT);
  219. }
  220. self::_get(Minz_Request::paramString('get', plaintext: true) ?: 'a');
  221. self::$state = Minz_Request::paramInt('state') ?: FreshRSS_Context::userConf()->default_state;
  222. $state_forced_by_user = Minz_Request::paramString('state', plaintext: true) !== '';
  223. if (!$state_forced_by_user) {
  224. if (FreshRSS_Context::userConf()->show_fav_unread && (self::isCurrentGet('s') || self::isCurrentGet('T') || self::isTag())) {
  225. self::$state = FreshRSS_Entry::STATE_NOT_READ | FreshRSS_Entry::STATE_READ;
  226. } elseif (FreshRSS_Context::userConf()->default_view === 'all') {
  227. self::$state = FreshRSS_Entry::STATE_NOT_READ | FreshRSS_Entry::STATE_READ;
  228. } elseif (FreshRSS_Context::userConf()->default_view === 'unread_or_favorite') {
  229. self::$state = FreshRSS_Entry::STATE_OR_NOT_READ | FreshRSS_Entry::STATE_OR_FAVORITE;
  230. } elseif (FreshRSS_Context::userConf()->default_view === 'adaptive' && self::$get_unread <= 0) {
  231. self::$state = FreshRSS_Entry::STATE_NOT_READ | FreshRSS_Entry::STATE_READ;
  232. }
  233. }
  234. self::$search = new FreshRSS_BooleanSearch(Minz_Request::paramString('search', plaintext: true));
  235. $default_order = null;
  236. $default_sort = null;
  237. if (Minz_Request::paramString('order', plaintext: true) === '' || Minz_Request::paramString('sort', plaintext: true) === '') {
  238. if (!empty(self::$current_get['feed'])) {
  239. $id = self::$current_get['feed'];
  240. // We most likely already have the feed object in cache
  241. $feed = FreshRSS_Category::findFeed(FreshRSS_Context::categories(), $id);
  242. if ($feed === null) {
  243. $feedDAO = FreshRSS_Factory::createFeedDao();
  244. $feed = $feedDAO->searchById($id);
  245. }
  246. $default_order = $feed?->defaultOrder();
  247. $default_sort = $feed?->defaultSort();
  248. } elseif (!empty(self::$current_get['category'])) {
  249. $id = self::$current_get['category'];
  250. // We most likely already have the category object in cache
  251. $category = FreshRSS_Context::categories()[$id] ?? null;
  252. if ($category === null) {
  253. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  254. $category = $categoryDAO->searchById($id);
  255. }
  256. $default_order = $category?->defaultOrder();
  257. $default_sort = $category?->defaultSort();
  258. }
  259. }
  260. $order = Minz_Request::paramString('order', plaintext: true) ?: $default_order ?: FreshRSS_Context::userConf()->sort_order;
  261. self::$order = in_array($order, ['ASC', 'DESC'], true) ? $order : 'DESC';
  262. $sort = Minz_Request::paramString('sort', plaintext: true) ?: $default_sort ?: FreshRSS_Context::userConf()->sort;
  263. self::$sort = in_array($sort, ['id', 'c.name', 'date', 'f.name', 'lastUserModified', 'length', 'link', 'title', 'rand'], true) ? $sort : 'id';
  264. if (in_array(self::$sort, ['c.name', 'f.name'], true)) {
  265. self::$secondary_sort = FreshRSS_Context::userConf()->secondary_sort;
  266. self::$secondary_sort_order = FreshRSS_Context::userConf()->secondary_sort_order;
  267. if ($order !== ($default_order ?: FreshRSS_Context::userConf()->sort_order)) {
  268. // User swapped order so swap secondary order as well
  269. self::$secondary_sort_order = self::$secondary_sort_order === 'DESC' ? 'ASC' : 'DESC';
  270. }
  271. }
  272. self::$number = Minz_Request::paramInt('nb') ?: FreshRSS_Context::userConf()->posts_per_page;
  273. if (self::$number > FreshRSS_Context::userConf()->max_posts_per_rss) {
  274. self::$number = max(
  275. FreshRSS_Context::userConf()->max_posts_per_rss,
  276. FreshRSS_Context::userConf()->posts_per_page);
  277. }
  278. self::$offset = Minz_Request::paramInt('offset');
  279. $id_max = Minz_Request::paramString('idMax', plaintext: true);
  280. self::$id_max = ctype_digit($id_max) ? $id_max : '0';
  281. $continuation_id = Minz_Request::paramString('cid', plaintext: true);
  282. self::$continuation_id = ctype_digit($continuation_id) ? $continuation_id : '0';
  283. self::$sinceHours = Minz_Request::paramInt('hours');
  284. }
  285. /**
  286. * Returns if the current state includes $state parameter.
  287. */
  288. public static function isStateEnabled(int $state): int {
  289. return self::$state & $state;
  290. }
  291. /**
  292. * Returns the current state with or without $state parameter.
  293. */
  294. public static function getRevertState(int $state): int {
  295. if (self::$state & $state) {
  296. return self::$state & ~$state;
  297. }
  298. return self::$state | $state;
  299. }
  300. /**
  301. * Return the current get as a string or an array.
  302. *
  303. * If $array is true, the first item of the returned value is 'f' or 'c' or 't' and the second is the id.
  304. * @phpstan-return ($asArray is true ? array{'a'|'A'|'c'|'f'|'i'|'s'|'t'|'T'|'Z',bool|int} : string)
  305. * @return string|array{string,bool|int}
  306. */
  307. public static function currentGet(bool $asArray = false): string|array {
  308. if (self::$current_get['all']) {
  309. return $asArray ? ['a', true] : 'a';
  310. } elseif (self::$current_get['A']) {
  311. return $asArray ? ['A', true] : 'A';
  312. } elseif (self::$current_get['important']) {
  313. return $asArray ? ['i', true] : 'i';
  314. } elseif (self::$current_get['starred']) {
  315. return $asArray ? ['s', true] : 's';
  316. } elseif (self::$current_get['feed']) {
  317. if ($asArray) {
  318. return ['f', self::$current_get['feed']];
  319. } else {
  320. return 'f_' . self::$current_get['feed'];
  321. }
  322. } elseif (self::$current_get['category']) {
  323. if ($asArray) {
  324. return ['c', self::$current_get['category']];
  325. } else {
  326. return 'c_' . self::$current_get['category'];
  327. }
  328. } elseif (self::$current_get['tag']) {
  329. if ($asArray) {
  330. return ['t', self::$current_get['tag']];
  331. } else {
  332. return 't_' . self::$current_get['tag'];
  333. }
  334. } elseif (self::$current_get['tags']) {
  335. return $asArray ? ['T', true] : 'T';
  336. } elseif (self::$current_get['Z']) {
  337. return $asArray ? ['Z', true] : 'Z';
  338. }
  339. return '';
  340. }
  341. /**
  342. * @return bool true if the current request targets all feeds (main view), false otherwise.
  343. */
  344. public static function isAll(): bool {
  345. return self::$current_get['all'] != false;
  346. }
  347. public static function isAllAndCategories(): bool {
  348. return self::$current_get['A'] != false;
  349. }
  350. public static function isAllAndArchived(): bool {
  351. return self::$current_get['Z'] != false;
  352. }
  353. /**
  354. * @return bool true if the current request targets important feeds, false otherwise.
  355. */
  356. public static function isImportant(): bool {
  357. return self::$current_get['important'] != false;
  358. }
  359. /**
  360. * @return bool true if the current request targets a category, false otherwise.
  361. */
  362. public static function isCategory(): bool {
  363. return self::$current_get['category'] != false;
  364. }
  365. /**
  366. * @return bool true if the current request targets a feed (and not a category or all articles), false otherwise.
  367. */
  368. public static function isFeed(): bool {
  369. return self::$current_get['feed'] != false;
  370. }
  371. /**
  372. * @return bool true if the current request targets a tag (though not all tags), false otherwise.
  373. */
  374. public static function isTag(): bool {
  375. return self::$current_get['tag'] != false;
  376. }
  377. /**
  378. * @return bool whether $get parameter corresponds to the $current_get attribute.
  379. */
  380. public static function isCurrentGet(string $get): bool {
  381. $type = substr($get, 0, 1);
  382. $id = substr($get, 2);
  383. return match ($type) {
  384. 'a' => self::$current_get['all'],
  385. 'A' => self::$current_get['A'],
  386. 'i' => self::$current_get['important'],
  387. 's' => self::$current_get['starred'],
  388. 'f' => self::$current_get['feed'] == $id,
  389. 'c' => self::$current_get['category'] == $id,
  390. 't' => self::$current_get['tag'] == $id,
  391. 'T' => self::$current_get['tags'] || self::$current_get['tag'],
  392. 'Z' => self::$current_get['Z'],
  393. default => false,
  394. };
  395. }
  396. /**
  397. * Set the current $get attribute.
  398. *
  399. * Valid $get parameter are:
  400. * - a
  401. * - s
  402. * - f_<feed id>
  403. * - c_<category id>
  404. * - t_<tag id>
  405. *
  406. * $name and $get_unread attributes are also updated as $next_get
  407. * Raise an exception if id or $get is invalid.
  408. * @throws FreshRSS_Context_Exception
  409. * @throws Minz_ConfigurationNamespaceException
  410. * @throws Minz_PDOConnectionException
  411. */
  412. private static function _get(string $get): void {
  413. $type = $get[0];
  414. $id = (int)substr($get, 2);
  415. if (empty(self::$categories)) {
  416. // TODO: Check whether this section is used
  417. $catDAO = FreshRSS_Factory::createCategoryDao();
  418. $details = $type === 'f'; // Load additional feed details in the case of feed view
  419. self::$categories = $catDAO->listCategories(prePopulateFeeds: true, details: $details);
  420. }
  421. switch ($type) {
  422. case 'a': // All PRIORITY_MAIN_STREAM
  423. self::$current_get['all'] = true;
  424. self::$description = FreshRSS_Context::systemConf()->meta_description;
  425. self::$get_unread = self::$total_unread;
  426. break;
  427. case 'A': // All except PRIORITY_HIDDEN
  428. self::$current_get['A'] = true;
  429. self::$description = FreshRSS_Context::systemConf()->meta_description;
  430. self::$get_unread = self::$total_unread;
  431. break;
  432. case 'Z': // All including PRIORITY_HIDDEN
  433. self::$current_get['Z'] = true;
  434. self::$name = _t('index.feed.title');
  435. self::$description = FreshRSS_Context::systemConf()->meta_description;
  436. self::$get_unread = self::$total_unread;
  437. break;
  438. case 'i': // Priority important feeds
  439. self::$current_get['important'] = true;
  440. self::$name = _t('index.menu.important');
  441. self::$description = FreshRSS_Context::systemConf()->meta_description;
  442. self::$get_unread = self::$total_unread;
  443. break;
  444. case 's': // Starred. Deprecated: use $state instead
  445. self::$current_get['starred'] = true;
  446. self::$name = _t('index.feed.title_fav');
  447. self::$description = FreshRSS_Context::systemConf()->meta_description;
  448. self::$get_unread = self::$total_starred['unread'];
  449. // Update state if favorite is not yet enabled.
  450. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  451. break;
  452. case 'f': // Feed
  453. // We try to find the corresponding feed. When allowing robots, always retrieve the full feed including description
  454. $feed = FreshRSS_Context::systemConf()->allow_robots ? null : FreshRSS_Category::findFeed(self::$categories, $id);
  455. if ($feed === null) {
  456. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  457. }
  458. self::$current_get['feed'] = $id;
  459. self::$current_get['category'] = $feed->categoryId();
  460. self::$name = $feed->name();
  461. self::$description = $feed->description();
  462. self::$get_unread = $feed->nbNotRead();
  463. break;
  464. case 'c': // Category
  465. // We try to find the corresponding category.
  466. self::$current_get['category'] = $id;
  467. $cat = null;
  468. foreach (self::$categories as $category) {
  469. if ($category->id() === $id) {
  470. $cat = $category;
  471. break;
  472. }
  473. }
  474. if ($cat === null) {
  475. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  476. }
  477. self::$name = $cat->name();
  478. self::$get_unread = $cat->nbNotRead();
  479. break;
  480. case 't': // Tag (label)
  481. // We try to find the corresponding tag.
  482. self::$current_get['tag'] = $id;
  483. $tag = null;
  484. foreach (self::$tags as $t) {
  485. if ($t->id() === $id) {
  486. $tag = $t;
  487. break;
  488. }
  489. }
  490. if ($tag === null) {
  491. $tagDAO = FreshRSS_Factory::createTagDao();
  492. $tag = $tagDAO->searchById($id);
  493. if ($tag === null) {
  494. throw new FreshRSS_Context_Exception('Invalid tag: ' . $id);
  495. }
  496. }
  497. self::$name = $tag->name();
  498. self::$get_unread = $tag->nbUnread();
  499. break;
  500. case 'T': // Any tag (label)
  501. $tagDAO = FreshRSS_Factory::createTagDao();
  502. self::$current_get['tags'] = true;
  503. self::$name = _t('index.menu.mylabels');
  504. self::$get_unread = $tagDAO->countNotRead();
  505. break;
  506. default:
  507. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  508. }
  509. self::_nextGet();
  510. }
  511. /**
  512. * Set the value of $next_get attribute.
  513. */
  514. private static function _nextGet(): void {
  515. $get = self::currentGet();
  516. // By default, $next_get == $get
  517. self::$next_get = $get;
  518. if (empty(self::$categories)) {
  519. $catDAO = FreshRSS_Factory::createCategoryDao();
  520. self::$categories = $catDAO->listCategories(prePopulateFeeds: true);
  521. }
  522. if (FreshRSS_Context::userConf()->onread_jump_next && strlen($get) > 2) {
  523. $another_unread_id = '';
  524. $found_current_get = false;
  525. switch ($get[0]) {
  526. case 'f':
  527. // We search the next unread feed with the following priorities: next in same category, or previous in same category, or next, or previous.
  528. foreach (self::$categories as $cat) {
  529. $sameCat = false;
  530. foreach ($cat->feeds() as $feed) {
  531. if ($found_current_get) {
  532. if ($feed->nbNotRead() > 0) {
  533. $another_unread_id = $feed->id();
  534. break 2;
  535. }
  536. } elseif ($feed->id() == self::$current_get['feed']) {
  537. $found_current_get = true;
  538. } elseif ($feed->nbNotRead() > 0) {
  539. $another_unread_id = $feed->id();
  540. $sameCat = true;
  541. }
  542. }
  543. if ($found_current_get && $sameCat) {
  544. break;
  545. }
  546. }
  547. // If there is no more unread feed, show main stream
  548. self::$next_get = $another_unread_id == '' ? 'a' : 'f_' . $another_unread_id;
  549. break;
  550. case 'c':
  551. // We search the next category with at least one unread article.
  552. foreach (self::$categories as $cat) {
  553. if ($cat->id() == self::$current_get['category']) {
  554. // Here is our current category! Next one could be our
  555. // champion if it has unread articles.
  556. $found_current_get = true;
  557. continue;
  558. }
  559. if ($cat->nbNotRead(minPriority: FreshRSS_Feed::PRIORITY_CATEGORY) > 0) {
  560. $another_unread_id = $cat->id();
  561. if ($found_current_get) {
  562. // Unread articles and the current category has
  563. // already been found? Leave the loop!
  564. break;
  565. }
  566. }
  567. }
  568. // If there is no more unread category, show main stream
  569. self::$next_get = $another_unread_id == '' ? 'a' : 'c_' . $another_unread_id;
  570. break;
  571. case 't':
  572. // We can't know what the next unread tag is because entries can be in multiple tags
  573. // so marking all entries in a tag can indirectly mark all entries in multiple tags.
  574. // Default is to return to the current tag, so mark it as next_get = 'a' instead when
  575. // userconf -> onread_jump_next so the readAction knows to jump to the next unread
  576. // tag.
  577. self::$next_get = 'a';
  578. break;
  579. }
  580. }
  581. }
  582. /**
  583. * Determine if the auto remove is available in the current context.
  584. * This feature is available if:
  585. * - it is activated in the configuration
  586. * - the "read" state is not enable
  587. * - the "unread" state is enable
  588. */
  589. public static function isAutoRemoveAvailable(): bool {
  590. return FreshRSS_Context::userConf()->auto_remove_article && !self::isStateEnabled(FreshRSS_Entry::STATE_READ) &&
  591. (self::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ) || self::isStateEnabled(FreshRSS_Entry::STATE_OR_NOT_READ));
  592. }
  593. /**
  594. * Determine if the "sticky post" option is enabled. It can be enable
  595. * by the user when it is selected in the configuration page or by the
  596. * application when the context allows to auto-remove articles when they
  597. * are read.
  598. */
  599. public static function isStickyPostEnabled(): bool {
  600. if (FreshRSS_Context::userConf()->sticky_post) {
  601. return true;
  602. }
  603. if (self::isAutoRemoveAvailable()) {
  604. return true;
  605. }
  606. return false;
  607. }
  608. public static function defaultTimeZone(): string {
  609. $timezone = ini_get('date.timezone');
  610. return $timezone != false ? $timezone : 'UTC';
  611. }
  612. }