TYPO3 CMS  TYPO3_6-2
FileWriter.php
Go to the documentation of this file.
1 <?php
3 
21 
29 class FileWriter extends AbstractWriter {
30 
36  protected $logFile = '';
37 
43  protected $defaultLogFile = 'typo3temp/logs/typo3.log';
44 
54  static protected $logFileHandles = array();
55 
62  public function __construct(array $options = array()) {
63  // the parent constructor reads $options and sets them
64  parent::__construct($options);
65  if (empty($options['logFile'])) {
66  $this->setLogFile($this->defaultLogFile);
67  }
68  }
69 
73  public function __destruct() {
74  $this->closeLogFile();
75  }
76 
84  public function setLogFile($relativeLogFile) {
85  $logFile = $relativeLogFile;
86  // Skip handling if logFile is a stream resource. This is used by unit tests with vfs:// directories
87  if (FALSE === strpos($logFile, '://') && !PathUtility::isAbsolutePath($logFile)) {
89  if ($logFile === NULL) {
90  throw new \InvalidArgumentException('Log file path "' . $relativeLogFile . '" is not valid!', 1326411176);
91  }
92  }
93  $this->logFile = $logFile;
94  $this->openLogFile();
95 
96  return $this;
97  }
98 
104  public function getLogFile() {
105  return $this->logFile;
106  }
107 
115  public function writeLog(LogRecord $record) {
116  $timestamp = date('r', (int)$record->getCreated());
117  $levelName = LogLevel::getName($record->getLevel());
118  $data = '';
119  $recordData = $record->getData();
120  if (!empty($recordData)) {
121  // According to PSR3 the exception-key may hold an \Exception
122  // Since json_encode() does not encode an exception, we run the _toString() here
123  if (isset($recordData['exception']) && $recordData['exception'] instanceof \Exception) {
124  $recordData['exception'] = (string)$recordData['exception'];
125  }
126  $data = '- ' . json_encode($recordData);
127  }
128 
129  $message = sprintf(
130  '%s [%s] request="%s" component="%s": %s %s',
131  $timestamp,
132  $levelName,
133  $record->getRequestId(),
134  $record->getComponent(),
135  $record->getMessage(),
136  $data
137  );
138 
139  if (FALSE === fwrite(self::$logFileHandles[$this->logFile], $message . LF)) {
140  throw new \RuntimeException('Could not write log record to log file', 1345036335);
141  }
142 
143  return $this;
144  }
145 
152  protected function openLogFile() {
153  if (is_resource(self::$logFileHandles[$this->logFile])) {
154  return;
155  }
156 
157  $this->createLogFile();
158  self::$logFileHandles[$this->logFile] = fopen($this->logFile, 'a');
159  if (!is_resource(self::$logFileHandles[$this->logFile])) {
160  throw new \RuntimeException('Could not open log file "' . $this->logFile . '"', 1321804422);
161  }
162  }
163 
169  protected function closeLogFile() {
170  if (is_resource(self::$logFileHandles[$this->logFile])) {
171  fclose(self::$logFileHandles[$this->logFile]);
172  unset(self::$logFileHandles[$this->logFile]);
173  }
174  }
175 
182  protected function createLogFile() {
183  if (file_exists($this->logFile)) {
184  return;
185  }
186  $logFileDirectory = dirname($this->logFile);
187  if (!@is_dir($logFileDirectory)) {
188  GeneralUtility::mkdir_deep($logFileDirectory);
189  // create .htaccess file if log file is within the site path
190  if (PathUtility::getCommonPrefix(array(PATH_site, $logFileDirectory)) === PATH_site) {
191  // only create .htaccess, if we created the directory on our own
192  $this->createHtaccessFile($logFileDirectory . '/.htaccess');
193  }
194  }
195  // create the log file
196  GeneralUtility::writeFile($this->logFile, '');
197  }
198 
205  protected function createHtaccessFile($htaccessFile) {
206  // write .htaccess file to protect the log file
207  if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['generateApacheHtaccess']) && !file_exists($htaccessFile)) {
208  $htaccessContent = '
209 # Apache < 2.3
210 <IfModule !mod_authz_core.c>
211  Order allow,deny
212  Deny from all
213  Satisfy All
214 </IfModule>
215 
216 # Apache ≥ 2.3
217 <IfModule mod_authz_core.c>
218  Require all denied
219 </IfModule>
220  ';
221  GeneralUtility::writeFile($htaccessFile, $htaccessContent);
222  }
223  }
224 
225 }
static mkdir_deep($directory, $deepDirectory='')
static writeFile($file, $content, $changePermissions=FALSE)
static getCommonPrefix(array $paths)
__construct(array $options=array())
Definition: FileWriter.php:62
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)
static getName($level)
Definition: LogLevel.php:111