#!/usr/bin/php
<?php
$vto_ip="192.168.1.39"; // IP Adresse der VTO2000A
$address = '192.168.1.1'; //IP Adresse des Servers wo dieses Script läuft
$port = 5000;
$password = "admin"; // Admin PW der VTO
$logfile = "/var/log/dooropen.txt";
$debug=1; // Für Debug Ausgaben aktivieren
openlog('FINGERINFO', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER | LOG_PERROR);
//Reduce errors
error_reporting(~E_WARNING);
syslog(LOG_INFO,'FINGERINFO started!');
$socket = stream_socket_server("tcp://$address:$port", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)\n";
die('Could not create socket');
}
while (true) {
while ($conn = stream_socket_accept($socket, -1, $peername)) {
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
if($debug) echo date("Y-m-d H:i:s").": Verbindung von: $peername angenommen.\n";
recordFinder();
fclose($conn);
}
}
fclose($socket);
//ENDE MAIN
function recordFinder() { //Lister der Türöffnungen von der VTO mittesl API abfragen
global $debug, $vto_ip, $password;
//Snapshot erstellen
if($debug) echo date("Y-m-d H:i:s").": Meldung von VTO empfangen, erstelle Snapshot\n";
$snapshot = shell_exec('avconv -y -i \'rtsp://admin:[email protected]\' -f image2 -vframes 1 -pix_fmt yuvj420p /home/cubie/fingersnapshot.jpg');
// Warten und dann User abfragen und Meldung zur App schicken
sleep(10);
$username = 'admin';
$header = array(
'Accept: application/json',
'Content-Type: application/json',
);
$body='{}';
$etime= date('Y-m-d')."%20".date('H:i:s'); //Endzeitstempel
$ttime=time()-3600; //temp. Zeitstempel der letzten Minute
$stime= date('Y-m-d')."%20".date('H:i:s',$ttime);
$url='http://'.$vto_ip.'/cgi-bin/recordFinder.cgi?action=find&name=AccessControlCardRec&StartTime='.$stime.'&EndTime='.$etime;
if($debug) echo "URL:$url\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch. CURLOPT_CUSTOMREQUEST, "GET");
$response = curl_exec($ch);
$name = decode_records($response);
shell_exec('/home/cubie/doornotify.php /home/cubie/fingersnapshot.jpg');
curl_close($ch);
}
function decode_records($response){ // Die letzte Türöffnung finden
global $debug;
if($debug) echo "RESPONSE: $response\n";
$index = substr($response, (strripos($response, "records")+8),2);
echo "letzterIndex:".$index."\n";
$lastarray = substr($response, strpos($response, "records[".$index, 6));
echo $lastarray."\n\n";
$time = substr($lastarray, (strpos($lastarray,"CreateTime=")+11),10);
echo "CreateTime:".date('r',$time)."\n";
$method = substr($lastarray, (strpos($lastarray,"Method=")+7),1);
echo "Method:".$method."\n";
$userid = substr($lastarray, (strpos($lastarray,"UserID=")+7),2);
echo "UserID:".$userid."\n";
user_name($userid,$method);
}
function user_name($userid,$method){
global $debug;
if($method=="4") $rueckgabe = "Remote"; // Öffnen via WebIf
if($method=="6"){ // Fingerprint
$users = array(
"01" => "Papa",
"02" => "Mama",
"03" => "Kind1",
"04" => "Kind2"
);
$rueckgabe = $users[$userid];
}
if($debug) echo "Username:".$rueckgabe." hat Tür geöffnet.\n";
logging("User $rueckgabe hat die Tuer geöffnet");
return($rueckgabe);
}
function logging($txt){
global $logfile;
$fp_log = fopen($logfile,"w");
fwrite($fp_log, date("Y-m-d H:i:s").": $txt\n");
fclose($fp_log);
}
?>