]>
git.wh0rd.org - tt-rss.git/blob - classes/opml.php
2 class Opml
extends Handler_Protected
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("export", "import");
7 return array_search($method, $csrf_ignored) !== false;
11 $output_name = $_REQUEST["filename"];
12 if (!$output_name) $output_name = "TinyTinyRSS.opml";
14 $show_settings = $_REQUEST["settings"];
16 $owner_uid = $_SESSION["uid"];
18 $rc = $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1));
24 $owner_uid = $_SESSION["uid"];
26 header('Content-Type: text/html; charset=utf-8');
30 <link rel=\"stylesheet\" href=\"css/utility.css\" type=\"text/css\">
31 <title>".__("OPML Utility")."</title>
32 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
35 <div class=\"floatingLogo\"><img src=\"images/logo_small.png\"></div>
36 <h1>".__('OPML Utility')."</h1><div class='content'>";
38 add_feed_category("Imported feeds");
40 $this->opml_notice(__("Importing OPML..."));
42 $this->opml_import($owner_uid);
44 print "<br><form method=\"GET\" action=\"prefs.php\">
45 <input type=\"submit\" value=\"".__("Return to preferences")."\">
48 print "</div></body></html>";
55 private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds=false) {
57 $cat_id = (int) $cat_id;
59 if ($hide_private_feeds)
60 $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
67 $sth = $this->pdo
->prepare("SELECT title FROM ttrss_feed_categories WHERE id = ?
69 $sth->execute([$cat_id, $owner_uid]);
71 $cat_title = htmlspecialchars($row['title']);
74 if ($cat_title) $out .= "<outline text=\"$cat_title\">\n";
76 $sth = $this->pdo
->prepare("SELECT id,title
77 FROM ttrss_feed_categories WHERE
78 (parent_cat = :cat OR (:cat = 0 AND parent_cat IS NULL)) AND
79 owner_uid = :uid ORDER BY order_id, title");
81 $sth->execute([':cat' => $cat_id, ':uid' => $owner_uid]);
83 while ($line = $sth->fetch()) {
84 $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds);
87 $fsth = $this->pdo
->prepare("select title, feed_url, site_url
88 FROM ttrss_feeds WHERE
89 (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND owner_uid = :uid AND $hide_qpart
90 ORDER BY order_id, title");
92 $fsth->execute([':cat' => $cat_id, ':uid' => $owner_uid]);
94 while ($fline = $fsth->fetch()) {
95 $title = htmlspecialchars($fline["title"]);
96 $url = htmlspecialchars($fline["feed_url"]);
97 $site_url = htmlspecialchars($fline["site_url"]);
100 $html_url_qpart = "htmlUrl=\"$site_url\"";
102 $html_url_qpart = "";
105 $out .= "<outline type=\"rss\" text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
108 if ($cat_title) $out .= "</outline>\n";
113 function opml_export($name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
114 if (!$owner_uid) return;
116 if (!isset($_REQUEST["debug"])) {
117 header("Content-type: application/xml+opml");
118 header("Content-Disposition: attachment; filename=" . $name );
120 header("Content-type: text/xml");
123 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
125 $out .= "<opml version=\"1.0\">";
127 <dateCreated>" . date("r", time()) . "</dateCreated>
128 <title>Tiny Tiny RSS Feed Export</title>
132 $out .= $this->opml_export_category($owner_uid, 0, $hide_private_feeds);
134 # export tt-rss settings
136 if ($include_settings) {
137 $out .= "<outline text=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
139 $sth = $this->pdo
->prepare("SELECT pref_name, value FROM ttrss_user_prefs WHERE
140 profile IS NULL AND owner_uid = ? ORDER BY pref_name");
141 $sth->execute([$owner_uid]);
143 while ($line = $sth->fetch()) {
144 $name = $line["pref_name"];
145 $value = htmlspecialchars($line["value"]);
147 $out .= "<outline pref-name=\"$name\" value=\"$value\"/>";
150 $out .= "</outline>";
152 $out .= "<outline text=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION
."\">";
154 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_labels2 WHERE
156 $sth->execute([$owner_uid]);
158 while ($line = $sth->fetch()) {
159 $name = htmlspecialchars($line['caption']);
160 $fg_color = htmlspecialchars($line['fg_color']);
161 $bg_color = htmlspecialchars($line['bg_color']);
163 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
167 $out .= "</outline>";
169 $out .= "<outline text=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION
."\">";
171 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2
172 WHERE owner_uid = ? ORDER BY id");
173 $sth->execute([$owner_uid]);
175 while ($line = $sth->fetch()) {
176 foreach (array('enabled', 'match_any_rule', 'inverse') as $b) {
177 $line[$b] = sql_bool_to_bool($line[$b]);
180 $line["rules"] = array();
181 $line["actions"] = array();
183 $tmph = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_rules
184 WHERE filter_id = ?");
185 $tmph->execute([$line['id']]);
187 while ($tmp_line = $tmph->fetch()) {
188 unset($tmp_line["id"]);
189 unset($tmp_line["filter_id"]);
191 $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]);
193 if (!$tmp_line["match_on"]) {
194 if ($cat_filter && $tmp_line["cat_id"] ||
$tmp_line["feed_id"]) {
195 $tmp_line["feed"] = Feeds
::getFeedTitle(
196 $cat_filter ?
$tmp_line["cat_id"] : $tmp_line["feed_id"],
199 $tmp_line["feed"] = "";
203 foreach (json_decode($tmp_line["match_on"], true) as $feed_id) {
205 if (strpos($feed_id, "CAT:") === 0) {
206 $feed_id = (int)substr($feed_id, 4);
208 array_push($match, [Feeds
::getCategoryTitle($feed_id), true, false]);
210 array_push($match, [0, true, true]);
214 array_push($match, [Feeds
::getFeedTitle((int)$feed_id), false, false]);
216 array_push($match, [0, false, true]);
221 $tmp_line["match"] = $match;
222 unset($tmp_line["match_on"]);
225 $tmp_line["cat_filter"] = sql_bool_to_bool($tmp_line["cat_filter"]);
226 $tmp_line["inverse"] = sql_bool_to_bool($tmp_line["inverse"]);
228 unset($tmp_line["feed_id"]);
229 unset($tmp_line["cat_id"]);
231 array_push($line["rules"], $tmp_line);
234 $tmph = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
235 WHERE filter_id = ?");
236 $tmph->execute([$line['id']]);
238 while ($tmp_line = $tmph->fetch()) {
239 unset($tmp_line["id"]);
240 unset($tmp_line["filter_id"]);
242 array_push($line["actions"], $tmp_line);
246 unset($line["owner_uid"]);
247 $filter = json_encode($line);
249 $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>";
254 $out .= "</outline>";
257 $out .= "</body></opml>";
260 $doc = new DOMDocument();
261 $doc->formatOutput
= true;
262 $doc->preserveWhiteSpace
= false;
265 $xpath = new DOMXpath($doc);
266 $outlines = $xpath->query("//outline[@title]");
268 // cleanup empty categories
269 foreach ($outlines as $node) {
270 if ($node->getElementsByTagName('outline')->length
== 0)
271 $node->parentNode
->removeChild($node);
274 $res = $doc->saveXML();
276 /* // saveXML uses a two-space indent. Change to tabs.
277 $res = preg_replace_callback('/^(?: )+/mu',
280 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
288 private function opml_import_feed($node, $cat_id, $owner_uid) {
289 $attrs = $node->attributes
;
291 $feed_title = mb_substr($attrs->getNamedItem('text')->nodeValue
, 0, 250);
292 if (!$feed_title) $feed_title = mb_substr($attrs->getNamedItem('title')->nodeValue
, 0, 250);
294 $feed_url = $attrs->getNamedItem('xmlUrl')->nodeValue
;
295 if (!$feed_url) $feed_url = $attrs->getNamedItem('xmlURL')->nodeValue
;
297 $site_url = mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue
, 0, 250);
299 if ($feed_url && $feed_title) {
300 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
301 feed_url = ? AND owner_uid = ?");
302 $sth->execute([$feed_url, $owner_uid]);
304 if (!$sth->fetch()) {
305 #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
306 $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
308 if (!$cat_id) $cat_id = null;
310 $sth = $this->pdo
->prepare("INSERT INTO ttrss_feeds
311 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
312 (?, ?, ?, ?, ?, 0)");
314 $sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url]);
317 $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
322 private function opml_import_label($node, $owner_uid) {
323 $attrs = $node->attributes
;
324 $label_name = $attrs->getNamedItem('label-name')->nodeValue
;
327 $fg_color = $attrs->getNamedItem('label-fg-color')->nodeValue
;
328 $bg_color = $attrs->getNamedItem('label-bg-color')->nodeValue
;
330 if (!Labels
::find_id($label_name, $_SESSION['uid'])) {
331 $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
332 Labels
::create($label_name, $fg_color, $bg_color, $owner_uid);
334 $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
339 private function opml_import_preference($node) {
340 $attrs = $node->attributes
;
341 $pref_name = $attrs->getNamedItem('pref-name')->nodeValue
;
344 $pref_value = $attrs->getNamedItem('value')->nodeValue
;
346 $this->opml_notice(T_sprintf("Setting preference key %s to %s",
347 $pref_name, $pref_value));
349 set_pref($pref_name, $pref_value);
353 private function opml_import_filter($node) {
354 $attrs = $node->attributes
;
356 $filter_type = $attrs->getNamedItem('filter-type')->nodeValue
;
358 if ($filter_type == '2') {
359 $filter = json_decode($node->nodeValue
, true);
362 $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]);
363 $enabled = bool_to_sql_bool($filter["enabled"]);
364 $inverse = bool_to_sql_bool($filter["inverse"]);
365 $title = $filter["title"];
367 print "F: $title, $inverse, $enabled, $match_any_rule";
369 $sth = $this->pdo
->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid)
370 VALUES (?, ?, ?, ?, ?)");
372 $sth->execute([$match_any_rule, $enabled, $inverse, $title, $_SESSION['uid']]);
374 $sth = $this->pdo
->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
376 $sth->execute([$_SESSION['uid']]);
378 $row = $sth->fetch();
379 $filter_id = $row['id'];
382 $this->opml_notice(T_sprintf("Adding filter..."));
384 foreach ($filter["rules"] as $rule) {
388 if ($rule["match"]) {
392 foreach ($rule["match"] as $match) {
393 list ($name, $is_cat, $is_id) = $match;
396 array_push($match_on, ($is_cat ?
"CAT:" : "") . $name);
402 $tsth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds
403 WHERE title = ? AND owner_uid = ?");
405 $tsth->execute([$name, $_SESSION['uid']]);
407 if ($row = $tsth->fetch()) {
408 $match_id = $row['id'];
411 $tsth = $this->pdo
->prepare("SELECT id FROM ttrss_feed_categories
412 WHERE title = ? AND owner_uid = ?");
413 $tsth->execute([$name, $_SESSION['uid']]);
415 if ($row = $tsth->fetch()) {
416 $match_id = $row['id'];
420 if ($match_id) array_push($match_on, $match_id);
424 $reg_exp = $rule["reg_exp"];
425 $filter_type = (int)$rule["filter_type"];
426 $inverse = bool_to_sql_bool($rule["inverse"]);
427 $match_on = json_encode($match_on);
429 $usth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_rules
430 (feed_id,cat_id,match_on,filter_id,filter_type,reg_exp,cat_filter,inverse)
432 (NULL, NULL, ?, ?, ?, ?, false, ?)");
433 $usth->execute([$match_on, $filter_id, $filter_type, $reg_exp, $inverse]);
437 if (!$rule["cat_filter"]) {
438 $tsth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds
439 WHERE title = ? AND owner_uid = ?");
441 $tsth->execute([$rule['feed'], $_SESSION['uid']]);
443 if ($row = $tsth->fetch()) {
444 $feed_id = $row['id'];
447 $tsth = $this->pdo
->prepare("SELECT id FROM ttrss_feed_categories
448 WHERE title = ? AND owner_uid = ?");
450 $tsth->execute([$rule['feed'], $_SESSION['uid']]);
452 if ($row = $tsth->fetch()) {
453 $feed_id = $row['id'];
457 $cat_filter = bool_to_sql_bool($rule["cat_filter"]);
458 $reg_exp = $rule["reg_exp"];
459 $filter_type = (int)$rule["filter_type"];
460 $inverse = bool_to_sql_bool($rule["inverse"]);
462 $usth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_rules
463 (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter,inverse)
465 (?, ?, ?, ?, ?, ?, ?)");
466 $usth->execute([$feed_id, $cat_id, $filter_id, $filter_type, $reg_exp, $cat_filter, $inverse]);
470 foreach ($filter["actions"] as $action) {
472 $action_id = (int)$action["action_id"];
473 $action_param = $action["action_param"];
475 $usth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_actions
476 (filter_id,action_id,action_param)
479 $usth->execute([$filter_id, $action_id, $action_param]);
486 private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
487 $default_cat_id = (int) $this->get_feed_category('Imported feeds', false);
490 $cat_title = mb_substr($root_node->attributes
->getNamedItem('text')->nodeValue
, 0, 250);
493 $cat_title = mb_substr($root_node->attributes
->getNamedItem('title')->nodeValue
, 0, 250);
495 if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
496 $cat_id = $this->get_feed_category($cat_title, $parent_id);
498 if ($cat_id === false) {
499 add_feed_category($cat_title, $parent_id);
500 $cat_id = $this->get_feed_category($cat_title, $parent_id);
507 $outlines = $root_node->childNodes
;
510 $xpath = new DOMXpath($doc);
511 $outlines = $xpath->query("//opml/body/outline");
516 #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
517 $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ?
$cat_title : __("Uncategorized")));
519 foreach ($outlines as $node) {
520 if ($node->hasAttributes() && strtolower($node->tagName
) == "outline") {
521 $attrs = $node->attributes
;
522 $node_cat_title = $attrs->getNamedItem('text')->nodeValue
;
524 if (!$node_cat_title)
525 $node_cat_title = $attrs->getNamedItem('title')->nodeValue
;
527 $node_feed_url = $attrs->getNamedItem('xmlUrl')->nodeValue
;
529 if ($node_cat_title && !$node_feed_url) {
530 $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
534 $dst_cat_id = $default_cat_id;
536 $dst_cat_id = $cat_id;
539 switch ($cat_title) {
541 $this->opml_import_preference($node);
543 case "tt-rss-labels":
544 $this->opml_import_label($node, $owner_uid);
546 case "tt-rss-filters":
547 $this->opml_import_filter($node);
550 $this->opml_import_feed($node, $dst_cat_id, $owner_uid);
557 function opml_import($owner_uid) {
558 if (!$owner_uid) return;
562 if ($_FILES['opml_file']['error'] != 0) {
563 print_error(T_sprintf("Upload failed with error code %d",
564 $_FILES['opml_file']['error']));
568 if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) {
569 $tmp_file = tempnam(CACHE_DIR
. '/upload', 'opml');
571 $result = move_uploaded_file($_FILES['opml_file']['tmp_name'],
575 print_error(__("Unable to move uploaded file."));
579 print_error(__('Error: please upload OPML file.'));
583 if (is_file($tmp_file)) {
584 $doc = new DOMDocument();
585 libxml_disable_entity_loader(false);
586 $doc->load($tmp_file);
587 libxml_disable_entity_loader(true);
590 print_error(__('Error: unable to find moved OPML file.'));
595 $this->pdo
->beginTransaction();
596 $this->opml_import_category($doc, false, $owner_uid, false);
597 $this->pdo
->commit();
599 print_error(__('Error while parsing document.'));
603 private function opml_notice($msg) {
607 static function opml_publish_url(){
609 $url_path = get_self_url_prefix();
610 $url_path .= "/opml.php?op=publish&key=" .
611 get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]);
616 function get_feed_category($feed_cat, $parent_cat_id = false) {
618 $parent_cat_id = (int) $parent_cat_id;
620 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feed_categories
622 AND (parent_cat = :parent OR (:parent = 0 AND parent_cat IS NULL))
623 AND owner_uid = :uid");
625 $sth->execute([':title' => $feed_cat, ':parent' => $parent_cat_id, ':uid' => $_SESSION['uid']]);
627 if ($row = $sth->fetch()) {