UserQuery.php 8.6 KB

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