]> git.wh0rd.org - tt-rss.git/blob - opml.php
implement import/export of labels using OPML
[tt-rss.git] / opml.php
1 <?php
2 set_include_path(get_include_path() . PATH_SEPARATOR .
3 dirname(__FILE__) . "/include");
4
5 require_once "functions.php";
6 require_once "sessions.php";
7 require_once "sanity_check.php";
8 require_once "config.php";
9 require_once "db.php";
10 require_once "db-prefs.php";
11
12 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
13
14 if (!init_connection($link)) return;
15
16 function opml_import_domdoc($link, $owner_uid) {
17
18 if (is_file($_FILES['opml_file']['tmp_name'])) {
19 $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']);
20
21 $result = db_query($link, "SELECT id FROM
22 ttrss_feed_categories WHERE title = 'Imported feeds' AND
23 owner_uid = '$owner_uid' LIMIT 1");
24
25 if (db_num_rows($result) == 1) {
26 $default_cat_id = db_fetch_result($result, 0, "id");
27 } else {
28 $default_cat_id = 0;
29 }
30
31 if ($doc) {
32 $body = $doc->getElementsByTagName('body');
33
34 $xpath = new DOMXpath($doc);
35 $query = "/opml/body//outline";
36
37 $outlines = $xpath->query($query);
38
39 foreach ($outlines as $outline) {
40
41 $attributes = $outline->attributes;
42
43 $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
44 if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
45
46 $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
47 if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
48
49 $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue);
50 if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue);
51
52 $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue);
53
54 $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue);
55 $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue);
56
57 if ($cat_title && !$feed_url) {
58
59 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels') {
60
61 db_query($link, "BEGIN");
62
63 $result = db_query($link, "SELECT id FROM
64 ttrss_feed_categories WHERE title = '$cat_title' AND
65 owner_uid = '$owner_uid' LIMIT 1");
66
67 if (db_num_rows($result) == 0) {
68
69 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
70
71 db_query($link, "INSERT INTO ttrss_feed_categories
72 (title,owner_uid)
73 VALUES ('$cat_title', '$owner_uid')");
74 }
75
76 db_query($link, "COMMIT");
77 }
78 }
79
80 // print "$active_category : $feed_title : $feed_url<br>";
81
82 if ($pref_name) {
83 $parent_node = $outline->parentNode;
84
85 if ($parent_node && $parent_node->nodeName == "outline") {
86 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
87 if ($cat_check == "tt-rss-prefs") {
88 $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue);
89
90 printf("<li>".
91 __("Setting preference key %s to %s")."</li>",
92 $pref_name, $pref_value);
93
94 set_pref($link, $pref_name, $pref_value);
95
96 }
97 }
98 }
99
100 if ($label_name) {
101 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue);
102 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue);
103
104 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
105
106 printf("<li>".__("Adding label %s")."</li>", $label_name);
107
108 label_create($link, $label_name, $fg_color, $bg_color);
109 }
110 }
111
112 if (!$feed_title || !$feed_url) continue;
113
114 db_query($link, "BEGIN");
115
116 $cat_id = null;
117
118 $parent_node = $outline->parentNode;
119
120 if ($parent_node && $parent_node->nodeName == "outline") {
121 $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue;
122 if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue;
123
124 } else {
125 $element_category = '';
126 }
127
128 if ($element_category) {
129
130 $element_category = db_escape_string($element_category);
131
132 $result = db_query($link, "SELECT id FROM
133 ttrss_feed_categories WHERE title = '$element_category' AND
134 owner_uid = '$owner_uid' LIMIT 1");
135
136 if (db_num_rows($result) == 1) {
137 $cat_id = db_fetch_result($result, 0, "id");
138 }
139 }
140
141 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
142 feed_url = '$feed_url'
143 AND owner_uid = '$owner_uid'");
144
145 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
146 (<a target='_blank' href=\"$feed_url\">rss</a>)&nbsp;";
147
148 if (db_num_rows($result) > 0) {
149 print __('is already imported.');
150 } else {
151
152 if ($cat_id) {
153 $add_query = "INSERT INTO ttrss_feeds
154 (title, feed_url, owner_uid, cat_id, site_url) VALUES
155 ('$feed_title', '$feed_url', '$owner_uid',
156 '$cat_id', '$site_url')";
157
158 } else {
159 $add_query = "INSERT INTO ttrss_feeds
160 (title, feed_url, owner_uid, cat_id, site_url) VALUES
161 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
162 '$site_url')";
163
164 }
165
166 //print $add_query;
167 db_query($link, $add_query);
168
169 print __('OK');
170 }
171
172 print "</li>";
173
174 db_query($link, "COMMIT");
175 }
176
177 } else {
178 print_error(__('Error while parsing document.'));
179 }
180
181 } else {
182 print_error(__('Error: please upload OPML file.'));
183 }
184
185
186 }
187
188 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
189 if (!$_REQUEST["debug"]) {
190 header("Content-type: application/xml+opml");
191 header("Content-Disposition: attachment; filename=" . $name );
192 } else {
193 header("Content-type: text/xml");
194 }
195
196 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
197
198 print "<opml version=\"1.0\">";
199 print "<head>
200 <dateCreated>" . date("r", time()) . "</dateCreated>
201 <title>Tiny Tiny RSS Feed Export</title>
202 </head>";
203 print "<body>";
204
205 $cat_mode = false;
206
207 $select = "SELECT * ";
208 $where = "WHERE owner_uid = '$owner_uid'";
209 $orderby = "ORDER BY title";
210 if ($hide_private_feeds){
211 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
212 auth_login = '' AND auth_pass = ''";
213 }
214
215
216
217 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
218 $cat_mode = true;
219 $select = "SELECT
220 title, feed_url, site_url,
221 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
222 $orderby = "ORDER BY cat_title, title";
223
224 }
225 else{
226 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
227 print "<!-- feeding cats is not enabled -->";
228 print "<!-- $cat_feed -->";
229
230 }
231
232
233 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
234
235 $old_cat_title = "";
236
237 while ($line = db_fetch_assoc($result)) {
238 $title = htmlspecialchars($line["title"]);
239 $url = htmlspecialchars($line["feed_url"]);
240 $site_url = htmlspecialchars($line["site_url"]);
241
242 if ($cat_mode) {
243 $cat_title = htmlspecialchars($line["cat_title"]);
244
245 if ($old_cat_title != $cat_title) {
246 if ($old_cat_title) {
247 print "</outline>\n";
248 }
249
250 if ($cat_title) {
251 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
252 }
253
254 $old_cat_title = $cat_title;
255 }
256 }
257
258 if ($site_url) {
259 $html_url_qpart = "htmlUrl=\"$site_url\"";
260 } else {
261 $html_url_qpart = "";
262 }
263
264 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
265 }
266
267 if ($cat_mode && $old_cat_title) {
268 print "</outline>\n";
269 }
270
271 # export tt-rss settings
272
273 if ($include_settings) {
274 print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
275
276 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
277 profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
278
279 while ($line = db_fetch_assoc($result)) {
280
281 $name = $line["pref_name"];
282 $value = htmlspecialchars($line["value"]);
283
284 print "<outline pref-name=\"$name\" value=\"$value\">";
285
286 print "</outline>";
287
288 }
289
290 print "</outline>";
291
292 print "<outline title=\"tt-rss-labels\">";
293
294 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
295 owner_uid = " . $_SESSION['uid']);
296
297 while ($line = db_fetch_assoc($result)) {
298 $name = htmlspecialchars($line['caption']);
299 $fg_color = htmlspecialchars($line['fg_color']);
300 $bg_color = htmlspecialchars($line['bg_color']);
301
302 print "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
303
304 }
305
306 print "</outline>";
307 }
308
309 print "</body></opml>";
310 }
311
312 // FIXME there are some brackets issues here
313
314 $op = $_REQUEST["op"];
315 if (!$op) $op = "Export";
316
317 $output_name = $_REQUEST["filename"];
318 if (!$output_name) $output_name = "TinyTinyRSS.opml";
319
320 $show_settings = $_REQUEST["settings"];
321
322 if ($op == "Export") {
323
324 login_sequence($link);
325 $owner_uid = $_SESSION["uid"];
326 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
327 }
328
329 if ($op == "publish"){
330 $key = db_escape_string($_REQUEST["key"]);
331
332 $result = db_query($link, "SELECT owner_uid
333 FROM ttrss_access_keys WHERE
334 access_key = '$key' AND feed_id = 'OPML:Publish'");
335
336 if (db_num_rows($result) == 1) {
337 $owner_uid = db_fetch_result($result, 0, "owner_uid");
338 return opml_export($link, "", $owner_uid, true, false);
339 } else {
340 print "<error>User not found</error>";
341 }
342 }
343
344 if ($op == "Import") {
345
346 login_sequence($link);
347 $owner_uid = $_SESSION["uid"];
348
349 header('Content-Type: text/html; charset=utf-8');
350
351 print "<html>
352 <head>
353 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
354 <title>".__("OPML Utility")."</title>
355 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
356 </head>
357 <body>
358 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
359 <h1>".__('OPML Utility')."</h1>";
360
361 db_query($link, "BEGIN");
362
363 /* create Imported feeds category just in case */
364
365 $result = db_query($link, "SELECT id FROM
366 ttrss_feed_categories WHERE title = 'Imported feeds' AND
367 owner_uid = '$owner_uid' LIMIT 1");
368
369 if (db_num_rows($result) == 0) {
370 db_query($link, "INSERT INTO ttrss_feed_categories
371 (title,owner_uid)
372 VALUES ('Imported feeds', '$owner_uid')");
373 }
374
375 db_query($link, "COMMIT");
376
377 print "<p>".__("Importing OPML...")."</p>";
378 opml_import_domdoc($link, $owner_uid);
379
380 print "<br><form method=\"GET\" action=\"prefs.php\">
381 <input type=\"submit\" value=\"".__("Return to preferences")."\">
382 </form>";
383
384 print "</body></html>";
385
386 }
387
388 // if ($link) db_close($link);
389
390 ?>