]> git.wh0rd.org - tt-rss.git/blob - classes/opml.php
2c2c2ac0e6c1209f90a2177da343b98bb9d1a4b5
[tt-rss.git] / classes / opml.php
1 <?php
2 class Opml extends Handler_Protected {
3
4 function csrf_ignore($method) {
5 $csrf_ignored = array("export", "import");
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
10 function export() {
11 $output_name = $_REQUEST["filename"];
12 if (!$output_name) $output_name = "TinyTinyRSS.opml";
13
14 $show_settings = $_REQUEST["settings"];
15
16 $owner_uid = $_SESSION["uid"];
17 return $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1));
18 }
19
20 function import() {
21 $owner_uid = $_SESSION["uid"];
22
23 header('Content-Type: text/html; charset=utf-8');
24
25 print "<html>
26 <head>
27 <link rel=\"stylesheet\" href=\"css/utility.css\" type=\"text/css\">
28 <title>".__("OPML Utility")."</title>
29 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
30 </head>
31 <body>
32 <div class=\"floatingLogo\"><img src=\"images/logo_small.png\"></div>
33 <h1>".__('OPML Utility')."</h1><div class='content'>";
34
35 add_feed_category("Imported feeds");
36
37 $this->opml_notice(__("Importing OPML..."));
38 $this->opml_import($owner_uid);
39
40 print "<br><form method=\"GET\" action=\"prefs.php\">
41 <input type=\"submit\" value=\"".__("Return to preferences")."\">
42 </form>";
43
44 print "</div></body></html>";
45
46
47 }
48
49 // Export
50
51 private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds=false) {
52
53 if ($cat_id) {
54 $cat_qpart = "parent_cat = '$cat_id'";
55 $feed_cat_qpart = "cat_id = '$cat_id'";
56 } else {
57 $cat_qpart = "parent_cat IS NULL";
58 $feed_cat_qpart = "cat_id IS NULL";
59 }
60
61 if ($hide_private_feeds)
62 $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
63 else
64 $hide_qpart = "true";
65
66 $out = "";
67
68 if ($cat_id) {
69 $result = $this->dbh->query("SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
70 AND owner_uid = '$owner_uid'");
71 $cat_title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
72 }
73
74 if ($cat_title) $out .= "<outline text=\"$cat_title\">\n";
75
76 $result = $this->dbh->query("SELECT id,title
77 FROM ttrss_feed_categories WHERE
78 $cat_qpart AND owner_uid = '$owner_uid' ORDER BY order_id, title");
79
80 while ($line = $this->dbh->fetch_assoc($result)) {
81 $title = htmlspecialchars($line["title"]);
82 $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds);
83 }
84
85 $feeds_result = $this->dbh->query("select title, feed_url, site_url
86 from ttrss_feeds where $feed_cat_qpart AND owner_uid = '$owner_uid' AND $hide_qpart
87 order by order_id, title");
88
89 while ($fline = $this->dbh->fetch_assoc($feeds_result)) {
90 $title = htmlspecialchars($fline["title"]);
91 $url = htmlspecialchars($fline["feed_url"]);
92 $site_url = htmlspecialchars($fline["site_url"]);
93
94 if ($site_url) {
95 $html_url_qpart = "htmlUrl=\"$site_url\"";
96 } else {
97 $html_url_qpart = "";
98 }
99
100 $out .= "<outline type=\"rss\" text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
101 }
102
103 if ($cat_title) $out .= "</outline>\n";
104
105 return $out;
106 }
107
108 function opml_export($name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
109 if (!$owner_uid) return;
110
111 if (!isset($_REQUEST["debug"])) {
112 header("Content-type: application/xml+opml");
113 header("Content-Disposition: attachment; filename=" . $name );
114 } else {
115 header("Content-type: text/xml");
116 }
117
118 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
119
120 $out .= "<opml version=\"1.0\">";
121 $out .= "<head>
122 <dateCreated>" . date("r", time()) . "</dateCreated>
123 <title>Tiny Tiny RSS Feed Export</title>
124 </head>";
125 $out .= "<body>";
126
127 $out .= $this->opml_export_category($owner_uid, false, $hide_private_feeds);
128
129 # export tt-rss settings
130
131 if ($include_settings) {
132 $out .= "<outline text=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
133
134 $result = $this->dbh->query("SELECT pref_name, value FROM ttrss_user_prefs WHERE
135 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
136
137 while ($line = $this->dbh->fetch_assoc($result)) {
138 $name = $line["pref_name"];
139 $value = htmlspecialchars($line["value"]);
140
141 $out .= "<outline pref-name=\"$name\" value=\"$value\"/>";
142 }
143
144 $out .= "</outline>";
145
146 $out .= "<outline text=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
147
148 $result = $this->dbh->query("SELECT * FROM ttrss_labels2 WHERE
149 owner_uid = " . $_SESSION['uid']);
150
151 while ($line = $this->dbh->fetch_assoc($result)) {
152 $name = htmlspecialchars($line['caption']);
153 $fg_color = htmlspecialchars($line['fg_color']);
154 $bg_color = htmlspecialchars($line['bg_color']);
155
156 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
157
158 }
159
160 $out .= "</outline>";
161
162 $out .= "<outline text=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
163
164 $result = $this->dbh->query("SELECT * FROM ttrss_filters2
165 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY id");
166
167 while ($line = $this->dbh->fetch_assoc($result)) {
168 foreach (array('enabled', 'match_any_rule', 'inverse') as $b) {
169 $line[$b] = sql_bool_to_bool($line[$b]);
170 }
171
172 $line["rules"] = array();
173 $line["actions"] = array();
174
175 $tmp_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
176 WHERE filter_id = ".$line["id"]);
177
178 while ($tmp_line = $this->dbh->fetch_assoc($tmp_result)) {
179 unset($tmp_line["id"]);
180 unset($tmp_line["filter_id"]);
181
182 $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]);
183
184 if (!$tmp_line["match_on"]) {
185 if ($cat_filter && $tmp_line["cat_id"] || $tmp_line["feed_id"]) {
186 $tmp_line["feed"] = Feeds::getFeedTitle(
187 $cat_filter ? $tmp_line["cat_id"] : $tmp_line["feed_id"],
188 $cat_filter);
189 } else {
190 $tmp_line["feed"] = "";
191 }
192 } else {
193 $match = [];
194 foreach (json_decode($tmp_line["match_on"], true) as $feed_id) {
195
196 if (strpos($feed_id, "CAT:") === 0) {
197 $feed_id = (int)substr($feed_id, 4);
198 if ($feed_id) {
199 array_push($match, [Feeds::getCategoryTitle($feed_id), true, false]);
200 } else {
201 array_push($match, [0, true, true]);
202 }
203 } else {
204 if ($feed_id) {
205 array_push($match, [Feeds::getFeedTitle((int)$feed_id), false, false]);
206 } else {
207 array_push($match, [0, false, true]);
208 }
209 }
210 }
211
212 $tmp_line["match"] = $match;
213 unset($tmp_line["match_on"]);
214 }
215
216 $tmp_line["cat_filter"] = sql_bool_to_bool($tmp_line["cat_filter"]);
217 $tmp_line["inverse"] = sql_bool_to_bool($tmp_line["inverse"]);
218
219 unset($tmp_line["feed_id"]);
220 unset($tmp_line["cat_id"]);
221
222 array_push($line["rules"], $tmp_line);
223 }
224
225 $tmp_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
226 WHERE filter_id = ".$line["id"]);
227
228 while ($tmp_line = $this->dbh->fetch_assoc($tmp_result)) {
229 unset($tmp_line["id"]);
230 unset($tmp_line["filter_id"]);
231
232 array_push($line["actions"], $tmp_line);
233 }
234
235 unset($line["id"]);
236 unset($line["owner_uid"]);
237 $filter = json_encode($line);
238
239 $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>";
240
241 }
242
243
244 $out .= "</outline>";
245 }
246
247 $out .= "</body></opml>";
248
249 // Format output.
250 $doc = new DOMDocument();
251 $doc->formatOutput = true;
252 $doc->preserveWhiteSpace = false;
253 $doc->loadXML($out);
254
255 $xpath = new DOMXpath($doc);
256 $outlines = $xpath->query("//outline[@title]");
257
258 // cleanup empty categories
259 foreach ($outlines as $node) {
260 if ($node->getElementsByTagName('outline')->length == 0)
261 $node->parentNode->removeChild($node);
262 }
263
264 $res = $doc->saveXML();
265
266 /* // saveXML uses a two-space indent. Change to tabs.
267 $res = preg_replace_callback('/^(?: )+/mu',
268 create_function(
269 '$matches',
270 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
271 $res); */
272
273 print $res;
274 }
275
276 // Import
277
278 private function opml_import_feed($node, $cat_id, $owner_uid) {
279 $attrs = $node->attributes;
280
281 $feed_title = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250));
282 if (!$feed_title) $feed_title = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('title')->nodeValue, 0, 250));
283
284 $feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
285 if (!$feed_url) $feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlURL')->nodeValue);
286
287 $site_url = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue, 0, 250));
288
289 if ($feed_url && $feed_title) {
290 $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
291 feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
292
293 if ($this->dbh->num_rows($result) == 0) {
294 #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
295 $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
296
297 if (!$cat_id) $cat_id = 'NULL';
298
299 $query = "INSERT INTO ttrss_feeds
300 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
301 ('$feed_title', '$feed_url', '$owner_uid',
302 $cat_id, '$site_url', 0)";
303 $this->dbh->query($query);
304
305 } else {
306 $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
307 }
308 }
309 }
310
311 private function opml_import_label($node, $owner_uid) {
312 $attrs = $node->attributes;
313 $label_name = $this->dbh->escape_string($attrs->getNamedItem('label-name')->nodeValue);
314
315 if ($label_name) {
316 $fg_color = $this->dbh->escape_string($attrs->getNamedItem('label-fg-color')->nodeValue);
317 $bg_color = $this->dbh->escape_string($attrs->getNamedItem('label-bg-color')->nodeValue);
318
319 if (!Labels::find_id($label_name, $_SESSION['uid'])) {
320 $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
321 Labels::create($label_name, $fg_color, $bg_color, $owner_uid);
322 } else {
323 $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
324 }
325 }
326 }
327
328 private function opml_import_preference($node) {
329 $attrs = $node->attributes;
330 $pref_name = $this->dbh->escape_string($attrs->getNamedItem('pref-name')->nodeValue);
331
332 if ($pref_name) {
333 $pref_value = $this->dbh->escape_string($attrs->getNamedItem('value')->nodeValue);
334
335 $this->opml_notice(T_sprintf("Setting preference key %s to %s",
336 $pref_name, $pref_value));
337
338 set_pref($pref_name, $pref_value);
339 }
340 }
341
342 private function opml_import_filter($node) {
343 $attrs = $node->attributes;
344
345 $filter_type = $this->dbh->escape_string($attrs->getNamedItem('filter-type')->nodeValue);
346
347 if ($filter_type == '2') {
348 $filter = json_decode($node->nodeValue, true);
349
350 if ($filter) {
351 $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]);
352 $enabled = bool_to_sql_bool($filter["enabled"]);
353 $inverse = bool_to_sql_bool($filter["inverse"]);
354 $title = db_escape_string($filter["title"]);
355
356 $this->dbh->query("BEGIN");
357
358 $this->dbh->query("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid)
359 VALUES ($match_any_rule, $enabled, $inverse, '$title',
360 ".$_SESSION["uid"].")");
361
362 $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
363 owner_uid = ".$_SESSION["uid"]);
364 $filter_id = $this->dbh->fetch_result($result, 0, "id");
365
366 if ($filter_id) {
367 $this->opml_notice(T_sprintf("Adding filter..."));
368
369 foreach ($filter["rules"] as $rule) {
370 $feed_id = "NULL";
371 $cat_id = "NULL";
372
373 if ($rule["match"]) {
374
375 $match_on = [];
376
377 foreach ($rule["match"] as $match) {
378 list ($name, $is_cat, $is_id) = $match;
379
380 if ($is_id) {
381 array_push($match_on, ($is_cat ? "CAT:" : "") . $name);
382 } else {
383
384 $match_id = false;
385
386 if (!$is_cat) {
387 $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feeds
388 WHERE title = '" . $this->dbh->escape_string($name) . "' AND owner_uid = " . $_SESSION["uid"]);
389 if ($this->dbh->num_rows($tmp_result) > 0) {
390 $match_id = $this->dbh->fetch_result($tmp_result, 0, "id");
391 }
392 } else {
393 $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feed_categories
394 WHERE title = '" . $this->dbh->escape_string($name) . "' AND owner_uid = " . $_SESSION["uid"]);
395
396 if ($this->dbh->num_rows($tmp_result) > 0) {
397 $match_id = 'CAT:' . $this->dbh->fetch_result($tmp_result, 0, "id");
398 }
399 }
400
401 if ($match_id) array_push($match_on, $match_id);
402 }
403 }
404
405 $reg_exp = $this->dbh->escape_string($rule["reg_exp"]);
406 $filter_type = (int)$rule["filter_type"];
407 $inverse = bool_to_sql_bool($rule["inverse"]);
408 $match_on = $this->dbh->escape_string(json_encode($match_on));
409
410 $this->dbh->query("INSERT INTO ttrss_filters2_rules (feed_id,cat_id,match_on,filter_id,filter_type,reg_exp,cat_filter,inverse)
411 VALUES (NULL, NULL, '$match_on',$filter_id, $filter_type, '$reg_exp', false, $inverse)");
412
413 } else {
414
415 if (!$rule["cat_filter"]) {
416 $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feeds
417 WHERE title = '" . $this->dbh->escape_string($rule["feed"]) . "' AND owner_uid = " . $_SESSION["uid"]);
418 if ($this->dbh->num_rows($tmp_result) > 0) {
419 $feed_id = $this->dbh->fetch_result($tmp_result, 0, "id");
420 }
421 } else {
422 $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feed_categories
423 WHERE title = '" . $this->dbh->escape_string($rule["feed"]) . "' AND owner_uid = " . $_SESSION["uid"]);
424
425 if ($this->dbh->num_rows($tmp_result) > 0) {
426 $cat_id = $this->dbh->fetch_result($tmp_result, 0, "id");
427 }
428 }
429
430 $cat_filter = bool_to_sql_bool($rule["cat_filter"]);
431 $reg_exp = $this->dbh->escape_string($rule["reg_exp"]);
432 $filter_type = (int)$rule["filter_type"];
433 $inverse = bool_to_sql_bool($rule["inverse"]);
434
435 $this->dbh->query("INSERT INTO ttrss_filters2_rules (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter,inverse)
436 VALUES ($feed_id, $cat_id, $filter_id, $filter_type, '$reg_exp', $cat_filter,$inverse)");
437 }
438 }
439
440 foreach ($filter["actions"] as $action) {
441
442 $action_id = (int)$action["action_id"];
443 $action_param = $this->dbh->escape_string($action["action_param"]);
444
445 $this->dbh->query("INSERT INTO ttrss_filters2_actions (filter_id,action_id,action_param)
446 VALUES ($filter_id, $action_id, '$action_param')");
447 }
448 }
449
450 $this->dbh->query("COMMIT");
451 }
452 }
453 }
454
455 private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
456 $default_cat_id = (int) $this->get_feed_category('Imported feeds', false);
457
458 if ($root_node) {
459 $cat_title = $this->dbh->escape_string(mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250));
460
461 if (!$cat_title)
462 $cat_title = $this->dbh->escape_string(mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250));
463
464 if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
465 $cat_id = $this->get_feed_category($cat_title, $parent_id);
466 $this->dbh->query("BEGIN");
467 if ($cat_id === false) {
468 add_feed_category($cat_title, $parent_id);
469 $cat_id = $this->get_feed_category($cat_title, $parent_id);
470 }
471 $this->dbh->query("COMMIT");
472 } else {
473 $cat_id = 0;
474 }
475
476 $outlines = $root_node->childNodes;
477
478 } else {
479 $xpath = new DOMXpath($doc);
480 $outlines = $xpath->query("//opml/body/outline");
481
482 $cat_id = 0;
483 }
484
485 #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
486 $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")));
487
488 foreach ($outlines as $node) {
489 if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
490 $attrs = $node->attributes;
491 $node_cat_title = $this->dbh->escape_string($attrs->getNamedItem('text')->nodeValue);
492
493 if (!$node_cat_title)
494 $node_cat_title = $this->dbh->escape_string($attrs->getNamedItem('title')->nodeValue);
495
496 $node_feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
497
498 if ($node_cat_title && !$node_feed_url) {
499 $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
500 } else {
501
502 if (!$cat_id) {
503 $dst_cat_id = $default_cat_id;
504 } else {
505 $dst_cat_id = $cat_id;
506 }
507
508 switch ($cat_title) {
509 case "tt-rss-prefs":
510 $this->opml_import_preference($node);
511 break;
512 case "tt-rss-labels":
513 $this->opml_import_label($node, $owner_uid);
514 break;
515 case "tt-rss-filters":
516 $this->opml_import_filter($node);
517 break;
518 default:
519 $this->opml_import_feed($node, $dst_cat_id, $owner_uid);
520 }
521 }
522 }
523 }
524 }
525
526 function opml_import($owner_uid) {
527 if (!$owner_uid) return;
528
529 $doc = false;
530
531 if ($_FILES['opml_file']['error'] != 0) {
532 print_error(T_sprintf("Upload failed with error code %d",
533 $_FILES['opml_file']['error']));
534 return;
535 }
536
537 if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) {
538 $tmp_file = tempnam(CACHE_DIR . '/upload', 'opml');
539
540 $result = move_uploaded_file($_FILES['opml_file']['tmp_name'],
541 $tmp_file);
542
543 if (!$result) {
544 print_error(__("Unable to move uploaded file."));
545 return;
546 }
547 } else {
548 print_error(__('Error: please upload OPML file.'));
549 return;
550 }
551
552 if (is_file($tmp_file)) {
553 $doc = new DOMDocument();
554 libxml_disable_entity_loader(false);
555 $doc->load($tmp_file);
556 libxml_disable_entity_loader(true);
557 unlink($tmp_file);
558 } else if (!$doc) {
559 print_error(__('Error: unable to find moved OPML file.'));
560 return;
561 }
562
563 if ($doc) {
564 $this->opml_import_category($doc, false, $owner_uid, false);
565 } else {
566 print_error(__('Error while parsing document.'));
567 }
568 }
569
570 private function opml_notice($msg) {
571 print "$msg<br/>";
572 }
573
574 static function opml_publish_url(){
575
576 $url_path = get_self_url_prefix();
577 $url_path .= "/opml.php?op=publish&key=" .
578 get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]);
579
580 return $url_path;
581 }
582
583 function get_feed_category($feed_cat, $parent_cat_id = false) {
584 if ($parent_cat_id) {
585 $parent_qpart = "parent_cat = '$parent_cat_id'";
586 $parent_insert = "'$parent_cat_id'";
587 } else {
588 $parent_qpart = "parent_cat IS NULL";
589 $parent_insert = "NULL";
590 }
591
592 $result = db_query(
593 "SELECT id FROM ttrss_feed_categories
594 WHERE $parent_qpart AND title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]);
595
596 if (db_num_rows($result) == 0) {
597 return false;
598 } else {
599 return db_fetch_result($result, 0, "id");
600 }
601 }
602
603
604 }