opml.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. set_include_path(get_include_path() . PATH_SEPARATOR .
  3. dirname(__FILE__) . "/include");
  4. require_once "functions.php";
  5. require_once "sessions.php";
  6. require_once "sanity_check.php";
  7. require_once "config.php";
  8. require_once "db.php";
  9. require_once "db-prefs.php";
  10. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  11. if (!init_connection($link)) return;
  12. function opml_import_domdoc($link, $owner_uid) {
  13. if (is_file($_FILES['opml_file']['tmp_name'])) {
  14. $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']);
  15. $result = db_query($link, "SELECT id FROM
  16. ttrss_feed_categories WHERE title = 'Imported feeds' AND
  17. owner_uid = '$owner_uid' LIMIT 1");
  18. if (db_num_rows($result) == 1) {
  19. $default_cat_id = db_fetch_result($result, 0, "id");
  20. } else {
  21. $default_cat_id = 0;
  22. }
  23. if ($doc) {
  24. $body = $doc->getElementsByTagName('body');
  25. $xpath = new DOMXpath($doc);
  26. $query = "/opml/body//outline";
  27. $outlines = $xpath->query($query);
  28. foreach ($outlines as $outline) {
  29. $attributes = $outline->attributes;
  30. $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
  31. if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
  32. $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
  33. if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
  34. $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue);
  35. if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue);
  36. $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue);
  37. $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue);
  38. $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue);
  39. $filter_name = db_escape_string($attributes->getNamedItem('filter-name')->nodeValue);
  40. if ($cat_title && !$feed_url) {
  41. if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels' && $cat_title != 'tt-rss-filters') {
  42. db_query($link, "BEGIN");
  43. $result = db_query($link, "SELECT id FROM
  44. ttrss_feed_categories WHERE title = '$cat_title' AND
  45. owner_uid = '$owner_uid' LIMIT 1");
  46. if (db_num_rows($result) == 0) {
  47. printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
  48. db_query($link, "INSERT INTO ttrss_feed_categories
  49. (title,owner_uid)
  50. VALUES ('$cat_title', '$owner_uid')");
  51. }
  52. db_query($link, "COMMIT");
  53. }
  54. }
  55. // print "$active_category : $feed_title : $feed_url<br>";
  56. if ($pref_name) {
  57. $parent_node = $outline->parentNode;
  58. if ($parent_node && $parent_node->nodeName == "outline") {
  59. $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
  60. if ($cat_check == "tt-rss-prefs") {
  61. $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue);
  62. printf("<li>".
  63. __("Setting preference key %s to %s")."</li>",
  64. $pref_name, $pref_value);
  65. set_pref($link, $pref_name, $pref_value);
  66. }
  67. }
  68. }
  69. if ($label_name) {
  70. $parent_node = $outline->parentNode;
  71. if ($parent_node && $parent_node->nodeName == "outline") {
  72. $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
  73. if ($cat_check == "tt-rss-labels") {
  74. $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue);
  75. $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue);
  76. if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
  77. printf("<li>".__("Adding label %s")."</li>", htmlspecialchars($label_name));
  78. label_create($link, $label_name, $fg_color, $bg_color);
  79. } else {
  80. printf("<li>".__("Duplicate label: %s")."</li>",
  81. htmlspecialchars($label_name));
  82. }
  83. }
  84. }
  85. }
  86. if ($filter_name) {
  87. $parent_node = $outline->parentNode;
  88. if ($parent_node && $parent_node->nodeName == "outline") {
  89. $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
  90. if ($cat_check == "tt-rss-filters") {
  91. $filter = json_decode($outline->nodeValue, true);
  92. if ($filter) {
  93. $reg_exp = db_escape_string($filter['reg_exp']);
  94. $filter_type = (int)$filter['filter_type'];
  95. $action_id = (int)$filter['action_id'];
  96. $result = db_query($link, "SELECT id FROM ttrss_filters WHERE
  97. reg_exp = '$reg_exp' AND
  98. filter_type = '$filter_type' AND
  99. action_id = '$action_id' AND
  100. owner_uid = " .$_SESSION['uid']);
  101. if (db_num_rows($result) == 0) {
  102. $enabled = bool_to_sql_bool($filter['enabled']);
  103. $action_param = db_escape_string($filter['action_param']);
  104. $inverse = bool_to_sql_bool($filter['inverse']);
  105. $filter_param = db_escape_string($filter['filter_param']);
  106. $cat_filter = bool_to_sql_bool($filter['cat_filter']);
  107. $feed_url = db_escape_string($filter['feed_url']);
  108. $cat_title = db_escape_string($filter['cat_title']);
  109. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
  110. feed_url = '$feed_url' AND owner_uid = ".$_SESSION['uid']);
  111. if (db_num_rows($result) != 0) {
  112. $feed_id = db_fetch_result($result, 0, "id");
  113. } else {
  114. $feed_id = "NULL";
  115. }
  116. $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
  117. title = '$cat_title' AND owner_uid = ".$_SESSION['uid']);
  118. if (db_num_rows($result) != 0) {
  119. $cat_id = db_fetch_result($result, 0, "id");
  120. } else {
  121. $cat_id = "NULL";
  122. }
  123. printf("<li>".__("Adding filter %s")."</li>", htmlspecialchars($reg_exp));
  124. $query = "INSERT INTO ttrss_filters (filter_type, action_id,
  125. enabled, inverse, action_param, filter_param,
  126. cat_filter, feed_id,
  127. cat_id, reg_exp,
  128. owner_uid)
  129. VALUES ($filter_type, $action_id,
  130. $enabled, $inverse, '$action_param', '$filter_param',
  131. $cat_filter, $feed_id,
  132. $cat_id, '$reg_exp', ".
  133. $_SESSION['uid'].")";
  134. db_query($link, $query);
  135. } else {
  136. printf("<li>".__("Duplicate filter %s")."</li>", htmlspecialchars($reg_exp));
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if (!$feed_title || !$feed_url) continue;
  143. db_query($link, "BEGIN");
  144. $cat_id = null;
  145. $parent_node = $outline->parentNode;
  146. if ($parent_node && $parent_node->nodeName == "outline") {
  147. $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue;
  148. if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue;
  149. } else {
  150. $element_category = '';
  151. }
  152. if ($element_category) {
  153. $element_category = db_escape_string($element_category);
  154. $result = db_query($link, "SELECT id FROM
  155. ttrss_feed_categories WHERE title = '$element_category' AND
  156. owner_uid = '$owner_uid' LIMIT 1");
  157. if (db_num_rows($result) == 1) {
  158. $cat_id = db_fetch_result($result, 0, "id");
  159. }
  160. }
  161. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
  162. feed_url = '$feed_url'
  163. AND owner_uid = '$owner_uid'");
  164. print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
  165. (<a target='_blank' href=\"$feed_url\">rss</a>)&nbsp;";
  166. if (db_num_rows($result) > 0) {
  167. print __('is already imported.');
  168. } else {
  169. if ($cat_id) {
  170. $add_query = "INSERT INTO ttrss_feeds
  171. (title, feed_url, owner_uid, cat_id, site_url) VALUES
  172. ('$feed_title', '$feed_url', '$owner_uid',
  173. '$cat_id', '$site_url')";
  174. } else {
  175. $add_query = "INSERT INTO ttrss_feeds
  176. (title, feed_url, owner_uid, cat_id, site_url) VALUES
  177. ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
  178. '$site_url')";
  179. }
  180. //print $add_query;
  181. db_query($link, $add_query);
  182. print __('OK');
  183. }
  184. print "</li>";
  185. db_query($link, "COMMIT");
  186. }
  187. } else {
  188. print_error(__('Error while parsing document.'));
  189. }
  190. } else {
  191. print_error(__('Error: please upload OPML file.'));
  192. }
  193. }
  194. function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
  195. if (!$_REQUEST["debug"]) {
  196. header("Content-type: application/xml+opml");
  197. header("Content-Disposition: attachment; filename=" . $name );
  198. } else {
  199. header("Content-type: text/xml");
  200. }
  201. print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  202. print "<opml version=\"1.0\">";
  203. print "<head>
  204. <dateCreated>" . date("r", time()) . "</dateCreated>
  205. <title>Tiny Tiny RSS Feed Export</title>
  206. </head>";
  207. print "<body>";
  208. $cat_mode = false;
  209. $select = "SELECT * ";
  210. $where = "WHERE owner_uid = '$owner_uid'";
  211. $orderby = "ORDER BY title";
  212. if ($hide_private_feeds){
  213. $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
  214. auth_login = '' AND auth_pass = ''";
  215. }
  216. if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
  217. $cat_mode = true;
  218. $select = "SELECT
  219. title, feed_url, site_url,
  220. (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
  221. $orderby = "ORDER BY cat_title, title";
  222. }
  223. else{
  224. $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
  225. print "<!-- feeding cats is not enabled -->";
  226. print "<!-- $cat_feed -->";
  227. }
  228. $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
  229. $old_cat_title = "";
  230. while ($line = db_fetch_assoc($result)) {
  231. $title = htmlspecialchars($line["title"]);
  232. $url = htmlspecialchars($line["feed_url"]);
  233. $site_url = htmlspecialchars($line["site_url"]);
  234. if ($cat_mode) {
  235. $cat_title = htmlspecialchars($line["cat_title"]);
  236. if ($old_cat_title != $cat_title) {
  237. if ($old_cat_title) {
  238. print "</outline>\n";
  239. }
  240. if ($cat_title) {
  241. print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
  242. }
  243. $old_cat_title = $cat_title;
  244. }
  245. }
  246. if ($site_url) {
  247. $html_url_qpart = "htmlUrl=\"$site_url\"";
  248. } else {
  249. $html_url_qpart = "";
  250. }
  251. print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
  252. }
  253. if ($cat_mode && $old_cat_title) {
  254. print "</outline>\n";
  255. }
  256. # export tt-rss settings
  257. if ($include_settings) {
  258. print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
  259. $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
  260. profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
  261. while ($line = db_fetch_assoc($result)) {
  262. $name = $line["pref_name"];
  263. $value = htmlspecialchars($line["value"]);
  264. print "<outline pref-name=\"$name\" value=\"$value\">";
  265. print "</outline>";
  266. }
  267. print "</outline>";
  268. print "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
  269. $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
  270. owner_uid = " . $_SESSION['uid']);
  271. while ($line = db_fetch_assoc($result)) {
  272. $name = htmlspecialchars($line['caption']);
  273. $fg_color = htmlspecialchars($line['fg_color']);
  274. $bg_color = htmlspecialchars($line['bg_color']);
  275. print "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
  276. }
  277. print "</outline>";
  278. print "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
  279. $result = db_query($link, "SELECT filter_type,
  280. reg_exp,
  281. action_id,
  282. enabled,
  283. action_param,
  284. inverse,
  285. filter_param,
  286. cat_filter,
  287. ttrss_feeds.feed_url AS feed_url,
  288. ttrss_feed_categories.title AS cat_title
  289. FROM ttrss_filters
  290. LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
  291. LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
  292. WHERE
  293. ttrss_filters.owner_uid = " . $_SESSION['uid']);
  294. while ($line = db_fetch_assoc($result)) {
  295. $name = htmlspecialchars($line['reg_exp']);
  296. foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
  297. $line[$b] = sql_bool_to_bool($line[$b]);
  298. }
  299. $filter = json_encode($line);
  300. print "<outline filter-name=\"$name\">$filter</outline>";
  301. }
  302. print "</outline>";
  303. }
  304. print "</body></opml>";
  305. }
  306. // FIXME there are some brackets issues here
  307. $op = $_REQUEST["op"];
  308. if (!$op) $op = "Export";
  309. $output_name = $_REQUEST["filename"];
  310. if (!$output_name) $output_name = "TinyTinyRSS.opml";
  311. $show_settings = $_REQUEST["settings"];
  312. if ($op == "Export") {
  313. login_sequence($link);
  314. $owner_uid = $_SESSION["uid"];
  315. return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
  316. }
  317. if ($op == "publish"){
  318. $key = db_escape_string($_REQUEST["key"]);
  319. $result = db_query($link, "SELECT owner_uid
  320. FROM ttrss_access_keys WHERE
  321. access_key = '$key' AND feed_id = 'OPML:Publish'");
  322. if (db_num_rows($result) == 1) {
  323. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  324. return opml_export($link, "", $owner_uid, true, false);
  325. } else {
  326. print "<error>User not found</error>";
  327. }
  328. }
  329. if ($op == "Import") {
  330. login_sequence($link);
  331. $owner_uid = $_SESSION["uid"];
  332. header('Content-Type: text/html; charset=utf-8');
  333. print "<html>
  334. <head>
  335. <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
  336. <title>".__("OPML Utility")."</title>
  337. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  338. </head>
  339. <body>
  340. <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
  341. <h1>".__('OPML Utility')."</h1>";
  342. db_query($link, "BEGIN");
  343. /* create Imported feeds category just in case */
  344. $result = db_query($link, "SELECT id FROM
  345. ttrss_feed_categories WHERE title = 'Imported feeds' AND
  346. owner_uid = '$owner_uid' LIMIT 1");
  347. if (db_num_rows($result) == 0) {
  348. db_query($link, "INSERT INTO ttrss_feed_categories
  349. (title,owner_uid)
  350. VALUES ('Imported feeds', '$owner_uid')");
  351. }
  352. db_query($link, "COMMIT");
  353. print "<p>".__("Importing OPML...")."</p>";
  354. opml_import_domdoc($link, $owner_uid);
  355. print "<br><form method=\"GET\" action=\"prefs.php\">
  356. <input type=\"submit\" value=\"".__("Return to preferences")."\">
  357. </form>";
  358. print "</body></html>";
  359. }
  360. // if ($link) db_close($link);
  361. ?>