Context.php 12 KB

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