Search.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. require_once(LIB_PATH . '/lib_date.php');
  3. /**
  4. * Contains a search from the search form.
  5. *
  6. * It allows to extract meaningful bits of the search and store them in a
  7. * convenient object
  8. */
  9. class FreshRSS_Search {
  10. // This contains the user input string
  11. private $raw_input = '';
  12. // The following properties are extracted from the raw input
  13. private $feed_ids;
  14. private $intitle;
  15. private $min_date;
  16. private $max_date;
  17. private $min_pubdate;
  18. private $max_pubdate;
  19. private $inurl;
  20. private $author;
  21. private $tags;
  22. private $search;
  23. private $not_feed_ids;
  24. private $not_intitle;
  25. private $not_min_date;
  26. private $not_max_date;
  27. private $not_min_pubdate;
  28. private $not_max_pubdate;
  29. private $not_inurl;
  30. private $not_author;
  31. private $not_tags;
  32. private $not_search;
  33. public function __construct($input) {
  34. if ($input == '') {
  35. return;
  36. }
  37. $this->raw_input = $input;
  38. $input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
  39. $input = $this->parseNotFeedIds($input);
  40. $input = $this->parseNotPubdateSearch($input);
  41. $input = $this->parseNotDateSearch($input);
  42. $input = $this->parseNotIntitleSearch($input);
  43. $input = $this->parseNotAuthorSearch($input);
  44. $input = $this->parseNotInurlSearch($input);
  45. $input = $this->parseNotTagsSearch($input);
  46. $input = $this->parseFeedIds($input);
  47. $input = $this->parsePubdateSearch($input);
  48. $input = $this->parseDateSearch($input);
  49. $input = $this->parseIntitleSearch($input);
  50. $input = $this->parseAuthorSearch($input);
  51. $input = $this->parseInurlSearch($input);
  52. $input = $this->parseTagsSearch($input);
  53. $input = $this->parseNotSearch($input);
  54. $input = $this->parseSearch($input);
  55. }
  56. public function __toString() {
  57. return $this->getRawInput();
  58. }
  59. public function getRawInput() {
  60. return $this->raw_input;
  61. }
  62. public function getFeedIds() {
  63. return $this->feed_ids;
  64. }
  65. public function getNotFeedIds() {
  66. return $this->not_feed_ids;
  67. }
  68. public function getIntitle() {
  69. return $this->intitle;
  70. }
  71. public function getNotIntitle() {
  72. return $this->not_intitle;
  73. }
  74. public function getMinDate() {
  75. return $this->min_date;
  76. }
  77. public function getNotMinDate() {
  78. return $this->not_min_date;
  79. }
  80. public function setMinDate($value) {
  81. return $this->min_date = $value;
  82. }
  83. public function getMaxDate() {
  84. return $this->max_date;
  85. }
  86. public function getNotMaxDate() {
  87. return $this->not_max_date;
  88. }
  89. public function setMaxDate($value) {
  90. return $this->max_date = $value;
  91. }
  92. public function getMinPubdate() {
  93. return $this->min_pubdate;
  94. }
  95. public function getNotMinPubdate() {
  96. return $this->not_min_pubdate;
  97. }
  98. public function getMaxPubdate() {
  99. return $this->max_pubdate;
  100. }
  101. public function getNotMaxPubdate() {
  102. return $this->not_max_pubdate;
  103. }
  104. public function getInurl() {
  105. return $this->inurl;
  106. }
  107. public function getNotInurl() {
  108. return $this->not_inurl;
  109. }
  110. public function getAuthor() {
  111. return $this->author;
  112. }
  113. public function getNotAuthor() {
  114. return $this->not_author;
  115. }
  116. public function getTags() {
  117. return $this->tags;
  118. }
  119. public function getNotTags() {
  120. return $this->not_tags;
  121. }
  122. public function getSearch() {
  123. return $this->search;
  124. }
  125. public function getNotSearch() {
  126. return $this->not_search;
  127. }
  128. private static function removeEmptyValues($anArray) {
  129. return is_array($anArray) ? array_filter($anArray, function($value) { return $value !== ''; }) : array();
  130. }
  131. private static function decodeSpaces($value) {
  132. if (is_array($value)) {
  133. for ($i = count($value) - 1; $i >= 0; $i--) {
  134. $value[$i] = self::decodeSpaces($value[$i]);
  135. }
  136. } else {
  137. $value = trim(str_replace('+', ' ', $value));
  138. }
  139. return $value;
  140. }
  141. /**
  142. * Parse the search string to find feed IDs.
  143. *
  144. * @param string $input
  145. * @return string
  146. */
  147. private function parseFeedIds($input) {
  148. if (preg_match_all('/\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  149. $ids_lists = $matches['search'];
  150. $input = str_replace($matches[0], '', $input);
  151. $ids_lists = self::removeEmptyValues($ids_lists);
  152. if (!empty($ids_lists[0])) {
  153. $this->feed_ids = explode(',', $ids_lists[0]);
  154. array_filter($this->feed_ids, function($v) { $v != ''; });
  155. }
  156. }
  157. return $input;
  158. }
  159. private function parseNotFeedIds($input) {
  160. if (preg_match_all('/[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  161. $ids_lists = $matches['search'];
  162. $input = str_replace($matches[0], '', $input);
  163. $ids_lists = self::removeEmptyValues($ids_lists);
  164. if (!empty($ids_lists[0])) {
  165. $this->not_feed_ids = explode(',', $ids_lists[0]);
  166. array_filter($this->not_feed_ids, function($v) { $v != ''; });
  167. }
  168. }
  169. return $input;
  170. }
  171. /**
  172. * Parse the search string to find intitle keyword and the search related
  173. * to it.
  174. * The search is the first word following the keyword.
  175. *
  176. * @param string $input
  177. * @return string
  178. */
  179. private function parseIntitleSearch($input) {
  180. if (preg_match_all('/\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  181. $this->intitle = $matches['search'];
  182. $input = str_replace($matches[0], '', $input);
  183. }
  184. if (preg_match_all('/\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  185. $this->intitle = array_merge($this->intitle ? $this->intitle : array(), $matches['search']);
  186. $input = str_replace($matches[0], '', $input);
  187. }
  188. $this->intitle = self::removeEmptyValues($this->intitle);
  189. $this->intitle = self::decodeSpaces($this->intitle);
  190. return $input;
  191. }
  192. private function parseNotIntitleSearch($input) {
  193. if (preg_match_all('/[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  194. $this->not_intitle = $matches['search'];
  195. $input = str_replace($matches[0], '', $input);
  196. }
  197. if (preg_match_all('/[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  198. $this->not_intitle = array_merge($this->not_intitle ? $this->not_intitle : array(), $matches['search']);
  199. $input = str_replace($matches[0], '', $input);
  200. }
  201. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  202. $this->not_intitle = self::decodeSpaces($this->not_intitle);
  203. return $input;
  204. }
  205. /**
  206. * Parse the search string to find author keyword and the search related
  207. * to it.
  208. * The search is the first word following the keyword except when using
  209. * a delimiter. Supported delimiters are single quote (') and double
  210. * quotes (").
  211. *
  212. * @param string $input
  213. * @return string
  214. */
  215. private function parseAuthorSearch($input) {
  216. if (preg_match_all('/\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  217. $this->author = $matches['search'];
  218. $input = str_replace($matches[0], '', $input);
  219. }
  220. if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  221. $this->author = array_merge($this->author ? $this->author : array(), $matches['search']);
  222. $input = str_replace($matches[0], '', $input);
  223. }
  224. $this->author = self::removeEmptyValues($this->author);
  225. $this->author = self::decodeSpaces($this->author);
  226. return $input;
  227. }
  228. private function parseNotAuthorSearch($input) {
  229. if (preg_match_all('/[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  230. $this->not_author = $matches['search'];
  231. $input = str_replace($matches[0], '', $input);
  232. }
  233. if (preg_match_all('/[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  234. $this->not_author = array_merge($this->not_author ? $this->not_author : array(), $matches['search']);
  235. $input = str_replace($matches[0], '', $input);
  236. }
  237. $this->not_author = self::removeEmptyValues($this->not_author);
  238. $this->not_author = self::decodeSpaces($this->not_author);
  239. return $input;
  240. }
  241. /**
  242. * Parse the search string to find inurl keyword and the search related
  243. * to it.
  244. * The search is the first word following the keyword.
  245. *
  246. * @param string $input
  247. * @return string
  248. */
  249. private function parseInurlSearch($input) {
  250. if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
  251. $this->inurl = $matches['search'];
  252. $input = str_replace($matches[0], '', $input);
  253. }
  254. $this->inurl = self::removeEmptyValues($this->inurl);
  255. $this->inurl = self::decodeSpaces($this->inurl);
  256. return $input;
  257. }
  258. private function parseNotInurlSearch($input) {
  259. if (preg_match_all('/[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  260. $this->not_inurl = $matches['search'];
  261. $input = str_replace($matches[0], '', $input);
  262. }
  263. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  264. $this->not_inurl = self::decodeSpaces($this->not_inurl);
  265. return $input;
  266. }
  267. /**
  268. * Parse the search string to find date keyword and the search related
  269. * to it.
  270. * The search is the first word following the keyword.
  271. *
  272. * @param string $input
  273. * @return string
  274. */
  275. private function parseDateSearch($input) {
  276. if (preg_match_all('/\bdate:(?P<search>[^\s]*)/', $input, $matches)) {
  277. $input = str_replace($matches[0], '', $input);
  278. $dates = self::removeEmptyValues($matches['search']);
  279. if (!empty($dates[0])) {
  280. list($this->min_date, $this->max_date) = parseDateInterval($dates[0]);
  281. }
  282. }
  283. return $input;
  284. }
  285. private function parseNotDateSearch($input) {
  286. if (preg_match_all('/[!-]date:(?P<search>[^\s]*)/', $input, $matches)) {
  287. $input = str_replace($matches[0], '', $input);
  288. $dates = self::removeEmptyValues($matches['search']);
  289. if (!empty($dates[0])) {
  290. list($this->not_min_date, $this->not_max_date) = parseDateInterval($dates[0]);
  291. }
  292. }
  293. return $input;
  294. }
  295. /**
  296. * Parse the search string to find pubdate keyword and the search related
  297. * to it.
  298. * The search is the first word following the keyword.
  299. *
  300. * @param string $input
  301. * @return string
  302. */
  303. private function parsePubdateSearch($input) {
  304. if (preg_match_all('/\bpubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  305. $input = str_replace($matches[0], '', $input);
  306. $dates = self::removeEmptyValues($matches['search']);
  307. if (!empty($dates[0])) {
  308. list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($dates[0]);
  309. }
  310. }
  311. return $input;
  312. }
  313. private function parseNotPubdateSearch($input) {
  314. if (preg_match_all('/[!-]pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  315. $input = str_replace($matches[0], '', $input);
  316. $dates = self::removeEmptyValues($matches['search']);
  317. if (!empty($dates[0])) {
  318. list($this->not_min_pubdate, $this->not_max_pubdate) = parseDateInterval($dates[0]);
  319. }
  320. }
  321. return $input;
  322. }
  323. /**
  324. * Parse the search string to find tags keyword (# followed by a word)
  325. * and the search related to it.
  326. * The search is the first word following the #.
  327. *
  328. * @param string $input
  329. * @return string
  330. */
  331. private function parseTagsSearch($input) {
  332. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  333. $this->tags = $matches['search'];
  334. $input = str_replace($matches[0], '', $input);
  335. }
  336. $this->tags = self::removeEmptyValues($this->tags);
  337. $this->tags = self::decodeSpaces($this->tags);
  338. return $input;
  339. }
  340. private function parseNotTagsSearch($input) {
  341. if (preg_match_all('/[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
  342. $this->not_tags = $matches['search'];
  343. $input = str_replace($matches[0], '', $input);
  344. }
  345. $this->not_tags = self::removeEmptyValues($this->not_tags);
  346. $this->not_tags = self::decodeSpaces($this->not_tags);
  347. return $input;
  348. }
  349. /**
  350. * Parse the search string to find search values.
  351. * Every word is a distinct search value, except when using a delimiter.
  352. * Supported delimiters are single quote (') and double quotes (").
  353. *
  354. * @param string $input
  355. * @return string
  356. */
  357. private function parseSearch($input) {
  358. $input = self::cleanSearch($input);
  359. if ($input == '') {
  360. return;
  361. }
  362. if (preg_match_all('/(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  363. $this->search = $matches['search'];
  364. $input = str_replace($matches[0], '', $input);
  365. }
  366. $input = self::cleanSearch($input);
  367. if ($input == '') {
  368. return;
  369. }
  370. if (is_array($this->search)) {
  371. $this->search = array_merge($this->search, explode(' ', $input));
  372. } else {
  373. $this->search = explode(' ', $input);
  374. }
  375. $this->search = self::decodeSpaces($this->search);
  376. }
  377. private function parseNotSearch($input) {
  378. $input = self::cleanSearch($input);
  379. if ($input == '') {
  380. return;
  381. }
  382. if (preg_match_all('/[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  383. $this->not_search = $matches['search'];
  384. $input = str_replace($matches[0], '', $input);
  385. }
  386. if ($input == '') {
  387. return;
  388. }
  389. if (preg_match_all('/[!-](?P<search>[^\s]+)/', $input, $matches)) {
  390. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : array(), $matches['search']);
  391. $input = str_replace($matches[0], '', $input);
  392. }
  393. $this->not_search = self::removeEmptyValues($this->not_search);
  394. $this->not_search = self::decodeSpaces($this->not_search);
  395. return $input;
  396. }
  397. /**
  398. * Remove all unnecessary spaces in the search
  399. *
  400. * @param string $input
  401. * @return string
  402. */
  403. private static function cleanSearch($input) {
  404. $input = preg_replace('/\s+/', ' ', $input);
  405. return trim($input);
  406. }
  407. }