<?php
// Define ABSPATH for template security
// Prevent any output before headers
// Increase execution time and memory limits
ini_set ( 'max_execution_time' , 120 ) ;
// Enable error reporting but log to file instead of display
ini_set ( 'error_log' , 'php_errors.log' ) ;
// Force content type and encoding
header ( 'Content-Type: text/html; charset=utf-8' ) ;
// Set default timezone
// Function to safely output HTML
function safe_html( $str ) {
}
// Function to handle fatal errors
function fatal_handler( ) {
if ( $error !== NULL && in_array ( $error [ 'type' ] , [ E_ERROR , E_PARSE , E_CORE_ERROR , E_COMPILE_ERROR ] ) ) { show_error_page( 'Fatal Error' , $error [ 'message' ] , $error [ 'file' ] , $error [ 'line' ] ) ;
}
}
// Function to show error page
function show_error_page( $title , $message , $file = null , $line = null ) {
$timeInfo = [
'range' => isset ( $GLOBALS [ 'rangeText' ] ) ?
$GLOBALS [ 'rangeText' ] : 'Unknown' , 'start' => isset ( $GLOBALS [ 'startTime' ] ) ?
date ( 'Y-m-d H:i:s' , $GLOBALS [ 'startTime' ] ) : 'Not set' , 'end' => isset ( $GLOBALS [ 'endTime' ] ) ?
date ( 'Y-m-d H:i:s' , $GLOBALS [ 'endTime' ] ) : 'Not set' , 'current' => date ( 'Y-m-d H:i:s' ) , ] ;
$debugInfo = [
'php_version' => PHP_VERSION ,
'server_software' => $_SERVER [ 'SERVER_SOFTWARE' ] ?? 'Unknown' ,
'device_ip' => isset ( $GLOBALS [ 'deviceIP' ] ) ?
$GLOBALS [ 'deviceIP' ] : 'Not set' ] ;
}
try {
// Rest of Code
class DahuaAccessLogs {
private $config ;
private $curl ;
public function __construct
( array $config ) { $this -> config = $config ;
}
public function getLogs( $startTime , $endTime ) {
try {
"http://%s /cgi-bin/recordFinder.cgi?action=find&name=AccessControlCardRec&StartTime=%d &EndTime=%d " ,
$this -> config [ 'deviceIP' ] ,
$startTime ,
$endTime
) ;
CURLOPT_URL => $url ,
CURLOPT_RETURNTRANSFER => true ,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_USERPWD => "{$this->config['username']} :{$this->config['password']} " ,
CURLOPT_HEADER => true ,
CURLOPT_TIMEOUT => 60
] ) ;
if ( $response === false ) {
throw new RuntimeException
( 'Connection Error: ' . curl_error ( $this -> curl ) ) ; }
$headerSize = curl_getinfo ( $this -> curl , CURLINFO_HEADER_SIZE
) ; $body = substr ( $response , $headerSize ) ;
if ( $httpCode !== 200 ) {
throw new RuntimeException( 'Failed to fetch access records. HTTP Code: ' . $httpCode ) ;
}
// Parse the response
$records = [ ] ;
$currentIndex = null ;
$currentRecord = [ ] ;
foreach ( $lines as $line ) {
if ( empty ( $line ) ) continue ;
if ( preg_match ( '/records\[(\d+)\]\.(\w+)=(.*)/' , $line , $matches ) ) { $index = $matches [ 1 ] ;
$field = $matches [ 2 ] ;
$value = $matches [ 3 ] ;
if ( $currentIndex !== $index ) {
if ( ! empty ( $currentRecord ) ) { $records [ ] = $currentRecord ;
}
$currentRecord = [ ] ;
$currentIndex = $index ;
}
$currentRecord [ $field ] = $value ;
}
}
if ( ! empty ( $currentRecord ) ) { $records [ ] = $currentRecord ;
}
return $records ;
} catch ( Exception $e ) {
echo "Error: " . $e -> getMessage ( ) . "\n " ;
return [ ] ;
}
}
public function __destruct( ) {
}
}
}
// Main execution code
$startTime = $endTime - ( 24 * 3600 ) ;
$config = [
'deviceIP' => '41.139.152.133:9903' ,
'username' => 'admin' ,
'password' => 'admin123'
] ;
$accessLogs = new DahuaAccessLogs( $config ) ;
$records = $accessLogs -> getLogs ( $startTime , $endTime ) ;
// Print records
echo "\n === Access Records for Last 24 Hours ===\n " ;
echo "Time Range: " . date ( 'Y-m-d H:i:s' , $startTime ) . " to " . date ( 'Y-m-d H:i:s' , $endTime ) . "\n " ; echo "Total Records: " . count ( $records ) . "\n " ; echo "----------------------------------------\n " ;
foreach ( $records as $record ) {
"Time: %s \n Device: %s \n Name: %s \n Card No: %s \n User ID: %s \n Door: %s \n Status: %s \n ----------------------------------------\n " ,
$record [ 'Time' ] ?? 'N/A' ,
$record [ 'DeviceName' ] ?? 'N/A' ,
$record [ 'Name' ] ?? 'N/A' ,
$record [ 'CardNo' ] ?? 'N/A' ,
$record [ 'UserID' ] ?? 'N/A' ,
$record [ 'Door' ] ?? 'N/A' ,
$record [ 'Status' ] ?? 'N/A'
) ;
}
} catch ( Exception $e ) {
echo "Error: " . $e -> getMessage ( ) . "\n " ;
}
// Flush output buffer
?>
PD9waHAKLy8gRGVmaW5lIEFCU1BBVEggZm9yIHRlbXBsYXRlIHNlY3VyaXR5CmRlZmluZSgnQUJTUEFUSCcsIGRpcm5hbWUoX19GSUxFX18pKTsKCi8vIFByZXZlbnQgYW55IG91dHB1dCBiZWZvcmUgaGVhZGVycwpvYl9zdGFydCgpOwoKLy8gSW5jcmVhc2UgZXhlY3V0aW9uIHRpbWUgYW5kIG1lbW9yeSBsaW1pdHMKc2V0X3RpbWVfbGltaXQoMTIwKTsgLy8gU2V0IHRvIDIgbWludXRlcwppbmlfc2V0KCdtZW1vcnlfbGltaXQnLCAnMjU2TScpOwppbmlfc2V0KCdtYXhfZXhlY3V0aW9uX3RpbWUnLCAxMjApOwoKLy8gRW5hYmxlIGVycm9yIHJlcG9ydGluZyBidXQgbG9nIHRvIGZpbGUgaW5zdGVhZCBvZiBkaXNwbGF5CmVycm9yX3JlcG9ydGluZyhFX0FMTCk7CmluaV9zZXQoJ2Rpc3BsYXlfZXJyb3JzJywgMCk7CmluaV9zZXQoJ2xvZ19lcnJvcnMnLCAxKTsKaW5pX3NldCgnZXJyb3JfbG9nJywgJ3BocF9lcnJvcnMubG9nJyk7CgovLyBGb3JjZSBjb250ZW50IHR5cGUgYW5kIGVuY29kaW5nCmhlYWRlcignQ29udGVudC1UeXBlOiB0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgnKTsKCi8vIFNldCBkZWZhdWx0IHRpbWV6b25lCmRhdGVfZGVmYXVsdF90aW1lem9uZV9zZXQoJ0FmcmljYS9OYWlyb2JpJyk7CgovLyBGdW5jdGlvbiB0byBzYWZlbHkgb3V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b2xDYXJkUmVjJlN0YXJ0VGltZT0lZCZFbmRUaW1lPSVkIiwKICAgICAgICAgICAgICAgICAgICAkdGhpcy0+Y29uZmlnWydkZXZpY2VJUCddLAogICAgICAgICAgICAgICAgICAgICRzdGFydFRpbWUsCiAgICAgICAgICAgICAgICAgICAgJGVuZFRpbWUKICAgICAgICAgICAgICAgICk7CgogICAgICAgICAgICAgICAgY3VybF9zZXRvcHRfYXJyYXkoJHRoaXMtPmN1cmwsIFsKICAgICAgICAgICAgICAgICAgICBDVVJMT1BUX1VSTCA9PiAkdXJsLAogICAgICAgICAgICAgICAgICAgIENVUkxPUFRfUkVUVVJOVFJBTlNGRVIgPT4gdHJ1ZSwKICAgICAgICAgICAgICAgICAgICBDVVJMT1BUX0hUVFBBVVRIID0+IENVUkxBVVRIX0RJR0VTVCwKICAgICAgICAgICAgICAgICAgICBDVVJMT1BUX1VTRVJQV0QgPT4gInskdGhpcy0+Y29uZmlnWyd1c2VybmFtZSddfTp7JHRoaXMtPmNvbmZpZ1sncGFzc3dvcmQnXX0iLAogICAgICAgICAgICAgICAgICAgIENVUkxPUFRfSEVBREVSID0+IHRydWUsCiAgICAgICAgICAgICAgICAgICAgQ1VSTE9QVF9USU1FT1VUID0+IDYwCiAgICAgICAgICAgICAgICBdKTsKCiAgICAgICAgICAgICAgICAkcmVzcG9uc2UgPSBjdXJsX2V4ZWMoJHRoaXMtPmN1cmwpOwogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBpZiAoJHJlc3BvbnNlID09PSBmYWxzZSkgewogICAgICAgICAgICAgICAgICAgIHRocm93IG5ldyBSdW50aW1lRXhjZXB0aW9uKCdDb25uZWN0aW9uIEVycm9yOiAnIC4gY3VybF9lcnJvcigkdGhpcy0+Y3VybCkpOwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgICRodHRwQ29kZSA9IGN1cmxfZ2V0aW5mbygkdGhpcy0+Y3VybCwgQ1VSTElORk9fSFRUUF9DT0RFKTsKICAgICAgICAgICAgICAgICRoZWFkZXJTaXplID0gY3VybF9nZXRpbmZvKCR0aGlzLT5jdXJsLCBDVVJMSU5GT19IRUFERVJfU0laRSk7CiAgICAgICAgICAgICAgICAkYm9keSA9IHN1YnN0cigkcmVzcG9uc2UsICRoZWFkZXJTaXplKTsKCiAgICAgICAgICAgICAgICBpZiAoJGh0dHBDb2RlICE9PSAyMDApIHsKICAgICAgICAgICAgICAgICAgICB0aHJvdyBuZXcgUnVudGltZUV4Y2VwdGlvbignRmFpbGVkIHRvIGZldGNoIGFjY2VzcyByZWNvcmRzLiBIVFRQIENvZGU6ICcgLiAkaHR0cENvZGUpOwogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIC8vIFBhcnNlIHRoZSByZXNwb25zZQogICAgICAgICAgICAgICAgJGxpbmVzID0gZXhwbG9kZSgiXG4iLCAkYm9keSk7CiAgICAgICAgICAgICAgICAkcmVjb3JkcyA9IFtdOwogICAgICAgICAgICAgICAgJGN1cnJlbnRJbmRleCA9IG51bGw7CiAgICAgICAgICAgICAgICAkY3VycmVudFJlY29yZCA9IFtdOwogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBmb3JlYWNoICgkbGluZXMgYXMgJGxpbmUpIHsKICAgICAgICAgICAgICAgICAgICAkbGluZSA9IHRyaW0oJGxpbmUpOwogICAgICAgICAgICAgICAgICAgIGlmIChlbXB0eSgkbGluZSkpIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgIGlmIChwcmVnX21hdGNoKCcvcmVjb3Jkc1xbKFxkKylcXVwuKFx3Kyk9KC4qKS8nLCAkbGluZSwgJG1hdGNoZXMpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICRpbmRleCA9ICRtYXRjaGVzWzFdOwogICAgICAgICAgICAgICAgICAgICAgICAkZmllbGQgPSAkbWF0Y2hlc1syXTsKICAgICAgICAgICAgICAgICAgICAgICAgJHZhbHVlID0gJG1hdGNoZXNbM107CiAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICBpZiAoJGN1cnJlbnRJbmRleCAhPT0gJGluZGV4KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoIWVtcHR5KCRjdXJyZW50UmVjb3JkKSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICRyZWNvcmRzW10gPSAkY3VycmVudFJlY29yZDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICRjdXJyZW50UmVjb3JkID0gW107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAkY3VycmVudEluZGV4ID0gJGluZGV4OwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAkY3VycmVudFJlY29yZFskZmllbGRdID0gJHZhbHVlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgaWYgKCFlbXB0eSgkY3VycmVudFJlY29yZCkpIHsKICAgICAgICAgICAgICAgICAgICAkcmVjb3Jkc1tdID0gJGN1cnJlbnRSZWNvcmQ7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgcmV0dXJuICRyZWNvcmRzOwoKICAgICAgICAgICAgfSBjYXRjaCAoRXhjZXB0aW9uICRlKSB7CiAgICAgICAgICAgICAgICBlY2hvICJFcnJvcjogIiAuICRlLT5nZXRNZXNzYWdlKCkgLiAiXG4iOwogICAgICAgICAgICAgICAgcmV0dXJuIFtdOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgZnVuY3Rpb24gX19kZXN0cnVjdCgpIHsKICAgICAgICAgICAgaWYgKGlzX3Jlc291cmNlKCR0aGlzLT5jdXJsKSkgewogICAgICAgICAgICAgICAgY3VybF9jbG9zZSgkdGhpcy0+Y3VybCk7Ci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