<?php
/*********************************************************************************
 * Gest'Ecole is a school management software developped by
 * DOX Conception Copyright (C) 2012-2013 DOX Conception
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the license published by DOX Conception.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY.  See the Software License for more details.
 * 
 * You should have received a copy of the software license along with
 * this program; if not, see:
 * http://www.doxconception.com/index.php/licence-de-gestecole.html
 ********************************************************************************/

  session_start();

  include "../config.php";

  include "../langues/fr-FR/commun.php";
  foreach ($Langue_Application AS $cle => $value)
  {
		$Langue[$cle]=$Langue_Application[$cle];
		$Langue_JS[$cle]=str_replace('"','&quot;',str_replace("'","&#039;",$Langue_Application[$cle]));
  }
  if ($_SESSION['language_application']!="fr-FR")
  {
		if (file_exists("../langues/".$_SESSION['language_application']."/commun.php"))
		{
			include "../langues/".$_SESSION['language_application']."/commun.php";
			foreach ($Langue_Application AS $cle => $value)
			{
				$Langue[$cle]=$Langue_Application[$cle];
				$Langue_JS[$cle]=str_replace('"','&quot;',str_replace("'","&#039;",$Langue_Application[$cle]));
			}
		}
  }
  
  include "../commun/fonctions.php";

  Connexion_DB();

	$table= array('personnels'=>'profs', 'eleves'=>'eleves');
	
	$fichier=$_GET['module_export'].date("YmdHis").".csv";
	$fp=fopen("../cache/upload/".$fichier,"w");
	$schema_insert = '';
	$csv_terminated = "\r\n";
	$csv_enclosed = '"';
	$csv_separator = ';';
	$csv_escaped = "\\";
	
	$result = mysql_query("SELECT * FROM `".$table[$_GET['module_export']]."`");
	$fields_cnt = mysql_num_fields($result);
	$schema_insert = '';
	for ($i = 0; $i < $fields_cnt; $i++) 
	{
		$schema_insert .= $csv_enclosed
									 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i)))
									 . $csv_enclosed;
		$schema_insert .= $csv_separator;
	} // end for
	$schema_insert  =trim(substr($schema_insert, 0, -1));
	fwrite($fp,$schema_insert . $csv_terminated);
	
	while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
	{
		$schema_insert = '';
		for ($j = 0; $j < $fields_cnt; $j++) 
		{
			if (!isset($row[$j]) || is_null($row[$j])) 
			{
					$schema_insert .= "NULL";
			} 
			elseif ($row[$j] == '0' || $row[$j] != '') 
			{
				$row[$j] = str_replace("\n", "<br />", str_replace("\r", "", $row[$j]));
				$schema_insert .= $csv_enclosed
												 . str_replace($csv_enclosed, $csv_enclosed . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
												 . $csv_enclosed;
			}
			else 
			{
					$schema_insert .= '';
			}
			if ($j < $fields_cnt-1) 
			{
					$schema_insert .= $csv_separator;
			}
		}
		fwrite($fp,$schema_insert . $csv_terminated);
//		fwrite($fp,utf8_encode($schema_insert) . $csv_terminated);
	}
	
	fclose($fp);

  header("content-type: text/html; charset=utf-8");
	header("Pragma: no-cache");
	header("Last-Modified: " . gmdate('D, d M Y H:i:s') . " GMT");
  header("Content-Disposition: attachment; filename=".$fichier);

  flush();

  readfile("../cache/upload/".$fichier);
?>