UserQuery.php 12 KB

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