/**
  * Test for PMA_convertString
  *
  * @return void
  * @test
  *
  * @group medium
  */
 public function testCharsetConversion()
 {
     $this->assertEquals('test', PMA_convertString('UTF-8', 'UTF-8', 'test'));
     // 6 represents an arbitrary value for testing the default case
     $GLOBALS['PMA_recoding_engine'] = 6;
     $this->assertEquals('test', PMA_convertString('UTF-8', 'flat', 'test'));
     // TODO: remove function_exists if recode_string exists on server
     if (@function_exists('recode_string')) {
         $GLOBALS['PMA_recoding_engine'] = PMA_CHARSET_RECODE;
         $this->assertEquals('Only That ecole & Can Be My Blame', PMA_convertString('UTF-8', 'flat', 'Only That école & Can Be My Blame'));
     }
     $GLOBALS['PMA_recoding_engine'] = PMA_CHARSET_ICONV;
     $GLOBALS['cfg']['IconvExtraParams'] = '//TRANSLIT';
     $this->assertEquals("This is the Euro symbol 'EUR'.", PMA_convertString('UTF-8', 'ISO-8859-1', "This is the Euro symbol '€'."));
 }
/**
 * Output handler for all exports, if needed buffering, it stores data into
 * $dump_buffer, otherwise it prints them out.
 *
 * @param string $line the insert statement
 *
 * @return bool Whether output succeeded
 */
