Context.php 12 KB

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