Location: PHPKode > projects > Libretto: Web download manager > libretto/includes/class.tail.php
<?php
/**
 * The class class.tail.php
 * 
 * The tail class is for structured reading of files
 * that are continuous increasing in lines (e.g. logfiles)
 */
//	...
 /**
 */
class	tail
{
/**
 * timestamp of last modify (mtime)
 */
var $timestamp = 0;

/**
 * size of file
 */
var $filesize;

/**
 * complete filename, including path
 */
var $filename;
var $flags;

/**
 * array with seperated lines of file
 */
var $linesarray = array();
var $outputarray = array();

/**
 * total number of lines
 */
var $lines;

/**
 * last line showed
 */
var $lastline;

/**
 * how many lines should be shown - defaults to 10
 */
var $showlines = 10;

/**
 * searchstring, might be a regular expression
 */
var $regexpr = '';

/**
 * highlight string
 */
var $highlight;

/**
 * constructor for tail-class
 *
 * @access public
 * @param string filename
 * @version 2005-12-14
 */
function tail($filename, $flags=0) {
	if (file_exists($filename)) {
		$this->filename = $filename;
		$this->flags = $flags;
	} else
		return null;
}

/**
 * @access public
 * @param integer format use defined consts to change format of output
 * @return array the upcoming lines
 * @version 2005-12-15
 */
function output() {
	$new_timestamp = filemtime($this->filename);
	// check for change
	if ($new_timestamp > $this->timestamp) {
		$this->timestamp = $new_timestamp;
		$this->openFile();
		if ( $this->regexpr )
			$this->doGrep();
		$this->linesarray = array();
		for ($i = ( $this->lines > $this->showlines ? $this->lines - $this->showlines : 0 );
			$i < $this->lines; $i++)
			$this->linesarray[] = $this->outputarray[$i];
	}
	return $this->linesarray;
}

function file2($filename, $flags) {
	$s = $this->filesize;
	if ( $s == 0 )
		return false;
	if ( $s > 2048 )
		$s = 2048;
	$fp = fopen($filename, "rb");
	fseek($fp, - $s, SEEK_END);
	$buffer = fread($fp, $s);
	fclose($fp);
	$lines = preg_split("/\r?\n|\r/", $buffer);
	return $lines;
}

/**
 * open file as set in tail::filename
 *
 * @access private
 * TODO optimize for memory usage (use fopen, fseek, fgets)
 * @version 2005-12-15
 */
function openFile() {
	$this->filesize = filesize($this->filename);
	$this->outputarray = $this->file2($this->filename, $this->flags);
	$this->lines = count($this->outputarray);
}

/**
 * search for a regular expression. highlight of searchstring is not possible, yet.
 *
 * @access public
 * @param string regexp RegExpr as searchstring (delimiter is set already). only lines containing this string are returned
 * @version 2005-12-15
 */
function setGrepRegExpr($regexp) {
	$this->regexpr = '~('.$regexp.')~';
	$this->timestamp = 0;
}

/**
 * search for a case-sensitive string
 *
 * @access public
 * @param string searchword a simple (single) word as searchstring. only lines containing this string are returned
 * @version 2005-12-15
 */
function setGrep($searchword) {
	$this->regexpr = "~.*(".$searchword.").*~i";
	$this->highlight = $searchword;
	$this->timestamp = 0;
}

/**
 * search for a case-INsensitive string
 *
 * @access public
 * @param string string a simple (single) word as searchstring. only lines containing this string are returned
 * @version 2005-12-15
 */
function setGrepi($string) {
	$this->regexpr = "~.*(".$string.").*~i";
	$this->highlight = $string;
	$this->timestamp = 0;
}

/**
 * shrink lines to that containing the regexp
 * @access private
 * @version 2005-12-15
 */
function doGrep() {
	if (strlen($this->regexpr) > 3) {
		$this->outputarray = preg_grep($this->regexpr,$this->outputarray);
		$this->lines = count($this->outputarray);
	}
}

/**
 * set the numbers of lines shown
 * @access public
 * @param integer lines the number of lines to be shown
 * @version 2005-12-15
 */
function setNumberOfLines($lines) {
	$this->showlines = $lines;
	$this->timestamp = 0;
}
}
?>
Return current item: Libretto: Web download manager