/**
  * Test for PMA_Kanji_fileConv
  *
  * @return void
  * @test
  */
 public function testFileConv()
 {
     $file_str = "教育漢字常用漢字";
     $filename = 'test.kanji';
     $file = fopen($filename, 'w');
     fputs($file, $file_str);
     fclose($file);
     $GLOBALS['kanji_encoding_list'] = 'ASCII,EUC-JP,SJIS,JIS';
     $result = PMA_Kanji_fileConv($filename, 'JIS', 'kana');
     $string = file_get_contents($result);
     PMA_Kanji_changeOrder();
     $expected = PMA_Kanji_strConv($file_str, 'JIS', 'kana');
     PMA_Kanji_changeOrder();
     $this->assertEquals($string, $expected);
     unlink($result);
 }
Example #2
0
/**
 * 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;
}
/**
 * Kanji file encoding convert
 * 2002/1/4 by Y.Kawada
 *
 * @param string $file the name of the file to convert
 * @param string $enc  the destination encoding code
 * @param string $kana set 'kana' convert to JIS-X208-kana
 *
 * @return string   the name of the converted file
 */
function PMA_Kanji_fileConv($file, $enc, $kana)
{
    if ($enc == '' && $kana == '') {
        return $file;
    }
    $tmpfname = tempnam('', $enc);
    $fpd = fopen($tmpfname, 'wb');
    $fps = fopen($file, 'r');
    PMA_Kanji_changeOrder();
    while (!feof($fps)) {
        $line = fgets($fps, 4096);
        $dist = PMA_Kanji_strConv($line, $enc, $kana);
        fputs($fpd, $dist);
    }
    // end while
    PMA_Kanji_changeOrder();
    fclose($fps);
    fclose($fpd);
    unlink($file);
    return $tmpfname;
}