4
0

UserQuery.php 9.2 KB

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