]> git.wh0rd.org - tt-rss.git/blame - opml.php
opml: support proper export of nested categories
[tt-rss.git] / opml.php
CommitLineData
1d3a17c7 1<?php
bbefea90 2 set_include_path(get_include_path() . PATH_SEPARATOR .
f03a795d 3 dirname(__FILE__) . "/include");
107d0cf3 4
fb074239 5 require_once "functions.php";
1559e374 6 require_once "sessions.php";
66581886 7 require_once "sanity_check.php";
9a4506c8 8 require_once "config.php";
8158c57a 9 require_once "db.php";
a0111294 10 require_once "db-prefs.php";
9a4506c8 11
0a13e84e 12 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
9f311df6 13
5f0a3741 14 if (!init_connection($link)) return;
4e9f5c24 15
80edb8b4
AD
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
c20b5168
AD
31 // Keep imported categories in order, after any pre-existing ones.
32 $new_cat_order_id = 0;
33 // Get the highest category order_id in use.
34 $result = db_query($link, "SELECT order_id FROM
35 ttrss_feed_categories WHERE owner_uid = '$owner_uid'
36 ORDER BY order_id DESC LIMIT 1");
37 if (db_num_rows($result) == 1) {
38 $new_cat_order_id = db_fetch_result($result, 0, "order_id");
39 }
40
80edb8b4
AD
41 if ($doc) {
42 $body = $doc->getElementsByTagName('body');
43
44 $xpath = new DOMXpath($doc);
45 $query = "/opml/body//outline";
46
47 $outlines = $xpath->query($query);
48
49 foreach ($outlines as $outline) {
50
bbefea90 51 $attributes = $outline->attributes;
80edb8b4 52
bbefea90
AD
53 $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
54 if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
80edb8b4 55
bbefea90
AD
56 $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
57 if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
80edb8b4 58
bbefea90
AD
59 $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue);
60 if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue);
80edb8b4 61
bbefea90 62 $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue);
80edb8b4 63
bbefea90
AD
64 $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue);
65 $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue);
f59928d7 66 $filter_name = db_escape_string($attributes->getNamedItem('filter-name')->nodeValue);
80edb8b4
AD
67
68 if ($cat_title && !$feed_url) {
69
f59928d7 70 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels' && $cat_title != 'tt-rss-filters') {
80edb8b4
AD
71
72 db_query($link, "BEGIN");
73
74 $result = db_query($link, "SELECT id FROM
75 ttrss_feed_categories WHERE title = '$cat_title' AND
76 owner_uid = '$owner_uid' LIMIT 1");
77
78 if (db_num_rows($result) == 0) {
c20b5168 79 $cat_order_id = ++$new_cat_order_id;
80edb8b4
AD
80
81 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
82
83 db_query($link, "INSERT INTO ttrss_feed_categories
c20b5168
AD
84 (title,owner_uid,order_id)
85 VALUES ('$cat_title', '$owner_uid', '$cat_order_id')");
80edb8b4
AD
86 }
87
88 db_query($link, "COMMIT");
89 }
90 }
91
92 // print "$active_category : $feed_title : $feed_url<br>";
93
94 if ($pref_name) {
95 $parent_node = $outline->parentNode;
96
97 if ($parent_node && $parent_node->nodeName == "outline") {
98 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
99 if ($cat_check == "tt-rss-prefs") {
100 $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue);
101
102 printf("<li>".
103 __("Setting preference key %s to %s")."</li>",
104 $pref_name, $pref_value);
105
106 set_pref($link, $pref_name, $pref_value);
107
108 }
109 }
110 }
111
bbefea90 112 if ($label_name) {
f59928d7
AD
113 $parent_node = $outline->parentNode;
114
115 if ($parent_node && $parent_node->nodeName == "outline") {
116 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
117 if ($cat_check == "tt-rss-labels") {
bbefea90 118
f59928d7
AD
119 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue);
120 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue);
bbefea90 121
f59928d7 122 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
34aa9e20 123 printf("<li>".__("Adding label %s")."</li>", htmlspecialchars($label_name));
f59928d7
AD
124 label_create($link, $label_name, $fg_color, $bg_color);
125 } else {
34aa9e20
AD
126 printf("<li>".__("Duplicate label: %s")."</li>",
127 htmlspecialchars($label_name));
f59928d7
AD
128 }
129 }
130 }
131 }
bbefea90 132
f59928d7
AD
133 if ($filter_name) {
134 $parent_node = $outline->parentNode;
135
136 if ($parent_node && $parent_node->nodeName == "outline") {
137 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
138 if ($cat_check == "tt-rss-filters") {
139 $filter = json_decode($outline->nodeValue, true);
140
141 if ($filter) {
34aa9e20
AD
142 $reg_exp = db_escape_string($filter['reg_exp']);
143 $filter_type = (int)$filter['filter_type'];
144 $action_id = (int)$filter['action_id'];
145
146 $result = db_query($link, "SELECT id FROM ttrss_filters WHERE
147 reg_exp = '$reg_exp' AND
148 filter_type = '$filter_type' AND
149 action_id = '$action_id' AND
150 owner_uid = " .$_SESSION['uid']);
151
152 if (db_num_rows($result) == 0) {
153 $enabled = bool_to_sql_bool($filter['enabled']);
154 $action_param = db_escape_string($filter['action_param']);
155 $inverse = bool_to_sql_bool($filter['inverse']);
156 $filter_param = db_escape_string($filter['filter_param']);
157 $cat_filter = bool_to_sql_bool($filter['cat_filter']);
158
159 $feed_url = db_escape_string($filter['feed_url']);
160 $cat_title = db_escape_string($filter['cat_title']);
161
162 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
163 feed_url = '$feed_url' AND owner_uid = ".$_SESSION['uid']);
164
165 if (db_num_rows($result) != 0) {
166 $feed_id = db_fetch_result($result, 0, "id");
167 } else {
168 $feed_id = "NULL";
169 }
170
171 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
172 title = '$cat_title' AND owner_uid = ".$_SESSION['uid']);
173
174 if (db_num_rows($result) != 0) {
175 $cat_id = db_fetch_result($result, 0, "id");
176 } else {
177 $cat_id = "NULL";
178 }
179
180 printf("<li>".__("Adding filter %s")."</li>", htmlspecialchars($reg_exp));
181
182 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
183 enabled, inverse, action_param, filter_param,
184 cat_filter, feed_id,
185 cat_id, reg_exp,
186 owner_uid)
187 VALUES ($filter_type, $action_id,
188 $enabled, $inverse, '$action_param', '$filter_param',
189 $cat_filter, $feed_id,
190 $cat_id, '$reg_exp', ".
191 $_SESSION['uid'].")";
192
193 db_query($link, $query);
194
195 } else {
196 printf("<li>".__("Duplicate filter %s")."</li>", htmlspecialchars($reg_exp));
197
198 }
f59928d7
AD
199 }
200 }
bbefea90
AD
201 }
202 }
203
80edb8b4
AD
204 if (!$feed_title || !$feed_url) continue;
205
206 db_query($link, "BEGIN");
207
208 $cat_id = null;
209
210 $parent_node = $outline->parentNode;
211
212 if ($parent_node && $parent_node->nodeName == "outline") {
213 $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue;
214 if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue;
215
216 } else {
217 $element_category = '';
218 }
219
220 if ($element_category) {
221
222 $element_category = db_escape_string($element_category);
223
224 $result = db_query($link, "SELECT id FROM
225 ttrss_feed_categories WHERE title = '$element_category' AND
226 owner_uid = '$owner_uid' LIMIT 1");
227
228 if (db_num_rows($result) == 1) {
229 $cat_id = db_fetch_result($result, 0, "id");
230 }
231 }
232
233 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
234 feed_url = '$feed_url'
235 AND owner_uid = '$owner_uid'");
236
237 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
238 (<a target='_blank' href=\"$feed_url\">rss</a>)&nbsp;";
239
240 if (db_num_rows($result) > 0) {
241 print __('is already imported.');
242 } else {
c20b5168
AD
243 // Get max order_id already in use. Increment.
244 $new_feed_order_id = 0; // these start at zero
245 $cat_id_qpart = $cat_id ? "cat_id = '$cat_id'" : "cat_id = '$default_cat_id'";
246 $result = db_query($link, "SELECT order_id FROM
247 ttrss_feeds WHERE owner_uid = '$owner_uid' AND $cat_id_qpart
248 ORDER BY order_id DESC LIMIT 1");
249 if (db_num_rows($result) == 1) {
250 $new_feed_order_id = db_fetch_result($result, 0, "order_id");
251 $new_feed_order_id++;
252 }
80edb8b4
AD
253
254 if ($cat_id) {
255 $add_query = "INSERT INTO ttrss_feeds
c20b5168 256 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
80edb8b4 257 ('$feed_title', '$feed_url', '$owner_uid',
c20b5168 258 '$cat_id', '$site_url', '$new_feed_order_id')";
80edb8b4
AD
259
260 } else {
261 $add_query = "INSERT INTO ttrss_feeds
c20b5168 262 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
80edb8b4 263 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
c20b5168 264 '$site_url', '$new_feed_order_id')";
80edb8b4
AD
265
266 }
267
268 //print $add_query;
269 db_query($link, $add_query);
270
271 print __('OK');
272 }
273
274 print "</li>";
275
276 db_query($link, "COMMIT");
277 }
278
279 } else {
280 print_error(__('Error while parsing document.'));
281 }
282
283 } else {
284 print_error(__('Error: please upload OPML file.'));
285 }
286
287
288 }
289
3a9d5c6e 290 function opml_export_category($link, $owner_uid, $cat_id, $hide_private_feeds=false) {
da49ccf5 291
3a9d5c6e
AD
292 if ($cat_id)
293 $cat_qpart = "parent_cat = '$cat_id'";
294 else
295 $cat_qpart = "parent_cat IS NULL";
fee909d5 296
3a9d5c6e
AD
297 if ($hide_private_feeds)
298 $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
299 else
300 $hide_qpart = "true";
fee909d5 301
3a9d5c6e 302 $out = "";
86e26f1a 303
3a9d5c6e
AD
304 $query = "SELECT
305 ttrss_feeds.title, feed_url, site_url, ttrss_feeds.order_id,
306 ttrss_feed_categories.id AS cat_id,
307 ttrss_feed_categories.title AS cat_title,
308 ttrss_feed_categories.order_id AS cat_order_id
309 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories ON (ttrss_feed_categories.id = ttrss_feeds.cat_id)
310 WHERE ttrss_feeds.owner_uid = '$owner_uid' AND $hide_qpart AND $cat_qpart
311 ORDER BY cat_order_id, cat_title, ttrss_feeds.order_id, title";
fee909d5 312
3a9d5c6e 313 #$out .= "<!-- $query -->";
da49ccf5 314
3a9d5c6e 315 $result = db_query($link, $query);
86e26f1a 316
da49ccf5 317 $old_cat_title = "";
9a4506c8 318
8158c57a 319 while ($line = db_fetch_assoc($result)) {
6e0584e9
AD
320 $title = htmlspecialchars($line["title"]);
321 $url = htmlspecialchars($line["feed_url"]);
fa7b8749 322 $site_url = htmlspecialchars($line["site_url"]);
9a4506c8 323
3a9d5c6e 324 $cat_title = htmlspecialchars($line["cat_title"]);
da49ccf5 325
3a9d5c6e
AD
326 if ($old_cat_title != $cat_title) {
327 if ($old_cat_title) {
328 $out .= "</outline>\n";
329 }
da49ccf5 330
3a9d5c6e
AD
331 if ($cat_title) {
332 $out .= "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
da49ccf5 333 }
3a9d5c6e
AD
334 $old_cat_title = $cat_title;
335
336 $cat_id = (int) $line["cat_id"];
337
338 if ($cat_id > 0)
339 $out .= opml_export_category($link, $owner_uid, $cat_id, $hide_private_feeds);
da49ccf5
AD
340 }
341
fa7b8749
AD
342 if ($site_url) {
343 $html_url_qpart = "htmlUrl=\"$site_url\"";
344 } else {
345 $html_url_qpart = "";
346 }
347
95562576 348 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
3a9d5c6e 349
9a4506c8
AD
350 }
351
3a9d5c6e 352 if ($old_cat_title) {
95562576 353 $out .= "</outline>\n";
da49ccf5
AD
354 }
355
3a9d5c6e
AD
356 return $out;
357 }
358
359 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
360 if (!isset($_REQUEST["debug"])) {
361 header("Content-type: application/xml+opml");
362 header("Content-Disposition: attachment; filename=" . $name );
363 } else {
364 header("Content-type: text/xml");
365 }
366
367 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
368
369 $out .= "<opml version=\"1.0\">";
370 $out .= "<head>
371 <dateCreated>" . date("r", time()) . "</dateCreated>
372 <title>Tiny Tiny RSS Feed Export</title>
373 </head>";
374 $out .= "<body>";
375
376 $out .= opml_export_category($link, $owner_uid, false, $hide_private_feeds);
377
e9558345
AD
378 # export tt-rss settings
379
442f326b 380 if ($include_settings) {
95562576 381 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
e9558345 382
442f326b 383 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
95562576 384 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
e9558345 385
442f326b 386 while ($line = db_fetch_assoc($result)) {
e9558345 387
442f326b
AD
388 $name = $line["pref_name"];
389 $value = htmlspecialchars($line["value"]);
0a13e84e 390
95562576 391 $out .= "<outline pref-name=\"$name\" value=\"$value\">";
e9558345 392
95562576 393 $out .= "</outline>";
e9558345 394
0a13e84e 395 }
e9558345 396
95562576 397 $out .= "</outline>";
bbefea90 398
95562576 399 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
bbefea90
AD
400
401 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
402 owner_uid = " . $_SESSION['uid']);
403
404 while ($line = db_fetch_assoc($result)) {
405 $name = htmlspecialchars($line['caption']);
406 $fg_color = htmlspecialchars($line['fg_color']);
407 $bg_color = htmlspecialchars($line['bg_color']);
408
95562576 409 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
bbefea90
AD
410
411 }
412
95562576 413 $out .= "</outline>";
7d926cfc 414
95562576 415 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
7d926cfc
AD
416
417 $result = db_query($link, "SELECT filter_type,
418 reg_exp,
419 action_id,
420 enabled,
421 action_param,
422 inverse,
423 filter_param,
424 cat_filter,
cfb665c0 425 ttrss_feeds.feed_url AS feed_url,
7d926cfc
AD
426 ttrss_feed_categories.title AS cat_title
427 FROM ttrss_filters
428 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
429 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
430 WHERE
431 ttrss_filters.owner_uid = " . $_SESSION['uid']);
432
433 while ($line = db_fetch_assoc($result)) {
434 $name = htmlspecialchars($line['reg_exp']);
435
436 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
437 $line[$b] = sql_bool_to_bool($line[$b]);
438 }
439
440 $filter = json_encode($line);
441
95562576 442 $out .= "<outline filter-name=\"$name\">$filter</outline>";
7d926cfc
AD
443
444 }
445
446
95562576 447 $out .= "</outline>";
442f326b 448 }
e9558345 449
95562576
AD
450 $out .= "</body></opml>";
451
452 // Format output.
453 $doc = new DOMDocument();
454 $doc->formatOutput = true;
455 $doc->preserveWhiteSpace = false;
456 $doc->loadXML($out);
457 $res = $doc->saveXML();
458
459 // saveXML uses a two-space indent. Change to tabs.
460 $res = preg_replace_callback('/^(?: )+/mu',
461 create_function(
462 '$matches',
463 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
464 $res);
465
466 print $res;
9a4506c8
AD
467 }
468
30f782a2
AD
469 // FIXME there are some brackets issues here
470
471 $op = $_REQUEST["op"];
95562576 472 if (!$op) $op = "Export";
08ae2a5b 473
95562576 474 $output_name = $_REQUEST["filename"];
08ae2a5b 475 if (!$output_name) $output_name = "TinyTinyRSS.opml";
0a13e84e 476
95562576 477 $show_settings = $_REQUEST["settings"];
0a13e84e 478
30f782a2 479 if ($op == "Export") {
0a13e84e 480
a4234239
MK
481 login_sequence($link);
482 $owner_uid = $_SESSION["uid"];
08ae2a5b 483 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
30f782a2 484 }
0a13e84e 485
442f326b 486 if ($op == "publish"){
86e26f1a
MK
487 $key = db_escape_string($_REQUEST["key"]);
488
2e7f046f
AD
489 $result = db_query($link, "SELECT owner_uid
490 FROM ttrss_access_keys WHERE
491 access_key = '$key' AND feed_id = 'OPML:Publish'");
86e26f1a
MK
492
493 if (db_num_rows($result) == 1) {
2e7f046f 494 $owner_uid = db_fetch_result($result, 0, "owner_uid");
08ae2a5b 495 return opml_export($link, "", $owner_uid, true, false);
86e26f1a
MK
496 } else {
497 print "<error>User not found</error>";
498 }
499 }
30f782a2 500
e98a3f65 501 if ($op == "Import") {
8158c57a 502
a4234239
MK
503 login_sequence($link);
504 $owner_uid = $_SESSION["uid"];
505
e9558345
AD
506 header('Content-Type: text/html; charset=utf-8');
507
e98a3f65
AD
508 print "<html>
509 <head>
ef59e6e8 510 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
1025ad87 511 <title>".__("OPML Utility")."</title>
e9558345 512 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
e98a3f65 513 </head>
04f6df27 514 <body>
0ae2bb2a 515 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
d1db26aa 516 <h1>".__('OPML Utility')."</h1>";
9f311df6 517
c03cf250
AD
518 db_query($link, "BEGIN");
519
520 /* create Imported feeds category just in case */
521
522 $result = db_query($link, "SELECT id FROM
523 ttrss_feed_categories WHERE title = 'Imported feeds' AND
524 owner_uid = '$owner_uid' LIMIT 1");
525
526 if (db_num_rows($result) == 0) {
527 db_query($link, "INSERT INTO ttrss_feed_categories
0a13e84e 528 (title,owner_uid)
c03cf250
AD
529 VALUES ('Imported feeds', '$owner_uid')");
530 }
531
532 db_query($link, "COMMIT");
533
0a13e84e 534 print "<p>".__("Importing OPML...")."</p>";
0a13e84e 535 opml_import_domdoc($link, $owner_uid);
9f311df6 536
d7c848d9 537 print "<br><form method=\"GET\" action=\"prefs.php\">
1025ad87 538 <input type=\"submit\" value=\"".__("Return to preferences")."\">
d7c848d9 539 </form>";
9f311df6 540
30f782a2 541 print "</body></html>";
9f311df6
AD
542
543 }
544
3d477c2c 545// if ($link) db_close($link);
9f311df6 546
9a4506c8 547?>