]>
Commit | Line | Data |
---|---|---|
30f782a2 AD |
1 | <?php |
2 | function opml_import_domdoc($link, $owner_uid) { | |
3 | ||
4 | if (is_file($_FILES['opml_file']['tmp_name'])) { | |
5 | $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']); | |
6 | ||
c03cf250 AD |
7 | $result = db_query($link, "SELECT id FROM |
8 | ttrss_feed_categories WHERE title = 'Imported feeds' AND | |
9 | owner_uid = '$owner_uid' LIMIT 1"); | |
10 | ||
11 | if (db_num_rows($result) == 1) { | |
12 | $default_cat_id = db_fetch_result($result, 0, "id"); | |
13 | } else { | |
14 | $default_cat_id = 0; | |
15 | } | |
16 | ||
30f782a2 AD |
17 | if ($doc) { |
18 | $body = $doc->getElementsByTagName('body'); | |
19 | ||
20 | $xpath = new DOMXpath($doc); | |
21 | $query = "/opml/body//outline"; | |
22 | ||
23 | $outlines = $xpath->query($query); | |
24 | ||
30f782a2 AD |
25 | foreach ($outlines as $outline) { |
26 | ||
27 | $feed_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); | |
28 | ||
29 | if (!$feed_title) { | |
30 | $feed_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); | |
31 | } | |
32 | ||
33 | $cat_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); | |
34 | ||
35 | if (!$cat_title) { | |
36 | $cat_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); | |
37 | } | |
38 | ||
39 | $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlUrl')->nodeValue); | |
40 | $site_url = db_escape_string($outline->attributes->getNamedItem('htmlUrl')->nodeValue); | |
41 | ||
42 | if ($cat_title && !$feed_url) { | |
43 | ||
44 | db_query($link, "BEGIN"); | |
45 | ||
46 | $result = db_query($link, "SELECT id FROM | |
47 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
48 | owner_uid = '$owner_uid' LIMIT 1"); | |
49 | ||
50 | if (db_num_rows($result) == 0) { | |
51 | ||
b6a6e262 | 52 | printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title); |
30f782a2 AD |
53 | |
54 | db_query($link, "INSERT INTO ttrss_feed_categories | |
55 | (title,owner_uid) | |
56 | VALUES ('$cat_title', '$owner_uid')"); | |
57 | } | |
58 | ||
59 | db_query($link, "COMMIT"); | |
60 | } | |
61 | ||
62 | // print "$active_category : $feed_title : $feed_url<br>"; | |
63 | ||
64 | if (!$feed_title || !$feed_url) continue; | |
65 | ||
66 | db_query($link, "BEGIN"); | |
67 | ||
68 | $cat_id = null; | |
69 | ||
70 | $parent_node = $outline->parentNode; | |
71 | ||
72 | if ($parent_node && $parent_node->nodeName == "outline") { | |
73 | $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
eb8b5c26 AD |
74 | if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue; |
75 | ||
30f782a2 AD |
76 | } else { |
77 | $element_category = ''; | |
78 | } | |
79 | ||
80 | if ($element_category) { | |
81 | ||
eb8b5c26 AD |
82 | $element_category = db_escape_string($element_category); |
83 | ||
30f782a2 AD |
84 | $result = db_query($link, "SELECT id FROM |
85 | ttrss_feed_categories WHERE title = '$element_category' AND | |
86 | owner_uid = '$owner_uid' LIMIT 1"); | |
87 | ||
88 | if (db_num_rows($result) == 1) { | |
89 | $cat_id = db_fetch_result($result, 0, "id"); | |
90 | } | |
91 | } | |
92 | ||
93 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE | |
f0053c6a | 94 | feed_url = '$feed_url' |
30f782a2 AD |
95 | AND owner_uid = '$owner_uid'"); |
96 | ||
b6a6e262 AD |
97 | print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b> |
98 | (<a target='_blank' href=\"$feed_url\">rss</a>) "; | |
30f782a2 AD |
99 | |
100 | if (db_num_rows($result) > 0) { | |
b6a6e262 | 101 | print __('is already imported.'); |
30f782a2 AD |
102 | } else { |
103 | ||
104 | if ($cat_id) { | |
105 | $add_query = "INSERT INTO ttrss_feeds | |
106 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
107 | ('$feed_title', '$feed_url', '$owner_uid', | |
108 | '$cat_id', '$site_url')"; | |
109 | ||
110 | } else { | |
111 | $add_query = "INSERT INTO ttrss_feeds | |
c03cf250 AD |
112 | (title, feed_url, owner_uid, cat_id, site_url) VALUES |
113 | ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id', | |
114 | '$site_url')"; | |
30f782a2 AD |
115 | |
116 | } | |
117 | ||
eb8b5c26 | 118 | //print $add_query; |
30f782a2 AD |
119 | db_query($link, $add_query); |
120 | ||
b6a6e262 | 121 | print __('OK'); |
30f782a2 AD |
122 | } |
123 | ||
b6a6e262 | 124 | print "</li>"; |
30f782a2 AD |
125 | |
126 | db_query($link, "COMMIT"); | |
127 | } | |
128 | ||
30f782a2 | 129 | } else { |
b6a6e262 | 130 | print_error(__('Error while parsing document.')); |
30f782a2 AD |
131 | } |
132 | ||
133 | } else { | |
b6a6e262 | 134 | print_error(__('Error: please upload OPML file.')); |
30f782a2 AD |
135 | } |
136 | ||
137 | ||
138 | } | |
139 | ?> |