NakedSecurityBridge.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class NakedSecurityBridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = 'ORelio';
  5. $this->name = $this->getName();
  6. $this->uri = $this->getURI();
  7. $this->description = 'Returns the newest articles.';
  8. $this->update = '2016-04-30';
  9. }
  10. public function collectData(array $param) {
  11. function StripRecursiveHTMLSection($string, $tag_name, $tag_start) {
  12. $open_tag = '<'.$tag_name;
  13. $close_tag = '</'.$tag_name.'>';
  14. $close_tag_length = strlen($close_tag);
  15. if (strpos($tag_start, $open_tag) === 0) {
  16. while (strpos($string, $tag_start) !== false) {
  17. $max_recursion = 100;
  18. $section_to_remove = null;
  19. $section_start = strpos($string, $tag_start);
  20. $search_offset = $section_start;
  21. do {
  22. $max_recursion--;
  23. $section_end = strpos($string, $close_tag, $search_offset);
  24. $search_offset = $section_end + $close_tag_length;
  25. $section_to_remove = substr($string, $section_start, $section_end - $section_start + $close_tag_length);
  26. $open_tag_count = substr_count($section_to_remove, $open_tag);
  27. $close_tag_count = substr_count($section_to_remove, $close_tag);
  28. } while ($open_tag_count > $close_tag_count && $max_recursion > 0);
  29. $string = str_replace($section_to_remove, '', $string);
  30. }
  31. }
  32. return $string;
  33. }
  34. $feedUrl = 'https://feeds.feedburner.com/nakedsecurity?format=xml';
  35. $html = $this->file_get_html($feedUrl) or $this->returnError('Could not request '.$this->getName().': '.$feedUrl, 500);
  36. $limit = 0;
  37. foreach ($html->find('item') as $element) {
  38. if ($limit < 10) {
  39. //Retrieve article Uri and get that page
  40. $article_uri = $element->find('guid', 0)->plaintext;
  41. $article_html = $this->file_get_html($article_uri) or $this->returnError('Could not request '.$this->getName().': '.$article_uri, 500);
  42. //Build article contents from corresponding elements
  43. $article_title = trim($element->find('title', 0)->plaintext);
  44. $article_image = $article_html->find('img.wp-post-image', 0)->src;
  45. $article_summary = strip_tags(html_entity_decode($element->find('description', 0)->plaintext));
  46. $article_content = $article_html->find('div.entry-content', 0)->innertext;
  47. $article_content = StripRecursiveHTMLSection($article_content , 'div', '<div class="entry-prefix"');
  48. $article_content = StripRecursiveHTMLSection($article_content , 'script', '<script');
  49. $article_content = StripRecursiveHTMLSection($article_content , 'aside', '<aside');
  50. $article_content = '<p><img src="'.$article_image.'" /></p><p><b>'.$article_summary.'</b></p>'.$article_content;
  51. //Build and add final item
  52. $item = new \Item();
  53. $item->uri = $article_uri;
  54. $item->title = $article_title;
  55. $item->thumbnailUri = $article_image;
  56. $item->author = $article_html->find('a[rel=author]', 0)->plaintext;
  57. $item->timestamp = strtotime($element->find('pubDate', 0)->plaintext);
  58. $item->content = $article_content;
  59. $this->items[] = $item;
  60. $limit++;
  61. }
  62. }
  63. }
  64. public function getName() {
  65. return 'Naked Security';
  66. }
  67. public function getURI() {
  68. return 'https://nakedsecurity.sophos.com/';
  69. }
  70. public function getCacheDuration() {
  71. return 3600; //1 hour
  72. }
  73. }