1<?php
2/***********************************************
3 * File      :   filelog.php
4 * Project   :   Z-Push
5 * Descr     :   Logging functionalities
6 *
7 * Created   :   13.11.2015
8 *
9 * Copyright 2007 - 2016 Zarafa Deutschland GmbH
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License, version 3,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 *
23 * Consult LICENSE file for details
24 ************************************************/
25
26class FileLog extends Log {
27
28    /**
29     * @var string|bool
30     */
31    private $log_to_user_file = false;
32
33    /**
34     * Constructor
35     */
36    public function __construct() {
37    }
38
39    /**
40     * Get the log user file.
41     *
42     * @access private
43     * @return string
44     */
45    private function getLogToUserFile() {
46        if ($this->log_to_user_file === false) {
47            if (in_array(strtolower($this->GetDevid()), ['','webservice','validate'])) {
48                $this->setLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log');
49            }
50            else {
51                $this->setLogToUserFile(
52                        preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) .'-'.
53                        (($this->GetAuthUser() != $this->GetUser()) ? preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetUser())) .'-' : '') .
54                        preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetDevid())) .
55                        '.log'
56                        );
57            }
58        }
59        return $this->log_to_user_file;
60    }
61
62    /**
63     * Set user log-file relative to log directory.
64     *
65     * @param string $value
66     *
67     * @access private
68     * @return void
69     */
70    private function setLogToUserFile($value) {
71        $this->log_to_user_file = $value;
72    }
73
74    /**
75     * Returns the string to be logged.
76     *
77     * @param int       $loglevel
78     * @param string    $message
79     * @param boolean   $includeUserDevice  puts username and device in the string, default: true
80     *
81     * @access public
82     * @return string
83     */
84    public function BuildLogString($loglevel, $message, $includeUserDevice = true) {
85        $log = Utils::GetFormattedTime() .' ['. str_pad($this->GetPid(),5," ",STR_PAD_LEFT) .'] '. $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO);
86
87        if ($includeUserDevice) {
88            // when the users differ, we need to log both
89            if (strcasecmp($this->GetAuthUser(), $this->GetUser()) == 0) {
90                $log .= ' ['. $this->GetUser() .']';
91            }
92            else {
93                $log .= ' ['. $this->GetAuthUser() . Request::IMPERSONATE_DELIM . $this->GetUser() .']';
94            }
95        }
96        if ($includeUserDevice && (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && $this->IsAuthUserInSpecialLogUsers()))) {
97            $log .= ' ['. $this->GetDevid() .']';
98        }
99        $log .= ' ' . $message;
100        return $log;
101    }
102
103    //
104    // Implementation of Log
105    //
106
107    /**
108     * Writes a log message to the general log.
109     *
110     * @param int $loglevel
111     * @param string $message
112     *
113     * @access protected
114     * @return void
115     */
116    protected function Write($loglevel, $message) {
117        $data = $this->BuildLogString($loglevel, $message) . PHP_EOL;
118        @file_put_contents(LOGFILE, $data, FILE_APPEND);
119    }
120
121    /**
122     * Writes a log message to the user specific log.
123     * @param int $loglevel
124     * @param string $message
125     *
126     * @access public
127     * @return void
128     */
129    public function WriteForUser($loglevel, $message) {
130        $data = $this->BuildLogString($loglevel, $message, false) . PHP_EOL;
131        @file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND);
132    }
133
134    /**
135     * This function is used as an event for log implementer.
136     * It happens when the a call to the Log function is finished.
137     *
138     * @access protected
139     * @return void
140     */
141    protected function afterLog($loglevel, $message) {
142        if ($loglevel & (LOGLEVEL_FATAL | LOGLEVEL_ERROR | LOGLEVEL_WARN)) {
143            $data = $this->BuildLogString($loglevel, $message) . PHP_EOL;
144            @file_put_contents(LOGERRORFILE, $data, FILE_APPEND);
145        }
146    }
147}
148