This class handles messages appearing while calling API functions of AlternC * Those messages are stored as a number (class-id) and a message * localized messages are available
*This class also handle inserting those messages into the logging * system in /var/log/alternc/bureau.log *
* * @copyright AlternC-Team 2000-2017 https://alternc.com/ */ class m_messages { /** Contains the messages and their ID */ var $arrMessages = array(); var $logfile = "/var/log/alternc/bureau.log"; /** List of possible message level */ var $ARRLEVEL = array("ERROR", "ALERT", "INFO"); /** CSS classes for each level */ var $ARRCSS = array( // emergency critical warning notice debug : currently not used (from PSR-3) "ERROR" => "alert-danger", "ALERT" => "alert-warning", "INFO" => "alert-success", ); public function __construct() { $this->init_msgs(); } /** * Record a message, insert it into the logfile. * * This function records a message, add it to the logfile, * and make it available for the web panel to print it later. * * @param string $cat The category of the msg array to work with * @param integer $clsid Which class raises this message * @param mixed $msg The message * @param array $param Non-mandatory array of string parameter for this message * @return boolean TRUE if the message got recorded, FALSE if not. * */ function raise($level = "ERROR", $clsid, $msg, $param = "") { $arrInfos = array(); $level = strtoupper($level); if (! in_array($level, $this->ARRLEVEL)) { throw new Exception('Missing or unknown level in a raise() call'); } $arrInfos['clsid'] = $clsid; $arrInfos['msg'] = $msg; $arrInfos['param'] = is_array($param)?$param:(empty($param)?"":array($param)); $this->arrMessages[$level][] = $arrInfos; $this->logAlternC($level,$arrInfos); return true; } /** * Reset the stored messages array */ function init_msgs() { // $me=debug_backtrace(); $this->log("messages", "init_msgs from ".$me[1]["class"].".".$me[1]["function"]); foreach ($this->ARRLEVEL as $v) { $this->arrMessages[$v] = array(); } } /** * Tell if there are stored messages for a specific level * or for all levels (if level is empty) * * @param string $level The level of the msg array to work with (if empty or not set, use all levels) * @return boolean TRUE if there is/are msg recorded. * */ function has_msgs($level="") { $level = strtoupper($level); if (in_array($level, $this->ARRLEVEL)) { return (count($this->arrMessages[$level]) > 0); } else { foreach ($this->arrMessages as $v) { if (count($v) > 0) return true; } return false; } } /** * Return a string of concateneted messages of all recorded messages * or only the last message * * @param string $level The level of the msg array to work with * @param boolean $all show all the messages or only the last one * * @access private * @return string Message. * */ function msg_str($level = "ERROR", $all = true) { $str = ""; $level = strtoupper($level); if (! in_array($level, $this->ARRLEVEL)) { throw new Exception('Missing or unknown level in a raise() call'); } if (! $this->has_msgs($level)) return ""; if ($all) { foreach ($this->arrMessages[$level] as $k => $arrMsg) { $args = $arrMsg['param']; if (is_array($args) && count($args) > 0) { array_unshift($args, $arrMsg['msg']); $str .= call_user_func_array("sprintf", $args) . "\n"; } else $str .= $arrMsg['msg'] . "\n"; } } else { $i = count($this->arrMessages[$level]) - 1; if ($i > 0) { $arr_msg=$this->arrMessages[$level][$i]; $args = $arr_msg['param']; if (is_array($args) && count($args) > 0) { array_unshift($args, $arr_msg['msg']); $str = call_user_func_array("sprintf", $args); } else $str = $arr_msg['msgId']; } } return $str; } /** * Return a message in HTML form with associated CSS * * @param string $level The level of the msg array to work with * @param string $sep The separator used to concatenate msgs * @param boolean $all show all the messages or only the last one * * @return string HTML message */ function msg_html($level = "ERROR", $all = true) { $level = strtoupper($level); if (! in_array($level, $this->ARRLEVEL)) { throw new Exception('Missing or unknown level in a raise() call'); } if (count($this->arrMessages[$level]) == 0) return ""; $str = $this->msg_str($level, $all); $str = "