Context.php 10 KB

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