]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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 | 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 | ||
41 | if (!$feed_url) | |
42 | $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlURL')->nodeValue); | |
43 | ||
44 | $site_url = db_escape_string($outline->attributes->getNamedItem('htmlUrl')->nodeValue); | |
45 | ||
46 | $pref_name = db_escape_string($outline->attributes->getNamedItem('pref-name')->nodeValue); | |
47 | ||
48 | if ($cat_title && !$feed_url) { | |
49 | ||
50 | if ($cat_title != "tt-rss-prefs") { | |
51 | ||
52 | db_query($link, "BEGIN"); | |
53 | ||
54 | $result = db_query($link, "SELECT id FROM | |
55 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
56 | owner_uid = '$owner_uid' LIMIT 1"); | |
57 | ||
58 | if (db_num_rows($result) == 0) { | |
59 | ||
60 | printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title); | |
61 | ||
62 | db_query($link, "INSERT INTO ttrss_feed_categories | |
63 | (title,owner_uid) | |
64 | VALUES ('$cat_title', '$owner_uid')"); | |
65 | } | |
66 | ||
67 | db_query($link, "COMMIT"); | |
68 | } | |
69 | } | |
70 | ||
71 | // print "$active_category : $feed_title : $feed_url<br>"; | |
72 | ||
73 | if ($pref_name) { | |
74 | $parent_node = $outline->parentNode; | |
75 | ||
76 | if ($parent_node && $parent_node->nodeName == "outline") { | |
77 | $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
78 | if ($cat_check == "tt-rss-prefs") { | |
79 | $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue); | |
80 | ||
81 | printf("<li>". | |
82 | __("Setting preference key %s to %s")."</li>", | |
83 | $pref_name, $pref_value); | |
84 | ||
85 | set_pref($link, $pref_name, $pref_value); | |
86 | ||
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | if (!$feed_title || !$feed_url) continue; | |
92 | ||
93 | db_query($link, "BEGIN"); | |
94 | ||
95 | $cat_id = null; | |
96 | ||
97 | $parent_node = $outline->parentNode; | |
98 | ||
99 | if ($parent_node && $parent_node->nodeName == "outline") { | |
100 | $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
101 | if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue; | |
102 | ||
103 | } else { | |
104 | $element_category = ''; | |
105 | } | |
106 | ||
107 | if ($element_category) { | |
108 | ||
109 | $element_category = db_escape_string($element_category); | |
110 | ||
111 | $result = db_query($link, "SELECT id FROM | |
112 | ttrss_feed_categories WHERE title = '$element_category' AND | |
113 | owner_uid = '$owner_uid' LIMIT 1"); | |
114 | ||
115 | if (db_num_rows($result) == 1) { | |
116 | $cat_id = db_fetch_result($result, 0, "id"); | |
117 | } | |
118 | } | |
119 | ||
120 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE | |
121 | feed_url = '$feed_url' | |
122 | AND owner_uid = '$owner_uid'"); | |
123 | ||
124 | print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b> | |
125 | (<a target='_blank' href=\"$feed_url\">rss</a>) "; | |
126 | ||
127 | if (db_num_rows($result) > 0) { | |
128 | print __('is already imported.'); | |
129 | } else { | |
130 | ||
131 | if ($cat_id) { | |
132 | $add_query = "INSERT INTO ttrss_feeds | |
133 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
134 | ('$feed_title', '$feed_url', '$owner_uid', | |
135 | '$cat_id', '$site_url')"; | |
136 | ||
137 | } else { | |
138 | $add_query = "INSERT INTO ttrss_feeds | |
139 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
140 | ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id', | |
141 | '$site_url')"; | |
142 | ||
143 | } | |
144 | ||
145 | //print $add_query; | |
146 | db_query($link, $add_query); | |
147 | ||
148 | print __('OK'); | |
149 | } | |
150 | ||
151 | print "</li>"; | |
152 | ||
153 | db_query($link, "COMMIT"); | |
154 | } | |
155 | ||
156 | } else { | |
157 | print_error(__('Error while parsing document.')); | |
158 | } | |
159 | ||
160 | } else { | |
161 | print_error(__('Error: please upload OPML file.')); | |
162 | } | |
163 | ||
164 | ||
165 | } | |
166 | ?> |