lib_opml.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. function opml_export ($cats) {
  3. $txt = '';
  4. foreach ($cats as $cat) {
  5. $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
  6. foreach ($cat['feeds'] as $feed) {
  7. $txt .= "\t" . '<outline text="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" description="' . htmlspecialchars($feed->description(), ENT_COMPAT, 'UTF-8') . '" />' . "\n";
  8. }
  9. $txt .= '</outline>' . "\n";
  10. }
  11. return $txt;
  12. }
  13. function opml_import ($xml) {
  14. $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
  15. $dom = new DOMDocument();
  16. $dom->recover = true;
  17. $dom->strictErrorChecking = false;
  18. $dom->loadXML($xml);
  19. $dom->encoding = 'UTF-8';
  20. $opml = simplexml_import_dom($dom);
  21. if (!$opml) {
  22. throw new FreshRSS_Opml_Exception ();
  23. }
  24. $catDAO = new FreshRSS_CategoryDAO();
  25. $catDAO->checkDefault();
  26. $defCat = $catDAO->getDefault();
  27. $categories = array ();
  28. $feeds = array ();
  29. foreach ($opml->body->outline as $outline) {
  30. if (!isset ($outline['xmlUrl'])) {
  31. // Catégorie
  32. $title = '';
  33. if (isset ($outline['text'])) {
  34. $title = (string) $outline['text'];
  35. } elseif (isset ($outline['title'])) {
  36. $title = (string) $outline['title'];
  37. }
  38. if ($title) {
  39. // Permet d'éviter les soucis au niveau des id :
  40. // ceux-ci sont générés en fonction de la date,
  41. // un flux pourrait être dans une catégorie X avec l'id Y
  42. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  43. // Y ne sera pas ajouté et le flux non plus vu que l'id
  44. // de sa catégorie n'exisera pas
  45. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  46. $catDAO = new FreshRSS_CategoryDAO ();
  47. $cat = $catDAO->searchByName ($title);
  48. if ($cat == null) {
  49. $cat = new FreshRSS_Category ($title);
  50. $values = array (
  51. 'name' => $cat->name ()
  52. );
  53. $cat->_id ($catDAO->addCategory ($values));
  54. }
  55. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  56. }
  57. } else {
  58. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  59. $feeds[] = getFeed ($outline, $defCat->id());
  60. }
  61. }
  62. return array ($categories, $feeds);
  63. }
  64. /**
  65. * import all feeds of a given outline tag
  66. */
  67. function getFeedsOutline ($outline, $cat_id) {
  68. $feeds = array ();
  69. foreach ($outline->children () as $child) {
  70. if (isset ($child['xmlUrl'])) {
  71. $feeds[] = getFeed ($child, $cat_id);
  72. } else {
  73. $feeds = array_merge(
  74. $feeds,
  75. getFeedsOutline ($child, $cat_id)
  76. );
  77. }
  78. }
  79. return $feeds;
  80. }
  81. function getFeed ($outline, $cat_id) {
  82. $url = (string) $outline['xmlUrl'];
  83. $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
  84. $title = '';
  85. if (isset ($outline['text'])) {
  86. $title = (string) $outline['text'];
  87. } elseif (isset ($outline['title'])) {
  88. $title = (string) $outline['title'];
  89. }
  90. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  91. $feed = new FreshRSS_Feed ($url);
  92. $feed->_category ($cat_id);
  93. $feed->_name ($title);
  94. if (isset($outline['htmlUrl'])) {
  95. $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
  96. }
  97. if (isset($outline['description'])) {
  98. $feed->_description(sanitizeHTML((string)$outline['description']));
  99. }
  100. return $feed;
  101. }