opml.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. $feed_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue);
  30. if (!$feed_title) {
  31. $feed_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue);
  32. }
  33. $cat_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue);
  34. if (!$cat_title) {
  35. $cat_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue);
  36. }
  37. $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlUrl')->nodeValue);
  38. if (!$feed_url)
  39. $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlURL')->nodeValue);
  40. $site_url = db_escape_string($outline->attributes->getNamedItem('htmlUrl')->nodeValue);
  41. $pref_name = db_escape_string($outline->attributes->getNamedItem('pref-name')->nodeValue);
  42. if ($cat_title && !$feed_url) {
  43. if ($cat_title != "tt-rss-prefs") {
  44. db_query($link, "BEGIN");
  45. $result = db_query($link, "SELECT id FROM
  46. ttrss_feed_categories WHERE title = '$cat_title' AND
  47. owner_uid = '$owner_uid' LIMIT 1");
  48. if (db_num_rows($result) == 0) {
  49. printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
  50. db_query($link, "INSERT INTO ttrss_feed_categories
  51. (title,owner_uid)
  52. VALUES ('$cat_title', '$owner_uid')");
  53. }
  54. db_query($link, "COMMIT");
  55. }
  56. }
  57. // print "$active_category : $feed_title : $feed_url<br>";
  58. if ($pref_name) {
  59. $parent_node = $outline->parentNode;
  60. if ($parent_node && $parent_node->nodeName == "outline") {
  61. $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
  62. if ($cat_check == "tt-rss-prefs") {
  63. $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue);
  64. printf("<li>".
  65. __("Setting preference key %s to %s")."</li>",
  66. $pref_name, $pref_value);
  67. set_pref($link, $pref_name, $pref_value);
  68. }
  69. }
  70. }
  71. if (!$feed_title || !$feed_url) continue;
  72. db_query($link, "BEGIN");
  73. $cat_id = null;
  74. $parent_node = $outline->parentNode;
  75. if ($parent_node && $parent_node->nodeName == "outline") {
  76. $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue;
  77. if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue;
  78. } else {
  79. $element_category = '';
  80. }
  81. if ($element_category) {
  82. $element_category = db_escape_string($element_category);
  83. $result = db_query($link, "SELECT id FROM
  84. ttrss_feed_categories WHERE title = '$element_category' AND
  85. owner_uid = '$owner_uid' LIMIT 1");
  86. if (db_num_rows($result) == 1) {
  87. $cat_id = db_fetch_result($result, 0, "id");
  88. }
  89. }
  90. $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
  91. feed_url = '$feed_url'
  92. AND owner_uid = '$owner_uid'");
  93. print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
  94. (<a target='_blank' href=\"$feed_url\">rss</a>)&nbsp;";
  95. if (db_num_rows($result) > 0) {
  96. print __('is already imported.');
  97. } else {
  98. if ($cat_id) {
  99. $add_query = "INSERT INTO ttrss_feeds
  100. (title, feed_url, owner_uid, cat_id, site_url) VALUES
  101. ('$feed_title', '$feed_url', '$owner_uid',
  102. '$cat_id', '$site_url')";
  103. } else {
  104. $add_query = "INSERT INTO ttrss_feeds
  105. (title, feed_url, owner_uid, cat_id, site_url) VALUES
  106. ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
  107. '$site_url')";
  108. }
  109. //print $add_query;
  110. db_query($link, $add_query);
  111. print __('OK');
  112. }
  113. print "</li>";
  114. db_query($link, "COMMIT");
  115. }
  116. } else {
  117. print_error(__('Error while parsing document.'));
  118. }
  119. } else {
  120. print_error(__('Error: please upload OPML file.'));
  121. }
  122. }
  123. function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
  124. if (!$_REQUEST["debug"]) {
  125. header("Content-type: application/xml+opml");
  126. } else {
  127. header("Content-type: text/xml");
  128. }
  129. header("Content-Disposition: attachment; filename=" . $name );
  130. print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  131. print "<opml version=\"1.0\">";
  132. print "<head>
  133. <dateCreated>" . date("r", time()) . "</dateCreated>
  134. <title>Tiny Tiny RSS Feed Export</title>
  135. </head>";
  136. print "<body>";
  137. $cat_mode = false;
  138. $select = "SELECT * ";
  139. $where = "WHERE owner_uid = '$owner_uid'";
  140. $orderby = "ORDER BY title";
  141. if ($hide_private_feeds){
  142. $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
  143. auth_login = '' AND auth_pass = ''";
  144. }
  145. if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
  146. $cat_mode = true;
  147. $select = "SELECT
  148. title, feed_url, site_url,
  149. (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
  150. $orderby = "ORDER BY cat_title, title";
  151. }
  152. else{
  153. $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
  154. print "<!-- feeding cats is not enabled -->";
  155. print "<!-- $cat_feed -->";
  156. }
  157. $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
  158. $old_cat_title = "";
  159. while ($line = db_fetch_assoc($result)) {
  160. $title = htmlspecialchars($line["title"]);
  161. $url = htmlspecialchars($line["feed_url"]);
  162. $site_url = htmlspecialchars($line["site_url"]);
  163. if ($cat_mode) {
  164. $cat_title = htmlspecialchars($line["cat_title"]);
  165. if ($old_cat_title != $cat_title) {
  166. if ($old_cat_title) {
  167. print "</outline>\n";
  168. }
  169. if ($cat_title) {
  170. print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
  171. }
  172. $old_cat_title = $cat_title;
  173. }
  174. }
  175. if ($site_url) {
  176. $html_url_qpart = "htmlUrl=\"$site_url\"";
  177. } else {
  178. $html_url_qpart = "";
  179. }
  180. print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
  181. }
  182. if ($cat_mode && $old_cat_title) {
  183. print "</outline>\n";
  184. }
  185. # export tt-rss settings
  186. if ($include_settings) {
  187. print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
  188. $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
  189. profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
  190. while ($line = db_fetch_assoc($result)) {
  191. $name = $line["pref_name"];
  192. $value = htmlspecialchars($line["value"]);
  193. print "<outline pref-name=\"$name\" value=\"$value\">";
  194. print "</outline>";
  195. }
  196. print "</outline>";
  197. }
  198. print "</body></opml>";
  199. }
  200. // FIXME there are some brackets issues here
  201. $op = $_REQUEST["op"];
  202. if (!$op) $op = "Export";
  203. $output_name = $_REQUEST["filename"];
  204. if (!$output_name) $output_name = "TinyTinyRSS.opml";
  205. $show_settings = $_REQUEST["settings"];
  206. if ($op == "Export") {
  207. login_sequence($link);
  208. $owner_uid = $_SESSION["uid"];
  209. return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
  210. }
  211. if ($op == "publish"){
  212. $key = db_escape_string($_REQUEST["key"]);
  213. $result = db_query($link, "SELECT owner_uid
  214. FROM ttrss_access_keys WHERE
  215. access_key = '$key' AND feed_id = 'OPML:Publish'");
  216. if (db_num_rows($result) == 1) {
  217. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  218. return opml_export($link, "", $owner_uid, true, false);
  219. } else {
  220. print "<error>User not found</error>";
  221. }
  222. }
  223. if ($op == "Import") {
  224. login_sequence($link);
  225. $owner_uid = $_SESSION["uid"];
  226. header('Content-Type: text/html; charset=utf-8');
  227. print "<html>
  228. <head>
  229. <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
  230. <title>".__("OPML Utility")."</title>
  231. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  232. </head>
  233. <body>
  234. <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
  235. <h1>".__('OPML Utility')."</h1>";
  236. db_query($link, "BEGIN");
  237. /* create Imported feeds category just in case */
  238. $result = db_query($link, "SELECT id FROM
  239. ttrss_feed_categories WHERE title = 'Imported feeds' AND
  240. owner_uid = '$owner_uid' LIMIT 1");
  241. if (db_num_rows($result) == 0) {
  242. db_query($link, "INSERT INTO ttrss_feed_categories
  243. (title,owner_uid)
  244. VALUES ('Imported feeds', '$owner_uid')");
  245. }
  246. db_query($link, "COMMIT");
  247. print "<p>".__("Importing OPML...")."</p>";
  248. opml_import_domdoc($link, $owner_uid);
  249. print "<br><form method=\"GET\" action=\"prefs.php\">
  250. <input type=\"submit\" value=\"".__("Return to preferences")."\">
  251. </form>";
  252. print "</body></html>";
  253. }
  254. // if ($link) db_close($link);
  255. ?>