function PMA_exportOutputHandler($line)
{
    global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
    // Kanji encoding convert feature
    if ($GLOBALS['output_kanji_conversion']) {
        $line = PMA_Kanji_strConv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
    }
    // If we have to buffer data, we will perform everything at once at the end
    if ($GLOBALS['buffer_needed']) {
        $dump_buffer .= $line;
        if ($GLOBALS['onfly_compression']) {
            $dump_buffer_len += mb_strlen($line);
            if ($dump_buffer_len > $GLOBALS['memory_limit']) {
                if ($GLOBALS['output_charset_conversion']) {
                    $dump_buffer = PMA_convertString('utf-8', $GLOBALS['charset'], $dump_buffer);
                }
                if ($GLOBALS['compression'] == 'gzip' && PMA_gzencodeNeeded()) {
                    // as a gzipped file
                    // without the optional parameter level because it bugs
                    $dump_buffer = gzencode($dump_buffer);
                }
                if ($GLOBALS['save_on_server']) {
                    $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
                    // Here, use strlen rather than mb_strlen to get the length
                    // in bytes to compare against the number of bytes written.
                    if ($write_result != strlen($dump_buffer)) {
                        $GLOBALS['message'] = PMA_Message::error(__('Insufficient space to save the file %s.'));
                        $GLOBALS['message']->addParam($save_filename);
                        return false;
                    }
                } else {
                    echo $dump_buffer;
                }
                $dump_buffer = '';
                $dump_buffer_len = 0;
            }
        } else {
            $time_now = time();
            if ($time_start >= $time_now + 30) {
                $time_start = $time_now;
                header('X-pmaPing: Pong');
            }
            // end if
        }
    } else {
        if ($GLOBALS['asfile']) {
            if ($GLOBALS['output_charset_conversion']) {
                $line = PMA_convertString('utf-8', $GLOBALS['charset'], $line);
            }
            if ($GLOBALS['save_on_server'] && mb_strlen($line) > 0) {
                $write_result = @fwrite($GLOBALS['file_handle'], $line);
                // Here, use strlen rather than mb_strlen to get the length
                // in bytes to compare against the number of bytes written.
                if (!$write_result || $write_result != strlen($line)) {
                    $GLOBALS['message'] = PMA_Message::error(__('Insufficient space to save the file %s.'));
                    $GLOBALS['message']->addParam($save_filename);
                    return false;
                }
                $time_now = time();
                if ($time_start >= $time_now + 30) {
                    $time_start = $time_now;
                    header('X-pmaPing: Pong');
                }
                // end if
            } else {
                // We export as file - output normally
                echo $line;
            }
        } else {
            // We export as html - replace special chars
            echo htmlspecialchars($line);
        }
    }
    return true;
}
Exemple #3
0
     } elseif ($export_type == 'database') {
         $active_page = 'db_export.php';
         include 'db_export.php';
     } else {
         $active_page = 'tbl_export.php';
         include 'tbl_export.php';
     }
     exit;
 }
 /**
  * Send the dump as a file...
  */
 if (!empty($asfile)) {
     // Convert the charset if required.
     if ($output_charset_conversion) {
         $dump_buffer = PMA_convertString('utf-8', $GLOBALS['charset_of_file'], $dump_buffer);
     }
     // Compression needed?
     if ($compression) {
         $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
     }
     /* If we saved on server, we have to close file now */
     if ($save_on_server) {
         $message = PMA_closeExportFile($file_handle, $dump_buffer, $save_filename);
         if ($export_type == 'server') {
             $active_page = 'server_export.php';
             include_once 'server_export.php';
         } elseif ($export_type == 'database') {
             $active_page = 'db_export.php';
             include_once 'db_export.php';
         } else {
 /**
  * converts charset of a mysql message, usually coming from mysql_error(),
  * into PMA charset, usually UTF-8
  * uses language to charset mapping from mysql/share/errmsg.txt
  * and charset names to ISO charset from information_schema.CHARACTER_SETS
  *
  * @param string $message the message
  *
  * @return string  $message
  */
 public function convertMessage($message)
 {
     // latin always last!
     $encodings = array('japanese' => 'EUC-JP', 'japanese-sjis' => 'Shift-JIS', 'korean' => 'EUC-KR', 'russian' => 'KOI8-R', 'ukrainian' => 'KOI8-U', 'greek' => 'ISO-8859-7', 'serbian' => 'CP1250', 'estonian' => 'ISO-8859-13', 'slovak' => 'ISO-8859-2', 'czech' => 'ISO-8859-2', 'hungarian' => 'ISO-8859-2', 'polish' => 'ISO-8859-2', 'romanian' => 'ISO-8859-2', 'spanish' => 'CP1252', 'swedish' => 'CP1252', 'italian' => 'CP1252', 'norwegian-ny' => 'CP1252', 'norwegian' => 'CP1252', 'portuguese' => 'CP1252', 'danish' => 'CP1252', 'dutch' => 'CP1252', 'english' => 'CP1252', 'french' => 'CP1252', 'german' => 'CP1252');
     $server_language = $this->fetchValue('SHOW VARIABLES LIKE \'language\';', 0, 1);
     if ($server_language) {
         $found = array();
         $match = preg_match('&(?:\\\\|\\/)([^\\\\\\/]*)(?:\\\\|\\/)$&i', $server_language, $found);
         if ($match) {
             $server_language = $found[1];
         }
     }
     if (!empty($server_language) && isset($encodings[$server_language])) {
         $encoding = $encodings[$server_language];
     } else {
         /* Fallback to CP1252 if we can not detect */
         $encoding = 'CP1252';
     }
     return PMA_convertString($encoding, 'utf-8', $message);
 }
 /**
  * converts charset of a mysql message, usually coming from mysql_error(),
  * into PMA charset, usually UTF-8
  * uses language to charset mapping from mysql/share/errmsg.txt
  * and charset names to ISO charset from information_schema.CHARACTER_SETS
  *
  * @param string $message the message
  *
  * @return string  $message
  */
 public function convertMessage($message)
 {
     // latin always last!
     // @todo some values are missing,
     // see https://mariadb.com/kb/en/mariadb/server-locale/
     $encodings = array('ja' => 'EUC-JP', 'ko' => 'EUC-KR', 'ru' => 'KOI8-R', 'uk' => 'KOI8-U', 'sr' => 'CP1250', 'et' => 'ISO-8859-13', 'sk' => 'ISO-8859-2', 'cz' => 'ISO-8859-2', 'hu' => 'ISO-8859-2', 'pl' => 'ISO-8859-2', 'ro' => 'ISO-8859-2', 'es' => 'CP1252', 'sv' => 'CP1252', 'it' => 'CP1252', 'no' => 'CP1252', 'pt' => 'CP1252', 'da' => 'CP1252', 'nl' => 'CP1252', 'en' => 'CP1252', 'fr' => 'CP1252', 'de' => 'CP1252');
     $server_language = PMA_Util::cacheGet('server_language', function () {
         return $GLOBALS['dbi']->fetchValue("SELECT @@lc_messages;");
     });
     if ($server_language) {
         $found = array();
         $match = preg_match('&([a-z][a-z])_&i', $server_language, $found);
         if ($match) {
             $server_language = $found[1];
         }
     }
     if (!empty($server_language) && isset($encodings[$server_language])) {
         $encoding = $encodings[$server_language];
     } else {
         /* Fallback to CP1252 if we can not detect */
         $encoding = 'CP1252';
     }
     return PMA_convertString($encoding, 'utf-8', $message);
 }
/**
 * Returns next part of imported file/buffer
 *
 * @param int $size size of buffer to read
 *                  (this is maximal size function will return)
 *
 * @return string part of file/buffer
 * @access public
 */
function PMA_importGetNextChunk($size = 32768)
{
    global $compression, $import_handle, $charset_conversion, $charset_of_file, $read_multiply;
    // Add some progression while reading large amount of data
    if ($read_multiply <= 8) {
        $size *= $read_multiply;
    } else {
        $size *= 8;
    }
    $read_multiply++;
    // We can not read too much
    if ($size > $GLOBALS['read_limit']) {
        $size = $GLOBALS['read_limit'];
    }
    if (PMA_checkTimeout()) {
        return false;
    }
    if ($GLOBALS['finished']) {
        return true;
    }
    if ($GLOBALS['import_file'] == 'none') {
        // Well this is not yet supported and tested,
        // but should return content of textarea
        if (mb_strlen($GLOBALS['import_text']) < $size) {
            $GLOBALS['finished'] = true;
            return $GLOBALS['import_text'];
        } else {
            $r = mb_substr($GLOBALS['import_text'], 0, $size);
            $GLOBALS['offset'] += $size;
            $GLOBALS['import_text'] = mb_substr($GLOBALS['import_text'], $size);
            return $r;
        }
    }
    switch ($compression) {
        case 'application/bzip2':
            $result = bzread($import_handle, $size);
            $GLOBALS['finished'] = feof($import_handle);
            break;
        case 'application/gzip':
            $result = gzread($import_handle, $size);
            $GLOBALS['finished'] = feof($import_handle);
            break;
        case 'application/zip':
            $result = mb_substr($GLOBALS['import_text'], 0, $size);
            $GLOBALS['import_text'] = mb_substr($GLOBALS['import_text'], $size);
            $GLOBALS['finished'] = empty($GLOBALS['import_text']);
            break;
        case 'none':
            $result = fread($import_handle, $size);
            $GLOBALS['finished'] = feof($import_handle);
            break;
    }
    $GLOBALS['offset'] += $size;
    if ($charset_conversion) {
        return PMA_convertString($charset_of_file, 'utf-8', $result);
    }
    /**
     * Skip possible byte order marks (I do not think we need more
     * charsets, but feel free to add more, you can use wikipedia for
     * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
     *
     * @todo BOM could be used for charset autodetection
     */
    if ($GLOBALS['offset'] == $size) {
        // UTF-8
        if (strncmp($result, "", 3) == 0) {
            $result = mb_substr($result, 3);
            // UTF-16 BE, LE
        } elseif (strncmp($result, "þÿ", 2) == 0 || strncmp($result, "ÿþ", 2) == 0) {
            $result = mb_substr($result, 2);
        }
    }
    return $result;
}