* @author Loic Vinet */ session_name('LetterBox2'); session_start(); require("class_functions.php"); require_once("class_db.php"); $source = session_save_path(); //----------------------- Field selection in the export.xml file-----------------------// $table_users = false; $table_doctypes = false; $table_sender = false; $table_models = false; $table_document = false; $xmlfile = simplexml_load_file($_SESSION['config']['exportlist']); $i_map = 0; foreach($xmlfile->TAG as $TAG) { $tab_export_name[$i_map] = $TAG->NAME; $tab_export_value[$i_map] = $TAG->VALUE; $tab_export_table[$i_map] = $TAG->TABLE; $i_map++; } //-----------------------Déclaration des tables nécessaires--------------------------------// for ($t=0; $t < count($tab_export_table); $t++) { if ($tab_export_table[$t] == $_SESSION['ressources'][0]['tablename']) { $table_document = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['doctypes']) { $table_doctypes = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['senders']) { $table_sender = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['models']) { $table_models = true; } if ($tab_export_table[$t] == $_SESSION['tablename']['users']) { $table_users = true; } } $tables = ''; if ($table_document == true) { $tables .= $_SESSION['ressources'][0]['tablename']." r , "; } if ($table_doctypes == true) { $tables .= $_SESSION['tablename']['doctypes']." d , "; } if ($table_sender == true) { $tables .= $_SESSION['tablename']['senders']." s , "; } //if ($table_models == true) { $tables .= $_SESSION['tablename']['models']." m , "; } if ($table_users == true) { $tables .= $_SESSION['tablename']['users']." u , "; } $tables = substr($tables, 0, -2); //echo $tables; exit(); //-----------------------Query execution----------------------------------------------------------// $db = new dbquery(); $db->connect(); $select = ""; // Fields positioning for ($i_query=0; $i_query <= $i_map; $i_query++) { $select .= substr($tab_export_table[$i_query], 0, 1).".".$tab_export_value[$i_query]." ,"; } $select = substr($select, 0, -4); $from = $tables; $where = $_SESSION['export_where_request']; //Création des jointures si nécessaire if ($table_doctypes == true) { $where .= "and r.TYPE_ID = d.TYPE_ID "; } if ($table_sender == true) { $where .= "and r.AUTHOR = s.NAME "; } if ($table_users == true) { $where .= "and ((DEST_USER = 'courrier') or (r.DEST_USER = u.USER_ID)) "; } $db->query("SELECT ".$select." FROM ".$from." WHERE ".$where); //----------------------- Result list making ----------------------------------------// for ($i_title = 0; $i_title < $i_map; $i_title++) { $the_line .= $tab_export_name[$i_title].";"; } $csv .= $the_line."\r\n"; while ($result = $db->fetch_object()) { $the_line = ""; for ($i_reg = 0; $i_reg< $i_map; $i_reg++) { $tmp_value_csv = $result->$tab_export_value[$i_reg]; $tmp_value_csv = str_replace(CHR(10), "", $tmp_value_csv); $tmp_value_csv = str_replace(CHR(13), "", $tmp_value_csv); $tmp_value_csv = str_replace(";", ",", $tmp_value_csv); $tmp_value_csv = str_replace("\\", ",", $tmp_value_csv); $the_line .= $tmp_value_csv.";"; } $csv .= $the_line."\r\n"; } //----------------------- Index file making ----------------------------------------// if ($_SESSION['config']['system'] == "true") { $slh = "/"; } else { $slh = "\\"; } $monfichier = fopen($source.$slh."export_".$_SESSION['user']['UserId'].".csv","w+,"); fwrite($monfichier, $csv); header("Pragma: no-cach"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=maarch.csv;"); readfile($source.$slh."export_".$_SESSION['user']['UserId'].".csv");