From db73e9bb4be016d9ab1b1207d760eddc9e3a6a07 Mon Sep 17 00:00:00 2001 From: Pepijn Over Date: Sat, 15 Mar 2014 00:35:35 +0100 Subject: [PATCH] introducing a router and controllers to allow more submodules in the future and more structured way of loading modules --- cron/status.cron.php | 5 +- index.php | 31 +--- install.php | 10 +- src/bootstrap.php | 2 - src/includes/psmconfig.inc.php | 7 +- src/lang/bg.lang.php | 16 +- src/lang/br.lang.php | 16 +- src/lang/de.lang.php | 16 +- src/lang/en.lang.php | 16 +- src/lang/fr.lang.php | 16 +- src/lang/kr.lang.php | 16 +- src/lang/nl.lang.php | 86 ++++----- ...class.php => AbstractController.class.php} | 18 +- src/psm/Module/Config/ConfigModule.class.php | 40 ++++ .../Controller/ConfigController.class.php} | 8 +- .../Controller/InstallController.class.php} | 17 +- src/psm/Module/ControllerInterface.class.php | 41 +++++ src/psm/Module/ModuleInterface.class.php | 14 +- .../Controller/LogController.class.php} | 10 +- .../Controller/ServerController.class.php} | 8 +- .../Controller/StatusController.class.php} | 20 +- .../Controller/UpdateController.class.php | 55 ++++++ src/psm/Module/Server/ServerModule.class.php | 42 +++++ .../Controller/LoginController.class.php} | 5 +- .../Controller/ProfileController.class.php | 0 .../Controller/UserController.class.php} | 10 +- src/psm/Module/User/UserModule.class.php | 41 +++++ src/psm/Router.class.php | 171 ++++++++++++++++++ src/templates/config.tpl.html | 2 +- src/templates/main.tpl.html | 5 +- src/templates/servers.tpl.html | 8 +- src/templates/users.tpl.html | 8 +- static/js/scripts.js | 4 +- 33 files changed, 574 insertions(+), 190 deletions(-) rename src/psm/Module/{AbstractModule.class.php => AbstractController.class.php} (94%) create mode 100644 src/psm/Module/Config/ConfigModule.class.php rename src/psm/Module/{Config.class.php => Config/Controller/ConfigController.class.php} (97%) mode change 100755 => 100644 rename src/psm/Module/{Install.class.php => Config/Controller/InstallController.class.php} (96%) create mode 100644 src/psm/Module/ControllerInterface.class.php rename src/psm/Module/{Log.class.php => Server/Controller/LogController.class.php} (94%) rename src/psm/Module/{Servers.class.php => Server/Controller/ServerController.class.php} (97%) rename src/psm/Module/{Status.class.php => Server/Controller/StatusController.class.php} (88%) create mode 100644 src/psm/Module/Server/Controller/UpdateController.class.php create mode 100644 src/psm/Module/Server/ServerModule.class.php rename src/psm/Module/{Login.class.php => User/Controller/LoginController.class.php} (97%) create mode 100644 src/psm/Module/User/Controller/ProfileController.class.php rename src/psm/Module/{Users.class.php => User/Controller/UserController.class.php} (96%) create mode 100644 src/psm/Module/User/UserModule.class.php create mode 100644 src/psm/Router.class.php diff --git a/cron/status.cron.php b/cron/status.cron.php index ee77309d..e73e3d0e 100755 --- a/cron/status.cron.php +++ b/cron/status.cron.php @@ -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); diff --git a/index.php b/index.php index f4825b8c..7f980d76 100755 --- a/index.php +++ b/index.php @@ -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(); \ No newline at end of file diff --git a/install.php b/install.php index d09f1ad1..9ba7377f 100755 --- a/install.php +++ b/install.php @@ -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(); - -?> \ No newline at end of file +$router = new psm\Router(); +$router->run('config_install'); diff --git a/src/bootstrap.php b/src/bootstrap.php index 68789847..c575fb1c 100755 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -95,5 +95,3 @@ if($db->status() && (!defined('PSM_INSTALL') || !PSM_INSTALL)) { } $lang = psm_get_conf('language', 'en'); psm_load_lang($lang); - -?> diff --git a/src/includes/psmconfig.inc.php b/src/includes/psmconfig.inc.php index cc9c4bfe..d90da462 100644 --- a/src/includes/psmconfig.inc.php +++ b/src/includes/psmconfig.inc.php @@ -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); \ No newline at end of file +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); \ No newline at end of file diff --git a/src/lang/bg.lang.php b/src/lang/bg.lang.php index 50edd14f..96c38ed5 100644 --- a/src/lang/bg.lang.php +++ b/src/lang/bg.lang.php @@ -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' => 'Име', diff --git a/src/lang/br.lang.php b/src/lang/br.lang.php index be4bcb92..23cb13e8 100755 --- a/src/lang/br.lang.php +++ b/src/lang/br.lang.php @@ -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', diff --git a/src/lang/de.lang.php b/src/lang/de.lang.php index affcbd5f..0826c8eb 100755 --- a/src/lang/de.lang.php +++ b/src/lang/de.lang.php @@ -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', diff --git a/src/lang/en.lang.php b/src/lang/en.lang.php index bdafc5d4..7641ee05 100755 --- a/src/lang/en.lang.php +++ b/src/lang/en.lang.php @@ -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', diff --git a/src/lang/fr.lang.php b/src/lang/fr.lang.php index 60faf91b..df512a13 100755 --- a/src/lang/fr.lang.php +++ b/src/lang/fr.lang.php @@ -28,13 +28,6 @@ $sm_lang = array( 'system' => array( 'title' => 'Serveur de Supervision', - 'servers' => 'Serveurs', - 'users' => 'Utilisateurs', - 'log' => 'Evenements', - 'status' => 'Status', - 'update' => 'Mise à 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 à jour', + 'user' => 'Utilisateurs', + 'help' => 'Aide', + ), 'users' => array( 'user' => 'Utilisateur', 'name' => 'Nom', diff --git a/src/lang/kr.lang.php b/src/lang/kr.lang.php index 8b1c61b3..7a062b99 100755 --- a/src/lang/kr.lang.php +++ b/src/lang/kr.lang.php @@ -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' => '이름', diff --git a/src/lang/nl.lang.php b/src/lang/nl.lang.php index 96ecd21b..39054221 100755 --- a/src/lang/nl.lang.php +++ b/src/lang/nl.lang.php @@ -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:

Server: %LABEL%
IP: %IP%
Poort: %PORT%
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.

%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.

%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.', ), ); diff --git a/src/psm/Module/AbstractModule.class.php b/src/psm/Module/AbstractController.class.php similarity index 94% rename from src/psm/Module/AbstractModule.class.php rename to src/psm/Module/AbstractController.class.php index 1a9d4625..947b9160 100755 --- a/src/psm/Module/AbstractModule.class.php +++ b/src/psm/Module/AbstractController.class.php @@ -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'), ) ); diff --git a/src/psm/Module/Config/ConfigModule.class.php b/src/psm/Module/Config/ConfigModule.class.php new file mode 100644 index 00000000..047b1564 --- /dev/null +++ b/src/psm/Module/Config/ConfigModule.class.php @@ -0,0 +1,40 @@ +. + * + * @package phpservermon + * @author Pepijn Over + * @copyright Copyright (c) 2008-2014 Pepijn Over + * @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', + ); + + } +} diff --git a/src/psm/Module/Config.class.php b/src/psm/Module/Config/Controller/ConfigController.class.php old mode 100755 new mode 100644 similarity index 97% rename from src/psm/Module/Config.class.php rename to src/psm/Module/Config/Controller/ConfigController.class.php index 327315f6..1b754960 --- a/src/psm/Module/Config.class.php +++ b/src/psm/Module/Config/Controller/ConfigController.class.php @@ -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(); } } - -?> \ No newline at end of file diff --git a/src/psm/Module/Install.class.php b/src/psm/Module/Config/Controller/InstallController.class.php similarity index 96% rename from src/psm/Module/Install.class.php rename to src/psm/Module/Config/Controller/InstallController.class.php index 70debf91..76d1e464 100755 --- a/src/psm/Module/Install.class.php +++ b/src/psm/Module/Config/Controller/InstallController.class.php @@ -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(); + } } diff --git a/src/psm/Module/ControllerInterface.class.php b/src/psm/Module/ControllerInterface.class.php new file mode 100644 index 00000000..b7b22350 --- /dev/null +++ b/src/psm/Module/ControllerInterface.class.php @@ -0,0 +1,41 @@ +. + * + * @package phpservermon + * @author Pepijn Over + * @copyright Copyright (c) 2008-2014 Pepijn Over + * @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(); +} diff --git a/src/psm/Module/ModuleInterface.class.php b/src/psm/Module/ModuleInterface.class.php index 2f5930be..b02fe705 100644 --- a/src/psm/Module/ModuleInterface.class.php +++ b/src/psm/Module/ModuleInterface.class.php @@ -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(); } - -?> \ No newline at end of file diff --git a/src/psm/Module/Log.class.php b/src/psm/Module/Server/Controller/LogController.class.php similarity index 94% rename from src/psm/Module/Log.class.php rename to src/psm/Module/Server/Controller/LogController.class.php index f5dc7885..e93bb1cd 100755 --- a/src/psm/Module/Log.class.php +++ b/src/psm/Module/Server/Controller/LogController.class.php @@ -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(); } } - -?> \ No newline at end of file diff --git a/src/psm/Module/Servers.class.php b/src/psm/Module/Server/Controller/ServerController.class.php similarity index 97% rename from src/psm/Module/Servers.class.php rename to src/psm/Module/Server/Controller/ServerController.class.php index 5b0976f9..a74cbd97 100755 --- a/src/psm/Module/Servers.class.php +++ b/src/psm/Module/Server/Controller/ServerController.class.php @@ -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(); } } - -?> \ No newline at end of file diff --git a/src/psm/Module/Status.class.php b/src/psm/Module/Server/Controller/StatusController.class.php similarity index 88% rename from src/psm/Module/Status.class.php rename to src/psm/Module/Server/Controller/StatusController.class.php index 98a04bfe..ae8cc94b 100755 --- a/src/psm/Module/Status.class.php +++ b/src/psm/Module/Server/Controller/StatusController.class.php @@ -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'))); } -} -?> \ No newline at end of file + protected function createHTMLLabels() { + $this->tpl->addTemplateData( + $this->getTemplateId(), + array( + 'subtitle' => psm_get_lang('menu', 'status'), + ) + ); + + return parent::createHTMLLabels(); + } +} diff --git a/src/psm/Module/Server/Controller/UpdateController.class.php b/src/psm/Module/Server/Controller/UpdateController.class.php new file mode 100644 index 00000000..63e1f662 --- /dev/null +++ b/src/psm/Module/Server/Controller/UpdateController.class.php @@ -0,0 +1,55 @@ +. + * + * @package phpservermon + * @author Michael Greenhill + * @author Pepijn Over + * @copyright Copyright (c) 2008-2014 Pepijn Over + * @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(); + } + +} diff --git a/src/psm/Module/Server/ServerModule.class.php b/src/psm/Module/Server/ServerModule.class.php new file mode 100644 index 00000000..2055192f --- /dev/null +++ b/src/psm/Module/Server/ServerModule.class.php @@ -0,0 +1,42 @@ +. + * + * @package phpservermon + * @author Pepijn Over + * @copyright Copyright (c) 2008-2014 Pepijn Over + * @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', + ); + + } +} diff --git a/src/psm/Module/Login.class.php b/src/psm/Module/User/Controller/LoginController.class.php similarity index 97% rename from src/psm/Module/Login.class.php rename to src/psm/Module/User/Controller/LoginController.class.php index 37d29149..58c31036 100644 --- a/src/psm/Module/Login.class.php +++ b/src/psm/Module/User/Controller/LoginController.class.php @@ -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); diff --git a/src/psm/Module/User/Controller/ProfileController.class.php b/src/psm/Module/User/Controller/ProfileController.class.php new file mode 100644 index 00000000..e69de29b diff --git a/src/psm/Module/Users.class.php b/src/psm/Module/User/Controller/UserController.class.php similarity index 96% rename from src/psm/Module/Users.class.php rename to src/psm/Module/User/Controller/UserController.class.php index b9f0fd4f..67f90b88 100755 --- a/src/psm/Module/Users.class.php +++ b/src/psm/Module/User/Controller/UserController.class.php @@ -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'), diff --git a/src/psm/Module/User/UserModule.class.php b/src/psm/Module/User/UserModule.class.php new file mode 100644 index 00000000..d7061e9b --- /dev/null +++ b/src/psm/Module/User/UserModule.class.php @@ -0,0 +1,41 @@ +. + * + * @package phpservermon + * @author Pepijn Over + * @copyright Copyright (c) 2008-2014 Pepijn Over + * @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', + ); + + } +} diff --git a/src/psm/Router.class.php b/src/psm/Router.class.php new file mode 100644 index 00000000..a9225c89 --- /dev/null +++ b/src/psm/Router.class.php @@ -0,0 +1,171 @@ +. + * + * @package phpservermon + * @author Pepijn Over + * @copyright Copyright (c) 2008-2014 Pepijn Over + * @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]; + } + } +} \ No newline at end of file diff --git a/src/templates/config.tpl.html b/src/templates/config.tpl.html index 7b574598..38af9114 100755 --- a/src/templates/config.tpl.html +++ b/src/templates/config.tpl.html @@ -1,7 +1,7 @@ {config_update}
-
+