]>
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 | ||
25 | print "<table>"; | |
26 | ||
27 | foreach ($outlines as $outline) { | |
28 | ||
29 | $feed_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); | |
30 | ||
31 | if (!$feed_title) { | |
32 | $feed_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); | |
33 | } | |
34 | ||
35 | $cat_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); | |
36 | ||
37 | if (!$cat_title) { | |
38 | $cat_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); | |
39 | } | |
40 | ||
41 | $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlUrl')->nodeValue); | |
42 | $site_url = db_escape_string($outline->attributes->getNamedItem('htmlUrl')->nodeValue); | |
43 | ||
44 | if ($cat_title && !$feed_url) { | |
45 | ||
46 | db_query($link, "BEGIN"); | |
47 | ||
48 | $result = db_query($link, "SELECT id FROM | |
49 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
50 | owner_uid = '$owner_uid' LIMIT 1"); | |
51 | ||
52 | if (db_num_rows($result) == 0) { | |
53 | ||
1025ad87 | 54 | printf(__("Adding category <b>%s</b>...<br>"), $cat_title); |
30f782a2 AD |
55 | |
56 | db_query($link, "INSERT INTO ttrss_feed_categories | |
57 | (title,owner_uid) | |
58 | VALUES ('$cat_title', '$owner_uid')"); | |
59 | } | |
60 | ||
61 | db_query($link, "COMMIT"); | |
62 | } | |
63 | ||
64 | // print "$active_category : $feed_title : $feed_url<br>"; | |
65 | ||
66 | if (!$feed_title || !$feed_url) continue; | |
67 | ||
68 | db_query($link, "BEGIN"); | |
69 | ||
70 | $cat_id = null; | |
71 | ||
72 | $parent_node = $outline->parentNode; | |
73 | ||
74 | if ($parent_node && $parent_node->nodeName == "outline") { | |
75 | $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
eb8b5c26 AD |
76 | if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue; |
77 | ||
30f782a2 AD |
78 | } else { |
79 | $element_category = ''; | |
80 | } | |
81 | ||
82 | if ($element_category) { | |
83 | ||
eb8b5c26 AD |
84 | $element_category = db_escape_string($element_category); |
85 | ||
30f782a2 AD |
86 | $result = db_query($link, "SELECT id FROM |
87 | ttrss_feed_categories WHERE title = '$element_category' AND | |
88 | owner_uid = '$owner_uid' LIMIT 1"); | |
89 | ||
90 | if (db_num_rows($result) == 1) { | |
91 | $cat_id = db_fetch_result($result, 0, "id"); | |
92 | } | |
93 | } | |
94 | ||
95 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE | |
f0053c6a | 96 | feed_url = '$feed_url' |
30f782a2 AD |
97 | AND owner_uid = '$owner_uid'"); |
98 | ||
ef59e6e8 AD |
99 | print "<tr><td><a target='_new' href='$site_url'><b>$feed_title</b></a></b> |
100 | (<a target='_new' href=\"$feed_url\">rss</a>)</td>"; | |
30f782a2 AD |
101 | |
102 | if (db_num_rows($result) > 0) { | |
1025ad87 | 103 | print "<td>".__('Already imported.')."</td>"; |
30f782a2 AD |
104 | } else { |
105 | ||
106 | if ($cat_id) { | |
107 | $add_query = "INSERT INTO ttrss_feeds | |
108 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
109 | ('$feed_title', '$feed_url', '$owner_uid', | |
110 | '$cat_id', '$site_url')"; | |
111 | ||
112 | } else { | |
113 | $add_query = "INSERT INTO ttrss_feeds | |
c03cf250 AD |
114 | (title, feed_url, owner_uid, cat_id, site_url) VALUES |
115 | ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id', | |
116 | '$site_url')"; | |
30f782a2 AD |
117 | |
118 | } | |
119 | ||
eb8b5c26 | 120 | //print $add_query; |
30f782a2 AD |
121 | db_query($link, $add_query); |
122 | ||
1025ad87 | 123 | print "<td><b>".__('Done.')."</b></td>"; |
30f782a2 AD |
124 | } |
125 | ||
126 | print "</tr>"; | |
127 | ||
128 | db_query($link, "COMMIT"); | |
129 | } | |
130 | ||
131 | print "</table>"; | |
132 | ||
133 | } else { | |
1025ad87 | 134 | print "<div class=\"error\">".__('Error while parsing document.')."</div>"; |
30f782a2 AD |
135 | } |
136 | ||
137 | } else { | |
1025ad87 | 138 | print "<div class=\"error\">".__('Error: please upload OPML file.')."</div>"; |
30f782a2 AD |
139 | } |
140 | ||
141 | ||
142 | } | |
143 | ?> |