Context.php 15 KB

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