lib_opml.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 === false) {
  49. $cat = new FreshRSS_Category ($title);
  50. $values = array (
  51. 'name' => $cat->name (),
  52. 'color' => $cat->color ()
  53. );
  54. $cat->_id ($catDAO->addCategory ($values));
  55. }
  56. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  57. }
  58. } else {
  59. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  60. $feeds[] = getFeed ($outline, $defCat->id());
  61. }
  62. }
  63. return array ($categories, $feeds);
  64. }
  65. /**
  66. * import all feeds of a given outline tag
  67. */
  68. function getFeedsOutline ($outline, $cat_id) {
  69. $feeds = array ();
  70. foreach ($outline->children () as $child) {
  71. if (isset ($child['xmlUrl'])) {
  72. $feeds[] = getFeed ($child, $cat_id);
  73. } else {
  74. $feeds = array_merge(
  75. $feeds,
  76. getFeedsOutline ($child, $cat_id)
  77. );
  78. }
  79. }
  80. return $feeds;
  81. }
  82. function getFeed ($outline, $cat_id) {
  83. $url = (string) $outline['xmlUrl'];
  84. $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
  85. $title = '';
  86. if (isset ($outline['text'])) {
  87. $title = (string) $outline['text'];
  88. } elseif (isset ($outline['title'])) {
  89. $title = (string) $outline['title'];
  90. }
  91. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  92. $feed = new FreshRSS_Feed ($url);
  93. $feed->_category ($cat_id);
  94. $feed->_name ($title);
  95. if (isset($outline['htmlUrl'])) {
  96. $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
  97. }
  98. if (isset($outline['description'])) {
  99. $feed->_description(sanitizeHTML((string)$outline['description']));
  100. }
  101. return $feed;
  102. }