init.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. class Import_Export extends Plugin implements IHandler {
  3. private $link;
  4. private $host;
  5. function init($host) {
  6. $this->link = $host->get_link();
  7. $this->host = $host;
  8. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  9. $host->add_command("xml-import", "import articles from XML", $this, ":", "FILE");
  10. }
  11. function about() {
  12. return array(1.0,
  13. "Imports and exports user data using neutral XML format",
  14. "fox");
  15. }
  16. function xml_import($args) {
  17. $filename = $args['xml_import'];
  18. if (!is_file($filename)) {
  19. print "error: input filename ($filename) doesn't exist.\n";
  20. return;
  21. }
  22. _debug("please enter your username:");
  23. $username = db_escape_string($this->link, trim(read_stdin()));
  24. _debug("importing $filename for user $username...\n");
  25. $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$username'");
  26. if (db_num_rows($result) == 0) {
  27. print "error: could not find user $username.\n";
  28. return;
  29. }
  30. $owner_uid = db_fetch_result($result, 0, "id");
  31. $this->perform_data_import($this->link, $filename, $owner_uid);
  32. }
  33. function save() {
  34. $example_value = db_escape_string($this->link, $_POST["example_value"]);
  35. echo "Value set to $example_value (not really)";
  36. }
  37. function get_prefs_js() {
  38. return file_get_contents(dirname(__FILE__) . "/import_export.js");
  39. }
  40. function hook_prefs_tab($args) {
  41. if ($args != "prefFeeds") return;
  42. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Import and export')."\">";
  43. print "<h3>" . __("Article archive") . "</h3>";
  44. print "<p>" . __("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances.") . "</p>";
  45. print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
  46. __('Export my data')."</button> ";
  47. print "<hr>";
  48. print "<iframe id=\"data_upload_iframe\"
  49. name=\"data_upload_iframe\" onload=\"dataImportComplete(this)\"
  50. style=\"width: 400px; height: 100px; display: none;\"></iframe>";
  51. print "<form name=\"import_form\" style='display : block' target=\"data_upload_iframe\"
  52. enctype=\"multipart/form-data\" method=\"POST\"
  53. action=\"backend.php\">
  54. <input id=\"export_file\" name=\"export_file\" type=\"file\">&nbsp;
  55. <input type=\"hidden\" name=\"op\" value=\"pluginhandler\">
  56. <input type=\"hidden\" name=\"plugin\" value=\"import_export\">
  57. <input type=\"hidden\" name=\"method\" value=\"dataimport\">
  58. <button dojoType=\"dijit.form.Button\" onclick=\"return importData();\" type=\"submit\">" .
  59. __('Import') . "</button>";
  60. print "</form>";
  61. print "</div>"; # pane
  62. }
  63. function csrf_ignore($method) {
  64. return in_array($method, array("exportget"));
  65. }
  66. function before($method) {
  67. return $_SESSION["uid"] != false;
  68. }
  69. function after() {
  70. return true;
  71. }
  72. function exportget() {
  73. $exportname = CACHE_DIR . "/export/" .
  74. sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
  75. if (file_exists($exportname)) {
  76. header("Content-type: text/xml");
  77. if (function_exists('gzencode')) {
  78. header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
  79. echo gzencode(file_get_contents($exportname));
  80. } else {
  81. header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
  82. echo file_get_contents($exportname);
  83. }
  84. } else {
  85. echo "File not found.";
  86. }
  87. }
  88. function exportrun() {
  89. $offset = (int) db_escape_string($this->link, $_REQUEST['offset']);
  90. $exported = 0;
  91. $limit = 250;
  92. if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
  93. $result = db_query($this->link, "SELECT
  94. ttrss_entries.guid,
  95. ttrss_entries.title,
  96. content,
  97. marked,
  98. published,
  99. score,
  100. note,
  101. link,
  102. tag_cache,
  103. label_cache,
  104. ttrss_feeds.title AS feed_title,
  105. ttrss_feeds.feed_url AS feed_url,
  106. ttrss_entries.updated
  107. FROM
  108. ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
  109. ttrss_entries
  110. WHERE
  111. (marked = true OR feed_id IS NULL) AND
  112. ref_id = ttrss_entries.id AND
  113. ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
  114. ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
  115. $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
  116. if ($offset == 0) {
  117. $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w");
  118. fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">");
  119. } else {
  120. $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a");
  121. }
  122. if ($fp) {
  123. while ($line = db_fetch_assoc($result)) {
  124. fputs($fp, "<article>");
  125. foreach ($line as $k => $v) {
  126. $v = str_replace("]]>", "]]]]><![CDATA[>", $v);
  127. fputs($fp, "<$k><![CDATA[$v]]></$k>");
  128. }
  129. fputs($fp, "</article>");
  130. }
  131. $exported = db_num_rows($result);
  132. if ($exported < $limit && $exported > 0) {
  133. fputs($fp, "</articles>");
  134. }
  135. fclose($fp);
  136. }
  137. }
  138. print json_encode(array("exported" => $exported));
  139. }
  140. function perform_data_import($link, $filename, $owner_uid) {
  141. $num_imported = 0;
  142. $num_processed = 0;
  143. $num_feeds_created = 0;
  144. $doc = @DOMDocument::load($filename);
  145. if (!$doc) {
  146. $contents = file_get_contents($filename);
  147. if ($contents) {
  148. $data = @gzuncompress($contents);
  149. }
  150. if (!$data) {
  151. $data = @gzdecode($contents);
  152. }
  153. if ($data)
  154. $doc = DOMDocument::loadXML($data);
  155. }
  156. if ($doc) {
  157. $xpath = new DOMXpath($doc);
  158. $container = $doc->firstChild;
  159. if ($container && $container->hasAttribute('schema-version')) {
  160. $schema_version = $container->getAttribute('schema-version');
  161. if ($schema_version != SCHEMA_VERSION) {
  162. print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
  163. return;
  164. }
  165. } else {
  166. print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
  167. return;
  168. }
  169. $articles = $xpath->query("//article");
  170. foreach ($articles as $article_node) {
  171. if ($article_node->childNodes) {
  172. $ref_id = 0;
  173. $article = array();
  174. foreach ($article_node->childNodes as $child) {
  175. if ($child->nodeName != 'label_cache')
  176. $article[$child->nodeName] = db_escape_string($this->link, $child->nodeValue);
  177. else
  178. $article[$child->nodeName] = $child->nodeValue;
  179. }
  180. //print_r($article);
  181. if ($article['guid']) {
  182. ++$num_processed;
  183. //db_query($link, "BEGIN");
  184. //print 'GUID:' . $article['guid'] . "\n";
  185. $result = db_query($link, "SELECT id FROM ttrss_entries
  186. WHERE guid = '".$article['guid']."'");
  187. if (db_num_rows($result) == 0) {
  188. $result = db_query($link,
  189. "INSERT INTO ttrss_entries
  190. (title,
  191. guid,
  192. link,
  193. updated,
  194. content,
  195. content_hash,
  196. no_orig_date,
  197. date_updated,
  198. date_entered,
  199. comments,
  200. num_comments,
  201. author)
  202. VALUES
  203. ('".$article['title']."',
  204. '".$article['guid']."',
  205. '".$article['link']."',
  206. '".$article['updated']."',
  207. '".$article['content']."',
  208. '".sha1($article['content'])."',
  209. false,
  210. NOW(),
  211. NOW(),
  212. '',
  213. '0',
  214. '')");
  215. $result = db_query($link, "SELECT id FROM ttrss_entries
  216. WHERE guid = '".$article['guid']."'");
  217. if (db_num_rows($result) != 0) {
  218. $ref_id = db_fetch_result($result, 0, "id");
  219. }
  220. } else {
  221. $ref_id = db_fetch_result($result, 0, "id");
  222. }
  223. //print "Got ref ID: $ref_id\n";
  224. if ($ref_id) {
  225. $feed_url = $article['feed_url'];
  226. $feed_title = $article['feed_title'];
  227. $feed = 'NULL';
  228. if ($feed_url && $feed_title) {
  229. $result = db_query($link, "SELECT id FROM ttrss_feeds
  230. WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
  231. if (db_num_rows($result) != 0) {
  232. $feed = db_fetch_result($result, 0, "id");
  233. } else {
  234. // try autocreating feed in Uncategorized...
  235. $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid,
  236. feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')");
  237. $result = db_query($link, "SELECT id FROM ttrss_feeds
  238. WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
  239. if (db_num_rows($result) != 0) {
  240. ++$num_feeds_created;
  241. $feed = db_fetch_result($result, 0, "id");
  242. }
  243. }
  244. }
  245. if ($feed != 'NULL')
  246. $feed_qpart = "feed_id = $feed";
  247. else
  248. $feed_qpart = "feed_id IS NULL";
  249. //print "$ref_id / $feed / " . $article['title'] . "\n";
  250. $result = db_query($link, "SELECT int_id FROM ttrss_user_entries
  251. WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart");
  252. if (db_num_rows($result) == 0) {
  253. $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked']));
  254. $published = bool_to_sql_bool(sql_bool_to_bool($article['published']));
  255. $score = (int) $article['score'];
  256. $tag_cache = $article['tag_cache'];
  257. $label_cache = db_escape_string($this->link, $article['label_cache']);
  258. $note = $article['note'];
  259. //print "Importing " . $article['title'] . "<br/>";
  260. ++$num_imported;
  261. $result = db_query($link,
  262. "INSERT INTO ttrss_user_entries
  263. (ref_id, owner_uid, feed_id, unread, last_read, marked,
  264. published, score, tag_cache, label_cache, uuid, note)
  265. VALUES ($ref_id, $owner_uid, $feed, false,
  266. NULL, $marked, $published, $score, '$tag_cache',
  267. '$label_cache', '', '$note')");
  268. $label_cache = json_decode($label_cache, true);
  269. if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
  270. foreach ($label_cache as $label) {
  271. label_create($link, $label[1],
  272. $label[2], $label[3], $owner_uid);
  273. label_add_article($link, $ref_id, $label[1], $owner_uid);
  274. }
  275. }
  276. //db_query($link, "COMMIT");
  277. }
  278. }
  279. }
  280. }
  281. }
  282. print "<p>" .
  283. __("Finished: ").
  284. vsprintf(ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
  285. vsprintf(ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
  286. vsprintf(ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
  287. "</p>";
  288. } else {
  289. print "<p>" . __("Could not load XML document.") . "</p>";
  290. }
  291. }
  292. function exportData() {
  293. print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>";
  294. print "<div align='center'>";
  295. print "<button dojoType=\"dijit.form.Button\"
  296. onclick=\"dijit.byId('dataExportDlg').prepare()\">".
  297. __('Prepare data')."</button>";
  298. print "<button dojoType=\"dijit.form.Button\"
  299. onclick=\"dijit.byId('dataExportDlg').hide()\">".
  300. __('Close this window')."</button>";
  301. print "</div>";
  302. }
  303. function dataImport() {
  304. header("Content-Type: text/html"); # required for iframe
  305. print "<div style='text-align : center'>";
  306. if ($_FILES['export_file']['error'] != 0) {
  307. print_error(T_sprintf("Upload failed with error code %d",
  308. $_FILES['export_file']['error']));
  309. return;
  310. }
  311. $tmp_file = false;
  312. if (is_uploaded_file($_FILES['export_file']['tmp_name'])) {
  313. $tmp_file = tempnam(CACHE_DIR . '/upload', 'export');
  314. $result = move_uploaded_file($_FILES['export_file']['tmp_name'],
  315. $tmp_file);
  316. if (!$result) {
  317. print_error(__("Unable to move uploaded file."));
  318. return;
  319. }
  320. } else {
  321. print_error(__('Error: please upload OPML file.'));
  322. return;
  323. }
  324. if (is_file($tmp_file)) {
  325. $this->perform_data_import($this->link, $tmp_file, $_SESSION['uid']);
  326. unlink($tmp_file);
  327. } else {
  328. print_error(__('No file uploaded.'));
  329. return;
  330. }
  331. print "<button dojoType=\"dijit.form.Button\"
  332. onclick=\"dijit.byId('dataImportDlg').hide()\">".
  333. __('Close this window')."</button>";
  334. print "</div>";
  335. }
  336. }
  337. ?>