init.php 11 KB

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