2017-08-15 00:30:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
LICENSE
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License (GPL)
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
To read the license please visit http://www.gnu.org/copyleft/gpl.html
|
|
|
|
----------------------------------------------------------------------
|
2017-10-06 10:00:27 +00:00
|
|
|
*/
|
2017-08-15 00:30:38 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Handle messages (error, warning, info, ok) appearing in API calls.
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* <p>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</p>
|
|
|
|
* <p>This class also handle inserting those messages into the logging
|
|
|
|
* system in /var/log/alternc/bureau.log
|
2017-08-15 00:30:38 +00:00
|
|
|
* </p>
|
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* @copyright AlternC-Team https://alternc.com/
|
2017-08-15 00:30:38 +00:00
|
|
|
*/
|
|
|
|
class m_messages {
|
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
/** Contains the messages and their ID */
|
2017-08-15 00:30:38 +00:00
|
|
|
var $arrMessages = array();
|
|
|
|
|
|
|
|
var $logfile = "/var/log/alternc/bureau.log";
|
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
/** List of possible message types */
|
2017-08-15 00:30:38 +00:00
|
|
|
var $ARRTYPES = array("ERROR", "ALERT", "INFO", "OK");
|
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
/** CSS classes for each type */
|
2017-08-15 00:30:38 +00:00
|
|
|
var $ARRCSS = array(
|
2017-10-06 10:00:27 +00:00
|
|
|
"ERROR" => "alert-danger",
|
|
|
|
"ALERT" => "alert-warning",
|
|
|
|
"INFO" => "alert-info",
|
|
|
|
"OK" => "alert-success"
|
2017-08-15 00:30:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
public function __construct() {
|
2017-10-06 10:00:27 +00:00
|
|
|
$this->init_msgs();
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Record a message, insert it into the logfile.
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* This function records a message, add it to the logfile,
|
|
|
|
* and make it available for the web panel to print it later.
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
|
|
|
* @param string $cat The category of the msg array to work with
|
2017-10-06 10:00:27 +00:00
|
|
|
* @param integer $clsid Which class raises this message
|
|
|
|
* @param mixed $msg The message
|
|
|
|
* @param string $param Non-mandatory string parameter for this message
|
|
|
|
* @return boolean TRUE if the message got recorded, FALSE if not.
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function raise($cat = "Error", $clsid, $msg, $param = "") {
|
2017-10-06 10:00:27 +00:00
|
|
|
$arrInfos = array();
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
$type = strtoupper($cat);
|
|
|
|
if (! in_array($type, $this->ARRTYPES)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
$arrInfos['clsid'] = $clsid;
|
|
|
|
$arrInfos['msg'] = $msg;
|
|
|
|
$arrInfos['param'] = is_array($param)?$param:(empty($param)?"":array($param));
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
$this->arrMessages[$type][] = $arrInfos;
|
2017-08-15 00:30:38 +00:00
|
|
|
|
|
|
|
$this->logAlternC($cat);
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-06 10:00:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the stored messages array
|
|
|
|
*/
|
2017-08-15 00:30:38 +00:00
|
|
|
function init_msgs() {
|
2017-10-06 10:00:27 +00:00
|
|
|
foreach ($this->ARRTYPES as $v) {
|
|
|
|
$this->arrMessages[$v] = array();
|
|
|
|
}
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Tell if there are stored messages for a specific level
|
|
|
|
* or for all levels (if level is empty)
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* @param string $cat The level of the msg array to work with
|
|
|
|
* @return boolean TRUE if there is/are msg recorded.
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function has_msgs($cat) {
|
2017-10-06 10:00:27 +00:00
|
|
|
$type = strtoupper($cat);
|
|
|
|
if (in_array($type, $this->ARRTYPES)) {
|
|
|
|
return (count($this->arrMessages[$type]) > 0);
|
|
|
|
} else {
|
|
|
|
foreach ($this->arrMessages as $v) {
|
|
|
|
if (count($v) > 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Return a string of concateneted messages of all recorded messages
|
|
|
|
* or only the last message
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* @param string $cat The level of the msg array to work with
|
2017-08-15 00:30:38 +00:00
|
|
|
* @param string $sep The separator used to concatenate msgs
|
|
|
|
* @param boolean $all show all the messages or only the last one
|
|
|
|
*
|
|
|
|
* @return string Message.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function msg_str($cat = "Error", $sep = "<li>", $all = true) {
|
2017-10-06 10:00:27 +00:00
|
|
|
$str = "";
|
|
|
|
|
|
|
|
$type = strtoupper($cat);
|
|
|
|
if (! in_array($type, $this->ARRTYPES)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->has_msgs($cat))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if ($all) {
|
|
|
|
foreach ($this->arrMessages[$type] as $k => $arrMsg) {
|
|
|
|
$args = $arrMsg['param'];
|
|
|
|
|
|
|
|
if (is_array($args) && count($args) > 0) {
|
|
|
|
array_unshift($args, $arrMsg['msg']);
|
|
|
|
if ($sep == "<li>")
|
|
|
|
$str .= "<li>" . call_user_func_array("sprintf", $args) . "</li>";
|
|
|
|
else
|
|
|
|
$str .= call_user_func_array("sprintf", $args) . $sep;
|
|
|
|
} else
|
|
|
|
if ($sep == "<li>")
|
|
|
|
$str .= "<li>" . $arrMsg['msg'] . "</li>";
|
|
|
|
else
|
|
|
|
$str .= $arrMsg['msg'] . $sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($sep == "<li>")
|
|
|
|
$str = "<ul>".$str."</ul>";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$i = count($this->arrMessages[$type]) - 1;
|
|
|
|
if ($i > 0) {
|
|
|
|
$arr_msg=$this->arrMessages[$type][$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'];
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
2017-10-06 10:00:27 +00:00
|
|
|
}
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
return $str;
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Return a message in HTML form with associated CSS
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* @param string $cat The level of the msg array to work with
|
2017-08-15 00:30:38 +00:00
|
|
|
* @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($cat = "Error", $sep = "<li>", $all = true) {
|
2017-10-06 10:00:27 +00:00
|
|
|
$type = strtoupper($cat);
|
|
|
|
if (! in_array($type, $this->ARRTYPES)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
if (count($this->arrMessages[$type]) == 0)
|
|
|
|
return "";
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
$str = $this->msg_str($cat, $sep, $all);
|
|
|
|
$str = "<div class='alert " . $this->ARRCSS[$type] . "'>" . $str . "</div>";
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
return $str;
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Return all the messages of all levels in HTML form with associated CSS
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
|
|
|
* @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_all($sep = "<li>", $all = true, $init = false) {
|
2017-10-06 10:00:27 +00:00
|
|
|
$msg="";
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
$msg.=$this->msg_html("Error", $sep, $all);
|
|
|
|
$msg.=$this->msg_html("Ok", $sep, $all);
|
|
|
|
$msg.=$this->msg_html("Info", $sep, $all);
|
|
|
|
$msg.=$this->msg_html("Alert", $sep, $all);
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
if ($init)
|
|
|
|
$this->init_msgs();
|
2017-08-15 00:30:38 +00:00
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
return $msg;
|
2017-08-15 00:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Log a message into /var/log/alternc/bureau.log
|
|
|
|
*
|
|
|
|
* This function logs the last message in the /var/log/alternc folder
|
|
|
|
* allowing sysadmins to know what's happened.
|
|
|
|
* automatically called by raise()
|
2017-08-15 00:30:38 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function logAlternC($cat = "Error") {
|
|
|
|
global $mem;
|
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
$type = strtoupper($cat);
|
|
|
|
if (! in_array($type, $this->ARRTYPES)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-15 00:30:38 +00:00
|
|
|
|
|
|
|
@file_put_contents($this->logfile, date("d/m/Y H:i:s") . " - " . get_remote_ip() . " - $type - " . $mem->user["login"] . " - " . $this->msg_str($cat, "", false), FILE_APPEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 10:00:27 +00:00
|
|
|
* Log an API function call into /var/log/alternc/bureau.log
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* This function logs in /var/log/alternc an API function call of AlternC
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
2017-10-06 10:00:27 +00:00
|
|
|
* @param integer $clsid Number of the class doing the call
|
|
|
|
* @param string $function Name of the called function
|
|
|
|
* @param string $param non-mandatory parameters of the API call
|
|
|
|
* @return boolean TRUE if the log where successfull, FALSE if not
|
2017-08-15 00:30:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function log($clsid, $function, $param = "") {
|
|
|
|
global $mem;
|
|
|
|
return @file_put_contents($this->logfile, date("d/m/Y H:i:s") . " - " . get_remote_ip() . " - CALL - " . $mem->user["login"] . " - $clsid - $function - $param\n", FILE_APPEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-06 10:00:27 +00:00
|
|
|
/* Class m_messages */
|