function importDatabase($importDatabase)
 {
     global $serendipity;
     set_time_limit(0);
     serendipity_db_begin_transaction();
     if ($this->get_config('dbversion', 2) == 3 && ($serendipity['dbType'] == 'mysql' || $serendipity['dbType'] == 'mysqli')) {
         #now there is a primary key we can use
         foreach ($importDatabase as $importToken) {
             $token = $importToken[0];
             $ham = $importToken[1];
             $spam = $importToken[2];
             $type = $importToken[3];
             $sql = "INSERT INTO\n                            {$serendipity['dbPrefix']}spamblock_bayes\n                                (token, ham, spam, type)\n                        VALUES\n                            ('{$token}', {$ham}, {$spam}, '{$type}')\n                        ON DUPLICATE KEY\n                            UPDATE\n                                ham = ham + VALUES(ham),\n                                spam = spam + VALUES(spam);";
             serendipity_db_query($sql);
             $result = mysql_error();
             if ($result != "") {
                 serendipity_db_end_transaction(false);
                 return $result;
             }
             if ($ham > 0) {
                 $this->set_config("{$type}_ham", $this->get_config("{$type}_ham", 0) + 1);
             }
             if ($spam > 0) {
                 $this->set_config("{$type}_spam", $this->get_config("{$type}_spam", 0) + 1);
             }
         }
     } else {
         if ($serendipity['dbType'] == 'sqlite') {
             foreach ($importDatabase as $importToken) {
                 $token = $importToken[0];
                 $ham = $importToken[1];
                 $spam = $importToken[2];
                 $type = $importToken[3];
                 $sql = "INSERT OR IGNORE INTO\n                            {$serendipity['dbPrefix']}spamblock_bayes\n                                (token, ham, spam, type)\n                        VALUES\n                            ('{$token}', 0, 0, '{$type}');";
                 serendipity_db_query($sql);
                 $sql = "UPDATE\n                            {$serendipity['dbPrefix']}spamblock_bayes\n                        SET\n                            ham = ham + {$ham}, spam = spam + {$spam}\n                        WHERE\n                            token = '{$token}' AND type = '{$type}'";
                 serendipity_db_query($sql);
                 if ($ham > 0) {
                     $this->set_config("{$type}_ham", $this->get_config("{$type}_ham", 0) + 1);
                 }
                 if ($spam > 0) {
                     $this->set_config("{$type}_spam", $this->get_config("{$type}_spam", 0) + 1);
                 }
             }
         } else {
             foreach ($importDatabase as $importToken) {
                 $token = $importToken[0];
                 $ham = $importToken[1];
                 $spam = $importToken[2];
                 $type = $importToken[3];
                 $sql = "SELECT\n                            token\n                        FROM\n                            {$serendipity['dbPrefix']}spamblock_bayes\n                        WHERE\n                             token = '{$token}' AND type = '{$type}'";
                 $tester = serendipity_db_query($sql);
                 if (empty($tester[0])) {
                     $sql = "INSERT INTO\n                            {$serendipity['dbPrefix']}spamblock_bayes\n                                (token, ham, spam, type)\n                        VALUES('{$token}', {$ham}, {$spam}, '{$type}')";
                 } else {
                     $sql = "UPDATE {$serendipity['dbPrefix']}spamblock_bayes\n                        SET\n                            ham = ham + {$ham},\n                            spam = spam + {$spam}\n                        WHERE token = '{$token}' AND type = '{$type}'";
                 }
                 serendipity_db_query($sql);
                 #NOTE: We do this wrongly, but as good as possible (really?).
                 #      The config is supposed to store the amount of
                 #      ham/spam-comments, not a guess of that.
                 if ($ham > 0) {
                     $this->set_config("{$type}_ham", $this->get_config("{$type}_ham", 0) + 1);
                 }
                 if ($spam > 0) {
                     $this->set_config("{$type}_spam", $this->get_config("{$type}_spam", 0) + 1);
                 }
             }
         }
     }
     serendipity_db_end_transaction(true);
     return true;
 }
 function import()
 {
     global $serendipity;
     $force = $this->data['mt_force'] == 'true';
     if ($this->data['autodiscovery'] == 'false') {
         $serendipity['noautodiscovery'] = 1;
     }
     // Rewritten to parse the file line by line. Can save quite some
     // memory on large blogs
     //$contents   = file_get_contents($_FILES['serendipity']['tmp_name']['import']['mt_dat']);
     $this->categories = serendipity_fetchCategories();
     $tasks = array();
     $entries = array();
     if (empty($_FILES['serendipity']['tmp_name']['import']['mt_dat'])) {
         $fh = fopen('/tmp/mt.dat', 'r');
     } else {
         $fh = fopen($_FILES['serendipity']['tmp_name']['import']['mt_dat'], 'r');
     }
     $entry = array();
     $el = "";
     $c_el = "";
     $skip = false;
     $is_comment = false;
     $is_trackback = false;
     $nofetch = false;
     while (!feof($fh)) {
         if ($nofetch === false) {
             $this->debug('Next line');
             $line = $this->decode(fgets($fh, 8192));
         } else {
             $this->debug('NO Next line');
             // Keep line from previous run.
             $nofetch = false;
         }
         if ($is_comment || $is_trackback) {
             $this->debug("COMMENT/TRACKBACK mode is active.");
             if (preg_match('/^--------/', $line)) {
                 $this->debug("Next full section requested.");
                 $is_comment = $is_trackback = false;
             } elseif (preg_match('/^-----/', $line)) {
                 $this->debug("Next partial section requested.");
                 if ($is_trackback) {
                     $this->debug("Parsing trackback.");
                     $entry['s9y_comments'][] = $this->doCommentWork($comment, $tasks, 'TRACKBACK');
                 } elseif ($is_comment) {
                     $this->debug("Parsing comment.");
                     $entry['s9y_comments'][] = $this->doCommentWork($comment, $tasks, 'NORMAL');
                 }
                 $el = $c_el = "";
             }
         }
         if ($skip && !preg_match('/^--------/', $line)) {
             $this->debug("No next section match, and skip is activated. Skipping '{$line}'");
             continue;
         }
         if (preg_match('/^--------/', $line)) {
             // We found the end marker of the current entry. Add to entries-Array
             $this->debug("End marker found. Parsing full entry.");
             $entries[] = $this->doEntryWork($entry, $tasks);
             $entry = array();
             $el = "";
             $c_el = "";
             $skip = false;
             $is_comment = false;
             $is_trackback = false;
         } elseif (preg_match('/^-----/', $line)) {
             $this->debug("New section match. Current EL: {$el}");
             unset($el);
             # DEBUG!
             if (empty($el)) {
                 $line = $this->decode(fgets($fh, 8192));
                 $this->debug("Inspecting next line: {$line}");
                 $tline = trim($line);
                 while (($is_comment || $is_trackback) && empty($tline)) {
                     $line = $this->decode(fgets($fh, 8192));
                     $tline = trim($line);
                     $this->debug("Continuing inspecting next line: {$line}");
                 }
                 if (preg_match('/^--------/', $line)) {
                     $this->debug('Next line is new element. End marker found. Parsing full entry.');
                     $entries[] = $this->doEntryWork($entry, $tasks);
                     $entry = array();
                     $el = "";
                     $c_el = "";
                     $skip = false;
                     $is_comment = false;
                     $is_trackback = false;
                 } elseif (preg_match('/^([A-Z\\s]+):/', $line, $matches)) {
                     $this->debug("Match result: {$matches['1']}");
                     if ($matches[1] == 'COMMENT') {
                         $this->debug("Marking COMMENT.");
                         $is_comment = true;
                         $is_trackback = false;
                         $comment = array();
                         $skip = false;
                     } elseif ($matches[1] == 'PING') {
                         $this->debug("Marking TRACKBACK");
                         $is_comment = false;
                         $is_trackback = true;
                         $comment = array();
                         $skip = false;
                     }
                     $this->debug("Setting EL to {$matches[1]}");
                     $el = $matches[1];
                     $c_el = "";
                 } else {
                     $this->debug("Could not parse next line. Keeping it for next cycle.");
                     $nofetch = true;
                 }
             } else {
                 $this->debug("Resetting EL to an empty string");
                 $el = $c_el = "";
             }
         } elseif (empty($el)) {
             $this->debug("EL is empty. Line is '{$line}'");
             $content = "";
             if (preg_match('/^([A-Z\\s]+):\\s+(.*)$/s', $line, $matches)) {
                 $this->debug("Section match {$matches[1]} found, input: {$matches[2]}");
                 $c_el = $matches[1];
                 $content = $matches[2];
             } elseif (!empty($c_el)) {
                 $this->debug("Still in subsection of previous run: {$c_el}.");
                 $content = trim($line);
             }
             if (!empty($content)) {
                 if ($is_comment || $is_trackback) {
                     $this->debug("Appending to comments: {$line}");
                     $comment[$c_el] = $content;
                 } else {
                     $this->debug("Appending to entry: {$line}");
                     if (isset($entry[$c_el])) {
                         $entry[$c_el] .= "" . $content;
                     } else {
                         $entry[$c_el] = $content;
                     }
                 }
             }
         } elseif ($is_comment || $is_trackback) {
             $this->debug("Appending Line in current Element {$el} to comments: {$line}");
             $comment[$el] .= $line;
         } else {
             $this->debug("Appending Line in current Element {$el} to entries: {$line}");
             $entry[$el] .= $line;
         }
     }
     fclose($fh);
     if (!sizeof($tasks) || $force == true) {
         serendipity_db_begin_transaction();
         foreach ($entries as $entry) {
             #echo '<pre>' . printR_($entry, true) . '</pre><br />';
             #continue;
             if (empty($entry['authorid'])) {
                 $entry['authorid'] = $serendipity['authorid'];
                 $entry['author'] = $serendipity['realname'];
             }
             if (!isset($entry['isdraft'])) {
                 $entry['isdraft'] = 'false';
             }
             if (!isset($entry['allow_comments'])) {
                 $entry['allow_comments'] = 'true';
             }
             $comments = $entry['s9y_comments'];
             $entryprops = $entry['props'];
             unset($entry['props']);
             unset($entry['s9y_comments']);
             if (!is_int($r = serendipity_updertEntry($entry))) {
                 echo '<div class="serendipityAdminMsgError"><img style="width: 22px; height: 22px; border: 0px; padding-right: 4px; vertical-align: middle" src="' . serendipity_getTemplateFile('admin/img/admin_msg_error.png') . '" alt="" />' . $r . '</div>';
             } else {
                 $this->debug('Saved entry ' . $r . ' (' . $entry['title'] . ')');
                 $entry['id'] = $r;
                 foreach ((array) $comments as $comment) {
                     $comment['entry_id'] = $r;
                     if ($rc = serendipity_db_insert('comments', $comment)) {
                         $cid = serendipity_db_insert_id('comments', 'id');
                         serendipity_approveComment($cid, $entry['id'], true);
                     } else {
                         echo '<div class="serendipityAdminMsgError"><img style="width: 22px; height: 22px; border: 0px; padding-right: 4px; vertical-align: middle" src="' . serendipity_getTemplateFile('admin/img/admin_msg_error.png') . '" alt="" />' . $rc . '</div>';
                     }
                 }
                 // Let the plugins do some additional stuff. Here it's used with
                 // event_entryproperties in mind to setup the nl2br-stuff
                 serendipity_plugin_api::hook_event('backend_import_entry', $entry, $entryprops);
             }
         }
         serendipity_db_end_transaction(true);
         return true;
     } else {
         return '<ul><li>' . implode('</li><li>', array_unique($tasks)) . '</li></ul>';
     }
 }