UserQuery.php 8.2 KB

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