]> git.wh0rd.org - tt-rss.git/blob - opml.php
fix owner_uid checking in OPML export
[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 ORDER BY cat_title,title");
41 } else {
42 $result = db_query($link, "SELECT * FROM ttrss_feeds
43 ORDER BY title WHERE owner_uid = '$owner_uid'");
44 }
45
46 $old_cat_title = "";
47
48 while ($line = db_fetch_assoc($result)) {
49 $title = htmlspecialchars($line["title"]);
50 $url = htmlspecialchars($line["feed_url"]);
51
52 if ($cat_mode) {
53 $cat_title = htmlspecialchars($line["cat_title"]);
54
55 if ($old_cat_title != $cat_title) {
56 if ($old_cat_title) {
57 print "</outline>\n";
58 }
59
60 if ($cat_title) {
61 print "<outline title=\"$cat_title\">\n";
62 }
63
64 $old_cat_title = $cat_title;
65 }
66 }
67
68 print "<outline text=\"$title\" xmlUrl=\"$url\"/>\n";
69 }
70
71 if ($cat_mode && $old_cat_title) {
72 print "</outline>\n";
73 }
74
75 print "</body></opml>";
76 }
77
78 if ($op == "Import") {
79
80 print "<html>
81 <head>
82 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
83 </head>
84 <body><h1>Importing OPML...</h1>
85 <div>";
86
87 if (WEB_DEMO_MODE) {
88 print "OPML import is disabled in demo-mode.";
89 print "<p><a class=\"button\" href=\"prefs.php\">
90 Return to preferences</a></div></body></html>";
91
92 return;
93 }
94
95 if (is_file($_FILES['opml_file']['tmp_name'])) {
96 $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
97
98 if ($dom) {
99 $root = $dom->document_element();
100
101 $body = $root->get_elements_by_tagname('body');
102
103 if ($body[0]) {
104 $body = $body[0];
105
106 $outlines = $body->get_elements_by_tagname('outline');
107
108 $active_category = '';
109
110 foreach ($outlines as $outline) {
111 $feed_title = $outline->get_attribute('text');
112 $cat_title = $outline->get_attribute('title');
113 $feed_url = $outline->get_attribute('xmlUrl');
114
115 if ($cat_title) {
116 $active_category = $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 if ($active_category) {
144
145 $result = db_query($link, "SELECT id FROM
146 ttrss_feed_categories WHERE title = '$active_category' AND
147 owner_uid = '$owner_uid' LIMIT 1");
148
149 if (db_num_rows($result) == 1) {
150 $cat_id = db_fetch_result($result, 0, "id");
151 }
152 }
153
154 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
155 (title = '$feed_title' OR feed_url = '$feed_url')
156 AND owner_uid = '$owner_uid'");
157
158 print "Feed <b>$feed_title</b> ($feed_url)... ";
159
160 if (db_num_rows($result) > 0) {
161 print " Already imported.<br>";
162 } else {
163
164 if ($cat_id) {
165 $add_query = "INSERT INTO ttrss_feeds
166 (title, feed_url, owner_uid, cat_id) VALUES
167 ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')";
168
169 } else {
170 $add_query = "INSERT INTO ttrss_feeds
171 (title, feed_url, owner_uid) VALUES
172 ('$feed_title', '$feed_url', '$owner_uid')";
173
174 }
175
176 db_query($link, $add_query);
177
178 print "<b>Done.</b><br>";
179 }
180
181 db_query($link, "COMMIT");
182 }
183
184 } else {
185 print "Error: can't find body element.";
186 }
187 } else {
188 print "Error while parsing document.";
189 }
190
191 } else {
192 print "Error: please upload OPML file.";
193 }
194
195 print "<p><a class=\"button\" href=\"prefs.php\">
196 Return to preferences</a>";
197
198 print "</div></body></html>";
199
200 }
201
202 // if ($link) db_close($link);
203
204 ?>