"web_js" ) ); $string = $_REQUEST['message']; $file = preg_replace( '/\w+:\/\/\w+\//', '', $_REQUEST['file'] ); if ( !empty( $_REQUEST['line'] ) ) $line = $_REQUEST['line']; else $line = NULL; $levels = array_flip(Logger::$codes); if ( !isset($levels[$_REQUEST['level']]) ) Panic( "Unexpected logger level '".$_REQUEST['level']."'" ); $level = $levels[$_REQUEST['level']]; Logger::fetch()->logPrint( $level, $string, $file, $line ); ajaxResponse(); break; } case 'query' : { if ( !canView( 'System' ) ) ajaxError( 'Insufficient permissions to view log entries' ); $minTime = isset($_REQUEST['minTime'])?$_REQUEST['minTime']:NULL; $maxTime = isset($_REQUEST['maxTime'])?$_REQUEST['maxTime']:NULL; $limit = isset($_REQUEST['limit'])?$_REQUEST['limit']:1000; $filter = isset($_REQUEST['filter'])?$_REQUEST['filter']:array(); $sortField = isset($_REQUEST['sortField'])?$_REQUEST['sortField']:'TimeKey'; $sortOrder = isset($_REQUEST['sortOrder'])?$_REQUEST['sortOrder']:'desc'; $filterFields = array( 'Component', 'Pid', 'Level', 'File', 'Line' ); //$filterSql = $filter?' where $countSql = "select count(*) as Total from Logs"; $total = dbFetchOne( $countSql, 'Total' ); $sql = "select * from Logs"; $where = array(); if ( $minTime ) $where[] = "TimeKey > ".dbEscape($minTime); elseif ( $maxTime ) $where[] = "TimeKey < ".dbEscape($maxTime); foreach ( $filter as $field=>$value ) if ( $field == 'Level' ) $where[] = dbEscape($field)." <= ".dbEscape($value); else $where[] = dbEscape($field)." = '".dbEscape($value)."'"; if ( count($where) ) $sql.= " where ".join( " and ", $where ); $sql .= " order by ".dbEscape($sortField)." ".dbEscape($sortOrder)." limit ".dbEscape($limit); $logs = array(); foreach ( dbFetchAll( $sql ) as $log ) { $log['DateTime'] = preg_replace( '/^\d+/', strftime( "%Y-%m-%d %H:%M:%S", intval($log['TimeKey']) ), $log['TimeKey'] ); $logs[] = $log; } $options = array(); $where = array(); foreach( $filter as $field=>$value ) $where[$field] = "$field = '".dbEscape($value)."'"; foreach( $filterFields as $field ) { $sql = "select distinct $field from Logs where not isnull($field)"; $fieldWhere = array_diff_key( $where, array( $field=>true ) ); if ( count($fieldWhere) ) $sql.= " and ".join( " and ", $fieldWhere ); $sql.= " order by $field asc"; if ( $field == 'Level' ) { foreach( dbFetchAll( $sql, $field ) as $value ) if ( $value <= Logger::INFO ) $options[$field][$value] = Logger::$codes[$value]; else $options[$field][$value] = "DB".$value; } else { foreach( dbFetchAll( $sql, $field ) as $value ) if ( $value != '' ) $options[$field][] = $value; } } if ( count($filter) ) { $sql = "select count(*) as Available from Logs where ".join( " and ", $where ); $available = dbFetchOne( $sql, 'Available' ); } ajaxResponse( array( 'updated' => preg_match( '/%/', DATE_FMT_CONSOLE_LONG )?strftime( DATE_FMT_CONSOLE_LONG ):date( DATE_FMT_CONSOLE_LONG ), 'total' => $total, 'available' => isset($available)?$available:$total, 'logs' => $logs, 'state' => logState(), 'options' => $options ) ); break; } case 'export' : { if ( !canView( 'System' ) ) ajaxError( 'Insufficient permissions to export logs' ); $minTime = isset($_REQUEST['minTime'])?$_REQUEST['minTime']:NULL; $maxTime = isset($_REQUEST['maxTime'])?$_REQUEST['maxTime']:NULL; if ( !is_null($minTime) && !is_null($maxTime) && $minTime > $maxTime ) { $tempTime = $minTime; $minTime = $maxTime; $maxTime = $tempTime; } //$limit = isset($_REQUEST['limit'])?$_REQUEST['limit']:1000; $filter = isset($_REQUEST['filter'])?$_REQUEST['filter']:array(); $sortField = isset($_REQUEST['sortField'])?$_REQUEST['sortField']:'TimeKey'; $sortOrder = isset($_REQUEST['sortOrder'])?$_REQUEST['sortOrder']:'asc'; $sql = "select * from Logs"; $where = array(); if ( $minTime ) { preg_match( '/(.+)(\.\d+)/', $minTime, $matches ); $minTime = strtotime($matches[1]).$matches[2]; $where[] = "TimeKey >= ".$minTime; } if ( $maxTime ) { preg_match( '/(.+)(\.\d+)/', $maxTime, $matches ); $maxTime = strtotime($matches[1]).$matches[2]; $where[] = "TimeKey <= ".$maxTime; } foreach ( $filter as $field=>$value ) if ( $value != '' ) if ( $field == 'Level' ) $where[] = dbEscape($field)." <= ".dbEscape($value); else $where[] = dbEscape($field)." = '".dbEscape($value)."'"; if ( count($where) ) $sql.= " where ".join( " and ", $where ); $sql .= " order by ".dbEscape($sortField)." ".dbEscape($sortOrder); //$sql .= " limit ".dbEscape($limit); $format = isset($_REQUEST['format'])?$_REQUEST['format']:'text'; switch( $format ) { case 'text' : $exportExt = "txt"; break; case 'tsv' : $exportExt = "tsv"; break; case 'html' : $exportExt = "html"; break; case 'xml' : $exportExt = "xml"; break; default : Fatal( "Unrecognised log export format '$format'" ); } $exportKey = substr(md5(rand()),0,8); $exportFile = "zm-log.$exportExt"; $exportPath = "temp/zm-log-$exportKey.$exportExt"; if ( !($exportFP = fopen( $exportPath, "w" )) ) Fatal( "Unable to open log export file $exportFile" ); $logs = array(); foreach ( dbFetchAll( $sql ) as $log ) { $log['DateTime'] = preg_replace( '/^\d+/', strftime( "%Y-%m-%d %H:%M:%S", intval($log['TimeKey']) ), $log['TimeKey'] ); $logs[] = $log; } switch( $format ) { case 'text' : { foreach ( $logs as $log ) { if ( $log['Line'] ) fprintf( $exportFP, "%s %s[%d].%s-%s/%d [%s]\n", $log['DateTime'], $log['Component'], $log['Pid'], $log['Code'], $log['File'], $log['Line'], $log['Message'] ); else fprintf( $exportFP, "%s %s[%d].%s-%s [%s]\n", $log['DateTime'], $log['Component'], $log['Pid'], $log['Code'], $log['File'], $log['Message'] ); } break; } case 'tsv' : { fprintf( $exportFP, $SLANG['DateTime']."\t".$SLANG['Component']."\t".$SLANG['Pid']."\t".$SLANG['Level']."\t".$SLANG['Message']."\t".$SLANG['File']."\t".$SLANG['Line']."\n" ); foreach ( $logs as $log ) { fprintf( $exportFP, "%s\t%s\t%d\t%s\t%s\t%s\t%s\n", $log['DateTime'], $log['Component'], $log['Pid'], $log['Code'], $log['Message'], $log['File'], $log['Line'] ); } break; } case 'html' : { fwrite( $exportFP, ' '.$SLANG['ZoneMinderLog'].'

'.$SLANG['ZoneMinderLog'].'

'.htmlspecialchars(preg_match( '/%/', DATE_FMT_CONSOLE_LONG )?strftime( DATE_FMT_CONSOLE_LONG ):date( DATE_FMT_CONSOLE_LONG )).'

'.count($logs).' '.$SLANG['Logs'].'

' ); foreach ( $logs as $log ) { $classLevel = $log['Level']; if ( $classLevel < Logger::FATAL ) $classLevel = Logger::FATAL; elseif ( $classLevel > Logger::DEBUG ) $classLevel = Logger::DEBUG; $logClass = 'log-'.strtolower(Logger::$codes[$classLevel]); fprintf( $exportFP, " \n", $logClass, $log['DateTime'], $log['Component'], $log['Pid'], $log['Code'], $log['Message'], $log['File'], $log['Line'] ); } fwrite( $exportFP, '
'.$SLANG['DateTime'].''.$SLANG['Component'].''.$SLANG['Pid'].''.$SLANG['Level'].''.$SLANG['Message'].''.$SLANG['File'].''.$SLANG['Line'].'
%s%s%d%s%s%s%s
' ); break; } case 'xml' : { fwrite( $exportFP, ' '.$_POST['selector'].'' ); foreach ( $filter as $field=>$value ) if ( $value != '' ) fwrite( $exportFP, ' <'.strtolower($field).'>'.htmlspecialchars($value).' ' ); fwrite( $exportFP, ' '.$SLANG['DateTime'].''.$SLANG['Component'].''.$SLANG['Pid'].''.$SLANG['Level'].''.$SLANG['Message'].''.$SLANG['File'].''.$SLANG['Line'].' ' ); foreach ( $logs as $log ) { fprintf( $exportFP, " %s %s %d %s %s %d \n", $log['DateTime'], $log['Component'], $log['Pid'], $log['Code'], utf8_decode( $log['Message'] ), $log['File'], $log['Line'] ); } fwrite( $exportFP, ' ' ); break; } $exportExt = "xml"; break; } fclose( $exportFP ); ajaxResponse( array( 'key' => $exportKey, 'format' => $format, ) ); break; } case 'download' : { if ( !canView( 'System' ) ) ajaxError( 'Insufficient permissions to download logs' ); if ( empty($_REQUEST['key']) ) Fatal( "No log export key given" ); $exportKey = $_REQUEST['key']; if ( empty($_REQUEST['format']) ) Fatal( "No log export format given" ); $format = $_REQUEST['format']; switch( $format ) { case 'text' : $exportExt = "txt"; break; case 'tsv' : $exportExt = "tsv"; break; case 'html' : $exportExt = "html"; break; case 'xml' : $exportExt = "xml"; break; default : Fatal( "Unrecognised log export format '$format'" ); } $exportFile = "zm-log.$exportExt"; $exportPath = "temp/zm-log-$exportKey.$exportExt"; header( "Pragma: public" ); header( "Expires: 0" ); header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header( "Cache-Control: private", false ); // required by certain browsers header( "Content-Description: File Transfer" ); header( 'Content-Disposition: attachment; filename="'.$exportFile.'"' ); header( "Content-Transfer-Encoding: binary" ); header( "Content-Type: application/force-download" ); header( "Content-Length: ".filesize($exportPath) ); readfile( $exportPath ); exit( 0 ); break; } } ajaxError( 'Unrecognised action or insufficient permissions' ); ?>