Alternc  latest
Alternc logiel libre pour l'hébergement
m_messages.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  ----------------------------------------------------------------------
5  LICENSE
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License (GPL)
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  To read the license please visit http://www.gnu.org/copyleft/gpl.html
18  ----------------------------------------------------------------------
19 */
20 
21 /**
22  * Handle messages (error, warning, info, ok) appearing in API calls.
23  *
24  * <p>This class handles messages appearing while calling API functions of AlternC
25  * Those messages are stored as a number (class-id) and a message
26  * localized messages are available</p>
27  * <p>This class also handle inserting those messages into the logging
28  * system in /var/log/alternc/bureau.log
29  * </p>
30  *
31  * @copyright AlternC-Team 2000-2017 https://alternc.com/
32  */
33 class m_messages {
34 
35  /** Contains the messages and their ID */
36  var $arrMessages = array();
37 
38  var $logfile = "/var/log/alternc/bureau.log";
39 
40  /** List of possible message level */
41  var $ARRLEVEL = array("ERROR", "ALERT", "INFO");
42 
43  /** CSS classes for each level */
44  var $ARRCSS = array(
45  // emergency critical warning notice debug : currently not used (from PSR-3)
46  "ERROR" => "alert-danger",
47  "ALERT" => "alert-warning",
48  "INFO" => "alert-success",
49  );
50 
51  public function __construct() {
52  $this->init_msgs();
53  }
54 
55 
56  /**
57  * Record a message, insert it into the logfile.
58  *
59  * This function records a message, add it to the logfile,
60  * and make it available for the web panel to print it later.
61  *
62  * @param string $cat The category of the msg array to work with
63  * @param integer $clsid Which class raises this message
64  * @param mixed $msg The message
65  * @param array $param Non-mandatory array of string parameter for this message
66  * @return boolean TRUE if the message got recorded, FALSE if not.
67  *
68  */
69  function raise($level = "ERROR", $clsid, $msg, $param = "") {
70  $arrInfos = array();
71 
72  $level = strtoupper($level);
73  if (! in_array($level, $this->ARRLEVEL)) {
74  throw new Exception('Missing or unknown level in a raise() call');
75  }
76 
77  $arrInfos['clsid'] = $clsid;
78  $arrInfos['msg'] = $msg;
79  $arrInfos['param'] = is_array($param)?$param:(empty($param)?"":array($param));
80 
81  $this->arrMessages[$level][] = $arrInfos;
82 
83  $this->logAlternC($level,$arrInfos);
84  return true;
85  }
86 
87 
88  /**
89  * Reset the stored messages array
90  */
91  function init_msgs() {
92  // $me=debug_backtrace(); $this->log("messages", "init_msgs from ".$me[1]["class"].".".$me[1]["function"]);
93  foreach ($this->ARRLEVEL as $v) {
94  $this->arrMessages[$v] = array();
95  }
96  }
97 
98 
99  /**
100  * Tell if there are stored messages for a specific level
101  * or for all levels (if level is empty)
102  *
103  * @param string $level The level of the msg array to work with (if empty or not set, use all levels)
104  * @return boolean TRUE if there is/are msg recorded.
105  *
106  */
107  function has_msgs($level="") {
108  $level = strtoupper($level);
109  if (in_array($level, $this->ARRLEVEL)) {
110  return (count($this->arrMessages[$level]) > 0);
111  } else {
112  foreach ($this->arrMessages as $v) {
113  if (count($v) > 0)
114  return true;
115  }
116  return false;
117  }
118  }
119 
120 
121  /**
122  * Return a string of concateneted messages of all recorded messages
123  * or only the last message
124  *
125  * @param string $level The level of the msg array to work with
126  * @param boolean $all show all the messages or only the last one
127  *
128  * @access private
129  * @return string Message.
130  *
131  */
132  function msg_str($level = "ERROR", $all = true) {
133  $str = "";
134 
135  $level = strtoupper($level);
136  if (! in_array($level, $this->ARRLEVEL)) {
137  throw new Exception('Missing or unknown level in a raise() call');
138  }
139 
140  if (! $this->has_msgs($level))
141  return "";
142 
143  if ($all) {
144  foreach ($this->arrMessages[$level] as $k => $arrMsg) {
145  $args = $arrMsg['param'];
146 
147  if (is_array($args) && count($args) > 0) {
148  array_unshift($args, $arrMsg['msg']);
149  $str .= call_user_func_array("sprintf", $args) . "\n";
150  } else
151  $str .= $arrMsg['msg'] . "\n";
152  }
153 
154  } else {
155  $i = count($this->arrMessages[$level]) - 1;
156  if ($i > 0) {
157  $arr_msg=$this->arrMessages[$level][$i];
158  $args = $arr_msg['param'];
159  if (is_array($args) && count($args) > 0) {
160  array_unshift($args, $arr_msg['msg']);
161  $str = call_user_func_array("sprintf", $args);
162  } else
163  $str = $arr_msg['msgId'];
164  }
165  }
166 
167  return $str;
168  }
169 
170 
171  /**
172  * Return a message in HTML form with associated CSS
173  *
174  * @param string $level The level of the msg array to work with
175  * @param string $sep The separator used to concatenate msgs
176  * @param boolean $all show all the messages or only the last one
177  *
178  * @return string HTML message
179  */
180  function msg_html($level = "ERROR", $all = true) {
181  $level = strtoupper($level);
182  if (! in_array($level, $this->ARRLEVEL)) {
183  throw new Exception('Missing or unknown level in a raise() call');
184  }
185 
186  if (count($this->arrMessages[$level]) == 0)
187  return "";
188 
189  $str = $this->msg_str($level, $all);
190  $str = "<div class='alert " . $this->ARRCSS[$level] . "'>" . nl2br($str) . "</div>";
191 
192  return $str;
193  }
194 
195 
196  /**
197  * Return all the messages of all levels in HTML form with associated CSS
198  *
199  * @param string $sep The separator used to concatenate msgs
200  * @param boolean $all show all the messages or only the last one
201  *
202  * @return string HTML message
203  */
204  function msg_html_all($all = true, $init = false) {
205  $msg="";
206 
207  $msg.=$this->msg_html("ERROR", $all);
208  $msg.=$this->msg_html("INFO", $all);
209  $msg.=$this->msg_html("ALERT", $all);
210 
211  if ($init)
212  $this->init_msgs();
213 
214  return $msg;
215  }
216 
217 
218  /**
219  * Log a message into /var/log/alternc/bureau.log
220  *
221  * This function logs the last message in the /var/log/alternc folder
222  * allowing sysadmins to know what's happened.
223  * automatically called by raise()
224  * @param string $level the error level
225  * @param array $arrMsg the array containing message info.
226  * @access private
227  */
228  function logAlternC($level = "ERROR", $arrMsg) {
229  global $mem;
230 
231  $args = $arrMsg['param'];
232 
233  if (is_array($args) && count($args) > 0) {
234  array_unshift($args, $arrMsg['msg']);
235  $str = call_user_func_array("sprintf", $args);
236  } else
237  $str = $arrMsg['msg'];
238 
239  @file_put_contents(
240  $this->logfile,
241  date("d/m/Y H:i:s") . " - " . get_remote_ip() . " - $level - " . $mem->user["login"] . " - " . $str . "\n",
242  FILE_APPEND
243  );
244  }
245 
246 
247  /**
248  * Log an API function call into /var/log/alternc/bureau.log
249  *
250  * This function logs in /var/log/alternc an API function call of AlternC
251  *
252  * @param integer $clsid Number of the class doing the call
253  * @param string $function Name of the called function
254  * @param string $param non-mandatory parameters of the API call
255  * @return boolean TRUE if the log where successfull, FALSE if not
256  *
257  */
258  function log($clsid, $function, $param = "") {
259  global $mem;
260  return @file_put_contents(
261  $this->logfile,
262  date("d/m/Y H:i:s") . " - " . get_remote_ip() . " - CALL - " . $mem->user["login"] . " - $clsid - $function - $param\n",
263  FILE_APPEND
264  );
265  }
266 
267  /**
268  * Log an API function call into /var/log/alternc/bureau.log
269  *
270  * This function logs in /var/log/alternc an API function call of AlternC
271  *
272  * @param integer $clsid Number of the class doing the call
273  * @param string $function Name of the called function
274  * @param string $param non-mandatory parameters of the API call
275  * @return boolean TRUE if the log where successfull, FALSE if not
276  *
277  */
278  function debug($clsid, $function, $param = "") {
279  global $mem;
280  if (variable_get("debug_panel", "0", "Set it to 1 to enable panel debug in /var/log/alternc/bureau.log")) {
281  return @file_put_contents(
282  $this->logfile,
283  date("d/m/Y H:i:s") . " - " . get_remote_ip() . " - DEBUG - " . $mem->user["login"] . " - $clsid - $function - $param\n",
284  FILE_APPEND
285  );
286  }
287  }
288 
289 } /* Class m_messages */
$mem
Definition: bootstrap.php:71
$msg
Definition: bootstrap.php:75
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
Handle messages (error, warning, info, ok) appearing in API calls.
Definition: m_messages.php:33
$ARRCSS
CSS classes for each level.
Definition: m_messages.php:44
msg_str($level="ERROR", $all=true)
Return a string of concateneted messages of all recorded messages or only the last message.
Definition: m_messages.php:132
log($clsid, $function, $param="")
Log an API function call into /var/log/alternc/bureau.log.
Definition: m_messages.php:258
msg_html($level="ERROR", $all=true)
Return a message in HTML form with associated CSS.
Definition: m_messages.php:180
has_msgs($level="")
Tell if there are stored messages for a specific level or for all levels (if level is empty)
Definition: m_messages.php:107
logAlternC($level="ERROR", $arrMsg)
Log a message into /var/log/alternc/bureau.log.
Definition: m_messages.php:228
msg_html_all($all=true, $init=false)
Return all the messages of all levels in HTML form with associated CSS.
Definition: m_messages.php:204
debug($clsid, $function, $param="")
Log an API function call into /var/log/alternc/bureau.log.
Definition: m_messages.php:278
$ARRLEVEL
List of possible message level.
Definition: m_messages.php:41
init_msgs()
Reset the stored messages array.
Definition: m_messages.php:91
$arrMessages
Contains the messages and their ID.
Definition: m_messages.php:36
get_remote_ip()
Return the remote IP.
Definition: functions.php:134
$i