1 | <?php |
||
2 | /** |
||
3 | * sysPass |
||
4 | * |
||
5 | * @author nuxsmin |
||
6 | * @link https://syspass.org |
||
7 | * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org |
||
8 | * |
||
9 | * This file is part of sysPass. |
||
10 | * |
||
11 | * sysPass is free software: you can redistribute it and/or modify |
||
12 | * it under the terms of the GNU General Public License as published by |
||
13 | * the Free Software Foundation, either version 3 of the License, or |
||
14 | * (at your option) any later version. |
||
15 | * |
||
16 | * sysPass is distributed in the hope that it will be useful, |
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
19 | * GNU General Public License for more details. |
||
20 | * |
||
21 | * You should have received a copy of the GNU General Public License |
||
22 | * along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
||
23 | */ |
||
24 | |||
25 | /** |
||
26 | * [type] [caller] data |
||
27 | */ |
||
28 | define('LOG_FORMAT', "[%s] [%s] %s"); |
||
29 | /** |
||
30 | * [timestamp] [type] [caller] data |
||
31 | */ |
||
32 | define('LOG_FORMAT_OWN', '[%s] syspass.%s: logger {"message":"%s","caller":"%s"}' . PHP_EOL); |
||
33 | |||
34 | /** |
||
35 | * Basic logger to handle some debugging and exception messages. |
||
36 | * |
||
37 | * It will log messages into syspass.log or PHP error log file. |
||
38 | * |
||
39 | * In order to log debugging messages, DEBUG constant must be set to true. |
||
40 | * |
||
41 | * A more advanced event logging should be handled through EventDispatcher |
||
42 | * |
||
43 | * @param mixed $data |
||
44 | * @param string $type |
||
45 | */ |
||
46 | function logger($data, $type = 'DEBUG') |
||
47 | { |
||
48 | if (!DEBUG && $type === 'DEBUG') { |
||
49 | return; |
||
50 | } |
||
51 | |||
52 | $date = date('Y-m-d H:i:s'); |
||
53 | $caller = getLastCaller(); |
||
54 | |||
55 | if (is_scalar($data)) { |
||
56 | $line = sprintf(LOG_FORMAT_OWN, $date, $type, $data, $caller); |
||
57 | } else { |
||
58 | $line = sprintf(LOG_FORMAT_OWN, $date, $type, print_r($data, true), $caller); |
||
59 | } |
||
60 | |||
61 | $useOwn = (!defined('LOG_FILE') |
||
62 | || !error_log($line, 3, LOG_FILE) |
||
63 | ); |
||
64 | |||
65 | if ($useOwn === false) { |
||
66 | if (is_scalar($data)) { |
||
67 | $line = sprintf(LOG_FORMAT, $type, $data, $caller); |
||
68 | } else { |
||
69 | $line = sprintf(LOG_FORMAT, $type, print_r($data, true), $caller); |
||
70 | } |
||
71 | |||
72 | error_log($line); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Print last callers from backtrace |
||
78 | */ |
||
79 | function printLastCallers() |
||
80 | { |
||
81 | logger(formatTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Print last caller from backtrace |
||
86 | * |
||
87 | * @param int $skip |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | function getLastCaller($skip = 2) |
||
92 | { |
||
93 | $callers = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5); |
||
94 | |||
95 | if (isset($callers[$skip], $callers[$skip]['class'], $callers[$skip]['function'])) { |
||
96 | return $callers[$skip]['class'] . '::' . $callers[$skip]['function']; |
||
97 | } |
||
98 | |||
99 | return 'N/A'; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param Throwable $e |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | function formatStackTrace(Throwable $e) |
||
108 | { |
||
109 | $out = []; |
||
110 | |||
111 | foreach ($e->getTrace() as $index => $trace) { |
||
112 | if (isset($trace['file'])) { |
||
113 | $file = sprintf('%s(%d)', $trace['file'], $trace['line']); |
||
114 | } else { |
||
115 | $file = '[internal function]'; |
||
116 | } |
||
117 | |||
118 | if (isset($trace['class'])) { |
||
119 | $function = sprintf('%s->%s', $trace['class'], $trace['function']); |
||
120 | } else { |
||
121 | $function = $trace['function']; |
||
122 | } |
||
123 | |||
124 | $args = []; |
||
125 | |||
126 | if (!empty($trace['args'])) { |
||
127 | foreach ($trace['args'] as $arg) { |
||
128 | $type = ucfirst(gettype($arg)); |
||
129 | |||
130 | if ($type === 'Object') { |
||
131 | $type = sprintf('Object(%s)', get_class($arg)); |
||
132 | } |
||
133 | |||
134 | $args[] = $type; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | $out[] = sprintf('#%d %s: %s(%s)', $index, $file, $function, implode(',', $args)); |
||
139 | } |
||
140 | |||
141 | return implode(PHP_EOL, $out); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Process an exception and log into the error log |
||
146 | * |
||
147 | * @param \Exception $exception |
||
148 | */ |
||
149 | function processException(\Exception $exception) |
||
150 | { |
||
151 | logger(sprintf("%s\n%s", __($exception->getMessage()), formatStackTrace($exception)), 'EXCEPTION'); |
||
152 | |||
153 | if (($previous = $exception->getPrevious()) !== null) { |
||
154 | logger(sprintf("(P) %s\n%s", __($previous->getMessage()), $previous->getTraceAsString()), 'EXCEPTION'); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param $trace |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | function formatTrace($trace) |
||
164 | { |
||
165 | $btLine = []; |
||
166 | $i = 0; |
||
167 | |||
168 | foreach ($trace as $caller) { |
||
169 | $class = isset($caller['class']) ? $caller['class'] : ''; |
||
170 | $file = isset($caller['file']) ? $caller['file'] : ''; |
||
171 | $line = isset($caller['line']) ? $caller['line'] : 0; |
||
172 | |||
173 | $btLine[] = sprintf('Caller %d: %s\%s (%s:%d)', $i, $class, $caller['function'], $file, $line); |
||
174 | $i++; |
||
175 | } |
||
176 | |||
177 | return implode(PHP_EOL, $btLine); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Alias gettext function |
||
182 | * |
||
183 | * @param string $message |
||
184 | * @param bool $translate Si es necesario traducir |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | function __($message, $translate = true) |
||
189 | { |
||
190 | return $translate === true && $message !== '' && mb_strlen($message) < 4096 ? gettext($message) : $message; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Returns an untranslated string (gettext placeholder). |
||
195 | * Dummy function to extract strings from source code |
||
196 | * |
||
197 | * @param string $message |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | function __u($message) |
||
202 | { |
||
203 | return $message; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Alias para obtener las locales de un dominio |
||
208 | * |
||
209 | * @param string $domain |
||
210 | * @param string $message |
||
211 | * @param bool $translate |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | function _t($domain, $message, $translate = true) |
||
216 | { |
||
217 | return $translate === true && $message !== '' && mb_strlen($message) < 4096 ? dgettext($domain, $message) : $message; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Capitalización de cadenas multi byte |
||
222 | * |
||
223 | * @param $string |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | function mb_ucfirst($string) |
||
228 | { |
||
229 | return mb_strtoupper(mb_substr($string, 0, 1)); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Devuelve el tiempo actual en coma flotante. |
||
234 | * Esta función se utiliza para calcular el tiempo de renderizado con coma flotante |
||
235 | * |
||
236 | * @param float $from |
||
237 | * |
||
238 | * @returns float con el tiempo actual |
||
239 | * @return float |
||
240 | */ |
||
241 | function getElapsedTime($from) |
||
242 | { |
||
243 | if ($from === 0) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
244 | return 0; |
||
245 | } |
||
246 | |||
247 | return microtime(true) - floatval($from); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Inicializar módulo |
||
252 | * |
||
253 | * @param $module |
||
254 | */ |
||
255 | function initModule($module) |
||
256 | { |
||
257 | $dir = dir(MODULES_PATH); |
||
258 | |||
259 | while (false !== ($entry = $dir->read())) { |
||
260 | $moduleFile = MODULES_PATH . DIRECTORY_SEPARATOR . $entry . DIRECTORY_SEPARATOR . 'module.php'; |
||
261 | |||
262 | if ($entry === $module && file_exists($moduleFile)) { |
||
263 | require $moduleFile; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | $dir->close(); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param $dir |
||
272 | * @param $levels |
||
273 | * |
||
274 | * @return bool|string |
||
275 | */ |
||
276 | function nDirname($dir, $levels) |
||
277 | { |
||
278 | if (version_compare(PHP_VERSION, '7.0') === -1) { |
||
279 | logger(realpath(dirname($dir) . str_repeat('../', $levels))); |
||
280 | |||
281 | return realpath(dirname($dir) . str_repeat('../', $levels)); |
||
282 | } |
||
283 | |||
284 | return dirname($dir, $levels); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Prints a fancy trace info using Xdebug extension |
||
289 | */ |
||
290 | function printTraceInfo() |
||
291 | { |
||
292 | if (DEBUG && extension_loaded('xdebug')) { |
||
293 | xdebug_print_function_stack(); |
||
294 | } |
||
295 | } |
||
296 |