/**
  * Save the profile
  */
 protected function executeSave()
 {
     if (empty($_POST)) {
         // dont process anything if no data has been posted
         return $this->executeIndex();
     }
     $validator = new \psm\Util\User\UserValidator($this->user);
     $user = $this->user->getUser();
     $fields = $this->profile_fields;
     $fields[] = 'password';
     $fields[] = 'password_repeat';
     $clean = array();
     foreach ($fields as $field) {
         if (isset($_POST[$field])) {
             $clean[$field] = trim(strip_tags($_POST[$field]));
         } else {
             $clean[$field] = '';
         }
     }
     // validate the lot
     try {
         $validator->username($clean['user_name'], $this->user->getUserId());
         $validator->email($clean['email']);
         // always validate password for new users,
         // but only validate it for existing users when they change it.
         if ($clean['password'] != '') {
             $validator->password($clean['password'], $clean['password_repeat']);
         }
     } catch (\InvalidArgumentException $e) {
         $this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error');
         return $this->executeIndex();
     }
     if (!empty($clean['password'])) {
         $password = $clean['password'];
     }
     unset($clean['password']);
     unset($clean['password_repeat']);
     $this->db->save(PSM_DB_PREFIX . 'users', $clean, array('user_id' => $this->user->getUserId()));
     if (isset($password)) {
         $this->user->changePassword($this->user->getUserId(), $password);
     }
     $this->addMessage(psm_get_lang('users', 'profile_updated'), 'success');
     return $this->executeIndex();
 }
 /**
  * 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()));
 }