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