PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/opml.php

https://github.com/kpadilha/Tiny-Tiny-RSS
PHP | 494 lines | 348 code | 132 blank | 14 comment | 69 complexity | a49879d51c746e8b3be29ac3185df427 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, LGPL-3.0, GPL-3.0
  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=\"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($this->link, "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 = db_query($this->link, "SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
  50. AND owner_uid = '$owner_uid'");
  51. $cat_title = htmlspecialchars(db_fetch_result($result, 0, "title"));
  52. }
  53. if ($cat_title) $out .= "<outline text=\"$cat_title\">\n";
  54. $result = db_query($this->link, "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 = db_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 = db_query($this->link, "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 = db_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 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 = db_query($this->link, "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 = db_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 = db_query($this->link, "SELECT * FROM ttrss_labels2 WHERE
  107. owner_uid = " . $_SESSION['uid']);
  108. while ($line = db_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 = db_query($this->link, "SELECT * FROM ttrss_filters2
  117. WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY id");
  118. while ($line = db_fetch_assoc($result)) {
  119. foreach (array('enabled', 'match_any_rule') as $b) {
  120. $line[$b] = sql_bool_to_bool($line[$b]);
  121. }
  122. $line["rules"] = array();
  123. $line["actions"] = array();
  124. $tmp_result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules
  125. WHERE filter_id = ".$line["id"]);
  126. while ($tmp_line = db_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($this->link,
  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. unset($tmp_line["feed_id"]);
  139. unset($tmp_line["cat_id"]);
  140. array_push($line["rules"], $tmp_line);
  141. }
  142. $tmp_result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions
  143. WHERE filter_id = ".$line["id"]);
  144. while ($tmp_line = db_fetch_assoc($tmp_result)) {
  145. unset($tmp_line["id"]);
  146. unset($tmp_line["filter_id"]);
  147. array_push($line["actions"], $tmp_line);
  148. }
  149. unset($line["id"]);
  150. unset($line["owner_uid"]);
  151. $filter = json_encode($line);
  152. $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>";
  153. }
  154. $out .= "</outline>";
  155. }
  156. $out .= "</body></opml>";
  157. // Format output.
  158. $doc = new DOMDocument();
  159. $doc->formatOutput = true;
  160. $doc->preserveWhiteSpace = false;
  161. $doc->loadXML($out);
  162. $xpath = new DOMXpath($doc);
  163. $outlines = $xpath->query("//outline[@title]");
  164. // cleanup empty categories
  165. foreach ($outlines as $node) {
  166. if ($node->getElementsByTagName('outline')->length == 0)
  167. $node->parentNode->removeChild($node);
  168. }
  169. $res = $doc->saveXML();
  170. /* // saveXML uses a two-space indent. Change to tabs.
  171. $res = preg_replace_callback('/^(?: )+/mu',
  172. create_function(
  173. '$matches',
  174. 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
  175. $res); */
  176. print $res;
  177. }
  178. // Import
  179. private function opml_import_feed($doc, $node, $cat_id, $owner_uid) {
  180. $attrs = $node->attributes;
  181. $feed_title = db_escape_string($this->link, mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250));
  182. if (!$feed_title) $feed_title = db_escape_string($this->link, mb_substr($attrs->getNamedItem('title')->nodeValue, 0, 250));
  183. $feed_url = db_escape_string($this->link, mb_substr($attrs->getNamedItem('xmlUrl')->nodeValue, 0, 250));
  184. if (!$feed_url) $feed_url = db_escape_string($this->link, mb_substr($attrs->getNamedItem('xmlURL')->nodeValue, 0, 250));
  185. $site_url = db_escape_string($this->link, mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue, 0, 250));
  186. if ($feed_url && $feed_title) {
  187. $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
  188. feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
  189. if (db_num_rows($result) == 0) {
  190. #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
  191. $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
  192. if (!$cat_id) $cat_id = 'NULL';
  193. $query = "INSERT INTO ttrss_feeds
  194. (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
  195. ('$feed_title', '$feed_url', '$owner_uid',
  196. $cat_id, '$site_url', 0)";
  197. db_query($this->link, $query);
  198. } else {
  199. $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
  200. }
  201. }
  202. }
  203. private function opml_import_label($doc, $node, $owner_uid) {
  204. $attrs = $node->attributes;
  205. $label_name = db_escape_string($this->link, $attrs->getNamedItem('label-name')->nodeValue);
  206. if ($label_name) {
  207. $fg_color = db_escape_string($this->link, $attrs->getNamedItem('label-fg-color')->nodeValue);
  208. $bg_color = db_escape_string($this->link, $attrs->getNamedItem('label-bg-color')->nodeValue);
  209. if (!label_find_id($this->link, $label_name, $_SESSION['uid'])) {
  210. $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
  211. label_create($this->link, $label_name, $fg_color, $bg_color, $owner_uid);
  212. } else {
  213. $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
  214. }
  215. }
  216. }
  217. private function opml_import_preference($doc, $node, $owner_uid) {
  218. $attrs = $node->attributes;
  219. $pref_name = db_escape_string($this->link, $attrs->getNamedItem('pref-name')->nodeValue);
  220. if ($pref_name) {
  221. $pref_value = db_escape_string($this->link, $attrs->getNamedItem('value')->nodeValue);
  222. $this->opml_notice(T_sprintf("Setting preference key %s to %s",
  223. $pref_name, $pref_value));
  224. set_pref($this->link, $pref_name, $pref_value);
  225. }
  226. }
  227. private function opml_import_filter($doc, $node, $owner_uid) {
  228. $attrs = $node->attributes;
  229. $filter_type = db_escape_string($this->link, $attrs->getNamedItem('filter-type')->nodeValue);
  230. if ($filter_type == '2') {
  231. $filter = json_decode($node->nodeValue, true);
  232. if ($filter) {
  233. $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]);
  234. $enabled = bool_to_sql_bool($filter["enabled"]);
  235. db_query($this->link, "BEGIN");
  236. db_query($this->link, "INSERT INTO ttrss_filters2 (match_any_rule,enabled,owner_uid)
  237. VALUES ($match_any_rule, $enabled,".$_SESSION["uid"].")");
  238. $result = db_query($this->link, "SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
  239. owner_uid = ".$_SESSION["uid"]);
  240. $filter_id = db_fetch_result($result, 0, "id");
  241. if ($filter_id) {
  242. $this->opml_notice(T_sprintf("Adding filter..."));
  243. foreach ($filter["rules"] as $rule) {
  244. $feed_id = "NULL";
  245. $cat_id = "NULL";
  246. if (!$rule["cat_filter"]) {
  247. $tmp_result = db_query($this->link, "SELECT id FROM ttrss_feeds
  248. WHERE title = '".db_escape_string($this->link, $rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
  249. if (db_num_rows($tmp_result) > 0) {
  250. $feed_id = db_fetch_result($tmp_result, 0, "id");
  251. }
  252. } else {
  253. $tmp_result = db_query($this->link, "SELECT id FROM ttrss_feed_categories
  254. WHERE title = '".db_escape_string($this->link, $rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
  255. if (db_num_rows($tmp_result) > 0) {
  256. $cat_id = db_fetch_result($tmp_result, 0, "id");
  257. }
  258. }
  259. $cat_filter = bool_to_sql_bool($rule["cat_filter"]);
  260. $reg_exp = db_escape_string($this->link, $rule["reg_exp"]);
  261. $filter_type = (int)$rule["filter_type"];
  262. db_query($this->link, "INSERT INTO ttrss_filters2_rules (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter)
  263. VALUES ($feed_id, $cat_id, $filter_id, $filter_type, '$reg_exp', $cat_filter)");
  264. }
  265. foreach ($filter["actions"] as $action) {
  266. $action_id = (int)$action["action_id"];
  267. $action_param = db_escape_string($this->link, $action["action_param"]);
  268. db_query($this->link, "INSERT INTO ttrss_filters2_actions (filter_id,action_id,action_param)
  269. VALUES ($filter_id, $action_id, '$action_param')");
  270. }
  271. }
  272. db_query($this->link, "COMMIT");
  273. }
  274. }
  275. }
  276. private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
  277. $body = $doc->getElementsByTagName('body');
  278. $default_cat_id = (int) get_feed_category($this->link, 'Imported feeds', false);
  279. if ($root_node) {
  280. $cat_title = db_escape_string($this->link, mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250));
  281. if (!$cat_title)
  282. $cat_title = db_escape_string($this->link, mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250));
  283. if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
  284. $cat_id = get_feed_category($this->link, $cat_title, $parent_id);
  285. db_query($this->link, "BEGIN");
  286. if ($cat_id === false) {
  287. add_feed_category($this->link, $cat_title, $parent_id);
  288. $cat_id = get_feed_category($this->link, $cat_title, $parent_id);
  289. }
  290. db_query($this->link, "COMMIT");
  291. } else {
  292. $cat_id = 0;
  293. }
  294. $outlines = $root_node->childNodes;
  295. } else {
  296. $xpath = new DOMXpath($doc);
  297. $outlines = $xpath->query("//opml/body/outline");
  298. $cat_id = 0;
  299. }
  300. #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
  301. $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")));
  302. foreach ($outlines as $node) {
  303. if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
  304. $attrs = $node->attributes;
  305. $node_cat_title = db_escape_string($this->link, $attrs->getNamedItem('text')->nodeValue);
  306. if (!$node_cat_title)
  307. $node_cat_title = db_escape_string($this->link, $attrs->getNamedItem('title')->nodeValue);
  308. $node_feed_url = db_escape_string($this->link, $attrs->getNamedItem('xmlUrl')->nodeValue);
  309. if ($node_cat_title && !$node_feed_url) {
  310. $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
  311. } else {
  312. if (!$cat_id) {
  313. $dst_cat_id = $default_cat_id;
  314. } else {
  315. $dst_cat_id = $cat_id;
  316. }
  317. switch ($cat_title) {
  318. case "tt-rss-prefs":
  319. $this->opml_import_preference($doc, $node, $owner_uid);
  320. break;
  321. case "tt-rss-labels":
  322. $this->opml_import_label($doc, $node, $owner_uid);
  323. break;
  324. case "tt-rss-filters":
  325. $this->opml_import_filter($doc, $node, $owner_uid);
  326. break;
  327. default:
  328. $this->opml_import_feed($doc, $node, $dst_cat_id, $owner_uid);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. function opml_import($owner_uid) {
  335. if (!$owner_uid) return;
  336. $debug = isset($_REQUEST["debug"]);
  337. $doc = false;
  338. # if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
  339. if (is_file($_FILES['opml_file']['tmp_name'])) {
  340. $doc = new DOMDocument();
  341. $doc->load($_FILES['opml_file']['tmp_name']);
  342. } else if (!$doc) {
  343. print_error(__('Error: please upload OPML file.'));
  344. return;
  345. }
  346. if ($doc) {
  347. $this->opml_import_category($doc, false, $owner_uid, false);
  348. } else {
  349. print_error(__('Error while parsing document.'));
  350. }
  351. }
  352. private function opml_notice($msg) {
  353. print "$msg<br/>";
  354. }
  355. static function opml_publish_url($link){
  356. $url_path = get_self_url_prefix();
  357. $url_path .= "/opml.php?op=publish&key=" .
  358. get_feed_access_key($link, 'OPML:Publish', false, $_SESSION["uid"]);
  359. return $url_path;
  360. }
  361. }
  362. ?>