Context.php 12 KB

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