opml.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. return $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1));
  13. }
  14. function import() {
  15. $owner_uid = $_SESSION["uid"];
  16. header('Content-Type: text/html; charset=utf-8');
  17. print "<html>
  18. <head>
  19. <link rel=\"stylesheet\" href=\"css/utility.css\" type=\"text/css\">
  20. <title>".__("OPML Utility")."</title>
  21. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  22. </head>
  23. <body>
  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. if ($cat_id) {
  37. $cat_qpart = "parent_cat = '$cat_id'";
  38. $feed_cat_qpart = "cat_id = '$cat_id'";
  39. } else {
  40. $cat_qpart = "parent_cat IS NULL";
  41. $feed_cat_qpart = "cat_id IS NULL";
  42. }
  43. if ($hide_private_feeds)
  44. $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
  45. else
  46. $hide_qpart = "true";
  47. $out = "";
  48. if ($cat_id) {
  49. $result = $this->dbh->query("SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
  50. AND owner_uid = '$owner_uid'");
  51. $cat_title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
  52. }
  53. if ($cat_title) $out .= "<outline text=\"$cat_title\">\n";
  54. $result = $this->dbh->query("SELECT id,title
  55. FROM ttrss_feed_categories WHERE
  56. $cat_qpart AND owner_uid = '$owner_uid' ORDER BY order_id, title");
  57. while ($line = $this->dbh->fetch_assoc($result)) {
  58. $title = htmlspecialchars($line["title"]);
  59. $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds);
  60. }
  61. $feeds_result = $this->dbh->query("select title, feed_url, site_url
  62. from ttrss_feeds where $feed_cat_qpart AND owner_uid = '$owner_uid' AND $hide_qpart
  63. order by order_id, title");
  64. while ($fline = $this->dbh->fetch_assoc($feeds_result)) {
  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, false, $hide_private_feeds);
  94. # export tt-rss settings
  95. if ($include_settings) {
  96. $out .= "<outline text=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
  97. $result = $this->dbh->query("SELECT pref_name, value FROM ttrss_user_prefs WHERE
  98. profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
  99. while ($line = $this->dbh->fetch_assoc($result)) {
  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. $result = $this->dbh->query("SELECT * FROM ttrss_labels2 WHERE
  107. owner_uid = " . $_SESSION['uid']);
  108. while ($line = $this->dbh->fetch_assoc($result)) {
  109. $name = htmlspecialchars($line['caption']);
  110. $fg_color = htmlspecialchars($line['fg_color']);
  111. $bg_color = htmlspecialchars($line['bg_color']);
  112. $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
  113. }
  114. $out .= "</outline>";
  115. $out .= "<outline text=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
  116. $result = $this->dbh->query("SELECT * FROM ttrss_filters2
  117. WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY id");
  118. while ($line = $this->dbh->fetch_assoc($result)) {
  119. foreach (array('enabled', 'match_any_rule', 'inverse') as $b) {
  120. $line[$b] = sql_bool_to_bool($line[$b]);
  121. }
  122. $line["rules"] = array();
  123. $line["actions"] = array();
  124. $tmp_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
  125. WHERE filter_id = ".$line["id"]);
  126. while ($tmp_line = $this->dbh->fetch_assoc($tmp_result)) {
  127. unset($tmp_line["id"]);
  128. unset($tmp_line["filter_id"]);
  129. $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]);
  130. if ($cat_filter && $tmp_line["cat_id"] || $tmp_line["feed_id"]) {
  131. $tmp_line["feed"] = getFeedTitle(
  132. $cat_filter ? $tmp_line["cat_id"] : $tmp_line["feed_id"],
  133. $cat_filter);
  134. } else {
  135. $tmp_line["feed"] = "";
  136. }
  137. $tmp_line["cat_filter"] = sql_bool_to_bool($tmp_line["cat_filter"]);
  138. $tmp_line["inverse"] = sql_bool_to_bool($tmp_line["inverse"]);
  139. unset($tmp_line["feed_id"]);
  140. unset($tmp_line["cat_id"]);
  141. array_push($line["rules"], $tmp_line);
  142. }
  143. $tmp_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
  144. WHERE filter_id = ".$line["id"]);
  145. while ($tmp_line = $this->dbh->fetch_assoc($tmp_result)) {
  146. unset($tmp_line["id"]);
  147. unset($tmp_line["filter_id"]);
  148. array_push($line["actions"], $tmp_line);
  149. }
  150. unset($line["id"]);
  151. unset($line["owner_uid"]);
  152. $filter = json_encode($line);
  153. $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>";
  154. }
  155. $out .= "</outline>";
  156. }
  157. $out .= "</body></opml>";
  158. // Format output.
  159. $doc = new DOMDocument();
  160. $doc->formatOutput = true;
  161. $doc->preserveWhiteSpace = false;
  162. $doc->loadXML($out);
  163. $xpath = new DOMXpath($doc);
  164. $outlines = $xpath->query("//outline[@title]");
  165. // cleanup empty categories
  166. foreach ($outlines as $node) {
  167. if ($node->getElementsByTagName('outline')->length == 0)
  168. $node->parentNode->removeChild($node);
  169. }
  170. $res = $doc->saveXML();
  171. /* // saveXML uses a two-space indent. Change to tabs.
  172. $res = preg_replace_callback('/^(?: )+/mu',
  173. create_function(
  174. '$matches',
  175. 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
  176. $res); */
  177. print $res;
  178. }
  179. // Import
  180. private function opml_import_feed($node, $cat_id, $owner_uid) {
  181. $attrs = $node->attributes;
  182. $feed_title = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250));
  183. if (!$feed_title) $feed_title = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('title')->nodeValue, 0, 250));
  184. $feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
  185. if (!$feed_url) $feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlURL')->nodeValue);
  186. $site_url = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue, 0, 250));
  187. if ($feed_url && $feed_title) {
  188. $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
  189. feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
  190. if ($this->dbh->num_rows($result) == 0) {
  191. #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
  192. $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
  193. if (!$cat_id) $cat_id = 'NULL';
  194. $query = "INSERT INTO ttrss_feeds
  195. (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
  196. ('$feed_title', '$feed_url', '$owner_uid',
  197. $cat_id, '$site_url', 0)";
  198. $this->dbh->query($query);
  199. } else {
  200. $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
  201. }
  202. }
  203. }
  204. private function opml_import_label($node, $owner_uid) {
  205. $attrs = $node->attributes;
  206. $label_name = $this->dbh->escape_string($attrs->getNamedItem('label-name')->nodeValue);
  207. if ($label_name) {
  208. $fg_color = $this->dbh->escape_string($attrs->getNamedItem('label-fg-color')->nodeValue);
  209. $bg_color = $this->dbh->escape_string($attrs->getNamedItem('label-bg-color')->nodeValue);
  210. if (!label_find_id($label_name, $_SESSION['uid'])) {
  211. $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
  212. label_create($label_name, $fg_color, $bg_color, $owner_uid);
  213. } else {
  214. $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
  215. }
  216. }
  217. }
  218. private function opml_import_preference($node) {
  219. $attrs = $node->attributes;
  220. $pref_name = $this->dbh->escape_string($attrs->getNamedItem('pref-name')->nodeValue);
  221. if ($pref_name) {
  222. $pref_value = $this->dbh->escape_string($attrs->getNamedItem('value')->nodeValue);
  223. $this->opml_notice(T_sprintf("Setting preference key %s to %s",
  224. $pref_name, $pref_value));
  225. set_pref($pref_name, $pref_value);
  226. }
  227. }
  228. private function opml_import_filter($node) {
  229. $attrs = $node->attributes;
  230. $filter_type = $this->dbh->escape_string($attrs->getNamedItem('filter-type')->nodeValue);
  231. if ($filter_type == '2') {
  232. $filter = json_decode($node->nodeValue, true);
  233. if ($filter) {
  234. $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]);
  235. $enabled = bool_to_sql_bool($filter["enabled"]);
  236. $inverse = bool_to_sql_bool($filter["inverse"]);
  237. $title = db_escape_string($filter["title"]);
  238. $this->dbh->query("BEGIN");
  239. $this->dbh->query("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid)
  240. VALUES ($match_any_rule, $enabled, $inverse, '$title',
  241. ".$_SESSION["uid"].")");
  242. $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
  243. owner_uid = ".$_SESSION["uid"]);
  244. $filter_id = $this->dbh->fetch_result($result, 0, "id");
  245. if ($filter_id) {
  246. $this->opml_notice(T_sprintf("Adding filter..."));
  247. foreach ($filter["rules"] as $rule) {
  248. $feed_id = "NULL";
  249. $cat_id = "NULL";
  250. if (!$rule["cat_filter"]) {
  251. $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feeds
  252. WHERE title = '".$this->dbh->escape_string($rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
  253. if ($this->dbh->num_rows($tmp_result) > 0) {
  254. $feed_id = $this->dbh->fetch_result($tmp_result, 0, "id");
  255. }
  256. } else {
  257. $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feed_categories
  258. WHERE title = '".$this->dbh->escape_string($rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
  259. if ($this->dbh->num_rows($tmp_result) > 0) {
  260. $cat_id = $this->dbh->fetch_result($tmp_result, 0, "id");
  261. }
  262. }
  263. $cat_filter = bool_to_sql_bool($rule["cat_filter"]);
  264. $reg_exp = $this->dbh->escape_string($rule["reg_exp"]);
  265. $filter_type = (int)$rule["filter_type"];
  266. $inverse = bool_to_sql_bool($rule["inverse"]);
  267. $this->dbh->query("INSERT INTO ttrss_filters2_rules (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter,inverse)
  268. VALUES ($feed_id, $cat_id, $filter_id, $filter_type, '$reg_exp', $cat_filter,$inverse)");
  269. }
  270. foreach ($filter["actions"] as $action) {
  271. $action_id = (int)$action["action_id"];
  272. $action_param = $this->dbh->escape_string($action["action_param"]);
  273. $this->dbh->query("INSERT INTO ttrss_filters2_actions (filter_id,action_id,action_param)
  274. VALUES ($filter_id, $action_id, '$action_param')");
  275. }
  276. }
  277. $this->dbh->query("COMMIT");
  278. }
  279. }
  280. }
  281. private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
  282. $default_cat_id = (int) get_feed_category('Imported feeds', false);
  283. if ($root_node) {
  284. $cat_title = $this->dbh->escape_string(mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250));
  285. if (!$cat_title)
  286. $cat_title = $this->dbh->escape_string(mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250));
  287. if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
  288. $cat_id = get_feed_category($cat_title, $parent_id);
  289. $this->dbh->query("BEGIN");
  290. if ($cat_id === false) {
  291. add_feed_category($cat_title, $parent_id);
  292. $cat_id = get_feed_category($cat_title, $parent_id);
  293. }
  294. $this->dbh->query("COMMIT");
  295. } else {
  296. $cat_id = 0;
  297. }
  298. $outlines = $root_node->childNodes;
  299. } else {
  300. $xpath = new DOMXpath($doc);
  301. $outlines = $xpath->query("//opml/body/outline");
  302. $cat_id = 0;
  303. }
  304. #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
  305. $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")));
  306. foreach ($outlines as $node) {
  307. if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
  308. $attrs = $node->attributes;
  309. $node_cat_title = $this->dbh->escape_string($attrs->getNamedItem('text')->nodeValue);
  310. if (!$node_cat_title)
  311. $node_cat_title = $this->dbh->escape_string($attrs->getNamedItem('title')->nodeValue);
  312. $node_feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
  313. if ($node_cat_title && !$node_feed_url) {
  314. $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
  315. } else {
  316. if (!$cat_id) {
  317. $dst_cat_id = $default_cat_id;
  318. } else {
  319. $dst_cat_id = $cat_id;
  320. }
  321. switch ($cat_title) {
  322. case "tt-rss-prefs":
  323. $this->opml_import_preference($node);
  324. break;
  325. case "tt-rss-labels":
  326. $this->opml_import_label($node, $owner_uid);
  327. break;
  328. case "tt-rss-filters":
  329. $this->opml_import_filter($node);
  330. break;
  331. default:
  332. $this->opml_import_feed($node, $dst_cat_id, $owner_uid);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. function opml_import($owner_uid) {
  339. if (!$owner_uid) return;
  340. $doc = false;
  341. if ($_FILES['opml_file']['error'] != 0) {
  342. print_error(T_sprintf("Upload failed with error code %d",
  343. $_FILES['opml_file']['error']));
  344. return;
  345. }
  346. if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) {
  347. $tmp_file = tempnam(CACHE_DIR . '/upload', 'opml');
  348. $result = move_uploaded_file($_FILES['opml_file']['tmp_name'],
  349. $tmp_file);
  350. if (!$result) {
  351. print_error(__("Unable to move uploaded file."));
  352. return;
  353. }
  354. } else {
  355. print_error(__('Error: please upload OPML file.'));
  356. return;
  357. }
  358. if (is_file($tmp_file)) {
  359. $doc = new DOMDocument();
  360. libxml_disable_entity_loader(false);
  361. $doc->load($tmp_file);
  362. libxml_disable_entity_loader(true);
  363. unlink($tmp_file);
  364. } else if (!$doc) {
  365. print_error(__('Error: unable to find moved OPML file.'));
  366. return;
  367. }
  368. if ($doc) {
  369. $this->opml_import_category($doc, false, $owner_uid, false);
  370. } else {
  371. print_error(__('Error while parsing document.'));
  372. }
  373. }
  374. private function opml_notice($msg) {
  375. print "$msg<br/>";
  376. }
  377. static function opml_publish_url(){
  378. $url_path = get_self_url_prefix();
  379. $url_path .= "/opml.php?op=publish&key=" .
  380. get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]);
  381. return $url_path;
  382. }
  383. }