]> git.wh0rd.org - tt-rss.git/blob - opml.php
OPML: preserve category and feed order on import
[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 // 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
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
51 $attributes = $outline->attributes;
52
53 $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
54 if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
55
56 $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
57 if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
58
59 $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue);
60 if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue);
61
62 $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue);
63
64 $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue);
65 $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue);
66 $filter_name = db_escape_string($attributes->getNamedItem('filter-name')->nodeValue);
67
68 if ($cat_title && !$feed_url) {
69
70 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels' && $cat_title != 'tt-rss-filters') {
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) {
79 $cat_order_id = ++$new_cat_order_id;
80
81 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
82
83 db_query($link, "INSERT INTO ttrss_feed_categories
84 (title,owner_uid,order_id)
85 VALUES ('$cat_title', '$owner_uid', '$cat_order_id')");
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
112 if ($label_name) {
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") {
118
119 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue);
120 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue);
121
122 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
123 printf("<li>".__("Adding label %s")."</li>", htmlspecialchars($label_name));
124 label_create($link, $label_name, $fg_color, $bg_color);
125 } else {
126 printf("<li>".__("Duplicate label: %s")."</li>",
127 htmlspecialchars($label_name));
128 }
129 }
130 }
131 }
132
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) {
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 }
199 }
200 }
201 }
202 }
203
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 {
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 }
253
254 if ($cat_id) {
255 $add_query = "INSERT INTO ttrss_feeds
256 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
257 ('$feed_title', '$feed_url', '$owner_uid',
258 '$cat_id', '$site_url', '$new_feed_order_id')";
259
260 } else {
261 $add_query = "INSERT INTO ttrss_feeds
262 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
263 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
264 '$site_url', '$new_feed_order_id')";
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
290 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
291 if (!$_REQUEST["debug"]) {
292 header("Content-type: application/xml+opml");
293 header("Content-Disposition: attachment; filename=" . $name );
294 } else {
295 header("Content-type: text/xml");
296 }
297
298 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
299
300 $out .= "<opml version=\"1.0\">";
301 $out .= "<head>
302 <dateCreated>" . date("r", time()) . "</dateCreated>
303 <title>Tiny Tiny RSS Feed Export</title>
304 </head>";
305 $out .= "<body>";
306
307 $cat_mode = false;
308
309 $select = "SELECT * ";
310 $where = "WHERE owner_uid = '$owner_uid'";
311 $orderby = "ORDER BY order_id, title";
312 if ($hide_private_feeds){
313 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
314 auth_login = '' AND auth_pass = ''";
315 }
316
317
318
319 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
320 $cat_mode = true;
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,
324 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
325 $orderby = "ORDER BY cat_order_id, cat_title, order_id, title";
326
327 }
328 else{
329 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
330 $out .= "<!-- feeding cats is not enabled -->";
331 $out .= "<!-- $cat_feed -->";
332
333 }
334
335
336 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
337
338 $old_cat_title = "";
339
340 while ($line = db_fetch_assoc($result)) {
341 $title = htmlspecialchars($line["title"]);
342 $url = htmlspecialchars($line["feed_url"]);
343 $site_url = htmlspecialchars($line["site_url"]);
344
345 if ($cat_mode) {
346 $cat_title = htmlspecialchars($line["cat_title"]);
347
348 if ($old_cat_title != $cat_title) {
349 if ($old_cat_title) {
350 $out .= "</outline>\n";
351 }
352
353 if ($cat_title) {
354 $out .= "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
355 }
356
357 $old_cat_title = $cat_title;
358 }
359 }
360
361 if ($site_url) {
362 $html_url_qpart = "htmlUrl=\"$site_url\"";
363 } else {
364 $html_url_qpart = "";
365 }
366
367 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
368 }
369
370 if ($cat_mode && $old_cat_title) {
371 $out .= "</outline>\n";
372 }
373
374 # export tt-rss settings
375
376 if ($include_settings) {
377 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
378
379 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
380 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
381
382 while ($line = db_fetch_assoc($result)) {
383
384 $name = $line["pref_name"];
385 $value = htmlspecialchars($line["value"]);
386
387 $out .= "<outline pref-name=\"$name\" value=\"$value\">";
388
389 $out .= "</outline>";
390
391 }
392
393 $out .= "</outline>";
394
395 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
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
405 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
406
407 }
408
409 $out .= "</outline>";
410
411 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
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,
421 ttrss_feeds.feed_url AS feed_url,
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
438 $out .= "<outline filter-name=\"$name\">$filter</outline>";
439
440 }
441
442
443 $out .= "</outline>";
444 }
445
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;
463 }
464
465 // FIXME there are some brackets issues here
466
467 $op = $_REQUEST["op"];
468 if (!$op) $op = "Export";
469
470 $output_name = $_REQUEST["filename"];
471 if (!$output_name) $output_name = "TinyTinyRSS.opml";
472
473 $show_settings = $_REQUEST["settings"];
474
475 if ($op == "Export") {
476
477 login_sequence($link);
478 $owner_uid = $_SESSION["uid"];
479 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
480 }
481
482 if ($op == "publish"){
483 $key = db_escape_string($_REQUEST["key"]);
484
485 $result = db_query($link, "SELECT owner_uid
486 FROM ttrss_access_keys WHERE
487 access_key = '$key' AND feed_id = 'OPML:Publish'");
488
489 if (db_num_rows($result) == 1) {
490 $owner_uid = db_fetch_result($result, 0, "owner_uid");
491 return opml_export($link, "", $owner_uid, true, false);
492 } else {
493 print "<error>User not found</error>";
494 }
495 }
496
497 if ($op == "Import") {
498
499 login_sequence($link);
500 $owner_uid = $_SESSION["uid"];
501
502 header('Content-Type: text/html; charset=utf-8');
503
504 print "<html>
505 <head>
506 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
507 <title>".__("OPML Utility")."</title>
508 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
509 </head>
510 <body>
511 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
512 <h1>".__('OPML Utility')."</h1>";
513
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
524 (title,owner_uid)
525 VALUES ('Imported feeds', '$owner_uid')");
526 }
527
528 db_query($link, "COMMIT");
529
530 print "<p>".__("Importing OPML...")."</p>";
531 opml_import_domdoc($link, $owner_uid);
532
533 print "<br><form method=\"GET\" action=\"prefs.php\">
534 <input type=\"submit\" value=\"".__("Return to preferences")."\">
535 </form>";
536
537 print "</body></html>";
538
539 }
540
541 // if ($link) db_close($link);
542
543 ?>