* @license PHP License * @package WB * @subpackage content */ /** * Load base class */ WBClass::load('WBContent'); /** * Content component: Tail * * Emulate Unix command tail * * @version 1.0.0 * @package WB * @subpackage content */ class WBContent_Tail extends WBContent { /** * my parameter list * * - lines: number of lines, see "tail -n " * - file: one file name or list of files * - asc: reverse order of lines * - cmd: actual command, default is tail, but grep with pipes and tail also work * - requiredgroup: name of user group * @var array */ protected $config = array( 'lines' => 20, 'file' => 'var/log/wombat.log', 'requiredgroup' => WBContent::GROUP_ANON, 'asc' => 0, 'cmd' => 'tail -n %d %s' ); /** * Collected lines */ private $lines = array(); /** * run * * run component * * @return array parameter list */ public function run() { if (!$this->isUserInGroup($this->config['requiredgroup'])) { return $this->config; } if (!is_array($this->config['file'])) { $this->config['file'] = array($this->config['file']); } if ($this->config['asc']) { sort($this->config['file']); } else { rsort($this->config['file']); } $this->lines = $this->tailFile(); if (!$this->config['asc']) { $this->lines = array_reverse($this->lines); } return $this->config; } /** * Do actual tail on file(s) * * Execute command for each file * * @return array $lines */ protected function tailFile() { $lines = array(); foreach ($this->config['file'] as $path) { $file = basename($path); $realpath = $path; if ('/' != $realpath[0]) { $realpath = WBParam::get('wb/dir/base') . '/' . $realpath; } if (!is_readable($realpath)) { continue; } $cmd = sprintf($this->config['cmd'], $this->config['lines'], $realpath); $tf = popen($cmd, 'r'); while ($line = fgets($tf)) { $lines[] = array( 'file' => $file, 'path' => $path, 'realpath' => $realpath, 'line' => $line ); } fclose($tf); } return $lines; } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { if ($this->isUserInGroup($this->config['requiredgroup'])) { $this->loadTemplates('list'); $this->tmpl->addRows('line_list_entry', $this->lines); $this->tmpl->addVar('file_list_entry', 'file', $this->config['file']); } else { $this->loadTemplates('anon'); } $this->tmpl->addGlobalVars($this->config); return $this->tmpl->getParsedTemplate('snippet'); } }