/plugins-dist/sites/action/importer_bookmarks_opml.php

https://bitbucket.org/re_al_/real.test.spip · PHP · 128 lines · 95 code · 18 blank · 15 comment · 22 complexity · 392c9d8de783a145cb9fabe6bf7c46c6 MD5 · raw file

  1. <?php
  2. /***************************************************************************\
  3. * SPIP, Systeme de publication pour l'internet *
  4. * *
  5. * Copyright (c) 2001-2016 *
  6. * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
  7. * *
  8. * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
  9. * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
  10. \***************************************************************************/
  11. if (!defined('_ECRIRE_INC_VERSION')) {
  12. return;
  13. }
  14. function action_importer_bookmarks_opml_dist($fichier_ok, $id_parent, $importer_statut_publie, $importer_tags) {
  15. $nb = 0;
  16. if (autoriser('importer', '_sites')) {
  17. $out = bookmarks_opml_parse($fichier_ok['contenu']);
  18. $nb = bookmarks_opml_insert($out, $id_parent, $importer_statut_publie, $importer_tags);
  19. }
  20. return $nb;
  21. }
  22. // http://www.stargeek.com/php_scripts.php?script=20&cat=blog
  23. function bookmarks_opml_parse(&$contenu) {
  24. global $blogs, $folder, $inOpmlfolder, $inOpmlItem;
  25. $inOpmlfolder = $inOpmlItem = false;
  26. $xp = xml_parser_create();
  27. xml_set_element_handler($xp, 'opml_startElement', 'opml_endElement');
  28. xml_parse($xp, $contenu, true);
  29. xml_parser_free($xp);
  30. return $blogs;
  31. }
  32. function opml_startElement($xp, $element, $attr) {
  33. global $blogs, $folder, $inOpmlfolder, $inOpmlItem;
  34. if (strcasecmp('outline', $element)) {
  35. return;
  36. }
  37. if (!array_key_exists('XMLURL', $attr) && (array_key_exists('TEXT', $attr) || array_key_exists('TITLE', $attr))) {
  38. //some opml use title instead of text to define a folder (ex: newzcrawler)
  39. $folder = $attr['TEXT'] ? $attr['TEXT'] : $attr['TITLE'];
  40. $inOpmlfolder = true;
  41. $inOpmlItem = false;
  42. } else {
  43. $inOpmlItem = true;
  44. if ($folder != '') {
  45. $blogs[$folder][] = $attr;
  46. } else {
  47. $blogs[] = $attr;
  48. }
  49. }
  50. }
  51. function opml_endElement($xp, $element) {
  52. global $blogs, $folder, $inOpmlfolder, $inOpmlItem;
  53. if (strcasecmp($element, "outline") === 0) {
  54. if (!$inOpmlItem && $inOpmlfolder) {
  55. // end of folder element!
  56. $inOpmlfolder = false;
  57. } else {
  58. // end of item element
  59. $inOpmlItem = false;
  60. }
  61. }
  62. return;
  63. }
  64. function bookmarks_opml_insert($tree, $id_parent, $importer_statut_publie, $importer_tags) {
  65. include_spip('action/editer_rubrique');
  66. include_spip('action/editer_site');
  67. $nb = 0;
  68. if (count($tree)) {
  69. foreach ($tree as $key => $item) {
  70. // cas d'un flux
  71. if (array_key_exists('XMLURL', $item)) {
  72. $statut = 'prop';
  73. if ($importer_statut_publie and autoriser('publierdans', 'rubrique', $id_parent)) {
  74. $statut = 'publie';
  75. }
  76. $now = time();
  77. if (!$id_syndic = sql_getfetsel('id_syndic', 'spip_syndic',
  78. 'id_rubrique=' . intval($id_parent) . " AND url_site=" . sql_quote($item['HTMLURL']))
  79. ) {
  80. $id_syndic = site_inserer($id_parent);
  81. $set = array(
  82. 'url_site' => $item['HTMLURL'],
  83. 'nom_site' => $item['TITLE'],
  84. 'url_syndic' => $item['XMLURL'],
  85. 'syndication' => 'oui',
  86. 'resume' => 'non',
  87. 'date' => date('Y-m-d H:i:s', $now),
  88. 'statut' => $statut
  89. );
  90. site_modifier($id_syndic, $set);
  91. $nb++;
  92. } else {
  93. $nb++;
  94. }
  95. } else {
  96. // cas d'un dossier
  97. $titre = $key;
  98. $id_rubrique = sql_getfetsel('id_rubrique', 'spip_rubriques',
  99. 'id_parent=' . intval($id_parent) . " AND titre=" . sql_quote($titre));
  100. if (!$id_rubrique and $id_rubrique = rubrique_inserer($id_parent)) {
  101. rubrique_modifier($id_rubrique, array('titre' => $titre));
  102. }
  103. if ($id_rubrique) {
  104. $nb += bookmarks_opml_insert($item, $id_rubrique, $importer_statut_publie, $importer_tags);
  105. }
  106. }
  107. }
  108. }
  109. return $nb;
  110. }