Skip to content
Snippets Groups Projects
Commit c29565fc authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

php8 only; added variable types; short array syntax; remove glyphicons

parent 60c6f1ad
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
......@@ -5,16 +5,46 @@ require_once 'user.class.php';
* class to log all project actions, ie. build, deploy etc.
*
* @author hahn
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types; short array syntax; remove glyphicons
*/
class Actionlog
{
private $_dbfile = '';
private $_aLoglevels = array("info", "warning", "error", "success"); // array of valid loglevels
private $_sIP = false;
private $_sUser = false;
private $_sProject = false;
private $_sCreate = '
/**
* database file
* @var string
*/
private string $_dbfile = '';
/**
* List of allowed loglevels
* @var array
*/
private array $_aLoglevels = ["info", "warning", "error", "success"]; // array of valid loglevels
/**
* Ip address of the client
* @var string
*/
private string $_sIP = '';
/**
* Authenticated user on the client
* @var string
*/
private string $_sUser = '';
/**
* Current project
* @var string
*/
private string $_sProject = '';
/**
* Create statement for the database
* @var string
*/
private string $_sCreate = '
CREATE TABLE "logs" (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
`time` DATETIME,
......@@ -39,13 +69,14 @@ class Actionlog
`message` TEXT
)';
*/
/**
* constructor - sets internal environment variables and checks existence
* Constructor - sets internal environment variables and checks existence
* of the database
* @global array $aConfig settings
* @param string $sProject project ID
*/
public function __construct($sProject = false)
public function __construct(string $sProject = '')
{
global $aConfig;
if (!isset($aConfig["appRootDir"])) {
......@@ -71,20 +102,22 @@ class Actionlog
}
/**
* create sqlite database - called in constructor if the file does not exist
* Create sqlite database - called in constructor if the file does not exist
*/
private function _createDb()
private function _createDb(): bool|PDOstatement
{
echo "try to create file $this->_dbfile ...<br>\n";
return $this->_makeQuery($this->_sCreate);
}
/**
* execute a sql statement
* Execute a sql statement and get the result
* TODO: support prepared statements
*
* @param string $sSql sql statement
* @return database object
* @return bool|PDO statement
*/
private function _makeQuery($sSql)
private function _makeQuery(string $sSql): bool|PDOstatement
{
// $this->_log(__FUNCTION__."($sSql)");
// echo "<pre>".htmlentities($sSql)."</pre>";
......@@ -100,16 +133,18 @@ class Actionlog
}
/**
* add a log message
* Add a log message
* TODO: supoport prepared statements
*
* @param string $sMessage message
* @param string $sAction project action; i.e. build, deploy, ...
* @param string $sLoglevel loglevel
* @return type
* @return bool|PDOstatement
*/
public function add($sMessage, $sAction = "", $sLoglevel = "info", $sTimeStart = false)
public function add(string $sMessage, string $sAction = "", string $sLoglevel = "info" /*, $sTimeStart = false */): bool|PDOstatement
{
if (array_search($sLoglevel, $this->_aLoglevels) === false) {
die(__class__ . ": loglevel $sLoglevel is invalid");
die(__CLASS__ . ": loglevel $sLoglevel is invalid");
}
$sql = "INSERT INTO `logs` (`time`, `loglevel`, `ip`, `user`, `project` ,`action`, `message`)
VALUES(
......@@ -143,18 +178,18 @@ class Actionlog
}
/**
* helper function to remove chars in a string
* Helper function to remove chars in a string
* @param string $sVal user value
* @param string $sOKChars good chars to keep
* @return string
*/
private function _filterAllowedChars($sVal, $sOKChars)
private function _filterAllowedChars(string $sVal, string $sOKChars): string
{
return preg_replace('/[^' . $sOKChars . ']/i', '', $sVal);
}
/**
* get log data
* Get log data
* @param array $aFilter with the following keys:
* 'project' - filter by project; will be mixed with where (see next key)
* 'from ' - time greater equal; time as string i.e. "2020-06-24" or "2020-06-24 11:00:00"
......@@ -163,15 +198,15 @@ class Actionlog
* 'limit' - limit clausel - part behind "LIMIT "
* @return array
*/
public function getLogs($aFilter = array())
public function getLogs(array $aFilter = []): array
{
// var_dump(R::findAll( 'log' ));
$aReturn = array();
$aReturn = [];
$sSql = 'SELECT `id`,`time`,`loglevel`,`ip`,`user`,`project`,`action`,`message` from logs ';
$sWhere = false;
$aWhere = array();
$aWhere = [];
if (isset($aFilter["project"]) && $aFilter["project"]) {
$aWhere[] = '`project`="' . $this->_filterAllowedChars($aFilter["project"], '[a-z0-9\-\_]') . '"';
}
......@@ -203,14 +238,14 @@ class Actionlog
}
/**
* render html code for a table with logs. The filter will be sent to
* Get html code for a table with logs. The filter will be sent to
* getLogs method.
* @param array $aFilter with the following keys:
* 'project' - filter by project; will be mixed with where (see next key)
* 'limit' - limit clausel - part behind "LIMIT "
* @return string
*/
public function renderLogs($aFilter = array(), $bIsFullsearch = false)
public function renderLogs(array $aFilter = [], bool $bIsFullsearch = false): string
{
$sReturn = '';
......@@ -220,35 +255,36 @@ class Actionlog
$bWasShown = true;
require_once 'formgen.class.php';
$oHtml = new htmlguielements();
// values for dropdowns - limit of lines; time
$aLimits = array(
'20' => array('label' => 20),
'50' => array('label' => 50),
'100' => array('label' => 100),
'' => array('label' => t("all")),
);
$aTimes = array(
date("Y-m-d", date("U")) => array('label' => t("class-actionlog-time-today")),
date("Y-m-d", date("U") - 60 * 60 * 24 * 1) => array('label' => t("class-actionlog-time-since-yesterday")),
date("Y-m-d", date("U") - 60 * 60 * 24 * 7) => array('label' => t("class-actionlog-time-for-1-week")),
date("Y-m-d", date("U") - 60 * 60 * 24 * 7 * 2) => array('label' => sprintf(t("class-actionlog-time-for-n-weeks"), "2")),
date("Y-m-d", date("U") - 60 * 60 * 24 * 7 * 4) => array('label' => sprintf(t("class-actionlog-time-for-n-weeks"), "4")),
'' => array('label' => t("all")),
);
$aForms = array(
'filter' => array(
'meta' => array(
$aLimits = [
'20' => ['label' => 20],
'50' => ['label' => 50],
'100' => ['label' => 100],
'' => ['label' => t("all")],
];
$aTimes = [
date("Y-m-d", date("U")) => ['label' => t("class-actionlog-time-today")],
date("Y-m-d", date("U") - 60 * 60 * 24 * 1) => ['label' => t("class-actionlog-time-since-yesterday")],
date("Y-m-d", date("U") - 60 * 60 * 24 * 7) => ['label' => t("class-actionlog-time-for-1-week")],
date("Y-m-d", date("U") - 60 * 60 * 24 * 7 * 2) => ['label' => sprintf(t("class-actionlog-time-for-n-weeks"), "2")],
date("Y-m-d", date("U") - 60 * 60 * 24 * 7 * 4) => ['label' => sprintf(t("class-actionlog-time-for-n-weeks"), "4")],
'' => ['label' => t("all")],
];
$aForms = [
'filter' => [
'meta' => [
'method' => 'GET',
'action' => '?',
'class' => 'form-inline',
),
'validate' => array(),
'form' => array(),
)
);
],
'validate' => [],
'form' => [],
]
];
// generate filter for log table
// $bIsFullsearch: true = show all inputs; false: no interactive search
......@@ -257,61 +293,61 @@ class Actionlog
// --- list of all projects in log
$sSql = 'SELECT DISTINCT(project) from `logs` order by project asc';
$aForms["filter"]["form"]['selectproject'] = array(
$aForms["filter"]["form"]['selectproject'] = [
'type' => 'select',
'name' => 'selectproject',
'label' => '<i class="glyphicon glyphicon-tag"></i> ' . t('project'),
'label' => $oHtml->getIcon('project') .'&nbsp;'. t('project'),
'class' => 'span2',
'onchange' => 'updateActionlog();',
'inline' => true,
);
$aForms["filter"]["form"]['selectproject']['options'][''] = array('label' => t("all"));
];
$aForms["filter"]["form"]['selectproject']['options'][''] = ['label' => t("all")];
foreach ($this->_makeQuery($sSql) as $row) {
if ($row[0]) {
$aForms["filter"]["form"]['selectproject']['options'][$row[0]] = array('label' => $row[0]);
$aForms["filter"]["form"]['selectproject']['options'][$row[0]] = ['label' => $row[0]];
}
}
$aForms["filter"]["form"]['selectfrom'] = array(
$aForms["filter"]["form"]['selectfrom'] = [
'type' => 'select',
'name' => 'selectWheretime',
'label' => '<i class="glyphicon glyphicon-calendar"></i> ' . t("class-actionlog-time"),
'label' => $oHtml->getIcon('calendar') .'&nbsp;'. t("class-actionlog-time"),
'class' => 'span2',
'onchange' => 'updateActionlog();',
'options' => $aTimes,
'inline' => true,
);
$aForms["filter"]["form"]['selectlimit'] = array(
];
$aForms["filter"]["form"]['selectlimit'] = [
'type' => 'select',
'name' => 'selectlimit',
'label' => '<i class="glyphicon glyphicon-list"></i> ' . t("class-actionlog-count"),
'label' => $oHtml->getIcon('list') .'&nbsp;'. t("class-actionlog-count"),
'class' => 'span1',
'onchange' => 'updateActionlog();',
'options' => $aLimits,
'inline' => true,
);
];
// force a line break
$aForms["filter"]["form"]['line'] = array(
$aForms["filter"]["form"]['line'] = [
'type' => 'markup',
'value' => '<div class="col-sm-12"></div>',
);
];
} else {
// write filters as hidden fields
if (isset($aFilter["project"])) {
$aForms["filter"]["form"]['selectproject'] = array(
$aForms["filter"]["form"]['selectproject'] = [
'type' => 'hidden',
'value' => $aFilter["project"]
);
];
}
if (isset($aFilter["limit"])) {
$aForms["filter"]["form"]['selectlimit'] = array(
$aForms["filter"]["form"]['selectlimit'] = [
'type' => 'hidden',
'value' => $aFilter["limit"]
);
];
}
}
$aForms["filter"]["form"]['efilterlogs'] = array(
$aForms["filter"]["form"]['efilterlogs'] = [
'type' => 'text',
'name' => 'efilterlogs',
'label' => '<i class="glyphicon glyphicon-filter"></i>' . t("overview-textsearch"),
......@@ -320,20 +356,20 @@ class Actionlog
'onkeypress' => 'filterLogTable();',
'onchange' => 'filterLogTable();',
//'size' => 20,
);
];
if (!$bIsFullsearch) {
$aForms["filter"]["form"]['btnalllogs'] = array(
$aForms["filter"]["form"]['btnalllogs'] = [
'type' => 'button',
'value' => t('show all'),
'class' => 'btn btn-secondary btnlogs',
'href' => '/deployment/all/setup/actionlog/',
'onclick' => 'location.href=\'/deployment/all/setup/actionlog/\'; return false;',
);
];
} else {
// $aForms["filter"]["form"]['closediv'] = array(
// $aForms["filter"]["form"]['closediv'] = [
// 'type' => 'markup',
// 'value' => '</div><!-- closediv -->',
// );
// ];
}
......
......@@ -10,10 +10,10 @@ require_once 'user.class.php';
class base {
/**
* logged in user
* @var object
* logged in user as user object
* @var user
*/
var $oUser=false;
public user $oUser;
/**
* init user with optional given user
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment