OPiQuotations  v.03.00.00 — April 5, 2019
log.inc
Go to the documentation of this file.
1 <?php // -*- coding: utf-8 -*-
2 
3 /** \file log.inc
4  * (September 3, 2016)
5  *
6  * \brief
7  * Function to write in errors log file.
8  *
9  * Piece of OPiQuotations.
10  * https://bitbucket.org/OPiMedia/opiquotations
11  *
12  * GPLv3 --- Copyright (C) 2014, 2016 Olivier Pirson
13  * http://www.opimedia.be/
14  *
15  * @package OPiQuotations
16  */
17 namespace OPiQuotations;
18 
19 
20 /**
21  * \brief
22  * Filename of errors log file.
23  */
24 const LOG_FILE = 'logs/log.txt';
25 
26 
27 
28 /**
29  * \brief
30  * Error handler function.
31  *
32  * Get all errors specified by error_reporting()
33  * and write message with to_log() function.
34  *
35  * If error is an E_USER_ERROR
36  * then exit,
37  * else continue.
38  *
39  * This handler must be activated by set_error_handler('\\OPiQuotations\\error_handler').
40  *
41  * See http://www.php.net/manual/en/function.set-error-handler.php
42  *
43  * @param int $errno
44  * @param string $errstr
45  * @param string $errfile
46  * @param int $errline
47  * @param array $errcontext
48  */
49 function error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
50  if (!(error_reporting() & $errno)) { // error not specified by error_reporting()
51  return false;
52  }
53 
54  $exit = ($errno === E_USER_ERROR);
55 
56  $message = 'PHP error '.$errno.' "'.$errstr.'"
57  line '.$errline.' in "'.$errfile.'"
58  '.($exit
59  ? 'exit'
60  : 'continue').'
61 '.print_r($errcontext, true).'debug_backtrace:
62 '.print_r(debug_backtrace(), true);
63 
64  to_log($message);
65 
66  if ($exit) {
67  exit(1);
68  }
69 
70  return true;
71 }
72 
73 
74 /**
75  * \brief
76  * If $s contains an element '[password] => ...'
77  * then hides the password value.
78  *
79  * @param string $s
80  */
81 function hide_password($s) {
82  require 'OPiQuotations/.private/db_login.inc';
83 
84  return (empty($db_password)
85  ? $s
86  : preg_replace('/'.preg_quote($db_password, '/').'/', '****',
87  preg_replace('/\[password\]\s*=>\s*\S+/i', '[password] => ****', $s)));
88 }
89 
90 
91 /**
92  * \brief
93  * Append $message in LOG_FILE.
94  *
95  * If the specified mail in '.private/log_email.inc' is not empty
96  * then send also the message to this mail.
97  *
98  * If $message is not a string
99  * then a warning is added to the message
100  * and $message is converted to a string.
101  *
102  * @param mixed $message
103  */
104 function to_log($message) {
105  if (!is_string($message) ) {
106  $message = 'This error message wasn\'t a string!
107 '.print_r($message, true);
108  }
109 
110  $dt = new \DateTime();
111 
112  $message = '***************************************
113 *** '.$dt->format('r').' ***
114 ***************************************
115 '.hide_password($message).'
116 
117 ';
118 
119  #DEBUG
120  if (true) {
121  echo '<pre>to_log():
122 ';
123  var_dump($message);
124  echo '</pre>';
125  }
126  #DEBUG_END
127 
128  error_log($message, 3, realpath(realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.LOG_FILE));
129 
130  require_once 'OPiQuotations/.private/log_email.inc';
131 
132  if (!empty($log_email)) {
133  error_log($message, 1, $log_email);
134  }
135 }
136 
137 
138 return true;
139 
140 ?>
to_log($message)
Append $message in LOG_FILE.
Definition: log.inc:104
error_handler($errno, $errstr, $errfile, $errline, $errcontext)
Error handler function.
Definition: log.inc:49
const LOG_FILE
Filename of errors log file.
Definition: log.inc:24
hide_password($s)
If $s contains an element &#39;[password] => ...&#39; then hides the password value.
Definition: log.inc:81