2014-01-07 20:24:48 +01:00
|
|
|
<?php
|
2014-01-07 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* PHP Server Monitor
|
|
|
|
* Monitor your servers and websites.
|
2014-01-07 20:24:48 +01:00
|
|
|
*
|
|
|
|
* This file is part of PHP Server Monitor.
|
|
|
|
* PHP Server Monitor is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PHP Server Monitor 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-07 23:32:57 +01:00
|
|
|
*
|
|
|
|
* @package phpservermon
|
2015-02-27 11:34:56 +01:00
|
|
|
* @author Pepijn Over <pep@peplab.net>
|
|
|
|
* @copyright Copyright (c) 2008-2015 Pepijn Over <pep@peplab.net>
|
2014-01-07 23:32:57 +01:00
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
|
|
|
|
* @version Release: @package_version@
|
2014-02-10 23:48:43 +01:00
|
|
|
* @link http://www.phpservermonitor.org/
|
2014-01-07 23:32:57 +01:00
|
|
|
**/
|
2014-01-07 20:24:48 +01:00
|
|
|
|
2014-03-15 00:35:35 +01:00
|
|
|
namespace psm\Module\Server\Controller;
|
2014-01-29 00:57:36 +01:00
|
|
|
use psm\Service\Database;
|
2014-01-10 18:31:57 +01:00
|
|
|
|
2014-01-07 20:27:21 +01:00
|
|
|
/**
|
|
|
|
* Log module. Create the page to view previous log messages
|
|
|
|
*/
|
2014-03-16 02:04:49 +01:00
|
|
|
class LogController extends AbstractServerController {
|
2014-01-07 20:24:48 +01:00
|
|
|
|
2014-08-01 16:40:20 +02:00
|
|
|
function __construct(Database $db, \Twig_Environment $twig) {
|
|
|
|
parent::__construct($db, $twig);
|
2014-01-07 20:24:48 +01:00
|
|
|
|
2014-01-29 00:57:36 +01:00
|
|
|
$this->setActions('index', 'index');
|
2014-01-07 20:24:48 +01:00
|
|
|
}
|
|
|
|
|
2014-01-07 20:27:21 +01:00
|
|
|
/**
|
|
|
|
* Prepare the template with a list of all log entries
|
|
|
|
*/
|
2014-01-29 00:57:36 +01:00
|
|
|
protected function executeIndex() {
|
2014-08-01 16:40:20 +02:00
|
|
|
$this->twig->addGlobal('subtitle', psm_get_lang('menu', 'server_log'));
|
|
|
|
$tpl_data = array(
|
|
|
|
'label_status' => psm_get_lang('log', 'status'),
|
|
|
|
'label_email' => psm_get_lang('log', 'email'),
|
|
|
|
'label_sms' => psm_get_lang('log', 'sms'),
|
|
|
|
'label_pushover' => psm_get_lang('log', 'pushover'),
|
|
|
|
'label_title' => psm_get_lang('log', 'title'),
|
|
|
|
'label_server' => psm_get_lang('servers', 'server'),
|
|
|
|
'label_type' => psm_get_lang('log', 'type'),
|
|
|
|
'label_message' => psm_get_lang('system', 'message'),
|
|
|
|
'label_date' => psm_get_lang('system', 'date'),
|
|
|
|
'label_users' => ucfirst(psm_get_lang('menu', 'user')),
|
|
|
|
'label_no_logs' => psm_get_lang('log', 'no_logs'),
|
|
|
|
'tabs' => array(),
|
|
|
|
);
|
2014-07-31 12:11:32 +02:00
|
|
|
$log_types = array('status', 'email', 'sms', 'pushover');
|
2014-01-07 20:24:48 +01:00
|
|
|
|
2014-07-31 12:11:32 +02:00
|
|
|
foreach($log_types as $key) {
|
|
|
|
$records = $this->getEntries($key);
|
2014-01-07 20:24:48 +01:00
|
|
|
$log_count = count($records);
|
|
|
|
|
2014-08-01 16:40:20 +02:00
|
|
|
$tab_data = array(
|
|
|
|
'id' => $key,
|
|
|
|
'has_users' => ($key == 'status') ? false : true,
|
|
|
|
'no_logs' => ($log_count == 0) ? true : false,
|
|
|
|
'tab_active' => ($key == 'status') ? 'active' : '',
|
|
|
|
);
|
|
|
|
|
2014-01-07 20:24:48 +01:00
|
|
|
for ($x = 0; $x < $log_count; $x++) {
|
2014-04-19 15:21:36 +02:00
|
|
|
$record = &$records[$x];
|
|
|
|
$record['class'] = ($x & 1) ? 'odd' : 'even';
|
|
|
|
$record['users'] = '';
|
|
|
|
$record['server'] = $record['label'];
|
|
|
|
$record['type_icon'] = ($record['server_type'] == 'website') ? 'icon-globe' : 'icon-cog';
|
|
|
|
$record['type_title'] = psm_get_lang('servers', 'type_' . $record['server_type']);
|
|
|
|
$ip = '(' . $record['ip'];
|
|
|
|
if(!empty($record['port']) && (($record['server_type'] != 'website') || ($record['port'] != 80))) {
|
|
|
|
$ip .= ':' . $record['port'];
|
|
|
|
}
|
|
|
|
$ip .= ')';
|
|
|
|
$record['ip'] = $ip;
|
|
|
|
$record['datetime_format'] = psm_date($record['datetime']);
|
2014-01-07 20:24:48 +01:00
|
|
|
|
|
|
|
// fix up user list
|
2014-10-02 21:33:51 +02:00
|
|
|
$users = $this->getLogUsers($record['log_id']);
|
|
|
|
if(!empty($users)) {
|
2014-04-19 15:21:36 +02:00
|
|
|
$names = array();
|
2014-10-02 21:33:51 +02:00
|
|
|
foreach($users as $user) {
|
|
|
|
$names[] = $user['name'];
|
2014-04-19 15:21:36 +02:00
|
|
|
}
|
|
|
|
$record['users'] = implode('<br/>', $names);
|
|
|
|
$record['user_list'] = implode(' • ', $names);
|
2014-01-07 20:24:48 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 16:40:20 +02:00
|
|
|
$tab_data['entries'] = $records;
|
|
|
|
$tpl_data['tabs'][] = $tab_data;
|
2014-01-07 20:24:48 +01:00
|
|
|
}
|
2014-08-01 16:40:20 +02:00
|
|
|
return $this->twig->render('module/server/log.tpl.html', $tpl_data);
|
2014-01-07 20:24:48 +01:00
|
|
|
}
|
|
|
|
|
2014-01-07 20:27:21 +01:00
|
|
|
/**
|
|
|
|
* Get all the log entries for a specific $type
|
|
|
|
*
|
|
|
|
* @param string $type status/email/sms
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-01-07 20:24:48 +01:00
|
|
|
public function getEntries($type) {
|
2014-03-16 02:04:49 +01:00
|
|
|
$sql_join = '';
|
2015-02-27 14:25:34 +01:00
|
|
|
if($this->getUser()->getUserLevel() > PSM_USER_ADMIN) {
|
2014-03-16 02:04:49 +01:00
|
|
|
// restrict by user_id
|
|
|
|
$sql_join = "JOIN `".PSM_DB_PREFIX."users_servers` AS `us` ON (
|
2015-02-27 14:25:34 +01:00
|
|
|
`us`.`user_id`={$this->getUser()->getUserId()}
|
2014-03-16 02:04:49 +01:00
|
|
|
AND `us`.`server_id`=`servers`.`server_id`
|
|
|
|
)";
|
|
|
|
}
|
2014-01-07 20:24:48 +01:00
|
|
|
$entries = $this->db->query(
|
|
|
|
'SELECT '.
|
|
|
|
'`servers`.`label`, '.
|
2014-04-19 15:21:36 +02:00
|
|
|
'`servers`.`ip`, '.
|
|
|
|
'`servers`.`port`, '.
|
|
|
|
'`servers`.`type` AS server_type, '.
|
2014-10-02 21:33:51 +02:00
|
|
|
'`log`.`log_id`, '.
|
2014-01-07 20:24:48 +01:00
|
|
|
'`log`.`type`, '.
|
|
|
|
'`log`.`message`, '.
|
2014-10-02 21:33:51 +02:00
|
|
|
'`log`.`datetime` '.
|
2014-01-10 18:31:57 +01:00
|
|
|
'FROM `'.PSM_DB_PREFIX.'log` AS `log` '.
|
|
|
|
'JOIN `'.PSM_DB_PREFIX.'servers` AS `servers` ON (`servers`.`server_id`=`log`.`server_id`) '.
|
2014-03-16 02:04:49 +01:00
|
|
|
$sql_join .
|
2014-01-07 20:24:48 +01:00
|
|
|
'WHERE `log`.`type`=\''.$type.'\' '.
|
|
|
|
'ORDER BY `datetime` DESC '.
|
|
|
|
'LIMIT 0,20'
|
|
|
|
);
|
|
|
|
return $entries;
|
|
|
|
}
|
2014-10-02 21:33:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the user entries for a specific $log_id
|
|
|
|
*
|
|
|
|
* @param $log_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getLogUsers($log_id) {
|
|
|
|
return $this->db->query(
|
|
|
|
"SELECT
|
|
|
|
u.`user_id`,
|
|
|
|
u.`name`
|
|
|
|
FROM `" . PSM_DB_PREFIX . "log_users` AS lu
|
|
|
|
LEFT JOIN `" . PSM_DB_PREFIX . "users` AS u ON lu.`user_id` = u.`user_id`
|
|
|
|
WHERE lu.`log_id` = " . (int)$log_id . "
|
|
|
|
ORDER BY u.`name` ASC"
|
|
|
|
);
|
|
|
|
}
|
2014-01-07 20:24:48 +01:00
|
|
|
}
|