opml.php 19 KB

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