<?php
header('Content-Type: application/json');
sleep(2);
function addPath($s) {
	return '../../log/'.$s;
}
require('../../functions.php');
$ip = substr($_SERVER['REMOTE_ADDR'], 0, -2).'00';
loadConfig();
unlock_all_files();
$data = array();
$logfiles = array('../../log.txt');
$logfiles = array_merge($logfiles,array_map('addPath',array_slice(scandir('../../log'),2)));
$found = false;
foreach($logfiles AS $file) {
	foreach(file($file) AS $line) {
		$line = explode(', ',$line);
		if($line[1]==$ip) {
			$data[] = implode('	',$line);
			$found = true;
		}
	}
}
$key = rand(1000,9999);
file_put_contents('tmp_data/'.$ip.'.xlsx',implode('',$data));
chmod('tmp_data/'.$ip.'.xlsx', 0000);
file_put_contents('tmp_data/'.$ip.'.key.txt',$key);
chmod('tmp_data/'.$ip.'.key.txt', 0000);
echo '{"data_found":'.($found==true ? '1' : '0').',"remote_key":'.$key.'}';
?>