array(
'verify_peer' => false,
),
);
$cxContext = stream_context_create($aContext);
$user_json = file_get_contents('https://USERNAME:PASSWORD@PASCOMIP/ebis-gmbh/services/identity/', False, $cxContext);
$user_res = json_decode($user_json, true);
$user_data = '';
// hallo, ich initialisiere meine Variablen ...
$file_res = null;
$error = null;
$dl = null;
$data_res = null;
$csvdata = null;
$json = null;
foreach ($user_res as $user) {
$file_res = null;
$error = null;
$dl = null;
$data_res = null;
$csvdata = null;
$json = null;
if ($user['faxextension'] != '') {
$res_to = explode(' ',$user['003use_bez']);
$to = $res_to[0].'.'.$res_to[1].'@XYZ.DE';
if(date('l') == "Monday"){
$data_res = "";
$files_res = "";
$csvdata = "";
$json = "";
$timefrom = date('Y-m-d', strtotime('-7 days'));
$timeto = date('Y-m-d', strtotime('-3 days'));
$json_src = file_get_contents('https://USERNAME:PASSWORD@PASCOMIP/PASCOM-INSTANZ/services/cdr/?srcinternal=false&dstusername='.$user['003use_name'].'&connectedfrom=180&timefrom=' . $timefrom . '&timeto=' . $timeto, False, $cxContext);
$json_dst = file_get_contents('https://USERNAME:PASSWORD@PASCOMIP/PASCOM-INSTANZ/services/cdr/?dstinternal=false&srcusername='.$user['003use_name'].'&connectedfrom=180&timefrom=' . $timefrom . '&timeto=' . $timeto, False, $cxContext);
$data_src_res = json_decode($json_src, true);
$data_dst_res = json_decode($json_dst, true);
}elseif(date('l') == "Tuesday"){
$data_res = "";
$files_res = "";
$csvdata = "";
$json = "";
$timefrom = date('Y-m-d', strtotime('-8 days'));
$timeto = date('Y-m-d', strtotime('-0 days'));
$json_src = file_get_contents('https://USERNAME:PASSWORD@PASCOMIP/PASCOM-INSTANZ/services/cdr/?srcinternal=false&dstusername='.$user['003use_name'].'&connectedfrom=180&timefrom=' . $timefrom . '&timeto=' . $timeto, False, $cxContext);
$json_dst = file_get_contents('https://USERNAME:PASSWORD@PASCOMIP/PASCOM-INSTANZ/services/cdr/?dstinternal=false&srcusername='.$user['003use_name'].'&connectedfrom=180&timefrom=' . $timefrom . '&timeto=' . $timeto, False, $cxContext);
$data_src_res = json_decode($json_src, true);
$data_dst_res = json_decode($json_dst, true);
}
$data_res = array_merge($data_src_res,$data_dst_res);
$columns = array_column($data_res, 'time');
array_multisort($columns, SORT_DESC, $data_res);
if (isset($data_res) and $data_res != '' and array_key_exists('0',$data_res)) {
$file_res .= 'Nummer;Name;Richtung;Dauer (min);Uhrzeit;Endzeit;Datum' . PHP_EOL;
foreach ($data_res as $data) {
$date_res = explode(" ", $data['time']);
$date_res2 = explode("T", $date_res[0]);
$date = explode("-", $date_res2[0]);
if ($data['dstPrefix'] == "") {
$inbound = 'eingehend';
$number = $data['srcNumber'];
$name = $data['srcName'];
} else {
$inbound = 'ausgehend';
$number = $data['dstNumber'];
$name = $data['dstName'];
}
$selectedTime = $date_res2[1];
$min = round(str_replace(".",",",$data['connected'])/60,0);
$endTime = strtotime("+".$min." minutes", strtotime($selectedTime));
$time = date('H:i:s', strtotime('+2 hours',$endTime));
$startTime = date('H:i:s', strtotime('+2 hours',strtotime($selectedTime)));
$file_res .= $number . ';'
. $name . ';'
. $inbound . ';'
. round(str_replace(".",",",$data['connected'])/60,0) . ';'
. $startTime
.';'.$time
. ';' . $date[2] . '.' . $date[1] . '.' . $date[0]
. PHP_EOL;
//print $file_res .'
';
}
if($data['dstUserName'] != ""){
$username=$data['dstUserName'];
}elseif($data['srcUserName'] != ""){
$username=$data['srcUserName'];
}
$fp = fopen("/var/www/html/telauswertung/foo/".$username . '.csv', 'w');
fwrite($fp, $file_res);
fclose($fp);
if (file_exists("/var/www/html/telauswertung/foo/".$username . '.csv')) {
$cr = "\n";
$csvdata = $file_res;
$thisfile = $user['003use_bez']." vom " .date('d.m.Y', strtotime('-7 days')) .' bis zum '.date('d.m.Y', strtotime('-3 days')).'.csv' ;
$encoded = chunk_split(base64_encode($csvdata));
// create the email and send it off
$subject = "Telefonauswertung vom " .date('d.m.Y', strtotime('-7 days')) .' bis zum '.date('d.m.Y', strtotime('-3 days')) ;
$from = "telefonauswertung@XYZ.DE";
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/mixed;
boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";
$message = '
This is a multi-part message in MIME format.
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Telefonauswertung
------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: application/octet-stream; name="';
$message .= "$thisfile";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "$thisfile";
$message .= '"
';
$message .= "$encoded";
$message .= '
------=_NextPart_001_0011_1234ABCD.4321FDAC--
';
//print '
MAIL
';
// now send the email
mail($to, $subject, $message, $headers, "-f$from");
}
}
}
}
?>