introducing a router and controllers to allow more submodules in the

future and more structured way of loading modules
This commit is contained in:
Pepijn Over 2014-03-15 00:35:35 +01:00
parent 59eddd9380
commit db73e9bb4b
33 changed files with 574 additions and 190 deletions

View File

@ -29,9 +29,10 @@
require_once dirname(__FILE__) . '/../src/bootstrap.php';
// prevent cron from running twice at the same time
// however if the cron has been running for 10 mins, we'll assume it died and run anyway
// however if the cron has been running for X mins, we'll assume it died and run anyway
// if you want to change PSM_CRON_TIMEOUT, have a look in src/includes/psmconfig.inc.php.
$time = time();
if(psm_get_conf('cron_running') == 1 && ($time - psm_get_conf('cron_running_time') < 600)) {
if(psm_get_conf('cron_running') == 1 && ($time - psm_get_conf('cron_running_time') < PSM_CRON_TIMEOUT)) {
die('Cron is already running. Exiting.');
}
psm_update_conf('cron_running', 1);

View File

@ -29,32 +29,5 @@ require 'src/bootstrap.php';
psm_no_cache();
$type = (!isset($_GET['type'])) ? 'servers' : $_GET['type'];
// if user is not logged in, load login module
$user = new \psm\Service\User($db);
if(!$user->isUserLoggedIn()) {
$type = 'login';
}
if($type == 'update') {
require 'cron/status.cron.php';
header('Location: ' . psm_build_url());
die();
}
$allowed_types = array('servers', 'users', 'log', 'config', 'status', 'login');
// make sure user selected a valid type. if so, include the file and add to template
if(!in_array($type, $allowed_types)) {
$type = $allowed_types[0];
}
$tpl = new \psm\Service\Template();
eval('$mod = new psm\Module\\'.ucfirst($type).'($db, $tpl);');
if($user->getUserLevel() > $mod->getMinUserLevelRequired()) {
die('You do not have the privileges to view this page.');
}
$mod->setUser($user);
// let the module prepare it's HTML code
$mod->initialize();
$router = new psm\Router();
$router->run();

View File

@ -29,11 +29,5 @@ define('PSM_INSTALL', true);
require 'src/bootstrap.php';
$type = 'install';
$tpl = new \psm\Service\Template();
$user = new \psm\Service\User($db);
$mod = new psm\Module\Install($db, $tpl);
$mod->setUser($user);
$mod->initialize();
?>
$router = new psm\Router();
$router->run('config_install');

View File

@ -95,5 +95,3 @@ if($db->status() && (!defined('PSM_INSTALL') || !PSM_INSTALL)) {
}
$lang = psm_get_conf('language', 'en');
psm_load_lang($lang);
?>

View File

@ -78,4 +78,9 @@ define("PSM_LOGIN_COOKIE_SECRET_KEY", "4w900de52e3ap7y77y8675jy6c594286");
/**
* Number of seconds the reset link is valid after sending it to the user.
*/
define('PSM_LOGIN_RESET_RUNTIME', 3600);
define('PSM_LOGIN_RESET_RUNTIME', 3600);
/**
* Number of seconds the cron is supposedly dead and we will run another cron anyway.
*/
define('PSM_CRON_TIMEOUT', 600);

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Мониторинг система',
'servers' => 'Сървъри',
'users' => 'Потребители',
'log' => 'Логове',
'status' => 'Статус',
'update' => 'Обнови данните',
'config' => 'Настройки',
'help' => 'Помощ',
'install' => 'Инсталация',
'action' => 'Действие',
'save' => 'Запиши',
@ -52,6 +45,15 @@ $sm_lang = array(
'back_to_top' => 'Нагоре',
'go_back' => 'Go back',
),
'menu' => array(
'config' => 'Настройки',
'server' => 'Сървъри',
'server_log' => 'Логове',
'server_status' => 'Статус',
'server_update' => 'Обнови данните',
'user' => 'Потребители',
'help' => 'Помощ',
),
'users' => array(
'user' => 'Потребител',
'name' => 'Име',

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Server Monitor',
'servers' => 'Servidores',
'users' => 'Usuários',
'log' => 'Log',
'status' => 'Status',
'update' => 'Atualização',
'config' => 'Configuração',
'help' => 'Ajuda',
'install' => 'Install',
'action' => 'Ação',
'save' => 'Salvar',
@ -52,6 +45,15 @@ $sm_lang = array(
'back_to_top' => 'Voltar ao topo',
'go_back' => 'Go back',
),
'menu' => array(
'config' => 'Configuração',
'server' => 'Servidores',
'server_log' => 'Log',
'server_status' => 'Status',
'server_update' => 'Atualização',
'user' => 'Usuários',
'help' => 'Ajuda',
),
'users' => array(
'user' => 'usuário',
'name' => 'Nome',

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Server Monitor',
'servers' => 'Server',
'users' => 'Benutzer',
'log' => 'Log',
'status' => 'Status',
'update' => 'Updates',
'config' => 'Einstellungen',
'help' => 'Hilfe',
'install' => 'Install',
'action' => 'Aktion',
'save' => 'Speichern',
@ -52,6 +45,15 @@ $sm_lang = array(
'back_to_top' => 'Back to top',
'go_back' => 'Go back',
),
'menu' => array(
'config' => 'Einstellungen',
'server' => 'Server',
'server_log' => 'Log',
'server_status' => 'Status',
'server_update' => 'Updates',
'user' => 'Benutzer',
'help' => 'Hilfe',
),
'users' => array(
'user' => 'Benutzer',
'name' => 'Name',

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Server Monitor',
'servers' => 'Servers',
'users' => 'Users',
'log' => 'Log',
'status' => 'Status',
'update' => 'Update',
'config' => 'Config',
'help' => 'Help',
'install' => 'Install',
'action' => 'Action',
'save' => 'Save',
@ -52,6 +45,15 @@ $sm_lang = array(
'back_to_top' => 'Back to top',
'go_back' => 'Go back',
),
'menu' => array(
'config' => 'Config',
'server' => 'Servers',
'server_log' => 'Log',
'server_status' => 'Status',
'server_update' => 'Update',
'user' => 'Users',
'help' => 'Help',
),
'users' => array(
'user' => 'user',
'name' => 'Name',

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Serveur de Supervision',
'servers' => 'Serveurs',
'users' => 'Utilisateurs',
'log' => 'Evenements',
'status' => 'Status',
'update' => 'Mise &agrave; jour',
'config' => 'Configuration',
'help' => 'Aide',
'install' => 'Install',
'action' => 'Action',
'save' => 'Enregistrer',
@ -52,6 +45,15 @@ $sm_lang = array(
'back_to_top' => 'Haut de page',
'go_back' => 'Go back',
),
'menu' => array(
'config' => 'Configuration',
'server' => 'Serveurs',
'server_log' => 'Evenements',
'server_status' => 'Status',
'server_update' => 'Mise &agrave; jour',
'user' => 'Utilisateurs',
'help' => 'Aide',
),
'users' => array(
'user' => 'Utilisateur',
'name' => 'Nom',

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Server Monitor',
'servers' => '서버목록',
'users' => '사용자',
'log' => '로그',
'status' => 'Status',
'update' => '업데이트',
'config' => '설정',
'help' => '도움말',
'install' => 'Install',
'action' => 'Action',
'save' => '저장',
@ -52,6 +45,15 @@ $sm_lang = array(
'back_to_top' => 'Back to top',
'go_back' => 'Go back',
),
'menu' => array(
'config' => '설정',
'server' => '서버목록',
'server_log' => '로그',
'server_status' => 'Status',
'server_update' => '업데이트',
'user' => '사용자',
'help' => '도움말',
),
'users' => array(
'user' => '사용자',
'name' => '이름',

View File

@ -28,13 +28,6 @@
$sm_lang = array(
'system' => array(
'title' => 'Server Monitor',
'servers' => 'Servers',
'users' => 'Gebruikers',
'log' => 'Log',
'status' => 'Status',
'update' => 'Update',
'config' => 'Config',
'help' => 'Help',
'install' => 'Install',
'action' => 'Actie',
'save' => 'Opslaan',
@ -52,29 +45,38 @@ $sm_lang = array(
'back_to_top' => 'Terug naar boven',
'go_back' => 'Terug',
),
'menu' => array(
'config' => 'Config',
'server' => 'Servers',
'server_log' => 'Log',
'server_status' => 'Status',
'server_update' => 'Update',
'user' => 'Gebruikers',
'help' => 'Help',
),
'users' => array(
'user' => 'gebruiker',
'name' => 'Naam',
'user_name' => 'Username',
'password' => 'Password',
'password_repeat' => 'Password repeat',
'password_leave_blank' => 'Leave blank to keep unchanged',
'user_name' => 'Gebruikersnaam',
'password' => 'Wachtwoord',
'password_repeat' => 'Herhaal wachtwoord',
'password_leave_blank' => 'Laat leeg om niet te wijzigen',
'level' => 'Level',
'level_10' => 'Administrator',
'level_20' => 'User',
'level_10' => 'Beheerder',
'level_20' => 'Gebruiker',
'mobile' => 'Mobiel',
'email' => 'Email',
'updated' => 'Gebruiker gewijzigd.',
'inserted' => 'Gebruiker toegevoegd.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',
'error_user_email_bad_length' => 'Email addresses must be between 5 and 255 characters.',
'error_user_email_invalid' => 'The email address is invalid.',
'error_user_level_invalid' => 'The given user level is invalid.',
'error_user_no_match' => 'The user could not be found in the database.',
'error_user_password_invalid' => 'The entered password is invalid.',
'error_user_password_no_match' => 'The entered passwords do not match.',
'error_user_name_bad_length' => 'Een gebruikersnaam moet tussen de 2 en 64 tekens zijn.',
'error_user_name_invalid' => 'Een gebruikersnaam mag alleen alfabetische tekens (a-z, A-Z), cijfers (0-9) en underscores (_) bevatten.',
'error_user_name_exists' => 'De opgegeven gebruikersnaam bestaat al.',
'error_user_email_bad_length' => 'Een email adres moet tussen de 5 en 255 tekens zijn.',
'error_user_email_invalid' => 'Het email adres is ongeldig.',
'error_user_level_invalid' => 'Het gebruikersniveau is ongeldig.',
'error_user_no_match' => 'De gebruiker kon niet worden toegevoegd aan de database.',
'error_user_password_invalid' => 'Het ingevulde wachtwoord is ongeldig.',
'error_user_password_no_match' => 'De ingevulde wachtwoorden komen niet overeen.',
),
'log' => array(
'title' => 'Log entries',
@ -166,26 +168,26 @@ $sm_lang = array(
'on_email_body' => "Server %LABEL% is weer online:<br/><br/>Server: %LABEL%<br/>IP: %IP%<br/>Poort: %PORT%<br/>Datum: %DATE%",
),
'login' => array(
'welcome_usermenu' => 'Welcome, %user_name%',
'title_sign_in' => 'Please sign in',
'title_forgot' => 'Forgot your password?',
'title_reset' => 'Reset your password',
'submit' => 'Submit',
'remember_me' => 'Remember me',
'welcome_usermenu' => 'Welkom, %user_name%',
'title_sign_in' => 'Log in',
'title_forgot' => 'Wachtwoord vergeten?',
'title_reset' => 'Herstel wachtwoord',
'submit' => 'Sla op',
'remember_me' => 'Onthoud mij',
'login' => 'Login',
'logout' => 'Logout',
'username' => 'Username',
'password' => 'Password',
'password_repeat' => 'Repeat password',
'password_forgot' => 'Forgot password?',
'password_reset' => 'Reset password',
'password_reset_email_subject' => 'Reset your password for PHP Server Monitor',
'password_reset_email_body' => 'Please use the following link to reset your password. Please note it expires in 1 hour.<br/><br/>%link%',
'error_user_incorrect' => 'The provided username could not be found.',
'error_login_incorrect' => 'The information is incorrect.',
'error_login_passwords_nomatch' => 'The provided passwords do not match.',
'error_reset_invalid_link' => 'The reset link you provided is invalid.',
'success_password_forgot' => 'An email has been sent to you with information how to reset your password.',
'success_password_reset' => 'Your password has been reset successfully. Please login.',
'logout' => 'Uitloggen',
'username' => 'Gebruikersnaam',
'password' => 'Wachtwoord',
'password_repeat' => 'Herhaal wachtwoord',
'password_forgot' => 'Wachtwoord vergeten?',
'password_reset' => 'Wachtwoord herstellen',
'password_reset_email_subject' => 'Wijzig je wachtwoord voor PHP Server Monitor',
'password_reset_email_body' => 'Gebruik de onderstaande link om uw wachtwoord te wijzigen. Let op, deze link verloopt na 1 uur.<br/><br/>%link%',
'error_user_incorrect' => 'De opgegeven gebruikersnaam is onjuist.',
'error_login_incorrect' => 'De informatie is niet juist.',
'error_login_passwords_nomatch' => 'De ingevulde wachtwoorden komen niet overeen.',
'error_reset_invalid_link' => 'De reset link is ongeldig.',
'success_password_forgot' => 'Er is een mail verstuurd met informatie om je wachtwoord aan te passen.',
'success_password_reset' => 'Je wachtwoord is aangepast. Je kunt nu inloggen.',
),
);

View File

@ -29,7 +29,7 @@ namespace psm\Module;
use psm\Service\Database;
use psm\Service\Template;
abstract class AbstractModule implements ModuleInterface {
abstract class AbstractController implements ControllerInterface {
/**
* Current mode. Can be used by modules to determine
@ -200,26 +200,23 @@ abstract class AbstractModule implements ModuleInterface {
* @return string
*/
protected function createHTMLMenu() {
// @todo globals..? seriously..?
global $type;
$ulvl = ($this->user) ? $this->user->getUserLevel() : PSM_USER_ANONYMOUS;
$tpl_id = 'main_menu';
$this->tpl->newTemplate($tpl_id, 'main.tpl.html');
$tpl_data = array(
'label_help' => psm_get_lang('system', 'help'),
'label_help' => psm_get_lang('menu', 'help'),
'label_logout' => psm_get_lang('login', 'logout'),
'url_logout' => psm_build_url(array('logout' => 1)),
);
switch($ulvl) {
case PSM_USER_ADMIN:
$items = array('servers', 'users', 'log', 'status', 'config', 'update');
$items = array('server', 'user', 'server_log', 'server_status', 'config', 'server_update');
break;
case PSM_USER_USER:
$items = array('servers', 'log', 'status', 'update');
$items = array('server', 'server_log', 'server_status', 'server_update');
break;
default:
$items = array();
@ -228,9 +225,9 @@ abstract class AbstractModule implements ModuleInterface {
$menu = array();
foreach($items as $key) {
$menu[] = array(
'active' => ($key == $type) ? 'active' : '',
'url' => psm_build_url(array('type' => $key)),
'label' => psm_get_lang('system', $key),
'active' => ($key == psm_GET('mod')) ? 'active' : '',
'url' => psm_build_url(array('mod' => $key)),
'label' => psm_get_lang('menu', $key),
);
}
if(!empty($menu)) {
@ -274,7 +271,6 @@ abstract class AbstractModule implements ModuleInterface {
'main',
array(
'title' => strtoupper(psm_get_lang('system', 'title')),
'subtitle' => psm_get_lang('system', $type),
'label_back_to_top' => psm_get_lang('system', 'back_to_top'),
)
);

View File

@ -0,0 +1,40 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* 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/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.2
**/
namespace psm\Module\Config;
use psm\Module\ModuleInterface;
class ConfigModule implements ModuleInterface {
public function getControllers() {
return array(
'config' => __NAMESPACE__ . '\Controller\ConfigController',
'install' => __NAMESPACE__ . '\Controller\InstallController',
);
}
}

View File

@ -25,11 +25,12 @@
* @link http://www.phpservermonitor.org/
**/
namespace psm\Module;
namespace psm\Module\Config\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
class Config extends AbstractModule {
class ConfigController extends AbstractController {
/**
* Checkboxes
@ -182,6 +183,7 @@ class Config extends AbstractModule {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'subtitle' => psm_get_lang('menu', 'config'),
'label_settings_email' => psm_get_lang('config', 'settings_email'),
'label_settings_sms' => psm_get_lang('config', 'settings_sms'),
'label_settings_notification' => psm_get_lang('config', 'settings_notification'),
@ -225,5 +227,3 @@ class Config extends AbstractModule {
return parent::createHTMLLabels();
}
}
?>

View File

@ -26,11 +26,12 @@
* @since phpservermon 2.1.0
**/
namespace psm\Module;
namespace psm\Module\Config\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
class Install extends AbstractModule {
class InstallController extends AbstractController {
/**
* Full path to config file
@ -47,6 +48,7 @@ class Install extends AbstractModule {
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
$this->setMinUserLevelRequired(PSM_USER_ANONYMOUS);
$this->addMenu(false);
$this->path_config = PSM_PATH_SRC . '../config.php';
@ -361,4 +363,15 @@ class Install extends AbstractModule {
return $version_from;
}
}
protected function createHTMLLabels() {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'subtitle' => psm_get_lang('system', 'install'),
)
);
return parent::createHTMLLabels();
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* 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/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.1
**/
namespace psm\Module;
use psm\Service\Database;
use psm\Service\Template;
interface ControllerInterface {
public function __construct(Database $db, Template $tpl);
/**
* Initialize the module
*/
public function initialize();
}

View File

@ -23,24 +23,14 @@
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.1
* @since phpservermon 2.2
**/
namespace psm\Module;
use psm\Service\Database;
use psm\Service\Template;
/**
* Public API for all modules
*/
interface ModuleInterface {
public function __construct(Database $db, Template $tpl);
/**
* Initialize the module
*/
public function initialize();
public function getControllers();
}
?>

View File

@ -25,14 +25,15 @@
* @link http://www.phpservermonitor.org/
**/
namespace psm\Module;
namespace psm\Module\Server\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
/**
* Log module. Create the page to view previous log messages
*/
class Log extends AbstractModule {
class LogController extends AbstractController {
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
@ -132,6 +133,7 @@ class Log extends AbstractModule {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'subtitle' => psm_get_lang('system', 'server_log'),
'label_status' => psm_get_lang('log', 'status'),
'label_email' => psm_get_lang('log', 'email'),
'label_sms' => psm_get_lang('log', 'sms'),
@ -140,12 +142,10 @@ class Log extends AbstractModule {
'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('system', 'users')),
'label_users' => ucfirst(psm_get_lang('menu', 'users')),
)
);
return parent::createHTMLLabels();
}
}
?>

View File

@ -25,14 +25,15 @@
* @link http://www.phpservermonitor.org/
**/
namespace psm\Module;
namespace psm\Module\Server\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
/**
* Server module. Add/edit/delete servers, show a list of all servers etc.
*/
class Servers extends AbstractModule {
class ServerController extends AbstractController {
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
@ -213,6 +214,7 @@ class Servers extends AbstractModule {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'subtitle' => psm_get_lang('menu', 'servers'),
'label_label' => psm_get_lang('servers', 'label'),
'label_domain' => psm_get_lang('servers', 'domain'),
'label_port' => psm_get_lang('servers', 'port'),
@ -237,5 +239,3 @@ class Servers extends AbstractModule {
return parent::createHTMLLabels();
}
}
?>

View File

@ -26,19 +26,20 @@
* @link http://www.phpservermonitor.org/
**/
namespace psm\Module;
namespace psm\Module\Server\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
/**
* Status module
*/
class Status extends AbstractModule {
class StatusController extends AbstractController {
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
$this->setActions('index', 'index');
$this->setActions(array('index'), 'index');
}
/**
@ -90,6 +91,15 @@ class Status extends AbstractModule {
$this->tpl->addTemplateData('main_auto_refresh', array('seconds' => 30));
$this->tpl->addTemplateData('main', array('auto_refresh' => $this->tpl->getTemplate('main_auto_refresh')));
}
}
?>
protected function createHTMLLabels() {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'subtitle' => psm_get_lang('menu', 'status'),
)
);
return parent::createHTMLLabels();
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* 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/>.
*
* @package phpservermon
* @author Michael Greenhill
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.2.0
**/
namespace psm\Module\Server\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
class UpdateController extends AbstractController {
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
$this->setActions('index', 'index');
}
protected function executeIndex() {
// fuck globals. but hey, dirty fixes, like you've done it.
global $db;
require PSM_PATH_SRC . '../cron/status.cron.php';
// redirect user to regular status page
header('Location: ' . psm_build_url(array(
'mod' => 'server_status'
)));
die();
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* 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/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.2
**/
namespace psm\Module\Server;
use psm\Module\ModuleInterface;
class ServerModule implements ModuleInterface {
public function getControllers() {
return array(
'server' => __NAMESPACE__ . '\Controller\ServerController',
'log' => __NAMESPACE__ . '\Controller\LogController',
'status' => __NAMESPACE__ . '\Controller\StatusController',
'update' => __NAMESPACE__ . '\Controller\UpdateController',
);
}
}

View File

@ -26,11 +26,12 @@
* @since phpservermon 2.2.0
**/
namespace psm\Module;
namespace psm\Module\User\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
class Login extends AbstractModule {
class LoginController extends AbstractController {
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);

View File

@ -25,7 +25,8 @@
* @link http://www.phpservermonitor.org/
**/
namespace psm\Module;
namespace psm\Module\User\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
@ -33,7 +34,7 @@ use psm\Service\Template;
* User module. Add, edit and delete users, or assign
* servers to users.
*/
class Users extends AbstractModule {
class UserController extends AbstractController {
public $servers;
/**
@ -257,7 +258,8 @@ class Users extends AbstractModule {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'label_users' => psm_get_lang('system', 'users'),
'subtitle' => psm_get_lang('menu', 'users'),
'label_users' => psm_get_lang('menu', 'users'),
'label_name' => psm_get_lang('users', 'name'),
'label_user_name' => psm_get_lang('users', 'user_name'),
'label_password' => psm_get_lang('users', 'password'),
@ -268,7 +270,7 @@ class Users extends AbstractModule {
'label_level_30' => psm_get_lang('users', 'level_30'),
'label_mobile' => psm_get_lang('users', 'mobile'),
'label_email' => psm_get_lang('users', 'email'),
'label_servers' => psm_get_lang('system', 'servers'),
'label_servers' => psm_get_lang('menu', 'server'),
'label_action' => psm_get_lang('system', 'action'),
'label_save' => psm_get_lang('system', 'save'),
'label_go_back' => psm_get_lang('system', 'go_back'),

View File

@ -0,0 +1,41 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* 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/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.2
**/
namespace psm\Module\User;
use psm\Module\ModuleInterface;
class UserModule implements ModuleInterface {
public function getControllers() {
return array(
'user' => __NAMESPACE__ . '\Controller\UserController',
'login' => __NAMESPACE__ . '\Controller\LoginController',
// 'profile' => __NAMESPACE__ . '\Controller\StatusController',
);
}
}

171
src/psm/Router.class.php Normal file
View File

@ -0,0 +1,171 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* 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/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.2
**/
namespace psm;
/**
* The router class opens the controller and initializes the module.
*
* The router has the list of available modules, and it will check them for
* available controllers.
* It uses a so-called $mod param to determine which controller to load.
* The $mod can either be passed as GET var or directly to the run() method.
* The $mod has 2 components, separated by an underscore. The first part determines
* the module to load, the second on the controller. It uses the keys defined in
* the module config. If the controller part is absent, it will always try to load
* the controller with the same name as the module.
*/
class Router {
/**
* Default module (if none given or invalid one)
* @var string $default_module
*/
public $default_module = 'server';
/**
* Controller map
* @var array $map
*/
protected $map = array();
/**
* Registered services
* @var array $services
*/
protected $services = array();
public function __construct() {
global $db;
$this->services['db'] = $db;
$this->services['tpl'] = new \psm\Service\Template();
$this->services['user'] = new \psm\Service\User($db);
$modules = $this->getModules();
foreach($modules as $id => $module) {
$this->map[$id] = $module->getControllers();
}
}
/**
* Get registered modules
*
* Note, the index of each module is also the key used for mapping getvars.
* @return array
*/
public function getModules() {
return array(
'config' => new Module\Config\ConfigModule(),
'server' => new Module\Server\ServerModule(),
'user' => new Module\User\UserModule(),
);
}
/**
* Run.
*
* The $mod param is in the format $module_$controller.
* If the "_$controller" part is omitted, it will attempt to load
* the controller with the same name as the module.
* If no mod is given it will attempt to load the default module.
* @param string $mod if empty, the mod getvar will be used, or fallback to default
* @throws \InvalidArgumentException
*/
public function run($mod = null) {
if($mod === null) {
$mod = psm_GET('mod', $this->default_module);
}
try {
$controller = $this->getController($mod);
} catch(\InvalidArgumentException $e) {
// invalid module, try the default one
// it that somehow also doesnt exist, we have a bit of an issue
// and we really have no reason catch it
$controller = $this->getController($this->default_module);
}
// get min required level for this controller and make sure the user matches
$min_lvl = $controller->getMinUserLevelRequired();
if($min_lvl < PSM_USER_ANONYMOUS) {
// if user is not logged in, load login module
if(!$this->services['user']->isUserLoggedIn()) {
// redirect to login
$controller = $this->getController('user_login');
} elseif($this->services['user']->getUserLevel() > $min_lvl) {
// @todo perhaps show a nice permission denied page
die('You do not have the privileges to view this page.');
}
}
$controller->setUser($this->services['user']);
// let the module prepare it's HTML code
$controller->initialize();
}
/**
* Get an instance of the requested mod.
* @param string $mod
* @return \psm\Module\ControllerInterface
* @throws \InvalidArgumentException
*/
public function getController($mod) {
$controller = $this->getControllerClass($mod);
if($controller === false) {
throw new \InvalidArgumentException('Controller is not registered');
}
$controller = new $controller($this->services['db'], $this->services['tpl']);
if(!$controller instanceof \psm\Module\ControllerInterface) {
throw new \Exception('Controller does not use ControllerInterface');
}
return $controller;
}
/**
* Get the classname of the controller for the provided mod
* @param string $mod
* @return string|false FALSE if not found, string otherwise
*/
protected function getControllerClass($mod) {
if(strpos($mod, '_') !== false) {
list($mod, $controller) = explode('_', $mod);
} else {
$controller = $mod;
}
if(!isset($this->map[$mod][$controller]) || !class_exists($this->map[$mod][$controller])) {
return false;
} else {
return $this->map[$mod][$controller];
}
}
}

View File

@ -1,7 +1,7 @@
<!--%tpl_config-->
{config_update}
<div class="span12">
<form class="form-horizontal well" action="index.php?type=config&action=save" id="edit_config" method="post">
<form class="form-horizontal well" action="index.php?mod=config&action=save" id="edit_config" method="post">
<ul class="nav nav-tabs">
<li class="active"><a href="#config-general" data-toggle="tab">{label_general}</a></li>
<li><a href="#config-email" data-toggle="tab">{label_settings_email}</a></li>

View File

@ -46,10 +46,7 @@
<div class="container">
<div class="page-header">
<h1>
{subtitle}
<small>&nbsp;</small>
</h1>
<h1>{subtitle}<small>&nbsp;</small></h1>
</div>
<div class="row-fluid">
<div class="span12">

View File

@ -1,7 +1,7 @@
<!--%tpl_servers_list-->
<div class="span12">
<div class="top_buutons">
<a class="btn btn-success" href="index.php?type=servers&action=edit">
<a class="btn btn-success" href="index.php?mod=server&action=edit">
<i class="icon-plus icon-white"></i>
{label_add_new}
</a>
@ -43,10 +43,10 @@
<td>{email}</td>
<td>{sms}</td>
<td>
<a class="btn btn-small" href="index.php?type=servers&amp;action=edit&amp;id={server_id}" title="{label_edit}">
<a class="btn btn-small" href="index.php?mod=server&amp;action=edit&amp;id={server_id}" title="{label_edit}">
<i class="icon-pencil"></i>
</a>
<a class="btn btn-small btn-danger" href="javascript:sm_delete('{server_id}', 'servers');" title="{label_delete}">
<a class="btn btn-small btn-danger" href="javascript:sm_delete('{server_id}', 'server');" title="{label_delete}">
<i class="icon-remove icon-white"></i>
</a>
</td>
@ -61,7 +61,7 @@
<!--%tpl_servers_update-->
<div class="span12">
<form class="form-horizontal well" action="index.php?type=servers&action=save&id={edit_server_id}" method="post">
<form class="form-horizontal well" action="index.php?mod=server&action=save&id={edit_server_id}" method="post">
<fieldset>
<legend>{titlemode}</legend>
<div class="control-group">

View File

@ -1,7 +1,7 @@
<!--%tpl_users_list-->
<div class="span12">
<div class="top_buutons">
<a class="btn btn-success" href="index.php?type=users&action=edit">
<a class="btn btn-success" href="index.php?mod=user&action=edit">
<i class="icon-plus icon-white"></i>
{label_add_new}
</a>
@ -28,10 +28,10 @@
<td>{email}</td>
<td>{emp_servers}</td>
<td>
<a class="btn btn-small" href="index.php?type=users&amp;action=edit&amp;id={user_id}" title="{label_edit}">
<a class="btn btn-small" href="index.php?mod=user&amp;action=edit&amp;id={user_id}" title="{label_edit}">
<i class="icon-pencil"></i>
</a>
<a class="btn btn-small btn-danger" href="javascript:sm_delete('{user_id}', 'users');" title="{label_delete}">
<a class="btn btn-small btn-danger" href="javascript:sm_delete('{user_id}', 'user');" title="{label_delete}">
<i class="icon-remove icon-white"></i>
</a>
</td>
@ -45,7 +45,7 @@
<!--%tpl_users_update-->
<div class="span12">
<form class="form-horizontal well" action="index.php?type=users&amp;action=save&amp;id={edit_user_id}" method="post">
<form class="form-horizontal well" action="index.php?mod=user&amp;action=save&amp;id={edit_user_id}" method="post">
<fieldset>
<legend>{titlemode}</legend>
<div class="control-group">

View File

@ -1,7 +1,7 @@
function sm_delete(id, type) {
function sm_delete(id, mod) {
var del = confirm("Are you sure you want to delete this record?");
if (del == true) {
var loc = 'index.php?action=delete&id=' + id + '&type=' + type;
var loc = 'index.php?action=delete&id=' + id + '&mod=' + mod;
window.location = loc;
}
}