]> git.wh0rd.org - tt-rss.git/blob - opml.php
more opml export tweaks, tiny category detection tweak on import
[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>
29 <dateCreated>" . date("r", time()) . "</dateCreated>
30 <title>Tiny Tiny RSS Feed Export</title>
31 </head>";
32 print "<body>";
33
34 $cat_mode = false;
35
36 if (get_pref($link, 'ENABLE_FEED_CATS')) {
37 $cat_mode = true;
38 $result = db_query($link, "SELECT
39 title,feed_url,site_url,
40 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title
41 FROM ttrss_feeds
42 WHERE
43 owner_uid = '$owner_uid'
44 ORDER BY cat_title,title");
45 } else {
46 $result = db_query($link, "SELECT * FROM ttrss_feeds
47 ORDER BY title WHERE owner_uid = '$owner_uid'");
48 }
49
50 $old_cat_title = "";
51
52 while ($line = db_fetch_assoc($result)) {
53 $title = htmlspecialchars($line["title"]);
54 $url = htmlspecialchars($line["feed_url"]);
55 $site_url = htmlspecialchars($line["site_url"]);
56
57 if ($cat_mode) {
58 $cat_title = htmlspecialchars($line["cat_title"]);
59
60 if ($old_cat_title != $cat_title) {
61 if ($old_cat_title) {
62 print "</outline>\n";
63 }
64
65 if ($cat_title) {
66 print "<outline title=\"$cat_title\">\n";
67 }
68
69 $old_cat_title = $cat_title;
70 }
71 }
72
73 if ($site_url) {
74 $html_url_qpart = "htmlUrl=\"$site_url\"";
75 } else {
76 $html_url_qpart = "";
77 }
78
79 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
80 }
81
82 if ($cat_mode && $old_cat_title) {
83 print "</outline>\n";
84 }
85
86 print "</body></opml>";
87 }
88
89 if ($op == "Import") {
90
91 print "<html>
92 <head>
93 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
94 </head>
95 <body>
96 <h1>Importing OPML...</h1>
97 <div class=\"opmlBody\">";
98
99 if (WEB_DEMO_MODE) {
100 print "OPML import is disabled in demo-mode.";
101 print "<p><a class=\"button\" href=\"prefs.php\">
102 Return to preferences</a></div></body></html>";
103
104 return;
105 }
106
107 if (is_file($_FILES['opml_file']['tmp_name'])) {
108 $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
109
110 if ($dom) {
111 $root = $dom->document_element();
112
113 $body = $root->get_elements_by_tagname('body');
114
115 if ($body[0]) {
116 $body = $body[0];
117
118 $outlines = $body->get_elements_by_tagname('outline');
119
120 foreach ($outlines as $outline) {
121 $feed_title = db_escape_string($outline->get_attribute('text'));
122 $cat_title = db_escape_string($outline->get_attribute('title'));
123 $feed_url = db_escape_string($outline->get_attribute('xmlUrl'));
124
125 if ($cat_title && !$feed_url) {
126
127 db_query($link, "BEGIN");
128
129 $result = db_query($link, "SELECT id FROM
130 ttrss_feed_categories WHERE title = '$cat_title' AND
131 owner_uid = '$owner_uid' LIMIT 1");
132
133 if (db_num_rows($result) == 0) {
134
135 print "Adding category <b>$cat_title</b>...<br>";
136
137 db_query($link, "INSERT INTO ttrss_feed_categories
138 (title,owner_uid) VALUES ('$cat_title', '$owner_uid')");
139 }
140
141 db_query($link, "COMMIT");
142 }
143
144 // print "$active_category : $feed_title : $xmlurl<br>";
145
146 if (!$feed_title || !$feed_url) continue;
147
148 db_query($link, "BEGIN");
149
150 $cat_id = null;
151
152 $parent_node = $outline->parent_node();
153
154 if ($parent_node && $parent_node->node_name() == "outline") {
155 $element_category = $parent_node->get_attribute('title');
156 } else {
157 $element_category = '';
158 }
159
160 if ($element_category) {
161
162 $result = db_query($link, "SELECT id FROM
163 ttrss_feed_categories WHERE title = '$element_category' AND
164 owner_uid = '$owner_uid' LIMIT 1");
165
166 if (db_num_rows($result) == 1) {
167 $cat_id = db_fetch_result($result, 0, "id");
168 }
169 }
170
171 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
172 (title = '$feed_title' OR feed_url = '$feed_url')
173 AND owner_uid = '$owner_uid'");
174
175 print "Feed <b>$feed_title</b> ($feed_url)... ";
176
177 if (db_num_rows($result) > 0) {
178 print " Already imported.<br>";
179 } else {
180
181 if ($cat_id) {
182 $add_query = "INSERT INTO ttrss_feeds
183 (title, feed_url, owner_uid, cat_id) VALUES
184 ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')";
185
186 } else {
187 $add_query = "INSERT INTO ttrss_feeds
188 (title, feed_url, owner_uid) VALUES
189 ('$feed_title', '$feed_url', '$owner_uid')";
190
191 }
192
193 db_query($link, $add_query);
194
195 print "<b>Done.</b><br>";
196 }
197
198 db_query($link, "COMMIT");
199 }
200
201 } else {
202 print "<div class=\"error\">Error: can't find body element.</div>";
203 }
204 } else {
205 print "<div class=\"error\">Error while parsing document.</div>";
206 }
207
208 } else {
209 print "<div class=\"error\">Error: please upload OPML file.</div>";
210 }
211
212 print "<p><a class=\"button\" href=\"prefs.php\">
213 Return to preferences</a>";
214
215 print "</div></body></html>";
216
217 }
218
219 // if ($link) db_close($link);
220
221 ?>