]> git.wh0rd.org - tt-rss.git/blob - opml.php
fix owner_uid checking in OPML export (path2)
[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><h1>Importing OPML...</h1>
87 <div>";
88
89 if (WEB_DEMO_MODE) {
90 print "OPML import is disabled in demo-mode.";
91 print "<p><a class=\"button\" href=\"prefs.php\">
92 Return to preferences</a></div></body></html>";
93
94 return;
95 }
96
97 if (is_file($_FILES['opml_file']['tmp_name'])) {
98 $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
99
100 if ($dom) {
101 $root = $dom->document_element();
102
103 $body = $root->get_elements_by_tagname('body');
104
105 if ($body[0]) {
106 $body = $body[0];
107
108 $outlines = $body->get_elements_by_tagname('outline');
109
110 $active_category = '';
111
112 foreach ($outlines as $outline) {
113 $feed_title = $outline->get_attribute('text');
114 $cat_title = $outline->get_attribute('title');
115 $feed_url = $outline->get_attribute('xmlUrl');
116
117 if ($cat_title) {
118 $active_category = $cat_title;
119
120 db_query($link, "BEGIN");
121
122 $result = db_query($link, "SELECT id FROM
123 ttrss_feed_categories WHERE title = '$cat_title' AND
124 owner_uid = '$owner_uid' LIMIT 1");
125
126 if (db_num_rows($result) == 0) {
127
128 print "Adding category <b>$cat_title</b>...<br>";
129
130 db_query($link, "INSERT INTO ttrss_feed_categories
131 (title,owner_uid) VALUES ('$cat_title', '$owner_uid')");
132 }
133
134 db_query($link, "COMMIT");
135 }
136
137 // print "$active_category : $feed_title : $xmlurl<br>";
138
139 if (!$feed_title || !$feed_url) continue;
140
141 db_query($link, "BEGIN");
142
143 $cat_id = null;
144
145 if ($active_category) {
146
147 $result = db_query($link, "SELECT id FROM
148 ttrss_feed_categories WHERE title = '$active_category' AND
149 owner_uid = '$owner_uid' LIMIT 1");
150
151 if (db_num_rows($result) == 1) {
152 $cat_id = db_fetch_result($result, 0, "id");
153 }
154 }
155
156 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
157 (title = '$feed_title' OR feed_url = '$feed_url')
158 AND owner_uid = '$owner_uid'");
159
160 print "Feed <b>$feed_title</b> ($feed_url)... ";
161
162 if (db_num_rows($result) > 0) {
163 print " Already imported.<br>";
164 } else {
165
166 if ($cat_id) {
167 $add_query = "INSERT INTO ttrss_feeds
168 (title, feed_url, owner_uid, cat_id) VALUES
169 ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')";
170
171 } else {
172 $add_query = "INSERT INTO ttrss_feeds
173 (title, feed_url, owner_uid) VALUES
174 ('$feed_title', '$feed_url', '$owner_uid')";
175
176 }
177
178 db_query($link, $add_query);
179
180 print "<b>Done.</b><br>";
181 }
182
183 db_query($link, "COMMIT");
184 }
185
186 } else {
187 print "Error: can't find body element.";
188 }
189 } else {
190 print "Error while parsing document.";
191 }
192
193 } else {
194 print "Error: please upload OPML file.";
195 }
196
197 print "<p><a class=\"button\" href=\"prefs.php\">
198 Return to preferences</a>";
199
200 print "</div></body></html>";
201
202 }
203
204 // if ($link) db_close($link);
205
206 ?>