Zefix_search/taskExecuter.php

119 lines
4.4 KiB
PHP

<?php
include "zefixAPI.php";
include "read_write_xlsx.php";
include "emailSender.php";
$maxExecutionTime = 120;
$taskDir = 'tasks';
$downloadDir = 'download';
$minTaskOldness = 10;
$latesEndTime = time() + $maxExecutionTime;
$smtppassword = getenv("smtppassword");
if(!is_dir($downloadDir)){
mkdir($downloadDir, 0755, true);
}
function doRequest($data, $filename, $username, $password)
{
$response = sendAPICompanySearchRequest($username, $password, $data);
$responseObject = json_decode($response, true);
if(array_key_exists("error", $responseObject)) {
return false;
}
$companyArray = $responseObject['list'];
$companyData = array();
foreach ($companyArray as $company) {
$companyFullData = json_decode(sendAPICompanyInfoRequest($username, $password, $company['uid']), true)[0];
$companyData[] = [$companyFullData['name'],$companyFullData['address']['street'], $companyFullData['address']['houseNumber'], $companyFullData['address']['swissZipCode'], $companyFullData['address']['city'],$companyFullData['uid'],$companyFullData['legalSeat'],$companyFullData['legalForm']['name']['de'],$companyFullData['status'],$companyFullData['sogcDate'],$companyFullData['deletionDate']];
}
if (! file_exists($filename)) {
// If File doesn't exist yet
$file = fopen($filename, 'w');
if($file) {
fputcsv($file, ['name', 'street', 'houseNumber', 'swissZipCode', 'city', 'uid', 'legalSeat', 'legalForm', 'status', 'sogcDate', 'deletionDate']);
}
} else {
// If file already exists
$file = fopen($filename, 'a');
}
if ($file) {
foreach ($companyData as $row){
fputcsv($file, $row);
}
fclose($file);
}
if($responseObject['hasMoreResults']) {
return $responseObject['maxOffset'] + 50;
} else {
return false;
}
}
// Stop executing bevore reaching PHP Max Execution Time
function convertcsvToXlsx(string $csvFile, string $xlsxFile)
{
$csvData = [];
if (($handle = fopen($csvFile, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$csvData[] = $data;
}
fclose($handle);
} else {
echo "Failed to open $csvFile for reading.";
}
write_xlsxFile($xlsxFile, $csvData);
}
while($latesEndTime - time() > 30){
// If the directory for Tasks is already created
if(is_dir($taskDir)) {
$taskfiles = scandir($taskDir);
$taskfiles = array_diff($taskfiles, array('.', '..'));
sort($taskfiles);
// if there are any taks which are older then min oldness. To be sure the file writing process is finished.
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + $minTaskOldness < time()) {
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
$task = json_decode($taskString, true);
// if there are any requests to do, do the first
if(count($task['requests']) > 0) {
$nextOffset = doRequest($task['requests'][0], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password);
if($nextOffset){
$task['requests'][0]['offset'] = $nextOffset;
} else {
array_shift($task['requests']);
}
$taskString = json_encode($task);
$taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w');
if($taskfile){
fwrite($taskfile, $taskString);
fclose($taskfile);
}
}
// if there are no Requests left, send the E-Mail
else {
convertcsvToXlsx(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]));
unlink(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
unlink($taskDir.'/'.$taskfiles[0]);
echo "Task File deleted";
echo "<br>";
sendEmail($task['email'], str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]), $smtppassword);
}
} else {
// acutal Time - start time
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
echo "sleeping 10 seconds";
echo "<br>";
sleep(10);
}
}
}
?>