<?php
session_start();
ini_set('max_execution_time', 300000);
include("include/setsession.php");
require_once("include/connect.inc");
mysqli_query($link, "SET character_set_results=utf8");

$values = mysqli_query("SELECT count(*) AS tot FROM ".$s_prefix."_historylog") or die('Query failed: ' . mysqli_error($link));
$row = mysqli_fetch_array($values, MYSQLI_ASSOC);
$count = $row['tot'];

$i=0;

$query_select="SELECT a.id, 
					a.email, 
					a.pwd,
					a.ip,
					DATE_FORMAT(a.date1, '%d-%m-%Y %k:%i:%S') as date1 
					FROM ".$s_prefix."_historylog a
					ORDER BY id DESC";
$values = mysqli_query($query_select) or die('Query failed: ' . mysqli_error($link));
while ($row = mysqli_fetch_array($values, MYSQLI_ASSOC)) {
	$responce->rows[$i]['id']=$row['id'];
	$responce->rows[$i]['cell']=array($row['date1'],
								$row['email'],
								$row['ip']);
								
	$i++;
}
echo json_encode($responce);





$file_name = "export.xml";
if(file_exists("upload/".$file_name)) unlink("upload/".$file_name);

fwrite (fopen ("upload/".$file_name,"a+"), '<?xml version="1.0" encoding="UTF-8"?>');
fwrite (fopen ("upload/".$file_name,"a+"), "\r\n");
fwrite (fopen ("upload/".$file_name,"a+"), "<Settings>\r\n");

/*
      <FileSource1>http://manager.lapresse.it/customerstorage/radio_19/gr_agr.mp3</FileSource1>
*/


$i=1;
$query_select = "SELECT * FROM ".$s_prefix."_tb_messagefb WHERE date BETWEEN '".$fromdb."' AND '".$todb."' AND id_channel=".$idchannel." AND id_list=".$idlist." ORDER BY id DESC";
$values = mysqli_query($query_select) or die('Query failed: '.$query_select.' '. mysqli_error($link));
while ($row = mysqli_fetch_array($values, MYSQLI_ASSOC)) {
	fwrite (fopen ("upload/".$file_name,"a+"), "\t<Row".$i.">\r\n\t\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<Enabled>1</Enabled>\r\n\t\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<Description>".$row['']."</Description>\r\n\t\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<Time>".$row['']."</Time>\r\n\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<Days>".$row['']."</Days>\r\n\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<Enabled1>".$row['']."</Enabled1>\r\n\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<FileSource1>".$row['']."</FileSource1>\r\n\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "<FileDest1>".$row['']."</FileDest1>\r\n\t");
	fwrite (fopen ("upload/".$file_name,"a+"), "</Row".$i.">\r\n");

	$i++;
}

fwrite (fopen ("upload/".$file_name,"a+"), "</Settings>");
$file = fopen ("upload/".$file_name,"a+");
fclose($file);

$txt.= '<br><br><br><b>Invio completato</b>';
//$txt.= '<br><a href="download.php?file=messages.xml&path='.$pathfb.'">Download messaggi</a><br>';

/*
$backupFile="facebook".date("dmY").".zip";  
if (file_exists($pathfb."/".$backupFile)) @unlink($pathfb."/".$backupFile);
require_once('../include/pclzip-2-8-2/pclzip.lib.php');  
$archive = new PclZip($pathfb."/".$backupFile);
$listOfFilesToCompress=$pathfb."/messages.xml";

for($i=1;$i<=count($hfer_img);$i++) {
	$txt.= '<a href="download.php?file='.$hfer_img[$i].'&path='.$pathfb.'/img">Download immagine</a><br>';
	$listOfFilesToCompress.=','.$pathfb.'/img/'.$hfer_img[$i];
}
  
$v_list = $archive->add($listOfFilesToCompress);  
if ($v_list == 0) {  
	die("Error : ".$archive->errorInfo(true));  
}  
*/
//Opzionale: faccio scaricare il file zip  
/*
header("Content-type: application/zip");  
header("Content-Disposition: attachment; filename=$pathfb/$backupFile");  
header("Content-Description: Backup");  
header("Content-Length: ".filesize($pathfb."/".$backupFile));  
readfile($pathfb."/".$backupFile);  
*/
//$txt.='<a href="'.$pathfb."/".$backupFile.'">Download ZIP</a>';

echo $txt;
?>