Search.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 implements \Stringable {
  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 list<string>|null */
  17. private ?array $entry_ids = null;
  18. /** @var list<int>|null */
  19. private ?array $feed_ids = null;
  20. /** @var list<int>|null */
  21. private ?array $category_ids = null;
  22. /** @var list<list<int>|'*'>|null */
  23. private $label_ids = null;
  24. /** @var list<list<string>>|null */
  25. private ?array $label_names = null;
  26. /** @var list<string>|null */
  27. private ?array $intitle = null;
  28. /** @var list<string>|null */
  29. private ?array $intitle_regex = null;
  30. /** @var list<string>|null */
  31. private ?array $intext = null;
  32. /** @var list<string>|null */
  33. private ?array $intext_regex = null;
  34. /** @var int|false|null */
  35. private $min_date = null;
  36. /** @var int|false|null */
  37. private $max_date = null;
  38. /** @var int|false|null */
  39. private $min_pubdate = null;
  40. /** @var int|false|null */
  41. private $max_pubdate = null;
  42. /** @var int|false|null */
  43. private $min_userdate = null;
  44. /** @var int|false|null */
  45. private $max_userdate = null;
  46. /** @var list<string>|null */
  47. private ?array $inurl = null;
  48. /** @var list<string>|null */
  49. private ?array $inurl_regex = null;
  50. /** @var list<string>|null */
  51. private ?array $author = null;
  52. /** @var list<string>|null */
  53. private ?array $author_regex = null;
  54. /** @var list<string>|null */
  55. private ?array $tags = null;
  56. /** @var list<string>|null */
  57. private ?array $tags_regex = null;
  58. /** @var list<string>|null */
  59. private ?array $search = null;
  60. /** @var list<string>|null */
  61. private ?array $search_regex = null;
  62. /** @var list<string>|null */
  63. private ?array $not_entry_ids = null;
  64. /** @var list<int>|null */
  65. private ?array $not_feed_ids = null;
  66. /** @var list<int>|null */
  67. private ?array $not_category_ids = null;
  68. /** @var list<list<int>|'*'>|null */
  69. private $not_label_ids = null;
  70. /** @var list<list<string>>|null */
  71. private ?array $not_label_names = null;
  72. /** @var list<string>|null */
  73. private ?array $not_intitle = null;
  74. /** @var list<string>|null */
  75. private ?array $not_intitle_regex = null;
  76. /** @var list<string>|null */
  77. private ?array $not_intext = null;
  78. /** @var list<string>|null */
  79. private ?array $not_intext_regex = null;
  80. /** @var int|false|null */
  81. private $not_min_date = null;
  82. /** @var int|false|null */
  83. private $not_max_date = null;
  84. /** @var int|false|null */
  85. private $not_min_pubdate = null;
  86. /** @var int|false|null */
  87. private $not_max_pubdate = null;
  88. /** @var int|false|null */
  89. private $not_min_userdate = null;
  90. /** @var int|false|null */
  91. private $not_max_userdate = null;
  92. /** @var list<string>|null */
  93. private ?array $not_inurl = null;
  94. /** @var list<string>|null */
  95. private ?array $not_inurl_regex = null;
  96. /** @var list<string>|null */
  97. private ?array $not_author = null;
  98. /** @var list<string>|null */
  99. private ?array $not_author_regex = null;
  100. /** @var list<string>|null */
  101. private ?array $not_tags = null;
  102. /** @var list<string>|null */
  103. private ?array $not_tags_regex = null;
  104. /** @var list<string>|null */
  105. private ?array $not_search = null;
  106. /** @var list<string>|null */
  107. private ?array $not_search_regex = null;
  108. public function __construct(string $input) {
  109. $input = self::cleanSearch($input);
  110. $input = self::unescape($input);
  111. $input = FreshRSS_BooleanSearch::unescapeLiteralParentheses($input);
  112. $this->raw_input = $input;
  113. $input = $this->parseNotEntryIds($input);
  114. $input = $this->parseNotFeedIds($input);
  115. $input = $this->parseNotCategoryIds($input);
  116. $input = $this->parseNotLabelIds($input);
  117. $input = $this->parseNotLabelNames($input);
  118. $input = $this->parseNotUserdateSearch($input);
  119. $input = $this->parseNotPubdateSearch($input);
  120. $input = $this->parseNotDateSearch($input);
  121. $input = $this->parseNotIntitleSearch($input);
  122. $input = $this->parseNotIntextSearch($input);
  123. $input = $this->parseNotAuthorSearch($input);
  124. $input = $this->parseNotInurlSearch($input);
  125. $input = $this->parseNotTagsSearch($input);
  126. $input = $this->parseEntryIds($input);
  127. $input = $this->parseFeedIds($input);
  128. $input = $this->parseCategoryIds($input);
  129. $input = $this->parseLabelIds($input);
  130. $input = $this->parseLabelNames($input);
  131. $input = $this->parseUserdateSearch($input);
  132. $input = $this->parsePubdateSearch($input);
  133. $input = $this->parseDateSearch($input);
  134. $input = $this->parseIntitleSearch($input);
  135. $input = $this->parseIntextSearch($input);
  136. $input = $this->parseAuthorSearch($input);
  137. $input = $this->parseInurlSearch($input);
  138. $input = $this->parseTagsSearch($input);
  139. $input = $this->parseQuotedSearch($input);
  140. $input = $this->parseNotSearch($input);
  141. $this->parseSearch($input);
  142. }
  143. #[\Override]
  144. public function __toString(): string {
  145. return $this->getRawInput();
  146. }
  147. public function getRawInput(): string {
  148. return $this->raw_input;
  149. }
  150. /** @return list<string>|null */
  151. public function getEntryIds(): ?array {
  152. return $this->entry_ids;
  153. }
  154. /** @return list<string>|null */
  155. public function getNotEntryIds(): ?array {
  156. return $this->not_entry_ids;
  157. }
  158. /** @return list<int>|null */
  159. public function getFeedIds(): ?array {
  160. return $this->feed_ids;
  161. }
  162. /** @return list<int>|null */
  163. public function getNotFeedIds(): ?array {
  164. return $this->not_feed_ids;
  165. }
  166. /** @return list<int>|null */
  167. public function getCategoryIds(): ?array {
  168. return $this->category_ids;
  169. }
  170. /** @return list<int>|null */
  171. public function getNotCategoryIds(): ?array {
  172. return $this->not_category_ids;
  173. }
  174. /** @return list<list<int>|'*'>|null */
  175. public function getLabelIds(): array|null {
  176. return $this->label_ids;
  177. }
  178. /** @return list<list<int>|'*'>|null */
  179. public function getNotLabelIds(): array|null {
  180. return $this->not_label_ids;
  181. }
  182. /** @return list<list<string>>|null */
  183. public function getLabelNames(): ?array {
  184. return $this->label_names;
  185. }
  186. /** @return list<list<string>>|null */
  187. public function getNotLabelNames(): ?array {
  188. return $this->not_label_names;
  189. }
  190. /** @return list<string>|null */
  191. public function getIntitle(): ?array {
  192. return $this->intitle;
  193. }
  194. /** @return list<string>|null */
  195. public function getIntitleRegex(): ?array {
  196. return $this->intitle_regex;
  197. }
  198. /** @return list<string>|null */
  199. public function getNotIntitle(): ?array {
  200. return $this->not_intitle;
  201. }
  202. /** @return list<string>|null */
  203. public function getNotIntitleRegex(): ?array {
  204. return $this->not_intitle_regex;
  205. }
  206. /** @return list<string>|null */
  207. public function getIntext(): ?array {
  208. return $this->intext;
  209. }
  210. /** @return list<string>|null */
  211. public function getIntextRegex(): ?array {
  212. return $this->intext_regex;
  213. }
  214. /** @return list<string>|null */
  215. public function getNotIntext(): ?array {
  216. return $this->not_intext;
  217. }
  218. /** @return list<string>|null */
  219. public function getNotIntextRegex(): ?array {
  220. return $this->not_intext_regex;
  221. }
  222. public function getMinDate(): ?int {
  223. return $this->min_date ?: null;
  224. }
  225. public function getNotMinDate(): ?int {
  226. return $this->not_min_date ?: null;
  227. }
  228. public function setMinDate(int $value): void {
  229. $this->min_date = $value;
  230. }
  231. public function getMaxDate(): ?int {
  232. return $this->max_date ?: null;
  233. }
  234. public function getNotMaxDate(): ?int {
  235. return $this->not_max_date ?: null;
  236. }
  237. public function setMaxDate(int $value): void {
  238. $this->max_date = $value;
  239. }
  240. public function getMinPubdate(): ?int {
  241. return $this->min_pubdate ?: null;
  242. }
  243. public function getNotMinPubdate(): ?int {
  244. return $this->not_min_pubdate ?: null;
  245. }
  246. public function getMaxPubdate(): ?int {
  247. return $this->max_pubdate ?: null;
  248. }
  249. public function getNotMaxPubdate(): ?int {
  250. return $this->not_max_pubdate ?: null;
  251. }
  252. public function setMaxPubdate(int $value): void {
  253. $this->max_pubdate = $value;
  254. }
  255. public function getMinUserdate(): ?int {
  256. return $this->min_userdate ?: null;
  257. }
  258. public function getNotMinUserdate(): ?int {
  259. return $this->not_min_userdate ?: null;
  260. }
  261. public function getMaxUserdate(): ?int {
  262. return $this->max_userdate ?: null;
  263. }
  264. public function getNotMaxUserdate(): ?int {
  265. return $this->not_max_userdate ?: null;
  266. }
  267. /** @return list<string>|null */
  268. public function getInurl(): ?array {
  269. return $this->inurl;
  270. }
  271. /** @return list<string>|null */
  272. public function getInurlRegex(): ?array {
  273. return $this->inurl_regex;
  274. }
  275. /** @return list<string>|null */
  276. public function getNotInurl(): ?array {
  277. return $this->not_inurl;
  278. }
  279. /** @return list<string>|null */
  280. public function getNotInurlRegex(): ?array {
  281. return $this->not_inurl_regex;
  282. }
  283. /** @return list<string>|null */
  284. public function getAuthor(): ?array {
  285. return $this->author;
  286. }
  287. /** @return list<string>|null */
  288. public function getAuthorRegex(): ?array {
  289. return $this->author_regex;
  290. }
  291. /** @return list<string>|null */
  292. public function getNotAuthor(): ?array {
  293. return $this->not_author;
  294. }
  295. /** @return list<string>|null */
  296. public function getNotAuthorRegex(): ?array {
  297. return $this->not_author_regex;
  298. }
  299. /** @return list<string>|null */
  300. public function getTags(): ?array {
  301. return $this->tags;
  302. }
  303. /** @return list<string>|null */
  304. public function getTagsRegex(): ?array {
  305. return $this->tags_regex;
  306. }
  307. /** @return list<string>|null */
  308. public function getNotTags(): ?array {
  309. return $this->not_tags;
  310. }
  311. /** @return list<string>|null */
  312. public function getNotTagsRegex(): ?array {
  313. return $this->not_tags_regex;
  314. }
  315. /** @return list<string>|null */
  316. public function getSearch(): ?array {
  317. return $this->search;
  318. }
  319. /** @return list<string>|null */
  320. public function getSearchRegex(): ?array {
  321. return $this->search_regex;
  322. }
  323. /** @return list<string>|null */
  324. public function getNotSearch(): ?array {
  325. return $this->not_search;
  326. }
  327. /** @return list<string>|null */
  328. public function getNotSearchRegex(): ?array {
  329. return $this->not_search_regex;
  330. }
  331. /**
  332. * @param list<string>|null $anArray
  333. * @return list<string>
  334. */
  335. private static function removeEmptyValues(?array $anArray): array {
  336. return empty($anArray) ? [] : array_values(array_filter($anArray, static fn(string $value) => $value !== ''));
  337. }
  338. /**
  339. * @param list<string>|string $value
  340. * @return ($value is string ? string : list<string>)
  341. */
  342. private static function decodeSpaces(array|string $value): array|string {
  343. if (is_array($value)) {
  344. foreach ($value as &$val) {
  345. $val = self::decodeSpaces($val);
  346. }
  347. } else {
  348. $value = trim(str_replace('+', ' ', $value));
  349. }
  350. return $value;
  351. }
  352. /**
  353. * @param list<string> $strings
  354. * @return list<string>
  355. */
  356. private static function htmlspecialchars_decodes(array $strings): array {
  357. return array_map(static fn(string $s) => htmlspecialchars_decode($s, ENT_QUOTES), $strings);
  358. }
  359. /**
  360. * Parse the search string to find entry (article) IDs.
  361. */
  362. private function parseEntryIds(string $input): string {
  363. if (preg_match_all('/\\be:(?P<search>[0-9,]*)/', $input, $matches)) {
  364. $input = str_replace($matches[0], '', $input);
  365. $ids_lists = $matches['search'];
  366. $this->entry_ids = [];
  367. foreach ($ids_lists as $ids_list) {
  368. $entry_ids = explode(',', $ids_list);
  369. $entry_ids = self::removeEmptyValues($entry_ids);
  370. if (!empty($entry_ids)) {
  371. $this->entry_ids = array_merge($this->entry_ids, $entry_ids);
  372. }
  373. }
  374. }
  375. return $input;
  376. }
  377. private function parseNotEntryIds(string $input): string {
  378. if (preg_match_all('/(?<=[\\s(]|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
  379. $input = str_replace($matches[0], '', $input);
  380. $ids_lists = $matches['search'];
  381. $this->not_entry_ids = [];
  382. foreach ($ids_lists as $ids_list) {
  383. $entry_ids = explode(',', $ids_list);
  384. $entry_ids = self::removeEmptyValues($entry_ids);
  385. if (!empty($entry_ids)) {
  386. $this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
  387. }
  388. }
  389. }
  390. return $input;
  391. }
  392. private function parseFeedIds(string $input): string {
  393. if (preg_match_all('/\\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  394. $input = str_replace($matches[0], '', $input);
  395. $ids_lists = $matches['search'];
  396. $this->feed_ids = [];
  397. foreach ($ids_lists as $ids_list) {
  398. $feed_ids = explode(',', $ids_list);
  399. $feed_ids = self::removeEmptyValues($feed_ids);
  400. /** @var list<int> $feed_ids */
  401. $feed_ids = array_map('intval', $feed_ids);
  402. if (!empty($feed_ids)) {
  403. $this->feed_ids = array_merge($this->feed_ids, $feed_ids);
  404. }
  405. }
  406. }
  407. return $input;
  408. }
  409. private function parseNotFeedIds(string $input): string {
  410. if (preg_match_all('/(?<=[\\s(]|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  411. $input = str_replace($matches[0], '', $input);
  412. $ids_lists = $matches['search'];
  413. $this->not_feed_ids = [];
  414. foreach ($ids_lists as $ids_list) {
  415. $feed_ids = explode(',', $ids_list);
  416. $feed_ids = self::removeEmptyValues($feed_ids);
  417. /** @var list<int> $feed_ids */
  418. $feed_ids = array_map('intval', $feed_ids);
  419. if (!empty($feed_ids)) {
  420. $this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
  421. }
  422. }
  423. }
  424. return $input;
  425. }
  426. private function parseCategoryIds(string $input): string {
  427. if (preg_match_all('/\\bc:(?P<search>[0-9,]*)/', $input, $matches)) {
  428. $input = str_replace($matches[0], '', $input);
  429. $ids_lists = $matches['search'];
  430. $this->category_ids = [];
  431. foreach ($ids_lists as $ids_list) {
  432. $category_ids = explode(',', $ids_list);
  433. $category_ids = self::removeEmptyValues($category_ids);
  434. /** @var list<int> $category_ids */
  435. $category_ids = array_map('intval', $category_ids);
  436. if (!empty($category_ids)) {
  437. $this->category_ids = array_merge($this->category_ids, $category_ids);
  438. }
  439. }
  440. }
  441. return $input;
  442. }
  443. private function parseNotCategoryIds(string $input): string {
  444. if (preg_match_all('/(?<=[\\s(]|^)[!-]c:(?P<search>[0-9,]*)/', $input, $matches)) {
  445. $input = str_replace($matches[0], '', $input);
  446. $ids_lists = $matches['search'];
  447. $this->not_category_ids = [];
  448. foreach ($ids_lists as $ids_list) {
  449. $category_ids = explode(',', $ids_list);
  450. $category_ids = self::removeEmptyValues($category_ids);
  451. /** @var list<int> $category_ids */
  452. $category_ids = array_map('intval', $category_ids);
  453. if (!empty($category_ids)) {
  454. $this->not_category_ids = array_merge($this->not_category_ids, $category_ids);
  455. }
  456. }
  457. }
  458. return $input;
  459. }
  460. /**
  461. * Parse the search string to find tags (labels) IDs.
  462. */
  463. private function parseLabelIds(string $input): string {
  464. if (preg_match_all('/\\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  465. $input = str_replace($matches[0], '', $input);
  466. $ids_lists = $matches['search'];
  467. $this->label_ids = [];
  468. foreach ($ids_lists as $ids_list) {
  469. if ($ids_list === '*') {
  470. $this->label_ids[] = '*';
  471. break;
  472. }
  473. $label_ids = explode(',', $ids_list);
  474. $label_ids = self::removeEmptyValues($label_ids);
  475. /** @var list<int> $label_ids */
  476. $label_ids = array_map('intval', $label_ids);
  477. if (!empty($label_ids)) {
  478. $this->label_ids[] = $label_ids;
  479. }
  480. }
  481. }
  482. return $input;
  483. }
  484. private function parseNotLabelIds(string $input): string {
  485. if (preg_match_all('/(?<=[\\s(]|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  486. $input = str_replace($matches[0], '', $input);
  487. $ids_lists = $matches['search'];
  488. $this->not_label_ids = [];
  489. foreach ($ids_lists as $ids_list) {
  490. if ($ids_list === '*') {
  491. $this->not_label_ids[] = '*';
  492. break;
  493. }
  494. $label_ids = explode(',', $ids_list);
  495. $label_ids = self::removeEmptyValues($label_ids);
  496. /** @var list<int> $label_ids */
  497. $label_ids = array_map('intval', $label_ids);
  498. if (!empty($label_ids)) {
  499. $this->not_label_ids[] = $label_ids;
  500. }
  501. }
  502. }
  503. return $input;
  504. }
  505. /**
  506. * Parse the search string to find tags (labels) names.
  507. */
  508. private function parseLabelNames(string $input): string {
  509. $names_lists = [];
  510. if (preg_match_all('/\\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  511. $names_lists = $matches['search'];
  512. $input = str_replace($matches[0], '', $input);
  513. }
  514. if (preg_match_all('/\\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  515. $names_lists = array_merge($names_lists, $matches['search']);
  516. $input = str_replace($matches[0], '', $input);
  517. }
  518. if (!empty($names_lists)) {
  519. $this->label_names = [];
  520. foreach ($names_lists as $names_list) {
  521. $names_array = explode(',', $names_list);
  522. $names_array = self::removeEmptyValues($names_array);
  523. if (!empty($names_array)) {
  524. $this->label_names[] = $names_array;
  525. }
  526. }
  527. }
  528. return $input;
  529. }
  530. /**
  531. * Parse the search string to find tags (labels) names to exclude.
  532. */
  533. private function parseNotLabelNames(string $input): string {
  534. $names_lists = [];
  535. if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  536. $names_lists = $matches['search'];
  537. $input = str_replace($matches[0], '', $input);
  538. }
  539. if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<search>[^\\s"]*)/', $input, $matches)) {
  540. $names_lists = array_merge($names_lists, $matches['search']);
  541. $input = str_replace($matches[0], '', $input);
  542. }
  543. if (!empty($names_lists)) {
  544. $this->not_label_names = [];
  545. foreach ($names_lists as $names_list) {
  546. $names_array = explode(',', $names_list);
  547. $names_array = self::removeEmptyValues($names_array);
  548. if (!empty($names_array)) {
  549. $this->not_label_names[] = $names_array;
  550. }
  551. }
  552. }
  553. return $input;
  554. }
  555. /**
  556. * Parse the search string to find intitle keyword and the search related to it.
  557. */
  558. private function parseIntitleSearch(string $input): string {
  559. if (preg_match_all('#\\bintitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  560. $this->intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  561. $input = str_replace($matches[0], '', $input);
  562. }
  563. if (preg_match_all('/\\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  564. $this->intitle = $matches['search'];
  565. $input = str_replace($matches[0], '', $input);
  566. }
  567. if (preg_match_all('/\\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  568. $this->intitle = array_merge($this->intitle ?? [], $matches['search']);
  569. $input = str_replace($matches[0], '', $input);
  570. }
  571. $this->intitle = self::removeEmptyValues($this->intitle);
  572. if (empty($this->intitle)) {
  573. $this->intitle = null;
  574. }
  575. return $input;
  576. }
  577. private function parseNotIntitleSearch(string $input): string {
  578. if (preg_match_all('#(?<=[\\s(]|^)[!-]intitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  579. $this->not_intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  580. $input = str_replace($matches[0], '', $input);
  581. }
  582. if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  583. $this->not_intitle = $matches['search'];
  584. $input = str_replace($matches[0], '', $input);
  585. }
  586. if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  587. $this->not_intitle = array_merge($this->not_intitle ?? [], $matches['search']);
  588. $input = str_replace($matches[0], '', $input);
  589. }
  590. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  591. if (empty($this->not_intitle)) {
  592. $this->not_intitle = null;
  593. }
  594. return $input;
  595. }
  596. /**
  597. * Parse the search string to find intext keyword and the search related to it.
  598. */
  599. private function parseIntextSearch(string $input): string {
  600. if (preg_match_all('#\\bintext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  601. $this->intext_regex = self::htmlspecialchars_decodes($matches['search']);
  602. $input = str_replace($matches[0], '', $input);
  603. }
  604. if (preg_match_all('/\\bintext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  605. $this->intext = $matches['search'];
  606. $input = str_replace($matches[0], '', $input);
  607. }
  608. if (preg_match_all('/\\bintext:(?P<search>[^\s"]*)/', $input, $matches)) {
  609. $this->intext = array_merge($this->intext ?? [], $matches['search']);
  610. $input = str_replace($matches[0], '', $input);
  611. }
  612. $this->intext = self::removeEmptyValues($this->intext);
  613. if (empty($this->intext)) {
  614. $this->intext = null;
  615. }
  616. return $input;
  617. }
  618. private function parseNotIntextSearch(string $input): string {
  619. if (preg_match_all('#(?<=[\\s(]|^)[!-]intext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  620. $this->not_intext_regex = self::htmlspecialchars_decodes($matches['search']);
  621. $input = str_replace($matches[0], '', $input);
  622. }
  623. if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  624. $this->not_intext = $matches['search'];
  625. $input = str_replace($matches[0], '', $input);
  626. }
  627. if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<search>[^\s"]*)/', $input, $matches)) {
  628. $this->not_intext = array_merge($this->not_intext ?? [], $matches['search']);
  629. $input = str_replace($matches[0], '', $input);
  630. }
  631. $this->not_intext = self::removeEmptyValues($this->not_intext);
  632. if (empty($this->not_intext)) {
  633. $this->not_intext = null;
  634. }
  635. return $input;
  636. }
  637. /**
  638. * Parse the search string to find author keyword and the search related to it.
  639. * The search is the first word following the keyword except when using
  640. * a delimiter. Supported delimiters are single quote (') and double quotes (").
  641. */
  642. private function parseAuthorSearch(string $input): string {
  643. if (preg_match_all('#\\bauthor:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  644. $this->author_regex = self::htmlspecialchars_decodes($matches['search']);
  645. $input = str_replace($matches[0], '', $input);
  646. }
  647. if (preg_match_all('/\\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  648. $this->author = $matches['search'];
  649. $input = str_replace($matches[0], '', $input);
  650. }
  651. if (preg_match_all('/\\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  652. $this->author = array_merge($this->author ?? [], $matches['search']);
  653. $input = str_replace($matches[0], '', $input);
  654. }
  655. $this->author = self::removeEmptyValues($this->author);
  656. if (empty($this->author)) {
  657. $this->author = null;
  658. }
  659. return $input;
  660. }
  661. private function parseNotAuthorSearch(string $input): string {
  662. if (preg_match_all('#(?<=[\\s(]|^)[!-]author:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  663. $this->not_author_regex = self::htmlspecialchars_decodes($matches['search']);
  664. $input = str_replace($matches[0], '', $input);
  665. }
  666. if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  667. $this->not_author = $matches['search'];
  668. $input = str_replace($matches[0], '', $input);
  669. }
  670. if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  671. $this->not_author = array_merge($this->not_author ?? [], $matches['search']);
  672. $input = str_replace($matches[0], '', $input);
  673. }
  674. $this->not_author = self::removeEmptyValues($this->not_author);
  675. if (empty($this->not_author)) {
  676. $this->not_author = null;
  677. }
  678. return $input;
  679. }
  680. /**
  681. * Parse the search string to find inurl keyword and the search related to it.
  682. * The search is the first word following the keyword.
  683. */
  684. private function parseInurlSearch(string $input): string {
  685. if (preg_match_all('#\\binurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  686. $this->inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  687. $input = str_replace($matches[0], '', $input);
  688. }
  689. if (preg_match_all('/\\binurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  690. $this->inurl = $matches['search'];
  691. $input = str_replace($matches[0], '', $input);
  692. }
  693. if (preg_match_all('/\\binurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  694. $this->inurl = $matches['search'];
  695. $input = str_replace($matches[0], '', $input);
  696. }
  697. $this->inurl = self::removeEmptyValues($this->inurl);
  698. if (empty($this->inurl)) {
  699. $this->inurl = null;
  700. }
  701. return $input;
  702. }
  703. private function parseNotInurlSearch(string $input): string {
  704. if (preg_match_all('#(?<=[\\s(]|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  705. $this->not_inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  706. $input = str_replace($matches[0], '', $input);
  707. }
  708. if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  709. $this->not_inurl = $matches['search'];
  710. $input = str_replace($matches[0], '', $input);
  711. }
  712. if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  713. $this->not_inurl = $matches['search'];
  714. $input = str_replace($matches[0], '', $input);
  715. }
  716. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  717. if (empty($this->not_inurl)) {
  718. $this->not_inurl = null;
  719. }
  720. return $input;
  721. }
  722. /**
  723. * Parse the search string to find date keyword and the search related to it.
  724. * The search is the first word following the keyword.
  725. */
  726. private function parseDateSearch(string $input): string {
  727. if (preg_match_all('/\\bdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  728. $input = str_replace($matches[0], '', $input);
  729. $dates = self::removeEmptyValues($matches['search']);
  730. if (!empty($dates[0])) {
  731. [$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
  732. }
  733. }
  734. return $input;
  735. }
  736. private function parseNotDateSearch(string $input): string {
  737. if (preg_match_all('/(?<=[\\s(]|^)[!-]date:(?P<search>[^\\s]*)/', $input, $matches)) {
  738. $input = str_replace($matches[0], '', $input);
  739. $dates = self::removeEmptyValues($matches['search']);
  740. if (!empty($dates[0])) {
  741. [$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
  742. }
  743. }
  744. return $input;
  745. }
  746. /**
  747. * Parse the search string to find pubdate keyword and the search related to it.
  748. * The search is the first word following the keyword.
  749. */
  750. private function parsePubdateSearch(string $input): string {
  751. if (preg_match_all('/\\bpubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  752. $input = str_replace($matches[0], '', $input);
  753. $dates = self::removeEmptyValues($matches['search']);
  754. if (!empty($dates[0])) {
  755. [$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
  756. }
  757. }
  758. return $input;
  759. }
  760. private function parseNotPubdateSearch(string $input): string {
  761. if (preg_match_all('/(?<=[\\s(]|^)[!-]pubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  762. $input = str_replace($matches[0], '', $input);
  763. $dates = self::removeEmptyValues($matches['search']);
  764. if (!empty($dates[0])) {
  765. [$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
  766. }
  767. }
  768. return $input;
  769. }
  770. /**
  771. * Parse the search string to find userdate keyword and the search related to it.
  772. * The search is the first word following the keyword.
  773. */
  774. private function parseUserdateSearch(string $input): string {
  775. if (preg_match_all('/\\buserdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  776. $input = str_replace($matches[0], '', $input);
  777. $dates = self::removeEmptyValues($matches['search']);
  778. if (!empty($dates[0])) {
  779. [$this->min_userdate, $this->max_userdate] = parseDateInterval($dates[0]);
  780. }
  781. }
  782. return $input;
  783. }
  784. private function parseNotUserdateSearch(string $input): string {
  785. if (preg_match_all('/(?<=[\\s(]|^)[!-]userdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  786. $input = str_replace($matches[0], '', $input);
  787. $dates = self::removeEmptyValues($matches['search']);
  788. if (!empty($dates[0])) {
  789. [$this->not_min_userdate, $this->not_max_userdate] = parseDateInterval($dates[0]);
  790. }
  791. }
  792. return $input;
  793. }
  794. /**
  795. * Parse the search string to find tags keyword (# followed by a word)
  796. * and the search related to it.
  797. * The search is the first word following the #.
  798. */
  799. private function parseTagsSearch(string $input): string {
  800. if (preg_match_all('%#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  801. $this->tags_regex = self::htmlspecialchars_decodes($matches['search']);
  802. $input = str_replace($matches[0], '', $input);
  803. }
  804. if (preg_match_all('/#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  805. $this->tags = $matches['search'];
  806. $input = str_replace($matches[0], '', $input);
  807. }
  808. if (preg_match_all('/#(?P<search>[^\\s]+)/', $input, $matches)) {
  809. $this->tags = $matches['search'];
  810. $input = str_replace($matches[0], '', $input);
  811. }
  812. $this->tags = self::removeEmptyValues($this->tags);
  813. if (empty($this->tags)) {
  814. $this->tags = null;
  815. } else {
  816. $this->tags = self::decodeSpaces($this->tags);
  817. }
  818. return $input;
  819. }
  820. private function parseNotTagsSearch(string $input): string {
  821. if (preg_match_all('%(?<=[\\s(]|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  822. $this->not_tags_regex = self::htmlspecialchars_decodes($matches['search']);
  823. $input = str_replace($matches[0], '', $input);
  824. }
  825. if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  826. $this->not_tags = $matches['search'];
  827. $input = str_replace($matches[0], '', $input);
  828. }
  829. if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<search>[^\\s]+)/', $input, $matches)) {
  830. $this->not_tags = $matches['search'];
  831. $input = str_replace($matches[0], '', $input);
  832. }
  833. $this->not_tags = self::removeEmptyValues($this->not_tags);
  834. if (empty($this->not_tags)) {
  835. $this->not_tags = null;
  836. } else {
  837. $this->not_tags = self::decodeSpaces($this->not_tags);
  838. }
  839. return $input;
  840. }
  841. /**
  842. * Parse the search string to find search values.
  843. * Every word is a distinct search value using a delimiter.
  844. * Supported delimiters are single quote (') and double quotes (") and regex (/).
  845. */
  846. private function parseQuotedSearch(string $input): string {
  847. $input = self::cleanSearch($input);
  848. if ($input === '') {
  849. return '';
  850. }
  851. if (preg_match_all('#(?<=[\\s(]|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  852. $this->search_regex = self::htmlspecialchars_decodes($matches['search']);
  853. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  854. $input = str_replace($matches[0], '', $input);
  855. }
  856. if (preg_match_all('/(?<=[\\s(]|^)(?<![!-\\\\])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  857. $this->search = $matches['search'];
  858. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  859. $input = str_replace($matches[0], '', $input);
  860. }
  861. return $input;
  862. }
  863. /**
  864. * Parse the search string to find search values.
  865. * Every word is a distinct search value.
  866. */
  867. private function parseSearch(string $input): string {
  868. $input = self::cleanSearch($input);
  869. if ($input === '') {
  870. return '';
  871. }
  872. if (is_array($this->search)) {
  873. $this->search = array_merge($this->search, explode(' ', $input));
  874. } else {
  875. $this->search = explode(' ', $input);
  876. }
  877. return $input;
  878. }
  879. private function parseNotSearch(string $input): string {
  880. $input = self::cleanSearch($input);
  881. if ($input === '') {
  882. return '';
  883. }
  884. if (preg_match_all('#(?<=[\\s(]|^)[!-](?P<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  885. $this->not_search_regex = self::htmlspecialchars_decodes($matches['search']);
  886. $input = str_replace($matches[0], '', $input);
  887. }
  888. if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  889. $this->not_search = $matches['search'];
  890. $input = str_replace($matches[0], '', $input);
  891. }
  892. $input = self::cleanSearch($input);
  893. if ($input === '') {
  894. return '';
  895. }
  896. if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<search>[^\\s]+)/', $input, $matches)) {
  897. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
  898. $input = str_replace($matches[0], '', $input);
  899. }
  900. $this->not_search = self::removeEmptyValues($this->not_search);
  901. return $input;
  902. }
  903. /**
  904. * Remove all unnecessary spaces in the search
  905. */
  906. private static function cleanSearch(string $input): string {
  907. $input = preg_replace('/\\s+/', ' ', $input);
  908. if (!is_string($input)) {
  909. return '';
  910. }
  911. return trim($input);
  912. }
  913. /** Remove escaping backslashes for parenthesis logic */
  914. private static function unescape(string $input): string {
  915. return str_replace(['\\(', '\\)'], ['(', ')'], $input);
  916. }
  917. }