Context.php 15 KB

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