]>
Commit | Line | Data |
---|---|---|
30f782a2 AD |
1 | <?php |
2 | function opml_import_domdoc($link, $owner_uid) { | |
3 | ||
e9558345 AD |
4 | $_FILES['opml_file']['tmp_name'] = '/home/fox/opml.php'; |
5 | ||
30f782a2 AD |
6 | if (is_file($_FILES['opml_file']['tmp_name'])) { |
7 | $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']); | |
8 | ||
c03cf250 AD |
9 | $result = db_query($link, "SELECT id FROM |
10 | ttrss_feed_categories WHERE title = 'Imported feeds' AND | |
11 | owner_uid = '$owner_uid' LIMIT 1"); | |
12 | ||
13 | if (db_num_rows($result) == 1) { | |
14 | $default_cat_id = db_fetch_result($result, 0, "id"); | |
15 | } else { | |
16 | $default_cat_id = 0; | |
17 | } | |
18 | ||
30f782a2 AD |
19 | if ($doc) { |
20 | $body = $doc->getElementsByTagName('body'); | |
21 | ||
22 | $xpath = new DOMXpath($doc); | |
23 | $query = "/opml/body//outline"; | |
24 | ||
25 | $outlines = $xpath->query($query); | |
26 | ||
30f782a2 AD |
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 | ||
e9558345 AD |
44 | $pref_name = db_escape_string($outline->attributes->getNamedItem('pref-name')->nodeValue); |
45 | ||
30f782a2 AD |
46 | if ($cat_title && !$feed_url) { |
47 | ||
e9558345 AD |
48 | if ($cat_title != "tt-rss-prefs") { |
49 | ||
50 | db_query($link, "BEGIN"); | |
51 | ||
52 | $result = db_query($link, "SELECT id FROM | |
53 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
54 | owner_uid = '$owner_uid' LIMIT 1"); | |
55 | ||
56 | if (db_num_rows($result) == 0) { | |
57 | ||
58 | printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title); | |
59 | ||
60 | db_query($link, "INSERT INTO ttrss_feed_categories | |
61 | (title,owner_uid) | |
62 | VALUES ('$cat_title', '$owner_uid')"); | |
63 | } | |
64 | ||
65 | db_query($link, "COMMIT"); | |
66 | } | |
67 | } | |
30f782a2 | 68 | |
e9558345 | 69 | // print "$active_category : $feed_title : $feed_url<br>"; |
30f782a2 | 70 | |
e9558345 AD |
71 | if ($pref_name) { |
72 | $parent_node = $outline->parentNode; | |
30f782a2 | 73 | |
e9558345 AD |
74 | if ($parent_node && $parent_node->nodeName == "outline") { |
75 | $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
76 | if ($cat_check == "tt-rss-prefs") { | |
77 | $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue); | |
30f782a2 | 78 | |
e9558345 AD |
79 | printf("<li>". |
80 | __("Setting preference key %s to %s")."</li>", | |
81 | $pref_name, $pref_value); | |
30f782a2 | 82 | |
e9558345 | 83 | set_pref($link, $pref_name, $pref_value); |
30f782a2 | 84 | |
e9558345 AD |
85 | } |
86 | } | |
87 | } | |
30f782a2 AD |
88 | |
89 | if (!$feed_title || !$feed_url) continue; | |
90 | ||
91 | db_query($link, "BEGIN"); | |
92 | ||
93 | $cat_id = null; | |
94 | ||
95 | $parent_node = $outline->parentNode; | |
96 | ||
97 | if ($parent_node && $parent_node->nodeName == "outline") { | |
98 | $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
eb8b5c26 AD |
99 | if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue; |
100 | ||
30f782a2 AD |
101 | } else { |
102 | $element_category = ''; | |
103 | } | |
104 | ||
105 | if ($element_category) { | |
106 | ||
eb8b5c26 AD |
107 | $element_category = db_escape_string($element_category); |
108 | ||
30f782a2 AD |
109 | $result = db_query($link, "SELECT id FROM |
110 | ttrss_feed_categories WHERE title = '$element_category' AND | |
111 | owner_uid = '$owner_uid' LIMIT 1"); | |
112 | ||
113 | if (db_num_rows($result) == 1) { | |
114 | $cat_id = db_fetch_result($result, 0, "id"); | |
115 | } | |
116 | } | |
117 | ||
118 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE | |
f0053c6a | 119 | feed_url = '$feed_url' |
30f782a2 AD |
120 | AND owner_uid = '$owner_uid'"); |
121 | ||
b6a6e262 AD |
122 | print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b> |
123 | (<a target='_blank' href=\"$feed_url\">rss</a>) "; | |
30f782a2 AD |
124 | |
125 | if (db_num_rows($result) > 0) { | |
b6a6e262 | 126 | print __('is already imported.'); |
30f782a2 AD |
127 | } else { |
128 | ||
129 | if ($cat_id) { | |
130 | $add_query = "INSERT INTO ttrss_feeds | |
131 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
132 | ('$feed_title', '$feed_url', '$owner_uid', | |
133 | '$cat_id', '$site_url')"; | |
134 | ||
135 | } else { | |
136 | $add_query = "INSERT INTO ttrss_feeds | |
c03cf250 AD |
137 | (title, feed_url, owner_uid, cat_id, site_url) VALUES |
138 | ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id', | |
139 | '$site_url')"; | |
30f782a2 AD |
140 | |
141 | } | |
142 | ||
eb8b5c26 | 143 | //print $add_query; |
30f782a2 AD |
144 | db_query($link, $add_query); |
145 | ||
b6a6e262 | 146 | print __('OK'); |
30f782a2 AD |
147 | } |
148 | ||
b6a6e262 | 149 | print "</li>"; |
30f782a2 AD |
150 | |
151 | db_query($link, "COMMIT"); | |
152 | } | |
153 | ||
30f782a2 | 154 | } else { |
b6a6e262 | 155 | print_error(__('Error while parsing document.')); |
30f782a2 AD |
156 | } |
157 | ||
158 | } else { | |
b6a6e262 | 159 | print_error(__('Error: please upload OPML file.')); |
30f782a2 AD |
160 | } |
161 | ||
162 | ||
163 | } | |
164 | ?> |