FilterBridge.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. class FilterBridge extends FeedExpander {
  3. const MAINTAINER = 'Frenzie';
  4. const NAME = 'Filter';
  5. const CACHE_TIMEOUT = 3600; // 1h
  6. const DESCRIPTION = 'Filters a feed of your choice';
  7. const URI = 'https://github.com/rss-bridge/rss-bridge';
  8. const PARAMETERS = array(array(
  9. 'url' => array(
  10. 'name' => 'Feed URL',
  11. 'required' => true,
  12. ),
  13. 'filter' => array(
  14. 'name' => 'Filter item title (regular expression)',
  15. 'required' => false,
  16. ),
  17. 'filter_type' => array(
  18. 'name' => 'Filter type',
  19. 'type' => 'list',
  20. 'required' => false,
  21. 'values' => array(
  22. 'Permit' => 'permit',
  23. 'Block' => 'block',
  24. ),
  25. 'defaultValue' => 'permit',
  26. ),
  27. 'title_from_content' => array(
  28. 'name' => 'Generate title from content',
  29. 'type' => 'checkbox',
  30. 'required' => false,
  31. )
  32. ));
  33. protected function parseItem($newItem){
  34. $item = parent::parseItem($newItem);
  35. if($this->getInput('title_from_content') && array_key_exists('content', $item)) {
  36. $content = str_get_html($item['content']);
  37. $pos = strpos($item['content'], ' ', 50);
  38. $item['title'] = substr(
  39. $content->plaintext,
  40. 0,
  41. $pos
  42. );
  43. if(strlen($content->plaintext) >= $pos) {
  44. $item['title'] .= '...';
  45. }
  46. }
  47. switch(true) {
  48. case $this->getFilterType() === 'permit':
  49. if (preg_match($this->getFilter(), $item['title'])) {
  50. return $item;
  51. }
  52. break;
  53. case $this->getFilterType() === 'block':
  54. if (!preg_match($this->getFilter(), $item['title'])) {
  55. return $item;
  56. }
  57. break;
  58. }
  59. return null;
  60. }
  61. protected function getFilter(){
  62. return '/' . $this->getInput('filter') . '/';
  63. }
  64. protected function getFilterType(){
  65. return $this->getInput('filter_type');
  66. }
  67. public function getURI(){
  68. $url = $this->getInput('url');
  69. if(empty($url)) {
  70. $url = parent::getURI();
  71. }
  72. return $url;
  73. }
  74. public function collectData(){
  75. if($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http') {
  76. // just in case someone find a way to access local files by playing with the url
  77. returnClientError('The url parameter must either refer to http or https protocol.');
  78. }
  79. try{
  80. $this->collectExpandableDatas($this->getURI());
  81. } catch (HttpException $e) {
  82. $this->collectExpandableDatas($this->getURI());
  83. }
  84. }
  85. }