Context.php 12 KB

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