FilterBridge.php 2.2 KB

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