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