]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/import_export/init.php
plugins/import_export: use PDO
[tt-rss.git] / plugins / import_export / init.php
old mode 100644 (file)
new mode 100755 (executable)
index a01a612..e7e036f
@@ -1,12 +1,10 @@
 <?php
 class Import_Export extends Plugin implements IHandler {
-
-       private $link;
        private $host;
 
        function init($host) {
-               $this->link = $host->get_link();
                $this->host = $host;
+               $this->pdo = Db::pdo();
 
                $host->add_hook($host::HOOK_PREFS_TAB, $this);
                $host->add_command("xml-import", "import articles from XML", $this, ":", "FILE");
@@ -18,6 +16,10 @@ class Import_Export extends Plugin implements IHandler {
                        "fox");
        }
 
+       private function bool_to_sql_bool($s) {
+               return $s ? 'true' : 'false';
+       }
+
        function xml_import($args) {
 
                $filename = $args['xml_import'];
@@ -29,24 +31,25 @@ class Import_Export extends Plugin implements IHandler {
 
                _debug("please enter your username:");
 
-               $username = db_escape_string($this->link, trim(read_stdin()));
+               $username = db_escape_string(trim(read_stdin()));
 
                _debug("importing $filename for user $username...\n");
 
-               $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$username'");
+               $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
+               $sth->execute([$username]);
 
-               if (db_num_rows($result) == 0) {
+               if ($sth->rowCount() == 0) {
                        print "error: could not find user $username.\n";
                        return;
                }
 
-               $owner_uid = db_fetch_result($result, 0, "id");
+               $owner_uid = $sth->fetchColumn(0);
 
-               $this->perform_data_import($this->link, $filename, $owner_uid);
+               $this->perform_data_import($filename, $owner_uid);
        }
 
        function save() {
-               $example_value = db_escape_string($this->link, $_POST["example_value"]);
+               $example_value = db_escape_string($_POST["example_value"]);
 
                echo "Value set to $example_value (not really)";
        }
@@ -60,9 +63,9 @@ class Import_Export extends Plugin implements IHandler {
 
                print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Import and export')."\">";
 
-               print "<h3>" . __("Article archive") . "</h3>";
+               print_notice(__("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version."));
 
-               print "<p>" . __("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances.") . "</p>";
+               print "<p>";
 
                print "<button dojoType=\"dijit.form.Button\" onclick=\"return exportData()\">".
                        __('Export my data')."</button> ";
@@ -85,6 +88,8 @@ class Import_Export extends Plugin implements IHandler {
 
                print "</form>";
 
+               print "</p>";
+
                print "</div>"; # pane
        }
 
@@ -92,6 +97,9 @@ class Import_Export extends Plugin implements IHandler {
                return in_array($method, array("exportget"));
        }
 
+       /**
+        * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+        */
        function before($method) {
                return $_SESSION["uid"] != false;
        }
@@ -100,6 +108,9 @@ class Import_Export extends Plugin implements IHandler {
                return true;
        }
 
+       /**
+        * @SuppressWarnings(unused)
+        */
        function exportget() {
                $exportname = CACHE_DIR . "/export/" .
                        sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
@@ -107,11 +118,13 @@ class Import_Export extends Plugin implements IHandler {
                if (file_exists($exportname)) {
                        header("Content-type: text/xml");
 
+                       $timestamp_suffix = date("Y-m-d", filemtime($exportname));
+
                        if (function_exists('gzencode')) {
-                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml.gz");
+                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz");
                                echo gzencode(file_get_contents($exportname));
                        } else {
-                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported.xml");
+                               header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml");
                                echo file_get_contents($exportname);
                        }
                } else {
@@ -120,12 +133,12 @@ class Import_Export extends Plugin implements IHandler {
        }
 
        function exportrun() {
-               $offset = (int) db_escape_string($this->link, $_REQUEST['offset']);
+               $offset = (int) $_REQUEST['offset'];
                $exported = 0;
                $limit = 250;
 
                if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
-                       $result = db_query($this->link, "SELECT
+                       $sth = $this->pdo->prepare("SELECT
                                        ttrss_entries.guid,
                                        ttrss_entries.title,
                                        content,
@@ -145,8 +158,9 @@ class Import_Export extends Plugin implements IHandler {
                                WHERE
                                        (marked = true OR feed_id IS NULL) AND
                                        ref_id = ttrss_entries.id AND
-                                       ttrss_user_entries.owner_uid = " . $_SESSION['uid'] . "
-                               ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
+                                       ttrss_user_entries.owner_uid = ?
+                               ORDER BY ttrss_entries.id LIMIT ? OFFSET ?");
+                       $sth->execute([$_SESSION['uid'], $limit, $offset]);
 
                        $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
 
@@ -159,7 +173,7 @@ class Import_Export extends Plugin implements IHandler {
 
                        if ($fp) {
 
-                               while ($line = db_fetch_assoc($result)) {
+                               while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
                                        fputs($fp, "<article>");
 
                                        foreach ($line as $k => $v) {
@@ -170,7 +184,7 @@ class Import_Export extends Plugin implements IHandler {
                                        fputs($fp, "</article>");
                                }
 
-                               $exported = db_num_rows($result);
+                               $exported = $sth->rowCount();
 
                                if ($exported < $limit && $exported > 0) {
                                        fputs($fp, "</articles>");
@@ -184,12 +198,14 @@ class Import_Export extends Plugin implements IHandler {
                print json_encode(array("exported" => $exported));
        }
 
-       function perform_data_import($link, $filename, $owner_uid) {
+       function perform_data_import($filename, $owner_uid) {
 
                $num_imported = 0;
                $num_processed = 0;
                $num_feeds_created = 0;
 
+               libxml_disable_entity_loader(false);
+
                $doc = @DOMDocument::load($filename);
 
                if (!$doc) {
@@ -207,6 +223,8 @@ class Import_Export extends Plugin implements IHandler {
                                $doc = DOMDocument::loadXML($data);
                }
 
+               libxml_disable_entity_loader(true);
+
                if ($doc) {
 
                        $xpath = new DOMXpath($doc);
@@ -236,10 +254,13 @@ class Import_Export extends Plugin implements IHandler {
                                        $article = array();
 
                                        foreach ($article_node->childNodes as $child) {
-                                               if ($child->nodeName != 'label_cache')
-                                                       $article[$child->nodeName] = db_escape_string($this->link, $child->nodeValue);
-                                               else
+                                               if ($child->nodeName == 'content') {
+                                                       $article[$child->nodeName] = db_escape_string($child->nodeValue, false);
+                                               } else if ($child->nodeName == 'label_cache') {
                                                        $article[$child->nodeName] = $child->nodeValue;
+                                               } else {
+                                                       $article[$child->nodeName] = db_escape_string($child->nodeValue);
+                                               }
                                        }
 
                                        //print_r($article);
@@ -248,16 +269,17 @@ class Import_Export extends Plugin implements IHandler {
 
                                                ++$num_processed;
 
-                                               //db_query($link, "BEGIN");
+                                               //db_query("BEGIN");
 
                                                //print 'GUID:' . $article['guid'] . "\n";
 
-                                               $result = db_query($link, "SELECT id FROM ttrss_entries
-                                                       WHERE guid = '".$article['guid']."'");
+                                               $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries
+                                                       WHERE guid = ?");
+                                               $sth->execute([$article['guid']]);
 
-                                               if (db_num_rows($result) == 0) {
+                                               if ($sth->rowCount() == 0) {
 
-                                                       $result = db_query($link,
+                                                       $sth = $this->pdo->prepare(
                                                                "INSERT INTO ttrss_entries
                                                                        (title,
                                                                        guid,
@@ -272,28 +294,37 @@ class Import_Export extends Plugin implements IHandler {
                                                                        num_comments,
                                                                        author)
                                                                VALUES
-                                                                       ('".$article['title']."',
-                                                                       '".$article['guid']."',
-                                                                       '".$article['link']."',
-                                                                       '".$article['updated']."',
-                                                                       '".$article['content']."',
-                                                                       '".sha1($article['content'])."',
+                                                                       (?,
+                                                                       ?,
+                                                                       ?,
+                                                                       ?,
+                                                                       ?,
+                                                                       ?,
                                                                        false,
                                                                        NOW(),
                                                                        NOW(),
                                                                        '',
                                                                        '0',
                                                                        '')");
-
-                                                       $result = db_query($link, "SELECT id FROM ttrss_entries
-                                                               WHERE guid = '".$article['guid']."'");
-
-                                                       if (db_num_rows($result) != 0) {
-                                                               $ref_id = db_fetch_result($result, 0, "id");
+                                                       $sth->execute([
+                                                               $article['title'],
+                                                               $article['guid'],
+                                                               $article['link'],
+                                                               $article['updated'],
+                                                               $article['content'],
+                                                               sha1($article['content'])
+                                                       ]);
+
+                                                       $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries
+                                                               WHERE guid = ?");
+                                                       $sth->execute([$article['guid']]);
+
+                                                       if ($sth->rowCount() != 0) {
+                                                               $ref_id = $sth->fetchColumn(0);
                                                        }
 
                                                } else {
-                                                       $ref_id = db_fetch_result($result, 0, "id");
+                                                       $ref_id = $sth->fetchColumn(0);
                                                }
 
                                                //print "Got ref ID: $ref_id\n";
@@ -306,24 +337,27 @@ class Import_Export extends Plugin implements IHandler {
                                                        $feed = 'NULL';
 
                                                        if ($feed_url && $feed_title) {
-                                                               $result = db_query($link, "SELECT id FROM ttrss_feeds
-                                                                       WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
+                                                               $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
+                                                                       WHERE feed_url = ? AND owner_uid = ?");
+                                                               $sth->execute([$feed_url, $owner_uid]);
 
-                                                               if (db_num_rows($result) != 0) {
-                                                                       $feed = db_fetch_result($result, 0, "id");
+                                                               if ($sth->rowCount() != 0) {
+                                                                       $feed = $sth->fetchColumn(0);
                                                                } else {
                                                                        // try autocreating feed in Uncategorized...
 
-                                                                       $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid,
-                                                                               feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')");
+                                                                       $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds (owner_uid,
+                                                                               feed_url, title) VALUES (?, ?, ?)");
+                                                                       $sth->execute([$owner_uid, $feed_url, $feed_title]);
 
-                                                                       $result = db_query($link, "SELECT id FROM ttrss_feeds
-                                                                               WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
+                                                                       $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
+                                                                               WHERE feed_url = ? AND owner_uid = ?");
+                                                                       $sth->execute([$feed_url, $owner_uid]);
 
-                                                                       if (db_num_rows($result) != 0) {
+                                                                       if ($sth->rowCount() != 0) {
                                                                                ++$num_feeds_created;
 
-                                                                               $feed = db_fetch_result($result, 0, "id");
+                                                                               $feed = $sth->fetchColumn(0);
                                                                        }
                                                                }
                                                        }
@@ -335,45 +369,46 @@ class Import_Export extends Plugin implements IHandler {
 
                                                        //print "$ref_id / $feed / " . $article['title'] . "\n";
 
-                                                       $result = db_query($link, "SELECT int_id FROM ttrss_user_entries
-                                                               WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart");
+                                                       $sth = $this->pdo->prepare("SELECT int_id FROM ttrss_user_entries
+                                                               WHERE ref_id = ? AND owner_uid = ? AND ?");
+                                                       $sth->execute([$ref_id, $owner_uid, $feed_qpart]);
 
-                                                       if (db_num_rows($result) == 0) {
+                                                       if ($sth->rowCount() == 0) {
 
-                                                               $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked']));
-                                                               $published = bool_to_sql_bool(sql_bool_to_bool($article['published']));
+                                                               $marked = $this->bool_to_sql_bool(sql_bool_to_bool($article['marked']));
+                                                               $published = $this->bool_to_sql_bool(sql_bool_to_bool($article['published']));
                                                                $score = (int) $article['score'];
 
                                                                $tag_cache = $article['tag_cache'];
-                                                               $label_cache = db_escape_string($this->link, $article['label_cache']);
                                                                $note = $article['note'];
 
                                                                //print "Importing " . $article['title'] . "<br/>";
 
                                                                ++$num_imported;
 
-                                                               $result = db_query($link,
+                                                               $sth = $this->pdo->prepare(
                                                                        "INSERT INTO ttrss_user_entries
                                                                        (ref_id, owner_uid, feed_id, unread, last_read, marked,
                                                                                published, score, tag_cache, label_cache, uuid, note)
-                                                                       VALUES ($ref_id, $owner_uid, $feed, false,
-                                                                               NULL, $marked, $published, $score, '$tag_cache',
-                                                                                       '$label_cache', '', '$note')");
+                                                                       VALUES (?, ?, ?, false,
+                                                                               NULL, ?, ?, ?, ?,
+                                                                                       '', '', ?)");
+                                                               $sth->execute([$ref_id, $owner_uid, $feed, $marked, $published, $score, $tag_cache, $note]);
 
-                                                               $label_cache = json_decode($label_cache, true);
+                                                               $label_cache = json_decode($article['label_cache'], true);
 
                                                                if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
                                                                        foreach ($label_cache as $label) {
 
-                                                                               label_create($link, $label[1],
+                                                                               Labels::create($label[1],
                                                                                        $label[2], $label[3], $owner_uid);
 
-                                                                               label_add_article($link, $ref_id, $label[1], $owner_uid);
+                                                                               Labels::add_article($ref_id, $label[1], $owner_uid);
 
                                                                        }
                                                                }
 
-                                                               //db_query($link, "COMMIT");
+                                                               //db_query("COMMIT");
                                                        }
                                                }
                                        }
@@ -382,9 +417,9 @@ class Import_Export extends Plugin implements IHandler {
 
                        print "<p>" .
                                __("Finished: ").
-                               vsprintf(ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
-                               vsprintf(ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
-                               vsprintf(ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
+                               vsprintf(_ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
+                               vsprintf(_ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
+                               vsprintf(_ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
                                        "</p>";
 
                } else {
@@ -418,34 +453,35 @@ class Import_Export extends Plugin implements IHandler {
                print "<div style='text-align : center'>";
 
                if ($_FILES['export_file']['error'] != 0) {
-                       print_error(T_sprintf("Upload failed with error code %d",
-                               $_FILES['export_file']['error']));
-                       return;
-               }
+                       print_error(T_sprintf("Upload failed with error code %d (%s)",
+                               $_FILES['export_file']['error'],
+                               get_upload_error_message($_FILES['export_file']['error'])));
+               } else {
 
-               $tmp_file = false;
+                       $tmp_file = false;
 
-               if (is_uploaded_file($_FILES['export_file']['tmp_name'])) {
-                       $tmp_file = tempnam(CACHE_DIR . '/upload', 'export');
+                       if (is_uploaded_file($_FILES['export_file']['tmp_name'])) {
+                               $tmp_file = tempnam(CACHE_DIR . '/upload', 'export');
 
-                       $result = move_uploaded_file($_FILES['export_file']['tmp_name'],
-                               $tmp_file);
+                               $result = move_uploaded_file($_FILES['export_file']['tmp_name'],
+                                       $tmp_file);
 
-                       if (!$result) {
-                               print_error(__("Unable to move uploaded file."));
+                               if (!$result) {
+                                       print_error(__("Unable to move uploaded file."));
+                                       return;
+                               }
+                       } else {
+                               print_error(__('Error: please upload OPML file.'));
                                return;
                        }
-               } else {
-                       print_error(__('Error: please upload OPML file.'));
-                       return;
-               }
 
-               if (is_file($tmp_file)) {
-                       $this->perform_data_import($this->link, $tmp_file, $_SESSION['uid']);
-                       unlink($tmp_file);
-               } else {
-                       print_error(__('No file uploaded.'));
-                       return;
+                       if (is_file($tmp_file)) {
+                               $this->perform_data_import($tmp_file, $_SESSION['uid']);
+                               unlink($tmp_file);
+                       } else {
+                               print_error(__('No file uploaded.'));
+                               return;
+                       }
                }
 
                print "<button dojoType=\"dijit.form.Button\"
@@ -456,6 +492,8 @@ class Import_Export extends Plugin implements IHandler {
 
        }
 
+       function api_version() {
+               return 2;
+       }
 
 }
-?>