protected function executeSaveLayout()
 {
     if ($this->isXHR()) {
         $layout = psm_POST('layout', 0);
         $this->getUser()->setUserPref('status_layout', $layout);
         $response = new \Symfony\Component\HttpFoundation\JsonResponse();
         $response->setData(array('layout' => $layout));
         return $response;
     }
 }
 /**
  * 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');
 }
 /**
  * Executes the saving of one of the servers
  */
 protected function executeSave()
 {
     if (empty($_POST)) {
         // dont process anything if no data has been posted
         return $this->executeIndex();
     }
     $clean = array('label' => trim(strip_tags(psm_POST('label', ''))), 'ip' => trim(strip_tags(psm_POST('ip', ''))), 'timeout' => isset($_POST['timeout']) && intval($_POST['timeout']) > 0 ? intval($_POST['timeout']) : null, 'port' => intval(psm_POST('port', 0)), 'type' => psm_POST('type', ''), 'pattern' => psm_POST('pattern', ''), 'warning_threshold' => intval(psm_POST('warning_threshold', 0)), 'active' => in_array($_POST['active'], array('yes', 'no')) ? $_POST['active'] : 'no', 'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no', 'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no', 'pushover' => in_array($_POST['pushover'], array('yes', 'no')) ? $_POST['pushover'] : 'no');
     // make sure websites start with http://
     if ($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') {
         $clean['ip'] = 'http://' . $clean['ip'];
     }
     // validate the lot
     $server_validator = new \psm\Util\Server\ServerValidator($this->db);
     try {
         if ($this->server_id > 0) {
             $server_validator->serverId($this->server_id);
         }
         $server_validator->label($clean['label']);
         $server_validator->type($clean['type']);
         $server_validator->ip($clean['ip'], $clean['type']);
         $server_validator->warningThreshold($clean['warning_threshold']);
     } catch (\InvalidArgumentException $ex) {
         $this->addMessage(psm_get_lang('servers', 'error_' . $ex->getMessage()), 'error');
         return $this->executeEdit();
     }
     // check for edit or add
     if ($this->server_id > 0) {
         // edit
         $this->db->save(PSM_DB_PREFIX . 'servers', $clean, array('server_id' => $this->server_id));
         $this->addMessage(psm_get_lang('servers', 'updated'), 'success');
     } else {
         // add
         $clean['status'] = 'on';
         $this->server_id = $this->db->save(PSM_DB_PREFIX . 'servers', $clean);
         $this->addMessage(psm_get_lang('servers', 'inserted'), 'success');
     }
     // update users
     $user_idc = psm_POST('user_id', array());
     $user_idc_save = array();
     foreach ($user_idc as $user_id) {
         $user_idc_save[] = array('user_id' => intval($user_id), 'server_id' => intval($this->server_id));
     }
     $this->db->delete(PSM_DB_PREFIX . 'users_servers', array('server_id' => $this->server_id));
     if (!empty($user_idc_save)) {
         // add all new users
         $this->db->insertMultiple(PSM_DB_PREFIX . 'users_servers', $user_idc_save);
     }
     $back_to = isset($_GET['back_to']) ? $_GET['back_to'] : 'index';
     if ($back_to == 'view') {
         return $this->initializeAction('view');
     } else {
         return $this->initializeAction('index');
     }
 }
 /**
  * Run the controller.
  *
  * @param string $action if NULL, the action will be retrieved from user input (GET/POST)
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function run($action = null)
 {
     if ($action === null) {
         $action = psm_GET('action', psm_POST('action', $this->action_default));
     }
     $this->xhr = (bool) psm_GET('xhr', psm_POST('xhr', false));
     if (!in_array($action, $this->actions) || !($result = $this->runAction($action))) {
         $result = $this->runAction($this->action_default);
     }
     if ($result instanceof Response) {
         return $result;
     }
     // no response returned from execute, create regular HTML
     return $this->createHTML($result);
 }
 /**
  * Execute the install and upgrade process to a newer version
  */
 protected function executeInstall()
 {
     if (!defined('PSM_DB_PREFIX') || !$this->db->status()) {
         return $this->executeConfig();
     }
     $add_user = false;
     // check if user submitted username + password in previous step
     // this would only be the case for new installs, and install from
     // before 3.0
     $new_user = array('user_name' => psm_POST('username'), 'name' => psm_POST('username'), 'password' => psm_POST('password'), 'password_repeat' => psm_POST('password_repeat'), 'email' => psm_POST('email', ''), 'mobile' => '', 'level' => PSM_USER_ADMIN, 'pushover_key' => '', 'pushover_device' => '');
     $validator = new \psm\Util\User\UserValidator($this->user);
     $logger = array($this, 'addMessage');
     $installer = new \psm\Util\Install\Installer($this->db, $logger);
     if ($this->isUpgrade()) {
         $this->addMessage('Upgrade process started.', 'info');
         $version_from = $this->getPreviousVersion();
         if ($version_from === false) {
             $this->addMessage('Unable to locate your previous version. Please run a fresh install.', 'error');
         } else {
             if (version_compare($version_from, PSM_VERSION, '=')) {
                 $this->addMessage('Your installation is already at the latest version.', 'success');
             } elseif (version_compare($version_from, PSM_VERSION, '>')) {
                 $this->addMessage('This installer does not support downgrading, sorry.', 'error');
             } else {
                 $this->addMessage('Upgrading from ' . $version_from . ' to ' . PSM_VERSION, 'info');
                 $installer->upgrade($version_from, PSM_VERSION);
             }
             if (version_compare($version_from, '3.0.0', '<')) {
                 $add_user = true;
             }
         }
     } else {
         // validate the lot
         try {
             $validator->email($new_user['email']);
             $validator->password($new_user['password'], $new_user['password_repeat']);
         } catch (\InvalidArgumentException $e) {
             $this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error');
             return $this->executeConfig();
         }
         $this->addMessage('Installation process started.', 'success');
         $installer->install();
         // add user
         $add_user = true;
     }
     if ($add_user) {
         unset($new_user['password_repeat']);
         $user_id = $this->db->save(PSM_DB_PREFIX . 'users', $new_user);
         if (intval($user_id) > 0) {
             $this->user->changePassword($user_id, $new_user['password']);
             $this->addMessage('User account has been created successfully.', 'success');
         } else {
             $this->addMessage('There was an error adding your user account.', 'error');
         }
     }
     return $this->twig->render('module/install/success.tpl.html', array('messages' => $this->getMessages()));
 }
 /**
  * Executes the saving of a user
  */
 protected function executeSave()
 {
     if (empty($_POST)) {
         // dont process anything if no data has been posted
         return $this->executeIndex();
     }
     $user_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
     $fields = array('name', 'user_name', 'password', 'password_repeat', 'level', 'mobile', 'pushover_key', 'pushover_device', 'email');
     $clean = array();
     foreach ($fields as $field) {
         if (isset($_POST[$field])) {
             $clean[$field] = trim(strip_tags($_POST[$field]));
         } else {
             $clean[$field] = '';
         }
     }
     // validate the lot
     try {
         $this->user_validator->username($clean['user_name'], $user_id);
         $this->user_validator->email($clean['email']);
         $this->user_validator->level($clean['level']);
         // always validate password for new users,
         // but only validate it for existing users when they change it.
         if ($user_id == 0 || $user_id > 0 && $clean['password'] != '') {
             $this->user_validator->password($clean['password'], $clean['password_repeat']);
         }
         if ($user_id > 0) {
             $this->user_validator->userId($user_id);
         }
     } catch (\InvalidArgumentException $e) {
         $this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error');
         return $this->executeEdit();
     }
     if (!empty($clean['password'])) {
         $password = $clean['password'];
     }
     unset($clean['password_repeat']);
     if ($user_id > 0) {
         // edit user
         unset($clean['password']);
         // password update is executed separately
         $this->db->save(PSM_DB_PREFIX . 'users', $clean, array('user_id' => $user_id));
         $this->addMessage(psm_get_lang('users', 'updated'), 'success');
     } else {
         // add user
         $clean['password'] = '';
         // password update is executed separately
         $user_id = $this->db->save(PSM_DB_PREFIX . 'users', $clean);
         $this->addMessage(psm_get_lang('users', 'inserted'), 'success');
     }
     if (isset($password)) {
         $this->user->changePassword($user_id, $password);
     }
     // update servers
     $server_idc = psm_POST('server_id', array());
     $server_idc_save = array();
     foreach ($server_idc as $server_id) {
         $server_idc_save[] = array('user_id' => $user_id, 'server_id' => intval($server_id));
     }
     // delete all existing records
     $this->db->delete(PSM_DB_PREFIX . 'users_servers', array('user_id' => $user_id));
     if (!empty($server_idc_save)) {
         // add all new servers
         $this->db->insertMultiple(PSM_DB_PREFIX . 'users_servers', $server_idc_save);
     }
     return $this->executeIndex();
 }