<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Académie de Bordeaux)

This file is part of GiBii/Melia.

GiBii/Melia 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, or
(at your option) any later version.

GiBii/Melia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GiBii/Melia; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

****************************************************************************/


function sql_addslashes($a_string) {
$a_string = str_replace('\\', '\\\\', $a_string);  // '\
$a_string = str_replace('\'', '\\\'', $a_string);
return $a_string;
}

function bon_crlf() {
$the_crlf = "\n";
if (!isset($GLOBALS['HTTP_USER_AGENT']))
    if (!empty($GLOBALS['HTTP_SERVER_VARS']) && isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT']))
        $GLOBALS['HTTP_USER_AGENT'] = $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
    else $GLOBALS['HTTP_USER_AGENT'] = @getenv('HTTP_USER_AGENT');
// Chercher "win"
if (!empty($GLOBALS['HTTP_USER_AGENT'])
    && ereg('[^(]*\((.*)\)[^)]*', $GLOBALS['HTTP_USER_AGENT'], $regs))
    if (eregi('Win', $regs[1])) $the_crlf = "\r\n";
return $the_crlf;
}

function is_table_temp($table) {
if (strpos($table,'new_',0)===0) return true;
if (strpos($table,'_conserves')>0) return true;
return false;
}


function get_table_def($db, $table) {
global $crlf;
$schema_create="";

$result = mysql_query('SHOW CREATE TABLE ' . $db . '.' . $table);
if ($result != FALSE && mysql_num_rows($result) > 0) {
    $tmpres         = mysql_fetch_array($result);
    $schema_create .= str_replace("\n", $crlf, $tmpres[1]).";".$crlf;
}
return $schema_create;
}

function get_table_content($db, $table, $where, $fonc_traiter) {
//pour sadmin, $where="" ; pour admin, $where=" WHERE etab_id=$etab_id"
global $crlf, $showcolumns;

$result = query("SELECT * FROM $table $where");
if ($result != FALSE) {
    // Chercher les champs entiers
    for ($j = 0; $j < mysql_num_fields($result); $j++) {
        $field_set[$j] = mysql_field_name($result, $j);
        $type          = mysql_field_type($result, $j);
        $field_num[$j]=($type == 'tinyint' || $type == 'smallint' ||
                        $type == 'mediumint' || $type == 'int' ||
                        $type == 'bigint'  ||$type == 'timestamp');
    	}
    // Fabriquer le patron
    $fields        = implode(', ', $field_set);
    $schema_insert = 'INSERT INTO ' . $table . ' (' . $fields . ') VALUES ';

    $field_count   = mysql_num_fields($result);
    $search  = array("\x0a","\x0d","\x1a");
    $replace = array("\\n","\\r","\Z");

    @set_time_limit(1200); // 20 minutes
    
	$nbinsert=0;
    while ($row = mysql_fetch_row($result)) {
    	//on balance une ligne de données MAIS avec un INSERT pour 100 enregistrements
        if ($nbinsert==0) {
        	$insert_line=$schema_insert;
        	}
        else $insert_line=",";
        $nbinsert++;
		for ($j = 0; $j < $field_count; $j++) {
            if (!isset($row[$j])) $values[]= 'NULL';
            else if (!empty($row[$j]))
                    if ($field_num[$j]) $values[]= $row[$j];
                    else $values[]= "'". str_replace($search, $replace, sql_addslashes($row[$j])) . "'";
                 else 
                    if ($field_num[$j]) $values[]= 0;
                    else $values[]= "''";
        	} // for
        $insert_line.='('.implode(',',$values).')';
        if ($nbinsert==100) {
        	$insert_line.=';';
        	$nbinsert=0;
        	}
        $fonc_traiter($insert_line);
        unset($values);
    	} // while
    $insert_line=';';
    if ($nbinsert) $fonc_traiter($insert_line);
} // if
return true;
} 

?>