Search.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 getMinUserdate(): ?int {
  253. return $this->min_userdate ?: null;
  254. }
  255. public function getNotMinUserdate(): ?int {
  256. return $this->not_min_userdate ?: null;
  257. }
  258. public function getMaxUserdate(): ?int {
  259. return $this->max_userdate ?: null;
  260. }
  261. public function getNotMaxUserdate(): ?int {
  262. return $this->not_max_userdate ?: null;
  263. }
  264. /** @return list<string>|null */
  265. public function getInurl(): ?array {
  266. return $this->inurl;
  267. }
  268. /** @return list<string>|null */
  269. public function getInurlRegex(): ?array {
  270. return $this->inurl_regex;
  271. }
  272. /** @return list<string>|null */
  273. public function getNotInurl(): ?array {
  274. return $this->not_inurl;
  275. }
  276. /** @return list<string>|null */
  277. public function getNotInurlRegex(): ?array {
  278. return $this->not_inurl_regex;
  279. }
  280. /** @return list<string>|null */
  281. public function getAuthor(): ?array {
  282. return $this->author;
  283. }
  284. /** @return list<string>|null */
  285. public function getAuthorRegex(): ?array {
  286. return $this->author_regex;
  287. }
  288. /** @return list<string>|null */
  289. public function getNotAuthor(): ?array {
  290. return $this->not_author;
  291. }
  292. /** @return list<string>|null */
  293. public function getNotAuthorRegex(): ?array {
  294. return $this->not_author_regex;
  295. }
  296. /** @return list<string>|null */
  297. public function getTags(): ?array {
  298. return $this->tags;
  299. }
  300. /** @return list<string>|null */
  301. public function getTagsRegex(): ?array {
  302. return $this->tags_regex;
  303. }
  304. /** @return list<string>|null */
  305. public function getNotTags(): ?array {
  306. return $this->not_tags;
  307. }
  308. /** @return list<string>|null */
  309. public function getNotTagsRegex(): ?array {
  310. return $this->not_tags_regex;
  311. }
  312. /** @return list<string>|null */
  313. public function getSearch(): ?array {
  314. return $this->search;
  315. }
  316. /** @return list<string>|null */
  317. public function getSearchRegex(): ?array {
  318. return $this->search_regex;
  319. }
  320. /** @return list<string>|null */
  321. public function getNotSearch(): ?array {
  322. return $this->not_search;
  323. }
  324. /** @return list<string>|null */
  325. public function getNotSearchRegex(): ?array {
  326. return $this->not_search_regex;
  327. }
  328. /**
  329. * @param list<string>|null $anArray
  330. * @return list<string>
  331. */
  332. private static function removeEmptyValues(?array $anArray): array {
  333. return empty($anArray) ? [] : array_values(array_filter($anArray, static fn(string $value) => $value !== ''));
  334. }
  335. /**
  336. * @param list<string>|string $value
  337. * @return ($value is string ? string : list<string>)
  338. */
  339. private static function decodeSpaces(array|string $value): array|string {
  340. if (is_array($value)) {
  341. foreach ($value as &$val) {
  342. $val = self::decodeSpaces($val);
  343. }
  344. } else {
  345. $value = trim(str_replace('+', ' ', $value));
  346. }
  347. return $value;
  348. }
  349. /**
  350. * @param list<string> $strings
  351. * @return list<string>
  352. */
  353. private static function htmlspecialchars_decodes(array $strings): array {
  354. return array_map(static fn(string $s) => htmlspecialchars_decode($s, ENT_QUOTES), $strings);
  355. }
  356. /**
  357. * Parse the search string to find entry (article) IDs.
  358. */
  359. private function parseEntryIds(string $input): string {
  360. if (preg_match_all('/\\be:(?P<search>[0-9,]*)/', $input, $matches)) {
  361. $input = str_replace($matches[0], '', $input);
  362. $ids_lists = $matches['search'];
  363. $this->entry_ids = [];
  364. foreach ($ids_lists as $ids_list) {
  365. $entry_ids = explode(',', $ids_list);
  366. $entry_ids = self::removeEmptyValues($entry_ids);
  367. if (!empty($entry_ids)) {
  368. $this->entry_ids = array_merge($this->entry_ids, $entry_ids);
  369. }
  370. }
  371. }
  372. return $input;
  373. }
  374. private function parseNotEntryIds(string $input): string {
  375. if (preg_match_all('/(?<=[\\s(]|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
  376. $input = str_replace($matches[0], '', $input);
  377. $ids_lists = $matches['search'];
  378. $this->not_entry_ids = [];
  379. foreach ($ids_lists as $ids_list) {
  380. $entry_ids = explode(',', $ids_list);
  381. $entry_ids = self::removeEmptyValues($entry_ids);
  382. if (!empty($entry_ids)) {
  383. $this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
  384. }
  385. }
  386. }
  387. return $input;
  388. }
  389. private function parseFeedIds(string $input): string {
  390. if (preg_match_all('/\\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  391. $input = str_replace($matches[0], '', $input);
  392. $ids_lists = $matches['search'];
  393. $this->feed_ids = [];
  394. foreach ($ids_lists as $ids_list) {
  395. $feed_ids = explode(',', $ids_list);
  396. $feed_ids = self::removeEmptyValues($feed_ids);
  397. /** @var list<int> $feed_ids */
  398. $feed_ids = array_map('intval', $feed_ids);
  399. if (!empty($feed_ids)) {
  400. $this->feed_ids = array_merge($this->feed_ids, $feed_ids);
  401. }
  402. }
  403. }
  404. return $input;
  405. }
  406. private function parseNotFeedIds(string $input): string {
  407. if (preg_match_all('/(?<=[\\s(]|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  408. $input = str_replace($matches[0], '', $input);
  409. $ids_lists = $matches['search'];
  410. $this->not_feed_ids = [];
  411. foreach ($ids_lists as $ids_list) {
  412. $feed_ids = explode(',', $ids_list);
  413. $feed_ids = self::removeEmptyValues($feed_ids);
  414. /** @var list<int> $feed_ids */
  415. $feed_ids = array_map('intval', $feed_ids);
  416. if (!empty($feed_ids)) {
  417. $this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
  418. }
  419. }
  420. }
  421. return $input;
  422. }
  423. private function parseCategoryIds(string $input): string {
  424. if (preg_match_all('/\\bc:(?P<search>[0-9,]*)/', $input, $matches)) {
  425. $input = str_replace($matches[0], '', $input);
  426. $ids_lists = $matches['search'];
  427. $this->category_ids = [];
  428. foreach ($ids_lists as $ids_list) {
  429. $category_ids = explode(',', $ids_list);
  430. $category_ids = self::removeEmptyValues($category_ids);
  431. /** @var list<int> $category_ids */
  432. $category_ids = array_map('intval', $category_ids);
  433. if (!empty($category_ids)) {
  434. $this->category_ids = array_merge($this->category_ids, $category_ids);
  435. }
  436. }
  437. }
  438. return $input;
  439. }
  440. private function parseNotCategoryIds(string $input): string {
  441. if (preg_match_all('/(?<=[\\s(]|^)[!-]c:(?P<search>[0-9,]*)/', $input, $matches)) {
  442. $input = str_replace($matches[0], '', $input);
  443. $ids_lists = $matches['search'];
  444. $this->not_category_ids = [];
  445. foreach ($ids_lists as $ids_list) {
  446. $category_ids = explode(',', $ids_list);
  447. $category_ids = self::removeEmptyValues($category_ids);
  448. /** @var list<int> $category_ids */
  449. $category_ids = array_map('intval', $category_ids);
  450. if (!empty($category_ids)) {
  451. $this->not_category_ids = array_merge($this->not_category_ids, $category_ids);
  452. }
  453. }
  454. }
  455. return $input;
  456. }
  457. /**
  458. * Parse the search string to find tags (labels) IDs.
  459. */
  460. private function parseLabelIds(string $input): string {
  461. if (preg_match_all('/\\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  462. $input = str_replace($matches[0], '', $input);
  463. $ids_lists = $matches['search'];
  464. $this->label_ids = [];
  465. foreach ($ids_lists as $ids_list) {
  466. if ($ids_list === '*') {
  467. $this->label_ids[] = '*';
  468. break;
  469. }
  470. $label_ids = explode(',', $ids_list);
  471. $label_ids = self::removeEmptyValues($label_ids);
  472. /** @var list<int> $label_ids */
  473. $label_ids = array_map('intval', $label_ids);
  474. if (!empty($label_ids)) {
  475. $this->label_ids[] = $label_ids;
  476. }
  477. }
  478. }
  479. return $input;
  480. }
  481. private function parseNotLabelIds(string $input): string {
  482. if (preg_match_all('/(?<=[\\s(]|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  483. $input = str_replace($matches[0], '', $input);
  484. $ids_lists = $matches['search'];
  485. $this->not_label_ids = [];
  486. foreach ($ids_lists as $ids_list) {
  487. if ($ids_list === '*') {
  488. $this->not_label_ids[] = '*';
  489. break;
  490. }
  491. $label_ids = explode(',', $ids_list);
  492. $label_ids = self::removeEmptyValues($label_ids);
  493. /** @var list<int> $label_ids */
  494. $label_ids = array_map('intval', $label_ids);
  495. if (!empty($label_ids)) {
  496. $this->not_label_ids[] = $label_ids;
  497. }
  498. }
  499. }
  500. return $input;
  501. }
  502. /**
  503. * Parse the search string to find tags (labels) names.
  504. */
  505. private function parseLabelNames(string $input): string {
  506. $names_lists = [];
  507. if (preg_match_all('/\\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  508. $names_lists = $matches['search'];
  509. $input = str_replace($matches[0], '', $input);
  510. }
  511. if (preg_match_all('/\\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  512. $names_lists = array_merge($names_lists, $matches['search']);
  513. $input = str_replace($matches[0], '', $input);
  514. }
  515. if (!empty($names_lists)) {
  516. $this->label_names = [];
  517. foreach ($names_lists as $names_list) {
  518. $names_array = explode(',', $names_list);
  519. $names_array = self::removeEmptyValues($names_array);
  520. if (!empty($names_array)) {
  521. $this->label_names[] = $names_array;
  522. }
  523. }
  524. }
  525. return $input;
  526. }
  527. /**
  528. * Parse the search string to find tags (labels) names to exclude.
  529. */
  530. private function parseNotLabelNames(string $input): string {
  531. $names_lists = [];
  532. if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  533. $names_lists = $matches['search'];
  534. $input = str_replace($matches[0], '', $input);
  535. }
  536. if (preg_match_all('/(?<=[\\s(]|^)[!-]labels?:(?P<search>[^\\s"]*)/', $input, $matches)) {
  537. $names_lists = array_merge($names_lists, $matches['search']);
  538. $input = str_replace($matches[0], '', $input);
  539. }
  540. if (!empty($names_lists)) {
  541. $this->not_label_names = [];
  542. foreach ($names_lists as $names_list) {
  543. $names_array = explode(',', $names_list);
  544. $names_array = self::removeEmptyValues($names_array);
  545. if (!empty($names_array)) {
  546. $this->not_label_names[] = $names_array;
  547. }
  548. }
  549. }
  550. return $input;
  551. }
  552. /**
  553. * Parse the search string to find intitle keyword and the search related to it.
  554. */
  555. private function parseIntitleSearch(string $input): string {
  556. if (preg_match_all('#\\bintitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  557. $this->intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  558. $input = str_replace($matches[0], '', $input);
  559. }
  560. if (preg_match_all('/\\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  561. $this->intitle = $matches['search'];
  562. $input = str_replace($matches[0], '', $input);
  563. }
  564. if (preg_match_all('/\\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  565. $this->intitle = array_merge($this->intitle ?? [], $matches['search']);
  566. $input = str_replace($matches[0], '', $input);
  567. }
  568. $this->intitle = self::removeEmptyValues($this->intitle);
  569. if (empty($this->intitle)) {
  570. $this->intitle = null;
  571. }
  572. return $input;
  573. }
  574. private function parseNotIntitleSearch(string $input): string {
  575. if (preg_match_all('#(?<=[\\s(]|^)[!-]intitle:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  576. $this->not_intitle_regex = self::htmlspecialchars_decodes($matches['search']);
  577. $input = str_replace($matches[0], '', $input);
  578. }
  579. if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  580. $this->not_intitle = $matches['search'];
  581. $input = str_replace($matches[0], '', $input);
  582. }
  583. if (preg_match_all('/(?<=[\\s(]|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  584. $this->not_intitle = array_merge($this->not_intitle ?? [], $matches['search']);
  585. $input = str_replace($matches[0], '', $input);
  586. }
  587. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  588. if (empty($this->not_intitle)) {
  589. $this->not_intitle = null;
  590. }
  591. return $input;
  592. }
  593. /**
  594. * Parse the search string to find intext keyword and the search related to it.
  595. */
  596. private function parseIntextSearch(string $input): string {
  597. if (preg_match_all('#\\bintext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  598. $this->intext_regex = self::htmlspecialchars_decodes($matches['search']);
  599. $input = str_replace($matches[0], '', $input);
  600. }
  601. if (preg_match_all('/\\bintext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  602. $this->intext = $matches['search'];
  603. $input = str_replace($matches[0], '', $input);
  604. }
  605. if (preg_match_all('/\\bintext:(?P<search>[^\s"]*)/', $input, $matches)) {
  606. $this->intext = array_merge($this->intext ?? [], $matches['search']);
  607. $input = str_replace($matches[0], '', $input);
  608. }
  609. $this->intext = self::removeEmptyValues($this->intext);
  610. if (empty($this->intext)) {
  611. $this->intext = null;
  612. }
  613. return $input;
  614. }
  615. private function parseNotIntextSearch(string $input): string {
  616. if (preg_match_all('#(?<=[\\s(]|^)[!-]intext:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  617. $this->not_intext_regex = self::htmlspecialchars_decodes($matches['search']);
  618. $input = str_replace($matches[0], '', $input);
  619. }
  620. if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  621. $this->not_intext = $matches['search'];
  622. $input = str_replace($matches[0], '', $input);
  623. }
  624. if (preg_match_all('/(?<=[\\s(]|^)[!-]intext:(?P<search>[^\s"]*)/', $input, $matches)) {
  625. $this->not_intext = array_merge($this->not_intext ?? [], $matches['search']);
  626. $input = str_replace($matches[0], '', $input);
  627. }
  628. $this->not_intext = self::removeEmptyValues($this->not_intext);
  629. if (empty($this->not_intext)) {
  630. $this->not_intext = null;
  631. }
  632. return $input;
  633. }
  634. /**
  635. * Parse the search string to find author keyword and the search related to it.
  636. * The search is the first word following the keyword except when using
  637. * a delimiter. Supported delimiters are single quote (') and double quotes (").
  638. */
  639. private function parseAuthorSearch(string $input): string {
  640. if (preg_match_all('#\\bauthor:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  641. $this->author_regex = self::htmlspecialchars_decodes($matches['search']);
  642. $input = str_replace($matches[0], '', $input);
  643. }
  644. if (preg_match_all('/\\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  645. $this->author = $matches['search'];
  646. $input = str_replace($matches[0], '', $input);
  647. }
  648. if (preg_match_all('/\\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  649. $this->author = array_merge($this->author ?? [], $matches['search']);
  650. $input = str_replace($matches[0], '', $input);
  651. }
  652. $this->author = self::removeEmptyValues($this->author);
  653. if (empty($this->author)) {
  654. $this->author = null;
  655. }
  656. return $input;
  657. }
  658. private function parseNotAuthorSearch(string $input): string {
  659. if (preg_match_all('#(?<=[\\s(]|^)[!-]author:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  660. $this->not_author_regex = self::htmlspecialchars_decodes($matches['search']);
  661. $input = str_replace($matches[0], '', $input);
  662. }
  663. if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  664. $this->not_author = $matches['search'];
  665. $input = str_replace($matches[0], '', $input);
  666. }
  667. if (preg_match_all('/(?<=[\\s(]|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  668. $this->not_author = array_merge($this->not_author ?? [], $matches['search']);
  669. $input = str_replace($matches[0], '', $input);
  670. }
  671. $this->not_author = self::removeEmptyValues($this->not_author);
  672. if (empty($this->not_author)) {
  673. $this->not_author = null;
  674. }
  675. return $input;
  676. }
  677. /**
  678. * Parse the search string to find inurl keyword and the search related to it.
  679. * The search is the first word following the keyword.
  680. */
  681. private function parseInurlSearch(string $input): string {
  682. if (preg_match_all('#\\binurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  683. $this->inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  684. $input = str_replace($matches[0], '', $input);
  685. }
  686. if (preg_match_all('/\\binurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  687. $this->inurl = $matches['search'];
  688. $input = str_replace($matches[0], '', $input);
  689. }
  690. if (preg_match_all('/\\binurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  691. $this->inurl = $matches['search'];
  692. $input = str_replace($matches[0], '', $input);
  693. }
  694. $this->inurl = self::removeEmptyValues($this->inurl);
  695. if (empty($this->inurl)) {
  696. $this->inurl = null;
  697. }
  698. return $input;
  699. }
  700. private function parseNotInurlSearch(string $input): string {
  701. if (preg_match_all('#(?<=[\\s(]|^)[!-]inurl:(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  702. $this->not_inurl_regex = self::htmlspecialchars_decodes($matches['search']);
  703. $input = str_replace($matches[0], '', $input);
  704. }
  705. if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  706. $this->not_inurl = $matches['search'];
  707. $input = str_replace($matches[0], '', $input);
  708. }
  709. if (preg_match_all('/(?<=[\\s(]|^)[!-]inurl:(?P<search>[^\\s]*)/', $input, $matches)) {
  710. $this->not_inurl = $matches['search'];
  711. $input = str_replace($matches[0], '', $input);
  712. }
  713. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  714. if (empty($this->not_inurl)) {
  715. $this->not_inurl = null;
  716. }
  717. return $input;
  718. }
  719. /**
  720. * Parse the search string to find date keyword and the search related to it.
  721. * The search is the first word following the keyword.
  722. */
  723. private function parseDateSearch(string $input): string {
  724. if (preg_match_all('/\\bdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  725. $input = str_replace($matches[0], '', $input);
  726. $dates = self::removeEmptyValues($matches['search']);
  727. if (!empty($dates[0])) {
  728. [$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
  729. }
  730. }
  731. return $input;
  732. }
  733. private function parseNotDateSearch(string $input): string {
  734. if (preg_match_all('/(?<=[\\s(]|^)[!-]date:(?P<search>[^\\s]*)/', $input, $matches)) {
  735. $input = str_replace($matches[0], '', $input);
  736. $dates = self::removeEmptyValues($matches['search']);
  737. if (!empty($dates[0])) {
  738. [$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
  739. }
  740. }
  741. return $input;
  742. }
  743. /**
  744. * Parse the search string to find pubdate keyword and the search related to it.
  745. * The search is the first word following the keyword.
  746. */
  747. private function parsePubdateSearch(string $input): string {
  748. if (preg_match_all('/\\bpubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  749. $input = str_replace($matches[0], '', $input);
  750. $dates = self::removeEmptyValues($matches['search']);
  751. if (!empty($dates[0])) {
  752. [$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
  753. }
  754. }
  755. return $input;
  756. }
  757. private function parseNotPubdateSearch(string $input): string {
  758. if (preg_match_all('/(?<=[\\s(]|^)[!-]pubdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  759. $input = str_replace($matches[0], '', $input);
  760. $dates = self::removeEmptyValues($matches['search']);
  761. if (!empty($dates[0])) {
  762. [$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
  763. }
  764. }
  765. return $input;
  766. }
  767. /**
  768. * Parse the search string to find userdate keyword and the search related to it.
  769. * The search is the first word following the keyword.
  770. */
  771. private function parseUserdateSearch(string $input): string {
  772. if (preg_match_all('/\\buserdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  773. $input = str_replace($matches[0], '', $input);
  774. $dates = self::removeEmptyValues($matches['search']);
  775. if (!empty($dates[0])) {
  776. [$this->min_userdate, $this->max_userdate] = parseDateInterval($dates[0]);
  777. }
  778. }
  779. return $input;
  780. }
  781. private function parseNotUserdateSearch(string $input): string {
  782. if (preg_match_all('/(?<=[\\s(]|^)[!-]userdate:(?P<search>[^\\s]*)/', $input, $matches)) {
  783. $input = str_replace($matches[0], '', $input);
  784. $dates = self::removeEmptyValues($matches['search']);
  785. if (!empty($dates[0])) {
  786. [$this->not_min_userdate, $this->not_max_userdate] = parseDateInterval($dates[0]);
  787. }
  788. }
  789. return $input;
  790. }
  791. /**
  792. * Parse the search string to find tags keyword (# followed by a word)
  793. * and the search related to it.
  794. * The search is the first word following the #.
  795. */
  796. private function parseTagsSearch(string $input): string {
  797. if (preg_match_all('%#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  798. $this->tags_regex = self::htmlspecialchars_decodes($matches['search']);
  799. $input = str_replace($matches[0], '', $input);
  800. }
  801. if (preg_match_all('/#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  802. $this->tags = $matches['search'];
  803. $input = str_replace($matches[0], '', $input);
  804. }
  805. if (preg_match_all('/#(?P<search>[^\\s]+)/', $input, $matches)) {
  806. $this->tags = $matches['search'];
  807. $input = str_replace($matches[0], '', $input);
  808. }
  809. $this->tags = self::removeEmptyValues($this->tags);
  810. if (empty($this->tags)) {
  811. $this->tags = null;
  812. } else {
  813. $this->tags = self::decodeSpaces($this->tags);
  814. }
  815. return $input;
  816. }
  817. private function parseNotTagsSearch(string $input): string {
  818. if (preg_match_all('%(?<=[\\s(]|^)[!-]#(?P<search>/.*?(?<!\\\\)/[im]*)%', $input, $matches)) {
  819. $this->not_tags_regex = self::htmlspecialchars_decodes($matches['search']);
  820. $input = str_replace($matches[0], '', $input);
  821. }
  822. if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  823. $this->not_tags = $matches['search'];
  824. $input = str_replace($matches[0], '', $input);
  825. }
  826. if (preg_match_all('/(?<=[\\s(]|^)[!-]#(?P<search>[^\\s]+)/', $input, $matches)) {
  827. $this->not_tags = $matches['search'];
  828. $input = str_replace($matches[0], '', $input);
  829. }
  830. $this->not_tags = self::removeEmptyValues($this->not_tags);
  831. if (empty($this->not_tags)) {
  832. $this->not_tags = null;
  833. } else {
  834. $this->not_tags = self::decodeSpaces($this->not_tags);
  835. }
  836. return $input;
  837. }
  838. /**
  839. * Parse the search string to find search values.
  840. * Every word is a distinct search value using a delimiter.
  841. * Supported delimiters are single quote (') and double quotes (") and regex (/).
  842. */
  843. private function parseQuotedSearch(string $input): string {
  844. $input = self::cleanSearch($input);
  845. if ($input === '') {
  846. return '';
  847. }
  848. if (preg_match_all('#(?<=[\\s(]|^)(?<![!-\\\\])(?P<search>/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  849. $this->search_regex = self::htmlspecialchars_decodes($matches['search']);
  850. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  851. $input = str_replace($matches[0], '', $input);
  852. }
  853. if (preg_match_all('/(?<=[\\s(]|^)(?<![!-\\\\])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  854. $this->search = $matches['search'];
  855. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  856. $input = str_replace($matches[0], '', $input);
  857. }
  858. return $input;
  859. }
  860. /**
  861. * Parse the search string to find search values.
  862. * Every word is a distinct search value.
  863. */
  864. private function parseSearch(string $input): string {
  865. $input = self::cleanSearch($input);
  866. if ($input === '') {
  867. return '';
  868. }
  869. if (is_array($this->search)) {
  870. $this->search = array_merge($this->search, explode(' ', $input));
  871. } else {
  872. $this->search = explode(' ', $input);
  873. }
  874. return $input;
  875. }
  876. private function parseNotSearch(string $input): string {
  877. $input = self::cleanSearch($input);
  878. if ($input === '') {
  879. return '';
  880. }
  881. if (preg_match_all('#(?<=[\\s(]|^)[!-](?P<search>(?<!\\\\)/.*?(?<!\\\\)/[im]*)#', $input, $matches)) {
  882. $this->not_search_regex = self::htmlspecialchars_decodes($matches['search']);
  883. $input = str_replace($matches[0], '', $input);
  884. }
  885. if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  886. $this->not_search = $matches['search'];
  887. $input = str_replace($matches[0], '', $input);
  888. }
  889. $input = self::cleanSearch($input);
  890. if ($input === '') {
  891. return '';
  892. }
  893. if (preg_match_all('/(?<=[\\s(]|^)[!-](?P<search>[^\\s]+)/', $input, $matches)) {
  894. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
  895. $input = str_replace($matches[0], '', $input);
  896. }
  897. $this->not_search = self::removeEmptyValues($this->not_search);
  898. return $input;
  899. }
  900. /**
  901. * Remove all unnecessary spaces in the search
  902. */
  903. private static function cleanSearch(string $input): string {
  904. $input = preg_replace('/\\s+/', ' ', $input);
  905. if (!is_string($input)) {
  906. return '';
  907. }
  908. return trim($input);
  909. }
  910. /** Remove escaping backslashes for parenthesis logic */
  911. private static function unescape(string $input): string {
  912. return str_replace(['\\(', '\\)'], ['(', ')'], $input);
  913. }
  914. }