Search.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. return $input;
  190. }
  191. private function parseNotIntitleSearch($input) {
  192. if (preg_match_all('/[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  193. $this->not_intitle = $matches['search'];
  194. $input = str_replace($matches[0], '', $input);
  195. }
  196. if (preg_match_all('/[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  197. $this->not_intitle = array_merge($this->not_intitle ? $this->not_intitle : array(), $matches['search']);
  198. $input = str_replace($matches[0], '', $input);
  199. }
  200. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  201. return $input;
  202. }
  203. /**
  204. * Parse the search string to find author keyword and the search related
  205. * to it.
  206. * The search is the first word following the keyword except when using
  207. * a delimiter. Supported delimiters are single quote (') and double
  208. * quotes (").
  209. *
  210. * @param string $input
  211. * @return string
  212. */
  213. private function parseAuthorSearch($input) {
  214. if (preg_match_all('/\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  215. $this->author = $matches['search'];
  216. $input = str_replace($matches[0], '', $input);
  217. }
  218. if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  219. $this->author = array_merge($this->author ? $this->author : array(), $matches['search']);
  220. $input = str_replace($matches[0], '', $input);
  221. }
  222. $this->author = self::removeEmptyValues($this->author);
  223. return $input;
  224. }
  225. private function parseNotAuthorSearch($input) {
  226. if (preg_match_all('/[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  227. $this->not_author = $matches['search'];
  228. $input = str_replace($matches[0], '', $input);
  229. }
  230. if (preg_match_all('/[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  231. $this->not_author = array_merge($this->not_author ? $this->not_author : array(), $matches['search']);
  232. $input = str_replace($matches[0], '', $input);
  233. }
  234. $this->not_author = self::removeEmptyValues($this->not_author);
  235. return $input;
  236. }
  237. /**
  238. * Parse the search string to find inurl keyword and the search related
  239. * to it.
  240. * The search is the first word following the keyword.
  241. *
  242. * @param string $input
  243. * @return string
  244. */
  245. private function parseInurlSearch($input) {
  246. if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
  247. $this->inurl = $matches['search'];
  248. $input = str_replace($matches[0], '', $input);
  249. }
  250. $this->inurl = self::removeEmptyValues($this->inurl);
  251. return $input;
  252. }
  253. private function parseNotInurlSearch($input) {
  254. if (preg_match_all('/[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  255. $this->not_inurl = $matches['search'];
  256. $input = str_replace($matches[0], '', $input);
  257. }
  258. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  259. return $input;
  260. }
  261. /**
  262. * Parse the search string to find date keyword and the search related
  263. * to it.
  264. * The search is the first word following the keyword.
  265. *
  266. * @param string $input
  267. * @return string
  268. */
  269. private function parseDateSearch($input) {
  270. if (preg_match_all('/\bdate:(?P<search>[^\s]*)/', $input, $matches)) {
  271. $input = str_replace($matches[0], '', $input);
  272. $dates = self::removeEmptyValues($matches['search']);
  273. if (!empty($dates[0])) {
  274. list($this->min_date, $this->max_date) = parseDateInterval($dates[0]);
  275. }
  276. }
  277. return $input;
  278. }
  279. private function parseNotDateSearch($input) {
  280. if (preg_match_all('/[!-]date:(?P<search>[^\s]*)/', $input, $matches)) {
  281. $input = str_replace($matches[0], '', $input);
  282. $dates = self::removeEmptyValues($matches['search']);
  283. if (!empty($dates[0])) {
  284. list($this->not_min_date, $this->not_max_date) = parseDateInterval($dates[0]);
  285. }
  286. }
  287. return $input;
  288. }
  289. /**
  290. * Parse the search string to find pubdate keyword and the search related
  291. * to it.
  292. * The search is the first word following the keyword.
  293. *
  294. * @param string $input
  295. * @return string
  296. */
  297. private function parsePubdateSearch($input) {
  298. if (preg_match_all('/\bpubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  299. $input = str_replace($matches[0], '', $input);
  300. $dates = self::removeEmptyValues($matches['search']);
  301. if (!empty($dates[0])) {
  302. list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($dates[0]);
  303. }
  304. }
  305. return $input;
  306. }
  307. private function parseNotPubdateSearch($input) {
  308. if (preg_match_all('/[!-]pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  309. $input = str_replace($matches[0], '', $input);
  310. $dates = self::removeEmptyValues($matches['search']);
  311. if (!empty($dates[0])) {
  312. list($this->not_min_pubdate, $this->not_max_pubdate) = parseDateInterval($dates[0]);
  313. }
  314. }
  315. return $input;
  316. }
  317. /**
  318. * Parse the search string to find tags keyword (# followed by a word)
  319. * and the search related to it.
  320. * The search is the first word following the #.
  321. *
  322. * @param string $input
  323. * @return string
  324. */
  325. private function parseTagsSearch($input) {
  326. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  327. $this->tags = $matches['search'];
  328. $input = str_replace($matches[0], '', $input);
  329. }
  330. $this->tags = self::removeEmptyValues($this->tags);
  331. $this->tags = self::decodeSpaces($this->tags);
  332. return $input;
  333. }
  334. private function parseNotTagsSearch($input) {
  335. if (preg_match_all('/[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
  336. $this->not_tags = $matches['search'];
  337. $input = str_replace($matches[0], '', $input);
  338. }
  339. $this->not_tags = self::removeEmptyValues($this->not_tags);
  340. $this->not_tags = self::decodeSpaces($this->not_tags);
  341. return $input;
  342. }
  343. /**
  344. * Parse the search string to find search values.
  345. * Every word is a distinct search value, except when using a delimiter.
  346. * Supported delimiters are single quote (') and double quotes (").
  347. *
  348. * @param string $input
  349. * @return string
  350. */
  351. private function parseSearch($input) {
  352. $input = self::cleanSearch($input);
  353. if ($input == '') {
  354. return;
  355. }
  356. if (preg_match_all('/(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  357. $this->search = $matches['search'];
  358. $input = str_replace($matches[0], '', $input);
  359. }
  360. $input = self::cleanSearch($input);
  361. if ($input == '') {
  362. return;
  363. }
  364. if (is_array($this->search)) {
  365. $this->search = array_merge($this->search, explode(' ', $input));
  366. } else {
  367. $this->search = explode(' ', $input);
  368. }
  369. }
  370. private function parseNotSearch($input) {
  371. $input = self::cleanSearch($input);
  372. if ($input == '') {
  373. return;
  374. }
  375. if (preg_match_all('/[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  376. $this->not_search = $matches['search'];
  377. $input = str_replace($matches[0], '', $input);
  378. }
  379. if ($input == '') {
  380. return;
  381. }
  382. if (preg_match_all('/[!-](?P<search>[^\s]+)/', $input, $matches)) {
  383. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : array(), $matches['search']);
  384. $input = str_replace($matches[0], '', $input);
  385. }
  386. $this->not_search = self::removeEmptyValues($this->not_search);
  387. return $input;
  388. }
  389. /**
  390. * Remove all unnecessary spaces in the search
  391. *
  392. * @param string $input
  393. * @return string
  394. */
  395. private static function cleanSearch($input) {
  396. $input = preg_replace('/\s+/', ' ', $input);
  397. return trim($input);
  398. }
  399. }