[SPIP] v3.2.1-->v3.2.2
[lhc/web/www.git] / www / ecrire / inc / log.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) {
14 return;
15 }
16
17 function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) {
18 static $test_repertoire = array();
19 static $compteur = array();
20 static $debugverb = ''; // pour ne pas le recalculer au reappel
21
22 if (is_null($logname) or !is_string($logname)) {
23 $logname = defined('_FILE_LOG') ? _FILE_LOG : 'spip';
24 }
25 if (!isset($compteur[$logname])) {
26 $compteur[$logname] = 0;
27 }
28 if ($logname != 'maj'
29 and defined('_MAX_LOG')
30 and (
31 $compteur[$logname]++ > _MAX_LOG
32 or !$GLOBALS['nombre_de_logs']
33 or !$GLOBALS['taille_des_logs']
34 )
35 ) {
36 return;
37 }
38
39 $logfile = ($logdir === null ? _DIR_LOG : $logdir)
40 . ($logname)
41 . ($logsuf === null ? _FILE_LOG_SUFFIX : $logsuf);
42
43 if (!isset($test_repertoire[$d = dirname($logfile)])) {
44 $test_repertoire[$d] = false; // eviter une recursivite en cas d'erreur de sous_repertoire
45 $test_repertoire[$d] = (@is_dir($d) ? true : (function_exists('sous_repertoire') ? sous_repertoire(
46 $d,
47 '',
48 false,
49 true
50 ) : false));
51 }
52
53 // si spip_log() dans mes_options, ou repertoire log/ non present, poser dans tmp/
54 if (!defined('_DIR_LOG') or !$test_repertoire[$d]) {
55 $logfile = _DIR_RACINE . _NOM_TEMPORAIRES_INACCESSIBLES . $logname . '.log';
56 }
57
58 $rotate = 0;
59 $pid = '(pid ' . @getmypid() . ')';
60
61 // accepter spip_log( Array )
62 if (!is_string($message)) {
63 $message = var_export($message, true);
64 }
65
66 if (!$debugverb and defined('_LOG_FILELINE') and _LOG_FILELINE) {
67 $debug = debug_backtrace();
68 $l = $debug[1]['line'];
69 $fi = $debug[1]['file'];
70 if (strncmp($fi, _ROOT_RACINE, strlen(_ROOT_RACINE)) == 0) {
71 $fi = substr($fi, strlen(_ROOT_RACINE));
72 }
73 $fu = isset($debug[2]['function']) ? $debug[2]['function'] : '';
74 $debugverb = "$fi:L$l:$fu" . '():';
75 }
76
77 $m = date('Y-m-d H:i:s') . ' ' . (isset($GLOBALS['ip']) ? $GLOBALS['ip'] : '') . ' ' . $pid . ' '
78 //distinguer les logs prives et publics dans les grep
79 . $debugverb
80 . (test_espace_prive() ? ':Pri:' : ':Pub:')
81 . preg_replace("/\n*$/", "\n", $message);
82
83
84 if (@is_readable($logfile)
85 and (!$s = @filesize($logfile) or $s > $GLOBALS['taille_des_logs'] * 1024)
86 ) {
87 $rotate = $GLOBALS['nombre_de_logs'];
88 $m .= "[-- rotate --]\n";
89 }
90
91 $f = @fopen($logfile, 'ab');
92 if ($f) {
93 fputs($f, (defined('_LOG_BRUT') and _LOG_BRUT) ? $m : str_replace('<', '&lt;', $m));
94 fclose($f);
95 }
96
97 if ($rotate-- > 0
98 and function_exists('spip_unlink')
99 ) {
100 spip_unlink($logfile . '.' . $rotate);
101 while ($rotate--) {
102 @rename($logfile . ($rotate ? '.' . $rotate : ''), $logfile . '.' . ($rotate + 1));
103 }
104 }
105
106 // Dupliquer les erreurs specifiques dans le log general
107 if ($logname !== _FILE_LOG
108 and defined('_FILE_LOG')
109 ) {
110 inc_log_dist($logname == 'maj' ? 'cf maj.log' : $message);
111 }
112 $debugverb = '';
113 }