<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
if (!defined('ADMIN_ALLOW') && !defined('ADMIN_MAX')) {
	die ("Access Denied");
}
if ($acces == 1) {
if($admintest && $adminsecu)  {
        incoh();
        include("includes/sql_parse.php");
        $sqlfile_tmpname = $HTTP_POST_FILES['sqlfile']['tmp_name'];
        $sqlfile_name = $HTTP_POST_FILES['sqlfile']['name'];
        $sqlfile_type = (!empty($HTTP_POST_FILES['sqlfile']['type'])) ? $HTTP_POST_FILES['sqlfile']['type'] : "";
//$_FILES
        if ($sqlfile_tmpname == '' || $sqlfile_name == '') die("ERREUR aucun fichier spécifié !");

        if( preg_match("/^(text\/[a-zA-Z]+)|(application\/(x\-)?gzip(\-compressed)?)|(application\/octet-stream)$/is", $sqlfile_type) )
        {
            if( preg_match("/\.gz$/is",$sqlfile_name) )
            {
                $do_gzip_compress = FALSE;
                $phpver = phpversion();
                if($phpver >= "4.0")
                {
                    if(extension_loaded("zlib"))
                    {
                        $do_gzip_compress = TRUE;
                    }
                }

                if($do_gzip_compress)
                {
                    $gz_ptr = gzopen($sqlfile_tmpname, 'rb');
                    $sql_query = "";
                    while( !gzeof($gz_ptr) )
                    {
                        $sql_query .= gzgets($gz_ptr, 100000);
                    }
                }
                else
                {
                    die("ERREUR impossible de décompresser ce fichier");
                }
            }
            else
            {
                $sql_query = fread(fopen($sqlfile_tmpname, 'r'), filesize($sqlfile_tmpname));
            }
        }
        else {
            die("ERREUR nom de fichier incorrect $sqlfile_type $sqlfile_name");
        }

        if($sql_query != "")
        {
            $sql_query = remove_remarks($sql_query);
            $pieces = split_sql_file($sql_query, ";\n");

            foreach($pieces AS $query) {
                set_time_limit(30);
                $db->sql_query($query);
            }
        }
        menu();
        Max_OpenTable();
        echo "<h2 align=\"center\">Ajout de $sqlfile_name en base de données</h2>";
        Max_CloseTable();
        incof();
}else {
	echo "Access Denied";
}