Search.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. declare(strict_types=1);
  3. require_once(LIB_PATH . '/lib_date.php');
  4. /**
  5. * Contains a search from the search form.
  6. *
  7. * It allows to extract meaningful bits of the search and store them in a
  8. * convenient object
  9. */
  10. class FreshRSS_Search {
  11. /**
  12. * This contains the user input string
  13. */
  14. private string $raw_input = '';
  15. // The following properties are extracted from the raw input
  16. /** @var array<string>|null */
  17. private ?array $entry_ids = null;
  18. /** @var array<int>|null */
  19. private ?array $feed_ids = null;
  20. /** @var array<int>|'*'|null */
  21. private $label_ids = null;
  22. /** @var array<string>|null */
  23. private ?array $label_names = null;
  24. /** @var array<string>|null */
  25. private ?array $intitle = null;
  26. /** @var array<string>|null */
  27. private ?array $intitle_regex = null;
  28. /** @var int|false|null */
  29. private $min_date = null;
  30. /** @var int|false|null */
  31. private $max_date = null;
  32. /** @var int|false|null */
  33. private $min_pubdate = null;
  34. /** @var int|false|null */
  35. private $max_pubdate = null;
  36. /** @var array<string>|null */
  37. private ?array $inurl = null;
  38. /** @var array<string>|null */
  39. private ?array $inurl_regex = null;
  40. /** @var array<string>|null */
  41. private ?array $author = null;
  42. /** @var array<string>|null */
  43. private ?array $author_regex = null;
  44. /** @var array<string>|null */
  45. private ?array $tags = null;
  46. /** @var array<string>|null */
  47. private ?array $tags_regex = null;
  48. /** @var array<string>|null */
  49. private ?array $search = null;
  50. /** @var array<string>|null */
  51. private ?array $search_regex = null;
  52. /** @var array<string>|null */
  53. private ?array $not_entry_ids = null;
  54. /** @var array<int>|null */
  55. private ?array $not_feed_ids = null;
  56. /** @var array<int>|'*'|null */
  57. private $not_label_ids = null;
  58. /** @var array<string>|null */
  59. private ?array $not_label_names = null;
  60. /** @var array<string>|null */
  61. private ?array $not_intitle = null;
  62. /** @var array<string>|null */
  63. private ?array $not_intitle_regex = null;
  64. /** @var int|false|null */
  65. private $not_min_date = null;
  66. /** @var int|false|null */
  67. private $not_max_date = null;
  68. /** @var int|false|null */
  69. private $not_min_pubdate = null;
  70. /** @var int|false|null */
  71. private $not_max_pubdate = null;
  72. /** @var array<string>|null */
  73. private ?array $not_inurl = null;
  74. /** @var array<string>|null */
  75. private ?array $not_inurl_regex = null;
  76. /** @var array<string>|null */
  77. private ?array $not_author = null;
  78. /** @var array<string>|null */
  79. private ?array $not_author_regex = null;
  80. /** @var array<string>|null */
  81. private ?array $not_tags = null;
  82. /** @var array<string>|null */
  83. private ?array $not_tags_regex = null;
  84. /** @var array<string>|null */
  85. private ?array $not_search = null;
  86. /** @var array<string>|null */
  87. private ?array $not_search_regex = null;
  88. public function __construct(string $input) {
  89. $input = self::cleanSearch($input);
  90. $input = self::unescape($input);
  91. $this->raw_input = $input;
  92. $input = $this->parseNotEntryIds($input);
  93. $input = $this->parseNotFeedIds($input);
  94. $input = $this->parseNotLabelIds($input);
  95. $input = $this->parseNotLabelNames($input);
  96. $input = $this->parseNotPubdateSearch($input);
  97. $input = $this->parseNotDateSearch($input);
  98. $input = $this->parseNotIntitleSearch($input);
  99. $input = $this->parseNotAuthorSearch($input);
  100. $input = $this->parseNotInurlSearch($input);
  101. $input = $this->parseNotTagsSearch($input);
  102. $input = $this->parseEntryIds($input);
  103. $input = $this->parseFeedIds($input);
  104. $input = $this->parseLabelIds($input);
  105. $input = $this->parseLabelNames($input);
  106. $input = $this->parsePubdateSearch($input);
  107. $input = $this->parseDateSearch($input);
  108. $input = $this->parseIntitleSearch($input);
  109. $input = $this->parseAuthorSearch($input);
  110. $input = $this->parseInurlSearch($input);
  111. $input = $this->parseTagsSearch($input);
  112. $input = $this->parseQuotedSearch($input);
  113. $input = $this->parseNotSearch($input);
  114. $this->parseSearch($input);
  115. }
  116. #[\Override]
  117. public function __toString(): string {
  118. return $this->getRawInput();
  119. }
  120. public function getRawInput(): string {
  121. return $this->raw_input;
  122. }
  123. /** @return array<string>|null */
  124. public function getEntryIds(): ?array {
  125. return $this->entry_ids;
  126. }
  127. /** @return array<string>|null */
  128. public function getNotEntryIds(): ?array {
  129. return $this->not_entry_ids;
  130. }
  131. /** @return array<int>|null */
  132. public function getFeedIds(): ?array {
  133. return $this->feed_ids;
  134. }
  135. /** @return array<int>|null */
  136. public function getNotFeedIds(): ?array {
  137. return $this->not_feed_ids;
  138. }
  139. /** @return array<int>|'*'|null */
  140. public function getLabelIds(): array|string|null {
  141. return $this->label_ids;
  142. }
  143. /** @return array<int>|'*'|null */
  144. public function getNotLabelIds(): array|string|null {
  145. return $this->not_label_ids;
  146. }
  147. /** @return array<string>|null */
  148. public function getLabelNames(): ?array {
  149. return $this->label_names;
  150. }
  151. /** @return array<string>|null */
  152. public function getNotLabelNames(): ?array {
  153. return $this->not_label_names;
  154. }
  155. /** @return array<string>|null */
  156. public function getIntitle(): ?array {
  157. return $this->intitle;
  158. }
  159. /** @return array<string>|null */
  160. public function getIntitleRegex(): ?array {
  161. return $this->intitle_regex;
  162. }
  163. /** @return array<string>|null */
  164. public function getNotIntitle(): ?array {
  165. return $this->not_intitle;
  166. }
  167. /** @return array<string>|null */
  168. public function getNotIntitleRegex(): ?array {
  169. return $this->not_intitle_regex;
  170. }
  171. public function getMinDate(): ?int {
  172. return $this->min_date ?: null;
  173. }
  174. public function getNotMinDate(): ?int {
  175. return $this->not_min_date ?: null;
  176. }
  177. public function setMinDate(int $value): void {
  178. $this->min_date = $value;
  179. }
  180. public function getMaxDate(): ?int {
  181. return $this->max_date ?: null;
  182. }
  183. public function getNotMaxDate(): ?int {
  184. return $this->not_max_date ?: null;
  185. }
  186. public function setMaxDate(int $value): void {
  187. $this->max_date = $value;
  188. }
  189. public function getMinPubdate(): ?int {
  190. return $this->min_pubdate ?: null;
  191. }
  192. public function getNotMinPubdate(): ?int {
  193. return $this->not_min_pubdate ?: null;
  194. }
  195. public function getMaxPubdate(): ?int {
  196. return $this->max_pubdate ?: null;
  197. }
  198. public function getNotMaxPubdate(): ?int {
  199. return $this->not_max_pubdate ?: null;
  200. }
  201. /** @return array<string>|null */
  202. public function getInurl(): ?array {
  203. return $this->inurl;
  204. }
  205. /** @return array<string>|null */
  206. public function getInurlRegex(): ?array {
  207. return $this->inurl_regex;
  208. }
  209. /** @return array<string>|null */
  210. public function getNotInurl(): ?array {
  211. return $this->not_inurl;
  212. }
  213. /** @return array<string>|null */
  214. public function getNotInurlRegex(): ?array {
  215. return $this->not_inurl_regex;
  216. }
  217. /** @return array<string>|null */
  218. public function getAuthor(): ?array {
  219. return $this->author;
  220. }
  221. /** @return array<string>|null */
  222. public function getAuthorRegex(): ?array {
  223. return $this->author_regex;
  224. }
  225. /** @return array<string>|null */
  226. public function getNotAuthor(): ?array {
  227. return $this->not_author;
  228. }
  229. /** @return array<string>|null */
  230. public function getNotAuthorRegex(): ?array {
  231. return $this->not_author_regex;
  232. }
  233. /** @return array<string>|null */
  234. public function getTags(): ?array {
  235. return $this->tags;
  236. }
  237. /** @return array<string>|null */
  238. public function getTagsRegex(): ?array {
  239. return $this->tags_regex;
  240. }
  241. /** @return array<string>|null */
  242. public function getNotTags(): ?array {
  243. return $this->not_tags;
  244. }
  245. /** @return array<string>|null */
  246. public function getNotTagsRegex(): ?array {
  247. return $this->not_tags_regex;
  248. }
  249. /** @return array<string>|null */
  250. public function getSearch(): ?array {
  251. return $this->search;
  252. }
  253. /** @return array<string>|null */
  254. public function getSearchRegex(): ?array {
  255. return $this->search_regex;
  256. }
  257. /** @return array<string>|null */
  258. public function getNotSearch(): ?array {
  259. return $this->not_search;
  260. }
  261. /** @return array<string>|null */
  262. public function getNotSearchRegex(): ?array {
  263. return $this->not_search_regex;
  264. }
  265. /**
  266. * @param array<string>|null $anArray
  267. * @return array<string>
  268. */
  269. private static function removeEmptyValues(?array $anArray): array {
  270. return empty($anArray) ? [] : array_filter($anArray, static fn(string $value) => $value !== '');
  271. }
  272. /**
  273. * @param array<string>|string $value
  274. * @return ($value is array ? array<string> : string)
  275. */
  276. private static function decodeSpaces($value): array|string {
  277. if (is_array($value)) {
  278. for ($i = count($value) - 1; $i >= 0; $i--) {
  279. $value[$i] = self::decodeSpaces($value[$i]);
  280. }
  281. } else {
  282. $value = trim(str_replace('+', ' ', $value));
  283. }
  284. return $value;
  285. }
  286. /**
  287. * @param array<string> $strings
  288. * @return array<string>
  289. */
  290. private static function htmlspecialchars_decodes(array $strings): array {
  291. return array_map(static fn(string $s) => htmlspecialchars_decode($s, ENT_QUOTES), $strings);
  292. }
  293. /**
  294. * Parse the search string to find entry (article) IDs.
  295. */
  296. private function parseEntryIds(string $input): string {
  297. if (preg_match_all('/\\be:(?P<search>[0-9,]*)/', $input, $matches)) {
  298. $input = str_replace($matches[0], '', $input);
  299. $ids_lists = $matches['search'];
  300. $this->entry_ids = [];
  301. foreach ($ids_lists as $ids_list) {
  302. $entry_ids = explode(',', $ids_list);
  303. $entry_ids = self::removeEmptyValues($entry_ids);
  304. if (!empty($entry_ids)) {
  305. $this->entry_ids = array_merge($this->entry_ids, $entry_ids);
  306. }
  307. }
  308. }
  309. return $input;
  310. }
  311. private function parseNotEntryIds(string $input): string {
  312. if (preg_match_all('/(?<=\\s|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
  313. $input = str_replace($matches[0], '', $input);
  314. $ids_lists = $matches['search'];
  315. $this->not_entry_ids = [];
  316. foreach ($ids_lists as $ids_list) {
  317. $entry_ids = explode(',', $ids_list);
  318. $entry_ids = self::removeEmptyValues($entry_ids);
  319. if (!empty($entry_ids)) {
  320. $this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
  321. }
  322. }
  323. }
  324. return $input;
  325. }
  326. private function parseFeedIds(string $input): string {
  327. if (preg_match_all('/\\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  328. $input = str_replace($matches[0], '', $input);
  329. $ids_lists = $matches['search'];
  330. $this->feed_ids = [];
  331. foreach ($ids_lists as $ids_list) {
  332. $feed_ids = explode(',', $ids_list);
  333. $feed_ids = self::removeEmptyValues($feed_ids);
  334. /** @var array<int> $feed_ids */
  335. $feed_ids = array_map('intval', $feed_ids);
  336. if (!empty($feed_ids)) {
  337. $this->feed_ids = array_merge($this->feed_ids, $feed_ids);
  338. }
  339. }
  340. }
  341. return $input;
  342. }
  343. private function parseNotFeedIds(string $input): string {
  344. if (preg_match_all('/(?<=\\s|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  345. $input = str_replace($matches[0], '', $input);
  346. $ids_lists = $matches['search'];
  347. $this->not_feed_ids = [];
  348. foreach ($ids_lists as $ids_list) {
  349. $feed_ids = explode(',', $ids_list);
  350. $feed_ids = self::removeEmptyValues($feed_ids);
  351. /** @var array<int> $feed_ids */
  352. $feed_ids = array_map('intval', $feed_ids);
  353. if (!empty($feed_ids)) {
  354. $this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
  355. }
  356. }
  357. }
  358. return $input;
  359. }
  360. /**
  361. * Parse the search string to find tags (labels) IDs.
  362. */
  363. private function parseLabelIds(string $input): string {
  364. if (preg_match_all('/\\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  365. $input = str_replace($matches[0], '', $input);
  366. $ids_lists = $matches['search'];
  367. $this->label_ids = [];
  368. foreach ($ids_lists as $ids_list) {
  369. if ($ids_list === '*') {
  370. $this->label_ids = '*';
  371. break;
  372. }
  373. $label_ids = explode(',', $ids_list);
  374. $label_ids = self::removeEmptyValues($label_ids);
  375. /** @var array<int> $label_ids */
  376. $label_ids = array_map('intval', $label_ids);
  377. if (!empty($label_ids)) {
  378. $this->label_ids = array_merge($this->label_ids, $label_ids);
  379. }
  380. }
  381. }
  382. return $input;
  383. }
  384. private function parseNotLabelIds(string $input): string {
  385. if (preg_match_all('/(?<=\\s|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  386. $input = str_replace($matches[0], '', $input);
  387. $ids_lists = $matches['search'];
  388. $this->not_label_ids = [];
  389. foreach ($ids_lists as $ids_list) {
  390. if ($ids_list === '*') {
  391. $this->not_label_ids = '*';
  392. break;
  393. }
  394. $label_ids = explode(',', $ids_list);
  395. $label_ids = self::removeEmptyValues($label_ids);
  396. /** @var array<int> $label_ids */
  397. $label_ids = array_map('intval', $label_ids);
  398. if (!empty($label_ids)) {
  399. $this->not_label_ids = array_merge($this->not_label_ids, $label_ids);
  400. }
  401. }
  402. }
  403. return $input;
  404. }
  405. /**
  406. * Parse the search string to find tags (labels) names.
  407. */
  408. private function parseLabelNames(string $input): string {
  409. $names_lists = [];
  410. if (preg_match_all('/\\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  411. $names_lists = $matches['search'];
  412. $input = str_replace($matches[0], '', $input);
  413. }
  414. if (preg_match_all('/\\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  415. $names_lists = array_merge($names_lists, $matches['search']);
  416. $input = str_replace($matches[0], '', $input);
  417. }
  418. if (!empty($names_lists)) {
  419. $this->label_names = [];
  420. foreach ($names_lists as $names_list) {
  421. $names_array = explode(',', $names_list);
  422. $names_array = self::removeEmptyValues($names_array);
  423. if (!empty($names_array)) {
  424. $this->label_names = array_merge($this->label_names, $names_array);
  425. }
  426. }
  427. }
  428. return $input;
  429. }
  430. /**
  431. * Parse the search string to find tags (labels) names to exclude.
  432. */
  433. private function parseNotLabelNames(string $input): string {
  434. $names_lists = [];
  435. if (preg_match_all('/(?<=\\s|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  436. $names_lists = $matches['search'];
  437. $input = str_replace($matches[0], '', $input);
  438. }
  439. if (preg_match_all('/(?<=\\s|^)[!-]labels?:(?P<search>[^\\s"]*)/', $input, $matches)) {
  440. $names_lists = array_merge($names_lists, $matches['search']);
  441. $input = str_replace($matches[0], '', $input);
  442. }
  443. if (!empty($names_lists)) {
  444. $this->not_label_names = [];
  445. foreach ($names_lists as $names_list) {
  446. $names_array = explode(',', $names_list);
  447. $names_array = self::removeEmptyValues($names_array);
  448. if (!empty($names_array)) {
  449. $this->not_label_names = array_merge($this->not_label_names, $names_array);
  450. }
  451. }
  452. }
  453. return $input;
  454. }
  455. /**
  456. * Parse the search string to find intitle keyword and the search related to it.
  457. * The search is the first word following the keyword.
  458. */
  459. private function parseIntitleSearch(string $input): string {
  460. if (preg_match_all('#\\bintitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  461. $this->intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  462. $input = str_replace($matches[0], '', $input);
  463. }
  464. if (preg_match_all('/\\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  465. $this->intitle = $matches['search'];
  466. $input = str_replace($matches[0], '', $input);
  467. }
  468. if (preg_match_all('/\\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  469. $this->intitle = array_merge($this->intitle ?: [], $matches['search']);
  470. $input = str_replace($matches[0], '', $input);
  471. }
  472. $this->intitle = self::removeEmptyValues($this->intitle);
  473. if (empty($this->intitle)) {
  474. $this->intitle = null;
  475. }
  476. return $input;
  477. }
  478. private function parseNotIntitleSearch(string $input): string {
  479. if (preg_match_all('#(?<=\\s|^)[!-]intitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  480. $this->not_intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  481. $input = str_replace($matches[0], '', $input);
  482. }
  483. if (preg_match_all('/(?<=\\s|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  484. $this->not_intitle = $matches['search'];
  485. $input = str_replace($matches[0], '', $input);
  486. }
  487. if (preg_match_all('/(?<=\\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  488. $this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);
  489. $input = str_replace($matches[0], '', $input);
  490. }
  491. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  492. if (empty($this->not_intitle)) {
  493. $this->not_intitle = null;
  494. }
  495. return $input;
  496. }
  497. /**
  498. * Parse the search string to find author keyword and the search related to it.
  499. * The search is the first word following the keyword except when using
  500. * a delimiter. Supported delimiters are single quote (') and double quotes (").
  501. */
  502. private function parseAuthorSearch(string $input): string {
  503. if (preg_match_all('#\\bauthor:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  504. $this->author_regex = self::htmlspecialchars_decodes($matches['search']);
  505. $input = str_replace($matches[0], '', $input);
  506. }
  507. if (preg_match_all('/\\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  508. $this->author = $matches['search'];
  509. $input = str_replace($matches[0], '', $input);
  510. }
  511. if (preg_match_all('/\\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  512. $this->author = array_merge($this->author ?: [], $matches['search']);
  513. $input = str_replace($matches[0], '', $input);
  514. }
  515. $this->author = self::removeEmptyValues($this->author);
  516. if (empty($this->author)) {
  517. $this->author = null;
  518. }
  519. return $input;
  520. }
  521. private function parseNotAuthorSearch(string $input): string {
  522. if (preg_match_all('#(?<=\\s|^)[!-]author:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  523. $this->not_author_regex = self::htmlspecialchars_decodes($matches['search']);
  524. $input = str_replace($matches[0], '', $input);
  525. }
  526. if (preg_match_all('/(?<=\\s|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  527. $this->not_author = $matches['search'];
  528. $input = str_replace($matches[0], '', $input);
  529. }
  530. if (preg_match_all('/(?<=\\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  531. $this->not_author = array_merge($this->not_author ?: [], $matches['search']);
  532. $input = str_replace($matches[0], '', $input);
  533. }
  534. $this->not_author = self::removeEmptyValues($this->not_author);
  535. if (empty($this->not_author)) {
  536. $this->not_author = null;
  537. }
  538. return $input;
  539. }
  540. /**
  541. * Parse the search string to find inurl keyword and the search related to it.
  542. * The search is the first word following the keyword.
  543. */
  544. private function parseInurlSearch(string $input): string {
  545. if (preg_match_all('#\\binurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  546. $this->inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  547. $input = str_replace($matches[0], '', $input);
  548. }
  549. if (preg_match_all('/\\binurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  550. $this->inurl = $matches['search'];
  551. $input = str_replace($matches[0], '', $input);
  552. }
  553. if (preg_match_all('/\\binurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  554. $this->inurl = $matches['search'];
  555. $input = str_replace($matches[0], '', $input);
  556. }
  557. $this->inurl = self::removeEmptyValues($this->inurl);
  558. if (empty($this->inurl)) {
  559. $this->inurl = null;
  560. }
  561. return $input;
  562. }
  563. private function parseNotInurlSearch(string $input): string {
  564. if (preg_match_all('#(?<=\\s|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  565. $this->not_inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  566. $input = str_replace($matches[0], '', $input);
  567. }
  568. if (preg_match_all('/(?<=\\s|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  569. $this->not_inurl = $matches['search'];
  570. $input = str_replace($matches[0], '', $input);
  571. }
  572. if (preg_match_all('/(?<=\\s|^)[!-]inurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  573. $this->not_inurl = $matches['search'];
  574. $input = str_replace($matches[0], '', $input);
  575. }
  576. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  577. if (empty($this->not_inurl)) {
  578. $this->not_inurl = null;
  579. }
  580. return $input;
  581. }
  582. /**
  583. * Parse the search string to find date keyword and the search related to it.
  584. * The search is the first word following the keyword.
  585. */
  586. private function parseDateSearch(string $input): string {
  587. if (preg_match_all('/\\bdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  588. $input = str_replace($matches[0], '', $input);
  589. $dates = self::removeEmptyValues($matches['search']);
  590. if (!empty($dates[0])) {
  591. [$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
  592. }
  593. }
  594. return $input;
  595. }
  596. private function parseNotDateSearch(string $input): string {
  597. if (preg_match_all('/(?<=\\s|^)[!-]date:(?P<search>[^\\s]*)/', $input, $matches)) {
  598. $input = str_replace($matches[0], '', $input);
  599. $dates = self::removeEmptyValues($matches['search']);
  600. if (!empty($dates[0])) {
  601. [$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
  602. }
  603. }
  604. return $input;
  605. }
  606. /**
  607. * Parse the search string to find pubdate keyword and the search related to it.
  608. * The search is the first word following the keyword.
  609. */
  610. private function parsePubdateSearch(string $input): string {
  611. if (preg_match_all('/\\bpubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  612. $input = str_replace($matches[0], '', $input);
  613. $dates = self::removeEmptyValues($matches['search']);
  614. if (!empty($dates[0])) {
  615. [$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
  616. }
  617. }
  618. return $input;
  619. }
  620. private function parseNotPubdateSearch(string $input): string {
  621. if (preg_match_all('/(?<=\\s|^)[!-]pubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  622. $input = str_replace($matches[0], '', $input);
  623. $dates = self::removeEmptyValues($matches['search']);
  624. if (!empty($dates[0])) {
  625. [$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
  626. }
  627. }
  628. return $input;
  629. }
  630. /**
  631. * Parse the search string to find tags keyword (# followed by a word)
  632. * and the search related to it.
  633. * The search is the first word following the #.
  634. */
  635. private function parseTagsSearch(string $input): string {
  636. if (preg_match_all('%#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  637. $this->tags_regex = self::htmlspecialchars_decodes($matches['search']);
  638. $input = str_replace($matches[0], '', $input);
  639. }
  640. if (preg_match_all('/#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  641. $this->tags = $matches['search'];
  642. $input = str_replace($matches[0], '', $input);
  643. }
  644. if (preg_match_all('/#(?P<search>[^\\s]+)/', $input, $matches)) {
  645. $this->tags = $matches['search'];
  646. $input = str_replace($matches[0], '', $input);
  647. }
  648. $this->tags = self::removeEmptyValues($this->tags);
  649. if (empty($this->tags)) {
  650. $this->tags = null;
  651. } else {
  652. $this->tags = self::decodeSpaces($this->tags);
  653. }
  654. return $input;
  655. }
  656. private function parseNotTagsSearch(string $input): string {
  657. if (preg_match_all('%(?<=\\s|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  658. $this->not_tags_regex = self::htmlspecialchars_decodes($matches['search']);
  659. $input = str_replace($matches[0], '', $input);
  660. }
  661. if (preg_match_all('/(?<=\\s|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  662. $this->not_tags = $matches['search'];
  663. $input = str_replace($matches[0], '', $input);
  664. }
  665. if (preg_match_all('/(?<=\\s|^)[!-]#(?P<search>[^\\s]+)/', $input, $matches)) {
  666. $this->not_tags = $matches['search'];
  667. $input = str_replace($matches[0], '', $input);
  668. }
  669. $this->not_tags = self::removeEmptyValues($this->not_tags);
  670. if (empty($this->not_tags)) {
  671. $this->not_tags = null;
  672. } else {
  673. $this->not_tags = self::decodeSpaces($this->not_tags);
  674. }
  675. return $input;
  676. }
  677. /**
  678. * Parse the search string to find search values.
  679. * Every word is a distinct search value using a delimiter.
  680. * Supported delimiters are single quote (') and double quotes (") and regex (/).
  681. */
  682. private function parseQuotedSearch(string $input): string {
  683. $input = self::cleanSearch($input);
  684. if ($input === '') {
  685. return '';
  686. }
  687. if (preg_match_all('#(?<=\\s|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  688. $this->search_regex = self::htmlspecialchars_decodes($matches['search']);
  689. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  690. $input = str_replace($matches[0], '', $input);
  691. }
  692. if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  693. $this->search = $matches['search'];
  694. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  695. $input = str_replace($matches[0], '', $input);
  696. }
  697. return $input;
  698. }
  699. /**
  700. * Parse the search string to find search values.
  701. * Every word is a distinct search value.
  702. */
  703. private function parseSearch(string $input): string {
  704. $input = self::cleanSearch($input);
  705. if ($input === '') {
  706. return '';
  707. }
  708. if (is_array($this->search)) {
  709. $this->search = array_merge($this->search, explode(' ', $input));
  710. } else {
  711. $this->search = explode(' ', $input);
  712. }
  713. return $input;
  714. }
  715. private function parseNotSearch(string $input): string {
  716. $input = self::cleanSearch($input);
  717. if ($input === '') {
  718. return '';
  719. }
  720. if (preg_match_all('#(?<=\\s|^)[!-](?P<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  721. $this->not_search_regex = self::htmlspecialchars_decodes($matches['search']);
  722. $input = str_replace($matches[0], '', $input);
  723. }
  724. if (preg_match_all('/(?<=\\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  725. $this->not_search = $matches['search'];
  726. $input = str_replace($matches[0], '', $input);
  727. }
  728. $input = self::cleanSearch($input);
  729. if ($input === '') {
  730. return '';
  731. }
  732. if (preg_match_all('/(?<=\\s|^)[!-](?P<search>[^\\s]+)/', $input, $matches)) {
  733. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
  734. $input = str_replace($matches[0], '', $input);
  735. }
  736. $this->not_search = self::removeEmptyValues($this->not_search);
  737. return $input;
  738. }
  739. /**
  740. * Remove all unnecessary spaces in the search
  741. */
  742. private static function cleanSearch(string $input): string {
  743. $input = preg_replace('/\\s+/', ' ', $input);
  744. if (!is_string($input)) {
  745. return '';
  746. }
  747. return trim($input);
  748. }
  749. /** Remove escaping backslashes for parenthesis logic */
  750. private static function unescape(string $input): string {
  751. return str_replace(['\\(', '\\)'], ['(', ')'], $input);
  752. }
  753. }