UserQuery.php 8.7 KB

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