]> git.wh0rd.org - tt-rss.git/blame - opml.php
OPML: preserve category and feed order on import
[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
08ae2a5b 290 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
e9558345
AD
291 if (!$_REQUEST["debug"]) {
292 header("Content-type: application/xml+opml");
bbefea90 293 header("Content-Disposition: attachment; filename=" . $name );
e9558345
AD
294 } else {
295 header("Content-type: text/xml");
296 }
08ae2a5b 297
95562576 298 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
4e9f5c24 299
95562576
AD
300 $out .= "<opml version=\"1.0\">";
301 $out .= "<head>
a90005e6
AD
302 <dateCreated>" . date("r", time()) . "</dateCreated>
303 <title>Tiny Tiny RSS Feed Export</title>
0a13e84e 304 </head>";
95562576 305 $out .= "<body>";
9a4506c8 306
da49ccf5 307 $cat_mode = false;
0a13e84e 308
95562576
AD
309 $select = "SELECT * ";
310 $where = "WHERE owner_uid = '$owner_uid'";
311 $orderby = "ORDER BY order_id, title";
86e26f1a 312 if ($hide_private_feeds){
0a13e84e 313 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
c107797c 314 auth_login = '' AND auth_pass = ''";
86e26f1a 315 }
da49ccf5 316
fee909d5
MK
317
318
319 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
da49ccf5 320 $cat_mode = true;
95562576
AD
321 $select = "SELECT
322 title, feed_url, site_url, order_id,
323 (SELECT order_id FROM ttrss_feed_categories WHERE id = cat_id) AS cat_order_id,
86e26f1a 324 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
95562576 325 $orderby = "ORDER BY cat_order_id, cat_title, order_id, title";
86e26f1a 326
da49ccf5 327 }
fee909d5
MK
328 else{
329 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
95562576
AD
330 $out .= "<!-- feeding cats is not enabled -->";
331 $out .= "<!-- $cat_feed -->";
fee909d5
MK
332
333 }
334
da49ccf5 335
95562576 336 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
86e26f1a 337
da49ccf5 338 $old_cat_title = "";
9a4506c8 339
8158c57a 340 while ($line = db_fetch_assoc($result)) {
6e0584e9
AD
341 $title = htmlspecialchars($line["title"]);
342 $url = htmlspecialchars($line["feed_url"]);
fa7b8749 343 $site_url = htmlspecialchars($line["site_url"]);
9a4506c8 344
da49ccf5
AD
345 if ($cat_mode) {
346 $cat_title = htmlspecialchars($line["cat_title"]);
347
348 if ($old_cat_title != $cat_title) {
349 if ($old_cat_title) {
95562576 350 $out .= "</outline>\n";
da49ccf5
AD
351 }
352
5b9af2b9 353 if ($cat_title) {
95562576 354 $out .= "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
5b9af2b9 355 }
da49ccf5
AD
356
357 $old_cat_title = $cat_title;
358 }
359 }
360
fa7b8749
AD
361 if ($site_url) {
362 $html_url_qpart = "htmlUrl=\"$site_url\"";
363 } else {
364 $html_url_qpart = "";
365 }
366
95562576 367 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
9a4506c8
AD
368 }
369
da49ccf5 370 if ($cat_mode && $old_cat_title) {
95562576 371 $out .= "</outline>\n";
da49ccf5
AD
372 }
373
e9558345
AD
374 # export tt-rss settings
375
442f326b 376 if ($include_settings) {
95562576 377 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
e9558345 378
442f326b 379 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
95562576 380 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
e9558345 381
442f326b 382 while ($line = db_fetch_assoc($result)) {
e9558345 383
442f326b
AD
384 $name = $line["pref_name"];
385 $value = htmlspecialchars($line["value"]);
0a13e84e 386
95562576 387 $out .= "<outline pref-name=\"$name\" value=\"$value\">";
e9558345 388
95562576 389 $out .= "</outline>";
e9558345 390
0a13e84e 391 }
e9558345 392
95562576 393 $out .= "</outline>";
bbefea90 394
95562576 395 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
bbefea90
AD
396
397 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
398 owner_uid = " . $_SESSION['uid']);
399
400 while ($line = db_fetch_assoc($result)) {
401 $name = htmlspecialchars($line['caption']);
402 $fg_color = htmlspecialchars($line['fg_color']);
403 $bg_color = htmlspecialchars($line['bg_color']);
404
95562576 405 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
bbefea90
AD
406
407 }
408
95562576 409 $out .= "</outline>";
7d926cfc 410
95562576 411 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
7d926cfc
AD
412
413 $result = db_query($link, "SELECT filter_type,
414 reg_exp,
415 action_id,
416 enabled,
417 action_param,
418 inverse,
419 filter_param,
420 cat_filter,
cfb665c0 421 ttrss_feeds.feed_url AS feed_url,
7d926cfc
AD
422 ttrss_feed_categories.title AS cat_title
423 FROM ttrss_filters
424 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
425 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
426 WHERE
427 ttrss_filters.owner_uid = " . $_SESSION['uid']);
428
429 while ($line = db_fetch_assoc($result)) {
430 $name = htmlspecialchars($line['reg_exp']);
431
432 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
433 $line[$b] = sql_bool_to_bool($line[$b]);
434 }
435
436 $filter = json_encode($line);
437
95562576 438 $out .= "<outline filter-name=\"$name\">$filter</outline>";
7d926cfc
AD
439
440 }
441
442
95562576 443 $out .= "</outline>";
442f326b 444 }
e9558345 445
95562576
AD
446 $out .= "</body></opml>";
447
448 // Format output.
449 $doc = new DOMDocument();
450 $doc->formatOutput = true;
451 $doc->preserveWhiteSpace = false;
452 $doc->loadXML($out);
453 $res = $doc->saveXML();
454
455 // saveXML uses a two-space indent. Change to tabs.
456 $res = preg_replace_callback('/^(?: )+/mu',
457 create_function(
458 '$matches',
459 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
460 $res);
461
462 print $res;
9a4506c8
AD
463 }
464
30f782a2
AD
465 // FIXME there are some brackets issues here
466
467 $op = $_REQUEST["op"];
95562576 468 if (!$op) $op = "Export";
08ae2a5b 469
95562576 470 $output_name = $_REQUEST["filename"];
08ae2a5b 471 if (!$output_name) $output_name = "TinyTinyRSS.opml";
0a13e84e 472
95562576 473 $show_settings = $_REQUEST["settings"];
0a13e84e 474
30f782a2 475 if ($op == "Export") {
0a13e84e 476
a4234239
MK
477 login_sequence($link);
478 $owner_uid = $_SESSION["uid"];
08ae2a5b 479 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
30f782a2 480 }
0a13e84e 481
442f326b 482 if ($op == "publish"){
86e26f1a
MK
483 $key = db_escape_string($_REQUEST["key"]);
484
2e7f046f
AD
485 $result = db_query($link, "SELECT owner_uid
486 FROM ttrss_access_keys WHERE
487 access_key = '$key' AND feed_id = 'OPML:Publish'");
86e26f1a
MK
488
489 if (db_num_rows($result) == 1) {
2e7f046f 490 $owner_uid = db_fetch_result($result, 0, "owner_uid");
08ae2a5b 491 return opml_export($link, "", $owner_uid, true, false);
86e26f1a
MK
492 } else {
493 print "<error>User not found</error>";
494 }
495 }
30f782a2 496
e98a3f65 497 if ($op == "Import") {
8158c57a 498
a4234239
MK
499 login_sequence($link);
500 $owner_uid = $_SESSION["uid"];
501
e9558345
AD
502 header('Content-Type: text/html; charset=utf-8');
503
e98a3f65
AD
504 print "<html>
505 <head>
ef59e6e8 506 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
1025ad87 507 <title>".__("OPML Utility")."</title>
e9558345 508 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
e98a3f65 509 </head>
04f6df27 510 <body>
0ae2bb2a 511 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
d1db26aa 512 <h1>".__('OPML Utility')."</h1>";
9f311df6 513
c03cf250
AD
514 db_query($link, "BEGIN");
515
516 /* create Imported feeds category just in case */
517
518 $result = db_query($link, "SELECT id FROM
519 ttrss_feed_categories WHERE title = 'Imported feeds' AND
520 owner_uid = '$owner_uid' LIMIT 1");
521
522 if (db_num_rows($result) == 0) {
523 db_query($link, "INSERT INTO ttrss_feed_categories
0a13e84e 524 (title,owner_uid)
c03cf250
AD
525 VALUES ('Imported feeds', '$owner_uid')");
526 }
527
528 db_query($link, "COMMIT");
529
0a13e84e 530 print "<p>".__("Importing OPML...")."</p>";
0a13e84e 531 opml_import_domdoc($link, $owner_uid);
9f311df6 532
d7c848d9 533 print "<br><form method=\"GET\" action=\"prefs.php\">
1025ad87 534 <input type=\"submit\" value=\"".__("Return to preferences")."\">
d7c848d9 535 </form>";
9f311df6 536
30f782a2 537 print "</body></html>";
9f311df6
AD
538
539 }
540
3d477c2c 541// if ($link) db_close($link);
9f311df6 542
9a4506c8 543?>