FilterBridge.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. ));
  27. protected function parseItem($newItem){
  28. $item = parent::parseItem($newItem);
  29. switch(true) {
  30. case $this->getFilterType() === 'permit':
  31. if (preg_match($this->getFilter(), $item['title'])) {
  32. return $item;
  33. }
  34. break;
  35. case $this->getFilterType() === 'block':
  36. if (!preg_match($this->getFilter(), $item['title'])) {
  37. return $item;
  38. }
  39. break;
  40. }
  41. return null;
  42. }
  43. protected function getFilter(){
  44. return '/' . $this->getInput('filter') . '/';
  45. }
  46. protected function getFilterType(){
  47. return $this->getInput('filter_type');
  48. }
  49. public function getURI(){
  50. $url = $this->getInput('url');
  51. if(empty($url)) {
  52. $url = parent::getURI();
  53. }
  54. return $url;
  55. }
  56. public function collectData(){
  57. if($this->getInput('url') && substr($this->getInput('url'), 0, strlen('http')) !== 'http') {
  58. // just in case someone find a way to access local files by playing with the url
  59. returnClientError('The url parameter must either refer to http or https protocol.');
  60. }
  61. try{
  62. $this->collectExpandableDatas($this->getURI());
  63. } catch (HttpException $e) {
  64. $this->collectExpandableDatas($this->getURI());
  65. }
  66. }
  67. }