]> git.wh0rd.org - tt-rss.git/blob - opml.php
fix OPML import handling uncategorized feeds, misc OPML touch-ups
[tt-rss.git] / opml.php
1 <?
2 session_start();
3
4 require_once "sanity_check.php";
5
6 // FIXME there are some brackets issues here
7
8 $op = $_REQUEST["op"];
9 if ($op == "Export") {
10 header("Content-type: application/xml");
11 print "<?xml version=\"1.0\"?>";
12 }
13
14 require_once "config.php";
15 require_once "db.php";
16 require_once "db-prefs.php";
17
18 $owner_uid = $_SESSION["uid"];
19
20 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
21
22 if (DB_TYPE == "pgsql") {
23 pg_query($link, "set client_encoding = 'utf-8'");
24 }
25
26 if ($op == "Export") {
27 print "<opml version=\"1.0\">";
28 print "<head><dateCreated>" . date("r", time()) . "</dateCreated></head>";
29 print "<body>";
30
31 $cat_mode = false;
32
33 if (get_pref($link, 'ENABLE_FEED_CATS')) {
34 $cat_mode = true;
35 $result = db_query($link, "SELECT
36 ttrss_feeds.feed_url AS feed_url,
37 ttrss_feeds.title AS title,
38 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title
39 FROM ttrss_feeds
40 WHERE
41 owner_uid = '$owner_uid'
42 ORDER BY cat_title,title");
43 } else {
44 $result = db_query($link, "SELECT * FROM ttrss_feeds
45 ORDER BY title WHERE owner_uid = '$owner_uid'");
46 }
47
48 $old_cat_title = "";
49
50 while ($line = db_fetch_assoc($result)) {
51 $title = htmlspecialchars($line["title"]);
52 $url = htmlspecialchars($line["feed_url"]);
53
54 if ($cat_mode) {
55 $cat_title = htmlspecialchars($line["cat_title"]);
56
57 if ($old_cat_title != $cat_title) {
58 if ($old_cat_title) {
59 print "</outline>\n";
60 }
61
62 if ($cat_title) {
63 print "<outline title=\"$cat_title\">\n";
64 }
65
66 $old_cat_title = $cat_title;
67 }
68 }
69
70 print "<outline text=\"$title\" xmlUrl=\"$url\"/>\n";
71 }
72
73 if ($cat_mode && $old_cat_title) {
74 print "</outline>\n";
75 }
76
77 print "</body></opml>";
78 }
79
80 if ($op == "Import") {
81
82 print "<html>
83 <head>
84 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
85 </head>
86 <body>
87 <h1>Importing OPML...</h1>
88 <div class=\"opmlBody\">";
89
90 if (WEB_DEMO_MODE) {
91 print "OPML import is disabled in demo-mode.";
92 print "<p><a class=\"button\" href=\"prefs.php\">
93 Return to preferences</a></div></body></html>";
94
95 return;
96 }
97
98 if (is_file($_FILES['opml_file']['tmp_name'])) {
99 $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
100
101 if ($dom) {
102 $root = $dom->document_element();
103
104 $body = $root->get_elements_by_tagname('body');
105
106 if ($body[0]) {
107 $body = $body[0];
108
109 $outlines = $body->get_elements_by_tagname('outline');
110
111 foreach ($outlines as $outline) {
112 $feed_title = db_escape_string($outline->get_attribute('text'));
113 $cat_title = db_escape_string($outline->get_attribute('title'));
114 $feed_url = db_escape_string($outline->get_attribute('xmlUrl'));
115
116 if ($cat_title) {
117
118 db_query($link, "BEGIN");
119
120 $result = db_query($link, "SELECT id FROM
121 ttrss_feed_categories WHERE title = '$cat_title' AND
122 owner_uid = '$owner_uid' LIMIT 1");
123
124 if (db_num_rows($result) == 0) {
125
126 print "Adding category <b>$cat_title</b>...<br>";
127
128 db_query($link, "INSERT INTO ttrss_feed_categories
129 (title,owner_uid) VALUES ('$cat_title', '$owner_uid')");
130 }
131
132 db_query($link, "COMMIT");
133 }
134
135 // print "$active_category : $feed_title : $xmlurl<br>";
136
137 if (!$feed_title || !$feed_url) continue;
138
139 db_query($link, "BEGIN");
140
141 $cat_id = null;
142
143 $parent_node = $outline->parent_node();
144
145 if ($parent_node && $parent_node->node_name() == "outline") {
146 $element_category = $parent_node->get_attribute('title');
147 } else {
148 $element_category = '';
149 }
150
151 if ($element_category) {
152
153 $result = db_query($link, "SELECT id FROM
154 ttrss_feed_categories WHERE title = '$element_category' AND
155 owner_uid = '$owner_uid' LIMIT 1");
156
157 if (db_num_rows($result) == 1) {
158 $cat_id = db_fetch_result($result, 0, "id");
159 }
160 }
161
162 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
163 (title = '$feed_title' OR feed_url = '$feed_url')
164 AND owner_uid = '$owner_uid'");
165
166 print "Feed <b>$feed_title</b> ($feed_url)... ";
167
168 if (db_num_rows($result) > 0) {
169 print " Already imported.<br>";
170 } else {
171
172 if ($cat_id) {
173 $add_query = "INSERT INTO ttrss_feeds
174 (title, feed_url, owner_uid, cat_id) VALUES
175 ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')";
176
177 } else {
178 $add_query = "INSERT INTO ttrss_feeds
179 (title, feed_url, owner_uid) VALUES
180 ('$feed_title', '$feed_url', '$owner_uid')";
181
182 }
183
184 db_query($link, $add_query);
185
186 print "<b>Done.</b><br>";
187 }
188
189 db_query($link, "COMMIT");
190 }
191
192 } else {
193 print "<div class=\"error\">Error: can't find body element.</div>";
194 }
195 } else {
196 print "<div class=\"error\">Error while parsing document.</div>";
197 }
198
199 } else {
200 print "<div class=\"error\">Error: please upload OPML file.</div>";
201 }
202
203 print "<p><a class=\"button\" href=\"prefs.php\">
204 Return to preferences</a>";
205
206 print "</div></body></html>";
207
208 }
209
210 // if ($link) db_close($link);
211
212 ?>