protected function executeIndex() { $autorun = $this->container->get('util.server.updatemanager'); $autorun->run(); header('Location: ' . psm_build_url(array('mod' => 'server_status'), true, false)); die; }
protected function executeIndex() { $autorun = new \psm\Util\Server\UpdateManager($this->db); $autorun->setUser($this->user); $autorun->run(); header('Location: ' . psm_build_url(array('mod' => 'server_status'), true, false)); die; }
/** * Show the profile page * @return string */ protected function executeIndex() { $this->twig->addGlobal('subtitle', psm_get_lang('users', 'profile')); $user = $this->user->getUser(null, true); $tpl_data = array('label_name' => psm_get_lang('users', 'name'), 'label_user_name' => psm_get_lang('users', 'user_name'), 'label_password' => psm_get_lang('users', 'password'), 'label_password_repeat' => psm_get_lang('users', 'password_repeat'), 'label_level' => psm_get_lang('users', 'level'), 'label_mobile' => psm_get_lang('users', 'mobile'), 'label_pushover' => psm_get_lang('users', 'pushover'), 'label_pushover_description' => psm_get_lang('users', 'pushover_description'), 'label_pushover_key' => psm_get_lang('users', 'pushover_key'), 'label_pushover_device' => psm_get_lang('users', 'pushover_device'), 'label_pushover_device_description' => psm_get_lang('users', 'pushover_device_description'), 'label_email' => psm_get_lang('users', 'email'), 'label_save' => psm_get_lang('system', 'save'), 'form_action' => psm_build_url(array('mod' => 'user_profile', 'action' => 'save')), 'level' => psm_get_lang('users', 'level_' . $user->level), 'placeholder_password' => psm_get_lang('users', 'password_leave_blank')); foreach ($this->profile_fields as $field) { $tpl_data[$field] = isset($user->{$field}) ? $user->{$field} : ''; } return $this->twig->render('module/user/profile.tpl.html', $tpl_data); }
/** * Format server data for display * @param array $server * @return array */ protected function formatServer($server) { $server['rtime'] = round((double) $server['rtime'], 4); $server['last_online'] = psm_timespan($server['last_online']); $server['last_check'] = psm_timespan($server['last_check']); $server['active'] = psm_get_lang('system', $server['active']); $server['email'] = psm_get_lang('system', $server['email']); $server['sms'] = psm_get_lang('system', $server['sms']); $server['pushover'] = psm_get_lang('system', $server['pushover']); if ($server['status'] == 'on' && $server['warning_threshold_counter'] > 0) { $server['status'] = 'warning'; } $server['error'] = htmlentities($server['error']); $server['type'] = psm_get_lang('servers', 'type_' . $server['type']); $server['timeout'] = $server['timeout'] > 0 ? $server['timeout'] : PSM_CURL_TIMEOUT; $url_actions = array('delete', 'edit', 'view'); foreach ($url_actions as $action) { $server['url_' . $action] = psm_build_url(array('mod' => 'server', 'action' => $action, 'id' => $server['server_id'])); } return $server; }
/** * Prepare the template to show a list of all servers * @todo move the background colurs to the config */ protected function executeIndex() { // set background color to black $this->black_background = true; $this->twig->addGlobal('subtitle', psm_get_lang('menu', 'server_status')); // add header accessories $layout = $this->getUser()->getUserPref('status_layout', 0); $layout_data = array('label_last_check' => psm_get_lang('servers', 'last_check'), 'label_last_online' => psm_get_lang('servers', 'last_online'), 'label_rtime' => psm_get_lang('servers', 'latency'), 'block_layout_active' => $layout == 0 ? 'active' : '', 'list_layout_active' => $layout != 0 ? 'active' : ''); $this->setHeaderAccessories($this->twig->render('module/server/status/header.tpl.html', $layout_data)); $this->addFooter(false); // get the active servers from database $servers = $this->getServers(); $layout_data['servers_offline'] = array(); $layout_data['servers_online'] = array(); foreach ($servers as $server) { if ($server['active'] == 'no') { continue; } $server['last_checked_nice'] = psm_timespan($server['last_check']); $server['last_online_nice'] = psm_timespan($server['last_online']); $server['url_view'] = psm_build_url(array('mod' => 'server', 'action' => 'view', 'id' => $server['server_id'], 'back_to' => 'server_status')); if ($server['status'] == "off") { $layout_data['servers_offline'][] = $server; } elseif ($server['warning_threshold_counter'] > 0) { $server['class_warning'] = 'warning'; $layout_data['servers_offline'][] = $server; } else { $layout_data['servers_online'][] = $server; } } $auto_refresh_seconds = psm_get_conf('auto_refresh_servers'); if (intval($auto_refresh_seconds) > 0) { $this->twig->addGlobal('auto_refresh', true); $this->twig->addGlobal('auto_refresh_seconds', $auto_refresh_seconds); } return $this->twig->render('module/server/status/index.tpl.html', $layout_data); }
/** * If a post has been done, gather all the posted data * and save it to the database */ protected function executeSave() { if (!empty($_POST)) { // save new config $clean = array('language' => $_POST['language'], 'sms_gateway' => $_POST['sms_gateway'], 'alert_type' => $_POST['alert_type'], 'email_smtp_security' => in_array($_POST['email_smtp_security'], array('', 'ssl', 'tls')) ? $_POST['email_smtp_security'] : '', 'auto_refresh_servers' => intval(psm_POST('auto_refresh_servers', 0)), 'log_retention_period' => intval(psm_POST('log_retention_period', 365)), 'password_encrypt_key' => psm_POST('password_encrypt_key', sha1(microtime()))); foreach ($this->checkboxes as $input_key) { $clean[$input_key] = isset($_POST[$input_key]) ? '1' : '0'; } foreach ($this->fields as $input_key) { if (isset($_POST[$input_key])) { $clean[$input_key] = $_POST[$input_key]; } } $language_refresh = $clean['language'] != psm_get_conf('language'); foreach ($clean as $key => $value) { psm_update_conf($key, $value); } $this->addMessage(psm_get_lang('config', 'updated'), 'success'); if (!empty($_POST['test_email'])) { $this->testEmail(); } elseif (!empty($_POST['test_sms'])) { $this->testSMS(); } elseif (!empty($_POST['test_pushover'])) { $this->testPushover(); } if ($language_refresh) { header('Location: ' . psm_build_url(array('mod' => 'config'), true, false)); die; } if (isset($_POST['general_submit'])) { $this->default_tab = 'general'; } elseif (isset($_POST['email_submit']) || !empty($_POST['test_email'])) { $this->default_tab = 'email'; } elseif (isset($_POST['sms_submit']) || !empty($_POST['test_sms'])) { $this->default_tab = 'sms'; } elseif (isset($_POST['pushover_submit']) || !empty($_POST['test_pushover'])) { $this->default_tab = 'pushover'; } } return $this->runAction('index'); }
/** * Prepare the view template */ protected function executeView() { if ($this->server_id == 0) { return $this->initializeAction('index'); } $server = $this->getServers($this->server_id); if (empty($server)) { return $this->initializeAction('index'); } $tpl_data = $this->getLabels(); $tpl_data = array_merge($tpl_data, $this->formatServer($server)); // create history HTML $history = new \psm\Util\Server\HistoryGraph($this->db, $this->twig); $tpl_data['html_history'] = $history->createHTML($this->server_id); // add edit/delete buttons for admins if ($this->user->getUserLevel() == PSM_USER_ADMIN) { $tpl_data['has_admin_actions'] = true; $tpl_data['url_edit'] = psm_build_url(array('mod' => 'server', 'action' => 'edit', 'id' => $this->server_id, 'back_to' => 'view')); $modal = new \psm\Util\Module\Modal($this->twig, 'delete', \psm\Util\Module\Modal::MODAL_TYPE_DANGER); $this->addModal($modal); $modal->setTitle(psm_get_lang('servers', 'delete_title')); $modal->setMessage(psm_get_lang('servers', 'delete_message')); $modal->setOKButtonLabel(psm_get_lang('system', 'delete')); } // add all available servers to the menu $servers = $this->getServers(); $tpl_data['options'] = array(); foreach ($servers as $i => $server_available) { $tpl_data['options'][] = array('class_active' => $server_available['server_id'] == $this->server_id ? 'active' : '', 'url' => psm_build_url(array('mod' => 'server', 'action' => 'view', 'id' => $server_available['server_id'])), 'label' => $server_available['label']); } $sidebar = new \psm\Util\Module\Sidebar($this->twig); $this->setSidebar($sidebar); // check which module the user came from, and add a link accordingly $back_to = isset($_GET['back_to']) && $_GET['back_to'] == 'server_status' ? $_GET['back_to'] : 'server'; $sidebar->addButton('go_back', psm_get_lang('system', 'go_back'), psm_build_url(array('mod' => $back_to)), 'th-list'); return $this->twig->render('module/server/server/view.tpl.html', $tpl_data); }
/** * This functions performs the pushover notifications * * @param array $users * @return boolean */ protected function notifyByPushover($users) { $userlist = array(); $pushover = psm_build_pushover(); if ($this->status_new === true) { $pushover->setPriority(0); } else { $pushover->setPriority(2); $pushover->setRetry(300); //Used with Priority = 2; Pushover will resend the notification every 60 seconds until the user accepts. $pushover->setExpire(3600); //Used with Priority = 2; Pushover will resend the notification every 60 seconds for 3600 seconds. After that point, it stops sending notifications. } $message = psm_parse_msg($this->status_new, 'pushover_message', $this->server); $pushover->setTitle(psm_parse_msg($this->status_new, 'pushover_title', $this->server)); $pushover->setMessage(str_replace('<br/>', "\n", $message)); $pushover->setUrl(psm_build_url()); $pushover->setUrlTitle(psm_get_lang('system', 'title')); foreach ($users as $user) { if (trim($user['pushover_key']) == '') { continue; } $userlist[] = $user['user_id']; $pushover->setUser($user['pushover_key']); if ($user['pushover_device'] != '') { $pushover->setDevice($user['pushover_device']); } $pushover->send(); } if (psm_get_conf('log_pushover')) { psm_add_log($this->server_id, 'pushover', $message, implode(',', $userlist)); } }
/** * Sends the password-reset-email. * @param int $user_id * @param string $user_email * @param string $user_password_reset_hash */ protected function sendPasswordForgotMail($user_id, $user_email, $user_password_reset_hash) { $mail = psm_build_mail(); $mail->Subject = psm_get_lang('login', 'password_reset_email_subject'); $url = psm_build_url(array('action' => 'reset', 'user_id' => $user_id, 'token' => $user_password_reset_hash), true, false); $body = psm_get_lang('login', 'password_reset_email_body'); $body = str_replace('%link%', $url, $body); $mail->Body = $body; $mail->AltBody = str_replace('<br/>', "\n", $body); $mail->AddAddress($user_email); $mail->Send(); }
/** * Create HTML code for the menu * @return string */ protected function createHTMLMenu() { $ulvl = $this->getUser()->getUserLevel(); $tpl_data = array('label_help' => psm_get_lang('menu', 'help'), 'label_profile' => psm_get_lang('users', 'profile'), 'label_logout' => psm_get_lang('login', 'logout'), 'url_profile' => psm_build_url(array('mod' => 'user_profile')), 'url_logout' => psm_build_url(array('logout' => 1))); switch ($ulvl) { case PSM_USER_ADMIN: $items = array('server_status', 'server', 'server_log', 'user', 'config', 'server_update'); break; case PSM_USER_USER: $items = array('server_status', 'server', 'server_log', 'server_update'); break; default: $items = array(); break; } $tpl_data['menu'] = array(); foreach ($items as $key) { $tpl_data['menu'][] = array('active' => $key == psm_GET('mod') ? 'active' : '', 'url' => psm_build_url(array('mod' => $key)), 'label' => psm_get_lang('menu', $key)); } if ($ulvl != PSM_USER_ANONYMOUS) { $user = $this->getUser()->getUser(); $tpl_data['label_usermenu'] = str_replace('%user_name%', $user->name, psm_get_lang('login', 'welcome_usermenu')); } return $this->twig->render('main/menu.tpl.html', $tpl_data); }
* 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 <*****@*****.**> * @copyright Copyright (c) 2008-2015 Pepijn Over <*****@*****.**> * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 * @version Release: @package_version@ * @link http://www.phpservermonitor.org/ **/ require __DIR__ . '/src/bootstrap.php'; psm_no_cache(); if (isset($_GET["logout"])) { $router->getService('user')->doLogout(); // logged out, redirect to login header('Location: ' . psm_build_url()); die; } $mod = psm_GET('mod', PSM_MODULE_DEFAULT); try { $router->run($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 $router->run(PSM_MODULE_DEFAULT); }
/** * Crate HTML for the update screen for a user * * @return string */ protected function executeEdit() { $user_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $fields_prefill = array('name', 'user_name', 'mobile', 'pushover_key', 'pushover_device', 'email'); if ($user_id == 0) { // insert mode $title = psm_get_lang('system', 'insert'); $placeholder_password = ''; $lvl_selected = PSM_USER_USER; // default level is regular user // attempt to prefill previously posted fields $edit_user = new \stdClass(); foreach ($fields_prefill as $field) { $edit_user->{$field} = isset($_POST[$field]) ? $_POST[$field] : ''; } // add inactive class to all servers foreach ($this->servers as &$server) { $server['class'] = 'inactive'; } } else { // edit mode try { $this->user_validator->userId($user_id); } catch (\InvalidArgumentException $e) { $this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error'); return $this->executeIndex(); } $edit_user = $this->user->getUser($user_id); $title = psm_get_lang('system', 'edit') . ' ' . $edit_user->name; $placeholder_password = psm_get_lang('users', 'password_leave_blank'); $lvl_selected = $edit_user->level; // select servers for this user $user_servers = $this->getUserServers($user_id); foreach ($this->servers as &$server) { if (in_array($server['server_id'], $user_servers)) { $server['edit_selected'] = 'selected="selected"'; $server['class'] = 'active'; } } } $tpl_data = array('titlemode' => $title, 'placeholder_password' => $placeholder_password, 'edit_user_id' => $user_id, 'url_save' => psm_build_url(array('mod' => 'user', 'action' => 'save', 'id' => $user_id)), 'servers' => $this->servers, 'user_level' => $lvl_selected); foreach ($fields_prefill as $field) { if (isset($edit_user->{$field})) { $tpl_data['edit_value_' . $field] = $edit_user->{$field}; } } $tpl_data['levels'] = array(); foreach ($this->user_validator->getUserLevels() as $lvl) { $tpl_data['levels'][] = array('value' => $lvl, 'label' => psm_get_lang('users', 'level_' . $lvl)); } $tpl_data = array_merge($this->getLabels(), $tpl_data); return $this->twig->render('module/user/user/update.tpl.html', $tpl_data); }
/** * 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 * @throws \LogicException */ public function run($mod = null) { if (!psm_is_cli() && isset($_GET["logout"])) { $this->services['user']->doLogout(); // logged out, redirect to login header('Location: ' . psm_build_url()); die; } 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(); $action = null; if ($min_lvl < PSM_USER_ANONYMOUS) { // if user is not logged in, load login module if (!$this->services['user']->isUserLoggedIn()) { $controller = $this->getController('user_login'); } elseif ($this->services['user']->getUserLevel() > $min_lvl) { $controller = $this->getController('error'); $action = '401'; } } $controller->setUser($this->services['user']); $response = $controller->initialize($action); if (!$response instanceof Response) { throw new \LogicException('Controller did not return a Response object.'); } $response->send(); }