Context.php 9.8 KB

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