UserQuery.php 12 KB

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