Context.php 12 KB

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