UserQuery.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Contains the description of a user query
  5. *
  6. * It allows to extract the meaningful bits of the query to be manipulated in an
  7. * easy way.
  8. */
  9. class FreshRSS_UserQuery {
  10. private bool $deprecated = false;
  11. private string $get = '';
  12. private string $get_name = '';
  13. private string $get_type = '';
  14. private string $name = '';
  15. private string $order = '';
  16. private FreshRSS_BooleanSearch $search;
  17. private int $state = 0;
  18. private string $url = '';
  19. private string $token = '';
  20. private bool $shareRss = false;
  21. private bool $shareOpml = false;
  22. /** @var array<int,FreshRSS_Category> $categories */
  23. private array $categories;
  24. /** @var array<int,FreshRSS_Tag> $labels */
  25. private array $labels;
  26. public static function generateToken(string $salt): string {
  27. if (!FreshRSS_Context::hasSystemConf()) {
  28. return '';
  29. }
  30. $hash = md5(FreshRSS_Context::systemConf()->salt . $salt . random_bytes(16));
  31. if (function_exists('gmp_init')) {
  32. // Shorten the hash if possible by converting from base 16 to base 62
  33. $hash = gmp_strval(gmp_init($hash, 16), 62);
  34. }
  35. return $hash;
  36. }
  37. /**
  38. * @param array{get?:string,name?:string,order?:string,search?:string,state?:int,url?:string,token?:string,shareRss?:bool,shareOpml?:bool} $query
  39. * @param array<int,FreshRSS_Category> $categories
  40. * @param array<int,FreshRSS_Tag> $labels
  41. */
  42. public function __construct(array $query, array $categories, array $labels) {
  43. $this->categories = $categories;
  44. $this->labels = $labels;
  45. if (isset($query['get'])) {
  46. $this->parseGet($query['get']);
  47. }
  48. if (isset($query['name'])) {
  49. $this->name = trim($query['name']);
  50. }
  51. if (isset($query['order'])) {
  52. $this->order = $query['order'];
  53. }
  54. if (empty($query['url'])) {
  55. if (!empty($query)) {
  56. unset($query['name']);
  57. $this->url = Minz_Url::display(['params' => $query]);
  58. }
  59. } else {
  60. $this->url = $query['url'];
  61. }
  62. if (!isset($query['search'])) {
  63. $query['search'] = '';
  64. }
  65. if (!empty($query['token'])) {
  66. $this->token = $query['token'];
  67. }
  68. if (isset($query['shareRss'])) {
  69. $this->shareRss = $query['shareRss'];
  70. }
  71. if (isset($query['shareOpml'])) {
  72. $this->shareOpml = $query['shareOpml'];
  73. }
  74. // linked too deeply with the search object, need to use dependency injection
  75. $this->search = new FreshRSS_BooleanSearch($query['search'], 0, 'AND', false);
  76. if (!empty($query['state'])) {
  77. $this->state = intval($query['state']);
  78. }
  79. }
  80. /**
  81. * Convert the current object to an array.
  82. *
  83. * @return array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string,'token'?:string}
  84. */
  85. public function toArray(): array {
  86. return array_filter([
  87. 'get' => $this->get,
  88. 'name' => $this->name,
  89. 'order' => $this->order,
  90. 'search' => $this->search->getRawInput(),
  91. 'state' => $this->state,
  92. 'url' => $this->url,
  93. 'token' => $this->token,
  94. 'shareRss' => $this->shareRss,
  95. 'shareOpml' => $this->shareOpml,
  96. ]);
  97. }
  98. /**
  99. * Parse the get parameter in the query string to extract its name and type
  100. */
  101. private function parseGet(string $get): void {
  102. $this->get = $get;
  103. if (preg_match('/(?P<type>[acfistT])(_(?P<id>\d+))?/', $get, $matches)) {
  104. $id = intval($matches['id'] ?? '0');
  105. switch ($matches['type']) {
  106. case 'a':
  107. $this->get_type = 'all';
  108. break;
  109. case 'c':
  110. $this->get_type = 'category';
  111. $c = $this->categories[$id] ?? null;
  112. $this->get_name = $c === null ? '' : $c->name();
  113. break;
  114. case 'f':
  115. $this->get_type = 'feed';
  116. $f = FreshRSS_Category::findFeed($this->categories, $id);
  117. $this->get_name = $f === null ? '' : $f->name();
  118. break;
  119. case 'i':
  120. $this->get_type = 'important';
  121. break;
  122. case 's':
  123. $this->get_type = 'favorite';
  124. break;
  125. case 't':
  126. $this->get_type = 'label';
  127. $l = $this->labels[$id] ?? null;
  128. $this->get_name = $l === null ? '' : $l->name();
  129. break;
  130. case 'T':
  131. $this->get_type = 'all_labels';
  132. break;
  133. }
  134. if ($this->get_name === '' && in_array($matches['type'], ['c', 'f', 't'], true)) {
  135. $this->deprecated = true;
  136. }
  137. }
  138. }
  139. /**
  140. * Check if the current user query is deprecated.
  141. * It is deprecated if the category or the feed used in the query are
  142. * not existing.
  143. */
  144. public function isDeprecated(): bool {
  145. return $this->deprecated;
  146. }
  147. /**
  148. * Check if the user query has parameters.
  149. * If the type is 'all', it is considered equal to no parameters
  150. */
  151. public function hasParameters(): bool {
  152. if ($this->get_type === 'all') {
  153. return false;
  154. }
  155. if ($this->hasSearch()) {
  156. return true;
  157. }
  158. if ($this->state) {
  159. return true;
  160. }
  161. if ($this->order) {
  162. return true;
  163. }
  164. if ($this->get) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * Check if there is a search in the search object
  171. */
  172. public function hasSearch(): bool {
  173. return $this->search->getRawInput() !== '';
  174. }
  175. public function getGet(): string {
  176. return $this->get;
  177. }
  178. public function getGetName(): string {
  179. return $this->get_name;
  180. }
  181. public function getGetType(): string {
  182. return $this->get_type;
  183. }
  184. public function getName(): string {
  185. return $this->name;
  186. }
  187. public function getOrder(): string {
  188. return $this->order ?: FreshRSS_Context::userConf()->sort_order;
  189. }
  190. public function getSearch(): FreshRSS_BooleanSearch {
  191. return $this->search;
  192. }
  193. public function getState(): int {
  194. $state = $this->state;
  195. if (!($state & FreshRSS_Entry::STATE_READ) && !($state & FreshRSS_Entry::STATE_NOT_READ)) {
  196. $state |= FreshRSS_Entry::STATE_READ | FreshRSS_Entry::STATE_NOT_READ;
  197. }
  198. if (!($state & FreshRSS_Entry::STATE_FAVORITE) && !($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  199. $state |= FreshRSS_Entry::STATE_FAVORITE | FreshRSS_Entry::STATE_NOT_FAVORITE;
  200. }
  201. return $state;
  202. }
  203. public function getUrl(): string {
  204. return $this->url;
  205. }
  206. public function getToken(): string {
  207. return $this->token;
  208. }
  209. public function setToken(string $token): void {
  210. $this->token = $token;
  211. }
  212. public function setShareRss(bool $shareRss): void {
  213. $this->shareRss = $shareRss;
  214. }
  215. public function shareRss(): bool {
  216. return $this->shareRss;
  217. }
  218. public function setShareOpml(bool $shareOpml): void {
  219. $this->shareOpml = $shareOpml;
  220. }
  221. public function shareOpml(): bool {
  222. return $this->shareOpml;
  223. }
  224. protected function sharedUrl(bool $xmlEscaped = true): string {
  225. $currentUser = Minz_User::name() ?? '';
  226. return Minz_Url::display("/api/query.php?user={$currentUser}&t={$this->token}", $xmlEscaped ? 'html' : '', true);
  227. }
  228. public function sharedUrlRss(bool $xmlEscaped = true): string {
  229. if ($this->shareRss && $this->token !== '') {
  230. return $this->sharedUrl($xmlEscaped) . ($xmlEscaped ? '&amp;' : '&') . 'f=rss';
  231. }
  232. return '';
  233. }
  234. public function sharedUrlHtml(bool $xmlEscaped = true): string {
  235. if ($this->shareRss && $this->token !== '') {
  236. return $this->sharedUrl($xmlEscaped) . ($xmlEscaped ? '&amp;' : '&') . 'f=html';
  237. }
  238. return '';
  239. }
  240. /**
  241. * OPML is only safe for some query types, otherwise it risks leaking unwanted feed information.
  242. */
  243. public function safeForOpml(): bool {
  244. return in_array($this->get_type, ['all', 'category', 'feed'], true);
  245. }
  246. public function sharedUrlOpml(bool $xmlEscaped = true): string {
  247. if ($this->shareOpml && $this->token !== '' && $this->safeForOpml()) {
  248. return $this->sharedUrl($xmlEscaped) . ($xmlEscaped ? '&amp;' : '&') . 'f=opml';
  249. }
  250. return '';
  251. }
  252. }