init.php 12 KB

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