opml.php 19 KB

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