<?
/*
* Controll of log files
*
* Wbarbaresco - Out/2003
*
* Example:
* $logs_errors = new wsy_log( "FILE" );
* $logs_errors->length = 10;
* $logs_errors->incluir( "MESSAGE" );
*
*/
class wsy_log{
# Name of file
var $file = "";
# Message error
var $error = "";
# If opened
var $opened = false;
# If loaded
var $loaded = false;
# If wrote
var $wrote = false;
# Array of logs
var $data = array();
# Delimiter
var $delimiter = "<WSY>";
# Number of registers in the log
var $length = 10;
// Load registers
function load(){
if ( !$this->opened ) { return false; }
$conteudo = @file_get_contents( $this->file );
$this->data = explode( $this->delimiter, $conteudo );
$nreg = count( $this->data );
$keys = array_keys($this->data);
for ( $x = 0; $x < $nreg; $x++ ) {
if ( trim($this->data[ $keys[$x] ]) == "" ) {
unset( $this->data[ $keys[$x] ] );
continue;
}
$this->data[ $keys[$x] ] = trim( $this->data[ $keys[$x] ] );
}
$this->loaded = true;
return true;
}
// Show data
function details(){
echo "<pre>";
$nreg = count( $this->data );
$keys = array_keys($this->data);
for ( $x = 0; $x < $nreg; $x++ ) {
echo "$x : ".$this->data[ $keys[$x] ]."\n";
}
echo "</pre>";
}
// Write data in the file
function write(){
if ( !$this->loaded ) { return false; }
$str = "";
$nreg = count( $this->data );
$keys = array_keys($this->data);
for ( $x = 0; $x < $nreg; $x++ ) {
if ( $this->data[ $keys[$x] ] != "" ) {
$str .= $this->data[ $keys[$x] ]."\n{$this->delimiter}\n";
}
}
$f = @fopen( $this->file, "w" );
if ( !$f ) {
$this->error = "Denied permission";
$this->wrote = false;
return false;
}
$this->wrote = true;
@fwrite( $f, $str );
@fclose($f);
}
// Insert new register
function insert( $str = "" ){
$this->load();
if ( !$this->loaded ) { return false; }
array_push( $this->data, date( "d/m/y H:i:s" )."\n".$str );
$this->delimiter_data();
$this->write();
}
// Delimiter of data
function delimiter_data(){
if ( $this->length > 0 ) {
$nreg = count( $this->data );
while( $nreg > $this->length ){
array_shift( $this->data );
$nreg--;
}
}
}
// Constructor
function wsy_log( $file ){
if ( !file_exists($file) ) {
$this->error = "File not Found. File: '$file'.";
$this->loaded = false;
$this->opened = false;
return false;
}
$this->file = $file;
$this->opened = true;
}
}
?>