* @package generis */ class common_log_ArchiveFileAppender extends common_log_SingleFileAppender { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Short description of attribute COMPRESSION_ZIP * * @access public * @var string */ const COMPRESSION_ZIP = 'zip'; /** * Short description of attribute COMPRESSION_NONE * * @access public * @var string */ const COMPRESSION_NONE = 'none'; /** * Short description of attribute directory * * @access public * @var string */ public $directory = ''; /** * Short description of attribute compression * * @access public * @var string */ public $compression = 'zip'; // --- OPERATIONS --- /** * Short description of method init * * @access public * @author Joel Bout, * @param array configuration * @return boolean */ public function init($configuration) { $returnValue = (bool) false; if (isset($configuration['directory']) && $configuration['directory']) { $this->directory = rtrim($configuration['directory'], DIRECTORY_SEPARATOR); } elseif (isset($configuration['file'])) { $this->directory = dirname($configuration['file']); } if (isset($configuration['compression'])) { if (is_bool($configuration['compression'])) { $this->compression = $configuration['compression'] ? self::COMPRESSION_ZIP : self::COMPRESSION_NONE; } else { switch ($configuration['compression']) { case self::COMPRESSION_ZIP: $this->compression = self::COMPRESSION_ZIP; break; case self::COMPRESSION_NONE: $this->compression = self::COMPRESSION_NONE; break; default: return false; } } } if (!empty($this->directory)) { $returnValue = parent::init($configuration); } else { $returnValue = false; } return (bool) $returnValue; } /** * Short description of method initFile * * @access public * @author Joel Bout, * @return mixed */ public function initFile() { if ($this->maxFileSize > 0 && file_exists($this->filename) && filesize($this->filename) >= $this->maxFileSize) { if ($this->compression == self::COMPRESSION_ZIP) { $zip = new ZipArchive(); $res = $zip->open($this->getAvailableArchiveFileName(), ZipArchive::CREATE); if ($res === true) { $zip->addFile($this->filename, basename($this->filename)); $zip->close(); unlink($this->filename); } else { //fail silently return false; } } elseif ($this->compression == self::COMPRESSION_NONE) { $success = rename($this->filename, $this->getAvailableArchiveFileName()); if (!$success) { //fail silently return false; } } else { //fail silently return false; } } $this->filehandle = @fopen($this->filename, 'a'); } /** * Short description of method getAvailableArchiveFileName * * @access private * @author Joel Bout, * @return string */ private function getAvailableArchiveFileName() { $returnValue = (string) ''; $filebase = basename($this->filename); $dotpos = strrpos($filebase, "."); if ($dotpos === false) { $dotpos = strlen($filebase); } $prefix = $this->directory . DIRECTORY_SEPARATOR . substr($filebase, 0, $dotpos) . "_" . date('Y-m-d'); $sufix = substr($filebase, $dotpos) . ($this->compression === self::COMPRESSION_ZIP ? '.zip' : ''); $count_string = ""; $count = 0; while (file_exists($prefix . $count_string . $sufix)) { $count_string = "_" . ++$count; } $returnValue = $prefix . $count_string . $sufix; return (string) $returnValue; } }