Context.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.txt');
  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. * Returns if the current state includes $state parameter.
  129. * @param int $state
  130. */
  131. public static function isStateEnabled($state) {
  132. return self::$state & $state;
  133. }
  134. /**
  135. * Returns the current state with or without $state parameter.
  136. * @param int $state
  137. */
  138. public static function getRevertState($state) {
  139. if (self::$state & $state) {
  140. return self::$state & ~$state;
  141. } else {
  142. return self::$state | $state;
  143. }
  144. }
  145. /**
  146. * Return the current get as a string or an array.
  147. *
  148. * If $array is true, the first item of the returned value is 'f' or 'c' and
  149. * the second is the id.
  150. */
  151. public static function currentGet($array = false) {
  152. if (self::$current_get['all']) {
  153. return 'a';
  154. } elseif (self::$current_get['starred']) {
  155. return 's';
  156. } elseif (self::$current_get['feed']) {
  157. if ($array) {
  158. return array('f', self::$current_get['feed']);
  159. } else {
  160. return 'f_' . self::$current_get['feed'];
  161. }
  162. } elseif (self::$current_get['category']) {
  163. if ($array) {
  164. return array('c', self::$current_get['category']);
  165. } else {
  166. return 'c_' . self::$current_get['category'];
  167. }
  168. } elseif (self::$current_get['tag']) {
  169. if ($array) {
  170. return array('t', self::$current_get['tag']);
  171. } else {
  172. return 't_' . self::$current_get['tag'];
  173. }
  174. } elseif (self::$current_get['tags']) {
  175. return 'T';
  176. }
  177. }
  178. /**
  179. * @return bool true if the current request targets all feeds (main view), false otherwise.
  180. */
  181. public static function isAll(): bool {
  182. return self::$current_get['all'] != false;
  183. }
  184. /**
  185. * @return bool true if the current request targets a category, false otherwise.
  186. */
  187. public static function isCategory(): bool {
  188. return self::$current_get['category'] != false;
  189. }
  190. /**
  191. * @return bool true if the current request targets a feed (and not a category or all articles), false otherwise.
  192. */
  193. public static function isFeed(): bool {
  194. return self::$current_get['feed'] != false;
  195. }
  196. /**
  197. * @return bool true if the current request targets a tag (though not all tags), false otherwise.
  198. */
  199. public static function isTag(): bool {
  200. return self::$current_get['tag'] != false;
  201. }
  202. /**
  203. * @return bool true if $get parameter correspond to the $current_get attribute.
  204. */
  205. public static function isCurrentGet($get): bool {
  206. $type = $get[0];
  207. $id = substr($get, 2);
  208. switch($type) {
  209. case 'a':
  210. return self::$current_get['all'];
  211. case 's':
  212. return self::$current_get['starred'];
  213. case 'f':
  214. return self::$current_get['feed'] == $id;
  215. case 'c':
  216. return self::$current_get['category'] == $id;
  217. case 't':
  218. return self::$current_get['tag'] == $id;
  219. case 'T':
  220. return self::$current_get['tags'] || self::$current_get['tag'];
  221. default:
  222. return false;
  223. }
  224. }
  225. /**
  226. * Set the current $get attribute.
  227. *
  228. * Valid $get parameter are:
  229. * - a
  230. * - s
  231. * - f_<feed id>
  232. * - c_<category id>
  233. * - t_<tag id>
  234. *
  235. * $name and $get_unread attributes are also updated as $next_get
  236. * Raise an exception if id or $get is invalid.
  237. */
  238. public static function _get($get) {
  239. $type = $get[0];
  240. $id = intval(substr($get, 2));
  241. if (empty(self::$categories)) {
  242. $catDAO = FreshRSS_Factory::createCategoryDao();
  243. self::$categories = $catDAO->listCategories();
  244. }
  245. switch($type) {
  246. case 'a':
  247. self::$current_get['all'] = true;
  248. self::$name = _t('index.feed.title');
  249. self::$description = self::$system_conf->meta_description;
  250. self::$get_unread = self::$total_unread;
  251. break;
  252. case 's':
  253. self::$current_get['starred'] = true;
  254. self::$name = _t('index.feed.title_fav');
  255. self::$description = self::$system_conf->meta_description;
  256. self::$get_unread = self::$total_starred['unread'];
  257. // Update state if favorite is not yet enabled.
  258. self::$state = self::$state | FreshRSS_Entry::STATE_FAVORITE;
  259. break;
  260. case 'f':
  261. // We try to find the corresponding feed. When allowing robots, always retrieve the full feed including description
  262. $feed = FreshRSS_Context::$system_conf->allow_robots ? null : FreshRSS_CategoryDAO::findFeed(self::$categories, $id);
  263. if ($feed === null) {
  264. $feedDAO = FreshRSS_Factory::createFeedDao();
  265. $feed = $feedDAO->searchById($id);
  266. if (!$feed) {
  267. throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
  268. }
  269. }
  270. self::$current_get['feed'] = $id;
  271. self::$current_get['category'] = $feed->category();
  272. self::$name = $feed->name();
  273. self::$description = $feed->description();
  274. self::$get_unread = $feed->nbNotRead();
  275. break;
  276. case 'c':
  277. // We try to find the corresponding category.
  278. self::$current_get['category'] = $id;
  279. if (!isset(self::$categories[$id])) {
  280. $catDAO = FreshRSS_Factory::createCategoryDao();
  281. $cat = $catDAO->searchById($id);
  282. if (!$cat) {
  283. throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
  284. }
  285. } else {
  286. $cat = self::$categories[$id];
  287. }
  288. self::$name = $cat->name();
  289. self::$get_unread = $cat->nbNotRead();
  290. break;
  291. case 't':
  292. // We try to find the corresponding tag.
  293. self::$current_get['tag'] = $id;
  294. if (!isset(self::$tags[$id])) {
  295. $tagDAO = FreshRSS_Factory::createTagDao();
  296. $tag = $tagDAO->searchById($id);
  297. if (!$tag) {
  298. throw new FreshRSS_Context_Exception('Invalid tag: ' . $id);
  299. }
  300. } else {
  301. $tag = self::$tags[$id];
  302. }
  303. self::$name = $tag->name();
  304. self::$get_unread = $tag->nbUnread();
  305. break;
  306. case 'T':
  307. $tagDAO = FreshRSS_Factory::createTagDao();
  308. self::$current_get['tags'] = true;
  309. self::$name = _t('index.menu.tags');
  310. self::$get_unread = $tagDAO->countNotRead();
  311. break;
  312. default:
  313. throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
  314. }
  315. self::_nextGet();
  316. }
  317. /**
  318. * Set the value of $next_get attribute.
  319. */
  320. private static function _nextGet() {
  321. $get = self::currentGet();
  322. // By default, $next_get == $get
  323. self::$next_get = $get;
  324. if (empty(self::$categories)) {
  325. $catDAO = FreshRSS_Factory::createCategoryDao();
  326. self::$categories = $catDAO->listCategories();
  327. }
  328. if (self::$user_conf->onread_jump_next && strlen($get) > 2) {
  329. $another_unread_id = '';
  330. $found_current_get = false;
  331. switch ($get[0]) {
  332. case 'f':
  333. // We search the next unread feed with the following priorities: next in same category, or previous in same category, or next, or previous.
  334. foreach (self::$categories as $cat) {
  335. $sameCat = false;
  336. foreach ($cat->feeds() as $feed) {
  337. if ($found_current_get) {
  338. if ($feed->nbNotRead() > 0) {
  339. $another_unread_id = $feed->id();
  340. break 2;
  341. }
  342. } elseif ($feed->id() == self::$current_get['feed']) {
  343. $found_current_get = true;
  344. } elseif ($feed->nbNotRead() > 0) {
  345. $another_unread_id = $feed->id();
  346. $sameCat = true;
  347. }
  348. }
  349. if ($found_current_get && $sameCat) {
  350. break;
  351. }
  352. }
  353. // If there is no more unread feed, show main stream
  354. self::$next_get = $another_unread_id == '' ? 'a' : 'f_' . $another_unread_id;
  355. break;
  356. case 'c':
  357. // We search the next category with at least one unread article.
  358. foreach (self::$categories as $cat) {
  359. if ($cat->id() == self::$current_get['category']) {
  360. // Here is our current category! Next one could be our
  361. // champion if it has unread articles.
  362. $found_current_get = true;
  363. continue;
  364. }
  365. if ($cat->nbNotRead() > 0) {
  366. $another_unread_id = $cat->id();
  367. if ($found_current_get) {
  368. // Unread articles and the current category has
  369. // already been found? Leave the loop!
  370. break;
  371. }
  372. }
  373. }
  374. // If there is no more unread category, show main stream
  375. self::$next_get = $another_unread_id == '' ? 'a' : 'c_' . $another_unread_id;
  376. break;
  377. }
  378. }
  379. }
  380. /**
  381. * Determine if the auto remove is available in the current context.
  382. * This feature is available if:
  383. * - it is activated in the configuration
  384. * - the "read" state is not enable
  385. * - the "unread" state is enable
  386. *
  387. * @return boolean
  388. */
  389. public static function isAutoRemoveAvailable() {
  390. if (!self::$user_conf->auto_remove_article) {
  391. return false;
  392. }
  393. if (self::isStateEnabled(FreshRSS_Entry::STATE_READ)) {
  394. return false;
  395. }
  396. if (!self::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ)) {
  397. return false;
  398. }
  399. return true;
  400. }
  401. /**
  402. * Determine if the "sticky post" option is enabled. It can be enable
  403. * by the user when it is selected in the configuration page or by the
  404. * application when the context allows to auto-remove articles when they
  405. * are read.
  406. *
  407. * @return boolean
  408. */
  409. public static function isStickyPostEnabled() {
  410. if (self::$user_conf->sticky_post) {
  411. return true;
  412. }
  413. if (self::isAutoRemoveAvailable()) {
  414. return true;
  415. }
  416. return false;
  417. }
  418. }