Example #1
0
/**
 * Reads (and decompresses) a (compressed) file into a string
 *
 * Origine : fonction provenant de PhpMyAdmin version 2.6.0-pl1
 * Licence : GNU
 * Auteurs : voir le fichier Documentation.txt ou Documentation.html de PhpMyAdmin.
 *
 * @param   string   the path to the file
 * @param   string   the MIME type of the file, if empty MIME type is autodetected
 *
 * @global  array    the phpMyAdmin configuration
 *
 * @return  string   the content of the file or
 *          boolean  FALSE in case of an error.
 */
function PMA_readFile($path, $mime = '', $dist = 0)
{
    global $cfg;
    if (!$dist) {
        if (!file_exists($path)) {
            return FALSE;
        }
    }
    switch ($mime) {
        case '':
            return PMA_readFile($path, 'text/plain', $dist);
        case 'text/plain':
            $content = '';
            $file = @fopen($path, 'rb');
            if (!$file) {
                return FALSE;
            }
            if (!$dist) {
                $content = fread($file, filesize($path));
            } else {
                while (!feof($file)) {
                    $content .= fread($file, 8192);
                }
            }
            fclose($file);
            break;
        default:
            return FALSE;
    }
    return $content;
}
Example #2
0
                $sql_query = PMA_readFile($sql_file, $sql_file_compression);
                if ($sql_query == FALSE) {
                    $message = $strFileCouldNotBeRead;
                }
            } else {
                $sql_file_new = $tmp_subdir . basename($sql_file);
                move_uploaded_file($sql_file, $sql_file_new);
                $sql_query = PMA_readFile($sql_file_new, $sql_file_compression);
                if ($sql_query == FALSE) {
                    $message = $strFileCouldNotBeRead;
                }
                unlink($sql_file_new);
            }
        } else {
            // read from the normal upload dir
            $sql_query = PMA_readFile($sql_file, $sql_file_compression);
            if ($sql_query == FALSE) {
                $message = $strFileCouldNotBeRead;
            }
        }
        // Convert the file's charset if necessary
        if ($cfg['AllowAnywhereRecoding'] && $allow_recoding && isset($charset_of_file) && $charset_of_file != $charset) {
            $sql_query = PMA_convert_string($charset_of_file, $charset, $sql_query);
        }
    }
    // end uploaded file stuff
}
// Kanji convert SQL textfile 2002/1/4 by Y.Kawada
if (@function_exists('PMA_kanji_str_conv')) {
    $sql_tmp = trim($sql_query);
    PMA_change_enc_order();
             // function is_writeable() is valid on PHP3 and 4
             if (!is_writeable($tmp_subdir)) {
                 $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
                 if ($docsql_text == FALSE) {
                     echo $strFileCouldNotBeRead;
                     exit;
                 }
             } else {
                 $sql_file_new = $tmp_subdir . basename($sql_file);
                 move_uploaded_file($sql_file, $sql_file_new);
                 $docsql_text = PMA_readFile($sql_file_new, $sql_file_compression);
                 unlink($sql_file_new);
             }
         } else {
             // read from the normal upload dir
             $docsql_text = PMA_readFile($sql_file, $sql_file_compression);
         }
         // Convert the file's charset if necessary
         if ($cfg['AllowAnywhereRecoding'] && $allow_recoding && isset($charset_of_file) && $charset_of_file != $charset) {
             $docsql_text = PMA_convert_string($charset_of_file, $charset, $docsql_text);
         }
         if (!isset($docsql_text) || $docsql_text == FALSE || $docsql_text == '') {
             echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
         } else {
             docsql_check('', $sql_file_name, $sql_file_name, $docsql_text);
         }
     }
     // end uploaded file stuff
 } else {
     // echo '<h1>Starting Import</h1>';
     $docpath = $cfg['docSQLDir'] . PMA_securePath($docpath);
Example #4
0
/**
 * Reads (and decompresses) a (compressed) file into a string
 *
 * @param   string   the path to the file
 * @param   string   the MIME type of the file, if empty MIME type is autodetected
 *
 * @global  array    the phpMyAdmin configuration
 *
 * @return  string   the content of the file or
 *          boolean  FALSE in case of an error.
 */
function PMA_readFile($path, $mime = '')
{
    global $cfg;
    if (!file_exists($path)) {
        return FALSE;
    }
    switch ($mime) {
        case '':
            $file = @fopen($path, 'rb');
            if (!$file) {
                return FALSE;
            }
            $test = fread($file, 3);
            fclose($file);
            if ($test[0] == chr(31) && $test[1] == chr(139)) {
                return PMA_readFile($path, 'application/x-gzip');
            }
            if ($test == 'BZh') {
                return PMA_readFile($path, 'application/x-bzip');
            }
            return PMA_readFile($path, 'text/plain');
        case 'text/plain':
            $file = @fopen($path, 'rb');
            if (!$file) {
                return FALSE;
            }
            $content = fread($file, filesize($path));
            fclose($file);
            break;
        case 'application/x-gzip':
            if ($cfg['GZipDump'] && @function_exists('gzopen')) {
                $file = @gzopen($path, 'rb');
                if (!$file) {
                    return FALSE;
                }
                $content = '';
                while (!gzeof($file)) {
                    $content .= gzgetc($file);
                }
                gzclose($file);
            } else {
                return FALSE;
            }
            break;
        case 'application/x-bzip':
            if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
                $file = @fopen($path, 'rb');
                if (!$file) {
                    return FALSE;
                }
                $content = fread($file, filesize($path));
                fclose($file);
                $content = bzdecompress($content);
            } else {
                return FALSE;
            }
            break;
        default:
            return FALSE;
    }
    return $content;
}
Example #5
0
 /**
  * Execute a SQL script
  *
  * @param $script, string : the CMS_file::FILE_SYSTEM SQL script filename
  *  This script can be SQL export provided by phpMyadmin or mysqldump, etc.
  * @param simulation : boolean, if true, only do a read of the script and if it contain sql data, return true.
  * @return boolean, true on success, false on failure
  * @access public
  */
 function executeSqlScript($script, $simulation = false)
 {
     //include PMA import functions
     require_once PATH_PACKAGES_FS . '/files/sqlDump.php';
     //read mysql version and set needed constant/vars for phpMyAdmin
     $q = new CMS_query('SELECT VERSION() AS version');
     $version = $q->getValue('version');
     $match = explode('.', $version);
     //read mysql file
     $query = PMA_readFile($script);
     //first, detect SQL file encoding
     $isUTF8 = io::isUTF8($query);
     //then, change charset declaration inside sql queries to match current Automne charset
     if (strtolower(APPLICATION_DEFAULT_ENCODING) != 'utf-8') {
         //if Automne is not in utf8, then table charset must be in latin1
         $query = str_ireplace(' CHARSET=utf8', ' CHARSET=latin1', $query);
         $query = str_ireplace('TYPE=MyISAM;', 'TYPE=MyISAM CHARSET=latin1;', $query);
     } else {
         //if Automne is in utf8, then table charset must be in utf8
         $query = str_ireplace(' CHARSET=latin1', ' CHARSET=utf8', $query);
         $query = str_ireplace('TYPE=MyISAM;', 'TYPE=MyISAM CHARSET=utf8;', $query);
     }
     //finally, clean it and split queries
     PMA_splitSqlFile($queries, $query, (int) sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2])));
     if (!$simulation) {
         //set connection charset accordingly to file charset
         if ($isUTF8) {
             $q = new CMS_query("SET NAMES 'utf8'");
         } else {
             $q = new CMS_query("SET NAMES 'latin1'");
         }
         //execute all queries
         $ok = true;
         foreach ($queries as $aQuery) {
             $q = new CMS_query($aQuery);
             $ok = $q->hasError() ? false : $ok;
         }
         //set connection charset accordingly to file charset
         if ($isUTF8) {
             $q = new CMS_query("SET NAMES 'latin1'");
         } else {
             $q = new CMS_query("SET NAMES 'utf8'");
         }
     } else {
         $ok = is_array($queries) && $queries ? true : false;
     }
     //reset connection charset
     if (io::strtolower(APPLICATION_DEFAULT_ENCODING) == 'utf-8') {
         //set connection to utf-8 charset
         $q = new CMS_query("SET NAMES 'utf8'");
     } else {
         $q = new CMS_query("SET NAMES 'latin1'");
     }
     return $ok;
 }