Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| common_log_ArchiveFileAppender | |
0.00% |
0 / 48 |
|
0.00% |
0 / 3 |
552 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
132 | |||
| initFile | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
72 | |||
| getAvailableArchiveFileName | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * Short description of class common_log_ArchiveFileAppender |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 32 | * @package generis |
| 33 | |
| 34 | */ |
| 35 | class common_log_ArchiveFileAppender extends common_log_SingleFileAppender |
| 36 | { |
| 37 | // --- ASSOCIATIONS --- |
| 38 | |
| 39 | |
| 40 | // --- ATTRIBUTES --- |
| 41 | |
| 42 | /** |
| 43 | * Short description of attribute COMPRESSION_ZIP |
| 44 | * |
| 45 | * @access public |
| 46 | * @var string |
| 47 | */ |
| 48 | public const COMPRESSION_ZIP = 'zip'; |
| 49 | |
| 50 | /** |
| 51 | * Short description of attribute COMPRESSION_NONE |
| 52 | * |
| 53 | * @access public |
| 54 | * @var string |
| 55 | */ |
| 56 | public const COMPRESSION_NONE = 'none'; |
| 57 | |
| 58 | /** |
| 59 | * Short description of attribute directory |
| 60 | * |
| 61 | * @access public |
| 62 | * @var string |
| 63 | */ |
| 64 | public $directory = ''; |
| 65 | |
| 66 | /** |
| 67 | * Short description of attribute compression |
| 68 | * |
| 69 | * @access public |
| 70 | * @var string |
| 71 | */ |
| 72 | public $compression = 'zip'; |
| 73 | |
| 74 | // --- OPERATIONS --- |
| 75 | |
| 76 | /** |
| 77 | * Short description of method init |
| 78 | * |
| 79 | * @access public |
| 80 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 81 | * @param array configuration |
| 82 | * @return boolean |
| 83 | */ |
| 84 | public function init($configuration) |
| 85 | { |
| 86 | $returnValue = (bool) false; |
| 87 | |
| 88 | |
| 89 | if (isset($configuration['directory']) && $configuration['directory']) { |
| 90 | $this->directory = rtrim($configuration['directory'], DIRECTORY_SEPARATOR); |
| 91 | } elseif (isset($configuration['file'])) { |
| 92 | $this->directory = dirname($configuration['file']); |
| 93 | } |
| 94 | if (isset($configuration['compression'])) { |
| 95 | if (is_bool($configuration['compression'])) { |
| 96 | $this->compression = $configuration['compression'] ? self::COMPRESSION_ZIP : self::COMPRESSION_NONE; |
| 97 | } else { |
| 98 | switch ($configuration['compression']) { |
| 99 | case self::COMPRESSION_ZIP: |
| 100 | $this->compression = self::COMPRESSION_ZIP; |
| 101 | break; |
| 102 | case self::COMPRESSION_NONE: |
| 103 | $this->compression = self::COMPRESSION_NONE; |
| 104 | break; |
| 105 | default: |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (!empty($this->directory)) { |
| 112 | $returnValue = parent::init($configuration); |
| 113 | } else { |
| 114 | $returnValue = false; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | return (bool) $returnValue; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Short description of method initFile |
| 123 | * |
| 124 | * @access public |
| 125 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 126 | * @return mixed |
| 127 | */ |
| 128 | public function initFile() |
| 129 | { |
| 130 | |
| 131 | if ($this->maxFileSize > 0 && file_exists($this->filename) && filesize($this->filename) >= $this->maxFileSize) { |
| 132 | if ($this->compression == self::COMPRESSION_ZIP) { |
| 133 | $zip = new ZipArchive(); |
| 134 | $res = $zip->open($this->getAvailableArchiveFileName(), ZipArchive::CREATE); |
| 135 | if ($res === true) { |
| 136 | $zip->addFile($this->filename, basename($this->filename)); |
| 137 | $zip->close(); |
| 138 | unlink($this->filename); |
| 139 | } else { |
| 140 | //fail silently |
| 141 | return false; |
| 142 | } |
| 143 | } elseif ($this->compression == self::COMPRESSION_NONE) { |
| 144 | $success = rename($this->filename, $this->getAvailableArchiveFileName()); |
| 145 | if (!$success) { |
| 146 | //fail silently |
| 147 | return false; |
| 148 | } |
| 149 | } else { |
| 150 | //fail silently |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | $this->filehandle = @fopen($this->filename, 'a'); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Short description of method getAvailableArchiveFileName |
| 159 | * |
| 160 | * @access private |
| 161 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 162 | * @return string |
| 163 | */ |
| 164 | private function getAvailableArchiveFileName() |
| 165 | { |
| 166 | $returnValue = (string) ''; |
| 167 | |
| 168 | |
| 169 | $filebase = basename($this->filename); |
| 170 | $dotpos = strrpos($filebase, "."); |
| 171 | if ($dotpos === false) { |
| 172 | $dotpos = strlen($filebase); |
| 173 | } |
| 174 | $prefix = $this->directory . DIRECTORY_SEPARATOR . substr($filebase, 0, $dotpos) . "_" . date('Y-m-d'); |
| 175 | $sufix = substr($filebase, $dotpos) . ($this->compression === self::COMPRESSION_ZIP ? '.zip' : ''); |
| 176 | $count_string = ""; |
| 177 | $count = 0; |
| 178 | while (file_exists($prefix . $count_string . $sufix)) { |
| 179 | $count_string = "_" . ++$count; |
| 180 | } |
| 181 | $returnValue = $prefix . $count_string . $sufix; |
| 182 | |
| 183 | |
| 184 | return (string) $returnValue; |
| 185 | } |
| 186 | } |