Context.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /**
  3. * The context object handles the current configuration file and different
  4. * useful functions associated to the current view state.
  5. */
  6. final class FreshRSS_Context {
  7. /**
  8. * @var FreshRSS_UserConfiguration|null
  9. */
  10. public static $user_conf = null;
  11. /**
  12. * @var FreshRSS_SystemConfiguration|null
  13. */
  14. public static $system_conf = null;
  15. public static $categories = array();
  16. public static $tags = array();
  17. public static $name = '';
  18. public static $description = '';
  19. public static $total_unread = 0;
  20. public static $total_starred = array(
  21. 'all' => 0,
  22. 'read' => 0,
  23. 'unread' => 0,
  24. );
  25. public static $get_unread = 0;
  26. public static $current_get = array(
  27. 'all' => false,
  28. 'starred' => false,
  29. 'feed' => false,
  30. 'category' => false,
  31. 'tag' => false,
  32. 'tags' => false,
  33. );
  34. public static $next_get = 'a';
  35. public static $state = 0;
  36. public static $order = 'DESC';
  37. public static $number = 0;
  38. /** @var FreshRSS_BooleanSearch */
  39. public static $search;
  40. public static $first_id = '';
  41. public static $next_id = '';
  42. public static $id_max = '';
  43. public static $sinceHours = 0;
  44. public static $isCli = false;
  45. /**
  46. * Initialize the context for the global system.
  47. */
  48. public static function initSystem(bool $reload = false): FreshRSS_SystemConfiguration {
  49. if ($reload || FreshRSS_Context::$system_conf == null) {
  50. //TODO: Keep in session what we need instead of always reloading from disk
  51. FreshRSS_Context::$system_conf = FreshRSS_SystemConfiguration::init(DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php');
  52. // Register the configuration setter for the system configuration
  53. $configurationSetter = new FreshRSS_ConfigurationSetter();
  54. FreshRSS_Context::$system_conf->_configurationSetter($configurationSetter);
  55. }
  56. return FreshRSS_Context::$system_conf;
  57. }
  58. /**
  59. * Initialize the context for the current user.
  60. * @return FreshRSS_UserConfiguration|false
  61. */
  62. public static function initUser(string $username = '', bool $userMustExist = true) {
  63. FreshRSS_Context::$user_conf = null;
  64. if (!isset($_SESSION)) {
  65. Minz_Session::init('FreshRSS');
  66. }
  67. Minz_Session::lock();
  68. if ($username == '') {
  69. $username = Minz_User::name() ?? '';
  70. }
  71. if (($username === Minz_User::INTERNAL_USER || FreshRSS_user_Controller::checkUsername($username)) &&
  72. (!$userMustExist || FreshRSS_user_Controller::userExists($username))) {
  73. try {
  74. //TODO: Keep in session what we need instead of always reloading from disk
  75. FreshRSS_Context::$user_conf = FreshRSS_UserConfiguration::init(
  76. USERS_PATH . '/' . $username . '/config.php',
  77. FRESHRSS_PATH . '/config-user.default.php',
  78. FreshRSS_Context::$system_conf->configurationSetter());
  79. Minz_User::change($username);
  80. } catch (Exception $ex) {
  81. Minz_Log::warning($ex->getMessage(), USERS_PATH . '/_/' . LOG_FILENAME);
  82. }
  83. }
  84. if (FreshRSS_Context::$user_conf == null) {
  85. Minz_Session::_params([
  86. 'loginOk' => false,
  87. Minz_User::CURRENT_USER => false,
  88. ]);
  89. }
  90. Minz_Session::unlock();
  91. if (FreshRSS_Context::$user_conf == null) {
  92. return false;
  93. }
  94. FreshRSS_Context::$search = new FreshRSS_BooleanSearch('');
  95. //Legacy
  96. $oldEntries = (int)FreshRSS_Context::$user_conf->param('old_entries', 0);
  97. $keepMin = (int)FreshRSS_Context::$user_conf->param('keep_history_default', -5);
  98. if ($oldEntries > 0 || $keepMin > -5) { //Freshrss < 1.15
  99. $archiving = FreshRSS_Context::$user_conf->archiving;
  100. $archiving['keep_max'] = false;
  101. if ($oldEntries > 0) {
  102. $archiving['keep_period'] = 'P' . $oldEntries . 'M';
  103. }
  104. if ($keepMin > 0) {
  105. $archiving['keep_min'] = $keepMin;
  106. } elseif ($keepMin == -1) { //Infinite
  107. $archiving['keep_period'] = false;
  108. $archiving['keep_min'] = false;
  109. }
  110. FreshRSS_Context::$user_conf->archiving = $archiving;
  111. }
  112. //Legacy < 1.16.1
  113. if (!in_array(FreshRSS_Context::$user_conf->display_categories, [ 'active', 'remember', 'all', 'none' ], true)) {
  114. FreshRSS_Context::$user_conf->display_categories = FreshRSS_Context::$user_conf->display_categories === true ? 'all' : 'active';
  115. }
  116. return FreshRSS_Context::$user_conf;
  117. }
  118. /**
  119. * This action updates the Context object by using request parameters.
  120. *
  121. * Parameters are:
  122. * - state (default: conf->default_view)
  123. * - search (default: empty string)
  124. * - order (default: conf->sort_order)
  125. * - nb (default: conf->posts_per_page)
  126. * - next (default: empty string)
  127. * - hours (default: 0)
  128. */
  129. public static function updateUsingRequest() {
  130. if (empty(self::$categories)) {
  131. $catDAO = FreshRSS_Factory::createCategoryDao();
  132. self::$categories = $catDAO->listSortedCategories();
  133. }
  134. // Update number of read / unread variables.
  135. $entryDAO = FreshRSS_Factory::createEntryDao();
  136. self::$total_starred = $entryDAO->countUnreadReadFavorites();
  137. self::$total_unread = FreshRSS_CategoryDAO::countUnread(
  138. self::$categories, 1
  139. );
  140. self::_get(Minz_Request::param('get', 'a'));
  141. self::$state = Minz_Request::param(
  142. 'state', self::$user_conf->default_state
  143. );
  144. $state_forced_by_user = Minz_Request::param('state', false) !== false;
  145. if (!$state_forced_by_user && !self::isStateEnabled(FreshRSS_Entry::STATE_READ)) {
  146. if (self::$user_conf->default_view === 'adaptive' && self::$get_unread <= 0) {
  147. self::$state |= FreshRSS_Entry::STATE_READ;
  148. }
  149. if (self::$user_conf->show_fav_unread &&
  150. (self::isCurrentGet('s') || self::isCurrentGet('T') || self::isTag())) {
  151. self::$state |= FreshRSS_Entry::STATE_READ;
  152. }
  153. }
  154. self::$search = new FreshRSS_BooleanSearch(Minz_Request::param('search', ''));
  155. self::$order = Minz_Request::param(
  156. 'order', self::$user_conf->sort_order
  157. );
  158. self::$number = intval(Minz_Request::param('nb', self::$user_conf->posts_per_page));
  159. if (self::$number > self::$user_conf->max_posts_per_rss) {
  160. self::$number = max(
  161. self::$user_conf->max_posts_per_rss,
  162. self::$user_conf->posts_per_page);
  163. }
  164. self::$first_id = Minz_Request::param('next', '');
  165. self::$sinceHours = intval(Minz_Request::param('hours', 0));
  166. }
  167. /**
  168. * Returns if the current state includes $state parameter.
  169. * @param int $state
  170. */
  171. public static function isStateEnabled($state) {
  172. return self::$state & $state;
  173. }
  174. /**
  175. * Returns the current state with or without $state parameter.
  176. * @param int $state
  177. */
  178. public static function getRevertState($state) {
  179. if (self::$state & $state) {
  180. return self::$state & ~$state;
  181. } else {
  182. return self::$state | $state;
  183. }
  184. }
  185. /**
  186. * Return the current get as a string or an array.
  187. *
  188. * If $array is true, the first item of the returned value is 'f' or 'c' and
  189. * the second is the id.
  190. * @return string|array{string,bool|int}
  191. */
  192. public static function currentGet($array = false) {
  193. if (self::$current_get['all']) {
  194. return 'a';
  195. } elseif (self::$current_get['starred']) {
  196. return 's';
  197. } elseif (self::$current_get['feed']) {
  198. if ($array) {
  199. return array('f', self::$current_get['feed']);
  200. } else {
  201. return 'f_' . self::$current_get['feed'];
  202. }
  203. } elseif (self::$current_get['category']) {
  204. if ($array) {
  205. return array('c', self::$current_get['category']);
  206. } else {
  207. return 'c_' . self::$current_get['category'];
  208. }
  209. } elseif (self::$current_get['tag']) {
  210. if ($array) {
  211. return array('t', self::$current_get['tag']);
  212. } else {
  213. return 't_' . self::$current_get['tag'];
  214. }
  215. } elseif (self::$current_get['tags']) {
  216. return 'T';
  217. }
  218. return '';
  219. }
  220. /**
  221. * @return bool true if the current request targets all feeds (main view), false otherwise.
  222. */
  223. public static function isAll(): bool {
  224. return self::$current_get['all'] != false;
  225. }
  226. /**
  227. * @return bool true if the current request targets a category, false otherwise.
  228. */
  229. public static function isCategory(): bool {
  230. return self::$current_get['category'] != false;
  231. }
  232. /**
  233. * @return bool true if the current request targets a feed (and not a category or all articles), false otherwise.
  234. */
  235. public static function isFeed(): bool {
  236. return self::$current_get['feed'] != false;
  237. }
  238. /**
  239. * @return bool true if the current request targets a tag (though not all tags), false otherwise.
  240. */
  241. public static function isTag(): bool {
  242. return self::$current_get['tag'] != false;
  243. }
  244. /**
  245. * @return bool true if $get parameter correspond to the $current_get attribute.
  246. */
  247. public static function isCurrentGet(string $get): bool {
  248. $type = substr($get, 0, 1);
  249. $id = substr($get, 2);
  250. switch($type) {
  251. case 'a':
  252. return self::$current_get['all'];
  253. case 's':
  254. return self::$current_get['starred'];
  255. case 'f':
  256. return self::$current_get['feed'] == $id;
  257. case 'c':
  258. return self::$current_get['category'] == $id;
  259. case 't':
  260. return self::$current_get['tag'] == $id;
  261. case 'T':
  262. return self::$current_get['tags'] || self::$current_get['tag'];
  263. default:
  264. return false;
  265. }
  266. }
  267. /**
  268. * Set the current $get attribute.
  269. *
  270. * Valid $get parameter are:
  271. * - a
  272. * - s
  273. * - f_<feed id>
  274. * - c_<category id>
  275. * - t_<tag id>
  276. *
  277. * $name and $get_unread attributes are also updated as $next_get
  278. * Raise an exception if id or $get is invalid.
  279. */
  280. public static function _get($get) {
  281. $type = $get[0];
  282. $id = intval(substr($get, 2));
  283. if (empty(self::$categories)) {
  284. $catDAO = FreshRSS_Factory::createCategoryDao();
  285. self::$categories = $catDAO->listCategories();
  286. }
  287. switch($type) {
  288. case 'a':
  289. self::$current_get['all'] = true;
  290. self::$name = _t('index.feed.title');
  291. self::$description = self::$system_conf->meta_description;
  292. self::$get_unread = self::$total_unread;
  293. break;
  294. case 's':
  295. self::$current_get['starred'] = true;
  296. self::$name = _t('index.feed.title_fav');
  297. self::$description = self::$system_conf->meta_description;
  298. self::$get_unread = self::$total_starred['unread'];
  299. // Update state if favorite is not yet enabled.
  300. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  301. break;
  302. case 'f':
  303. // We try to find the corresponding feed. When allowing robots, always retrieve the full feed including description
  304. $feed = FreshRSS_Context::$system_conf->allow_robots ? null : FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  305. if ($feed === null) {
  306. $feedDAO = FreshRSS_Factory::createFeedDao();
  307. $feed = $feedDAO->searchById($id);
  308. if (!$feed) {
  309. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  310. }
  311. }
  312. self::$current_get['feed'] = $id;
  313. self::$current_get['category'] = $feed->categoryId();
  314. self::$name = $feed->name();
  315. self::$description = $feed->description();
  316. self::$get_unread = $feed->nbNotRead();
  317. break;
  318. case 'c':
  319. // We try to find the corresponding category.
  320. self::$current_get['category'] = $id;
  321. if (!isset(self::$categories[$id])) {
  322. $catDAO = FreshRSS_Factory::createCategoryDao();
  323. $cat = $catDAO->searchById($id);
  324. if (!$cat) {
  325. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  326. }
  327. } else {
  328. $cat = self::$categories[$id];
  329. }
  330. self::$name = $cat->name();
  331. self::$get_unread = $cat->nbNotRead();
  332. break;
  333. case 't':
  334. // We try to find the corresponding tag.
  335. self::$current_get['tag'] = $id;
  336. if (!isset(self::$tags[$id])) {
  337. $tagDAO = FreshRSS_Factory::createTagDao();
  338. $tag = $tagDAO->searchById($id);
  339. if (!$tag) {
  340. throw new FreshRSS_Context_Exception('Invalid tag: ' . $id);
  341. }
  342. } else {
  343. $tag = self::$tags[$id];
  344. }
  345. self::$name = $tag->name();
  346. self::$get_unread = $tag->nbUnread();
  347. break;
  348. case 'T':
  349. $tagDAO = FreshRSS_Factory::createTagDao();
  350. self::$current_get['tags'] = true;
  351. self::$name = _t('index.menu.tags');
  352. self::$get_unread = $tagDAO->countNotRead();
  353. break;
  354. default:
  355. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  356. }
  357. self::_nextGet();
  358. }
  359. /**
  360. * Set the value of $next_get attribute.
  361. */
  362. private static function _nextGet() {
  363. $get = self::currentGet();
  364. // By default, $next_get == $get
  365. self::$next_get = $get;
  366. if (empty(self::$categories)) {
  367. $catDAO = FreshRSS_Factory::createCategoryDao();
  368. self::$categories = $catDAO->listCategories();
  369. }
  370. if (self::$user_conf->onread_jump_next && strlen($get) > 2) {
  371. $another_unread_id = '';
  372. $found_current_get = false;
  373. switch ($get[0]) {
  374. case 'f':
  375. // We search the next unread feed with the following priorities: next in same category, or previous in same category, or next, or previous.
  376. foreach (self::$categories as $cat) {
  377. $sameCat = false;
  378. foreach ($cat->feeds() as $feed) {
  379. if ($found_current_get) {
  380. if ($feed->nbNotRead() > 0) {
  381. $another_unread_id = $feed->id();
  382. break 2;
  383. }
  384. } elseif ($feed->id() == self::$current_get['feed']) {
  385. $found_current_get = true;
  386. } elseif ($feed->nbNotRead() > 0) {
  387. $another_unread_id = $feed->id();
  388. $sameCat = true;
  389. }
  390. }
  391. if ($found_current_get && $sameCat) {
  392. break;
  393. }
  394. }
  395. // If there is no more unread feed, show main stream
  396. self::$next_get = $another_unread_id == '' ? 'a' : 'f_' . $another_unread_id;
  397. break;
  398. case 'c':
  399. // We search the next category with at least one unread article.
  400. foreach (self::$categories as $cat) {
  401. if ($cat->id() == self::$current_get['category']) {
  402. // Here is our current category! Next one could be our
  403. // champion if it has unread articles.
  404. $found_current_get = true;
  405. continue;
  406. }
  407. if ($cat->nbNotRead() > 0) {
  408. $another_unread_id = $cat->id();
  409. if ($found_current_get) {
  410. // Unread articles and the current category has
  411. // already been found? Leave the loop!
  412. break;
  413. }
  414. }
  415. }
  416. // If there is no more unread category, show main stream
  417. self::$next_get = $another_unread_id == '' ? 'a' : 'c_' . $another_unread_id;
  418. break;
  419. }
  420. }
  421. }
  422. /**
  423. * Determine if the auto remove is available in the current context.
  424. * This feature is available if:
  425. * - it is activated in the configuration
  426. * - the "read" state is not enable
  427. * - the "unread" state is enable
  428. *
  429. * @return boolean
  430. */
  431. public static function isAutoRemoveAvailable() {
  432. if (!self::$user_conf->auto_remove_article) {
  433. return false;
  434. }
  435. if (self::isStateEnabled(FreshRSS_Entry::STATE_READ)) {
  436. return false;
  437. }
  438. if (!self::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ)) {
  439. return false;
  440. }
  441. return true;
  442. }
  443. /**
  444. * Determine if the "sticky post" option is enabled. It can be enable
  445. * by the user when it is selected in the configuration page or by the
  446. * application when the context allows to auto-remove articles when they
  447. * are read.
  448. *
  449. * @return boolean
  450. */
  451. public static function isStickyPostEnabled() {
  452. if (self::$user_conf->sticky_post) {
  453. return true;
  454. }
  455. if (self::isAutoRemoveAvailable()) {
  456. return true;
  457. }
  458. return false;
  459. }
  460. public static function defaultTimeZone(): string {
  461. $timezone = ini_get('date.timezone');
  462. return $timezone != '' ? $timezone : 'UTC';
  463. }
  464. }