diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index ca34965f..2849b9b3 100755 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -343,9 +343,10 @@ function psm_build_mail($from_name = null, $from_email = null) { * Generate a new link to the current monitor * @param array $params key value pairs * @param boolean $urlencode urlencode all params? + * @param boolean $htmlentities use entities in url? * @return string */ -function psm_build_url($params = array(), $urlencode = true) { +function psm_build_url($params = array(), $urlencode = true, $htmlentities = true) { $defports = array(80, 443); $url = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; if(!in_array($_SERVER['SERVER_PORT'], $defports)) { @@ -355,12 +356,13 @@ function psm_build_url($params = array(), $urlencode = true) { if($params != null) { $url .= '?'; + $delim = ($htmlentities) ? '&' : '&'; foreach($params as $k => $v) { if($urlencode) { $v = urlencode($v); } - $url .= '&' . $k . '=' . $v; + $url .= $delim . $k . '=' . $v; } } diff --git a/src/lang/bg.lang.php b/src/lang/bg.lang.php index 96c38ed5..082bb4d5 100644 --- a/src/lang/bg.lang.php +++ b/src/lang/bg.lang.php @@ -68,6 +68,8 @@ $sm_lang = array( 'email' => 'Имейл', 'updated' => 'Информацията за потребителя е обновена.', 'inserted' => 'Потребителят е добавен.', + 'profile' => 'Profile', + 'profile_updated' => 'Your profile has been updated.', '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.', diff --git a/src/lang/br.lang.php b/src/lang/br.lang.php index 23cb13e8..d1baf799 100755 --- a/src/lang/br.lang.php +++ b/src/lang/br.lang.php @@ -68,6 +68,8 @@ $sm_lang = array( 'email' => 'Email', 'updated' => 'Usuário atualizado.', 'inserted' => 'Usuário adicionado.', + 'profile' => 'Profile', + 'profile_updated' => 'Your profile has been updated.', '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.', diff --git a/src/lang/de.lang.php b/src/lang/de.lang.php index 0826c8eb..d5e94696 100755 --- a/src/lang/de.lang.php +++ b/src/lang/de.lang.php @@ -68,6 +68,8 @@ $sm_lang = array( 'email' => 'Email', 'updated' => 'Benutzer bearbeitet.', 'inserted' => 'Benutzer eingetragen.', + 'profile' => 'Profile', + 'profile_updated' => 'Your profile has been updated.', '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.', diff --git a/src/lang/en.lang.php b/src/lang/en.lang.php index 7641ee05..84de1dc2 100755 --- a/src/lang/en.lang.php +++ b/src/lang/en.lang.php @@ -68,8 +68,10 @@ $sm_lang = array( 'email' => 'Email', 'updated' => 'User updated.', 'inserted' => 'User added.', + 'profile' => 'Profile', + 'profile_updated' => 'Your profile has been updated.', '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_invalid' => 'The username 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.', diff --git a/src/lang/fr.lang.php b/src/lang/fr.lang.php index df512a13..7fae20d8 100755 --- a/src/lang/fr.lang.php +++ b/src/lang/fr.lang.php @@ -68,6 +68,8 @@ $sm_lang = array( 'email' => 'Email', 'updated' => 'Utilisateur mis à jour.', 'inserted' => 'Utilisateur ajouté.', + 'profile' => 'Profile', + 'profile_updated' => 'Your profile has been updated.', '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.', diff --git a/src/lang/kr.lang.php b/src/lang/kr.lang.php index 7a062b99..3e7982c0 100755 --- a/src/lang/kr.lang.php +++ b/src/lang/kr.lang.php @@ -68,6 +68,8 @@ $sm_lang = array( 'email' => 'Email', 'updated' => '수정되었습니다.', 'inserted' => '추가되었습니다.', + 'profile' => 'Profile', + 'profile_updated' => 'Your profile has been updated.', '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.', diff --git a/src/lang/nl.lang.php b/src/lang/nl.lang.php index 39054221..44aba170 100755 --- a/src/lang/nl.lang.php +++ b/src/lang/nl.lang.php @@ -68,6 +68,8 @@ $sm_lang = array( 'email' => 'Email', 'updated' => 'Gebruiker gewijzigd.', 'inserted' => 'Gebruiker toegevoegd.', + 'profile' => 'Profiel', + 'profile_updated' => 'Je profiel is bijgewerkt.', '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.', diff --git a/src/psm/Module/AbstractController.class.php b/src/psm/Module/AbstractController.class.php index 947b9160..79187456 100755 --- a/src/psm/Module/AbstractController.class.php +++ b/src/psm/Module/AbstractController.class.php @@ -207,7 +207,9 @@ abstract class AbstractController implements ControllerInterface { $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)), ); diff --git a/src/psm/Module/Server/Controller/UpdateController.class.php b/src/psm/Module/Server/Controller/UpdateController.class.php index 63e1f662..76c4a17d 100644 --- a/src/psm/Module/Server/Controller/UpdateController.class.php +++ b/src/psm/Module/Server/Controller/UpdateController.class.php @@ -48,7 +48,7 @@ class UpdateController extends AbstractController { // redirect user to regular status page header('Location: ' . psm_build_url(array( 'mod' => 'server_status' - ))); + ), true, false)); die(); } diff --git a/src/psm/Module/User/Controller/LoginController.class.php b/src/psm/Module/User/Controller/LoginController.class.php index 58c31036..2a959291 100644 --- a/src/psm/Module/User/Controller/LoginController.class.php +++ b/src/psm/Module/User/Controller/LoginController.class.php @@ -171,7 +171,7 @@ class LoginController extends AbstractController { '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; diff --git a/src/psm/Module/User/Controller/ProfileController.class.php b/src/psm/Module/User/Controller/ProfileController.class.php index e69de29b..5f6a5a2e 100644 --- a/src/psm/Module/User/Controller/ProfileController.class.php +++ b/src/psm/Module/User/Controller/ProfileController.class.php @@ -0,0 +1,142 @@ +. + * + * @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@ + * @since phpservermon 2.2.0 + **/ + +namespace psm\Module\User\Controller; +use psm\Module\AbstractController; +use psm\Service\Database; +use psm\Service\Template; + +class ProfileController extends AbstractController { + + /** + * Editable fields for the profile + * @var array $profile_fields + */ + protected $profile_fields = array('name', 'user_name', 'mobile', 'email'); + + function __construct(Database $db, Template $tpl) { + parent::__construct($db, $tpl); + + $this->setActions(array( + 'index', 'save', + ), 'index'); + } + + /** + * Show the profile page + */ + protected function executeIndex() { + $this->setTemplateId('user_profile', 'user/profile.tpl.html'); + + $user = $this->user->getUser(null, true); + + $tpl_data = array( + '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 : ''; + } + $this->tpl->addTemplateData($this->getTemplateId(), $tpl_data); + } + + /** + * 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(); + } + + // override parent::createHTMLLabels() + protected function createHTMLLabels() { + $this->tpl->addTemplateData( + $this->getTemplateId(), + array( + 'subtitle' => psm_get_lang('users', 'profile'), + '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_email' => psm_get_lang('users', 'email'), + 'label_save' => psm_get_lang('system', 'save'), + ) + ); + + return parent::createHTMLLabels(); + } +} diff --git a/src/psm/Module/User/UserModule.class.php b/src/psm/Module/User/UserModule.class.php index d7061e9b..4ff6f761 100644 --- a/src/psm/Module/User/UserModule.class.php +++ b/src/psm/Module/User/UserModule.class.php @@ -34,7 +34,7 @@ class UserModule implements ModuleInterface { return array( 'user' => __NAMESPACE__ . '\Controller\UserController', 'login' => __NAMESPACE__ . '\Controller\LoginController', -// 'profile' => __NAMESPACE__ . '\Controller\StatusController', + 'profile' => __NAMESPACE__ . '\Controller\ProfileController', ); } diff --git a/src/psm/Service/User.class.php b/src/psm/Service/User.class.php index 8b58fd44..cd273eae 100644 --- a/src/psm/Service/User.class.php +++ b/src/psm/Service/User.class.php @@ -93,9 +93,11 @@ class User { /** * Get user by id, or get current user. + * @param int $user_id if null it will attempt current user id + * @param boolean $flush if TRUE it will query db regardless of whether we already have the data * @return object|boolean FALSE if user not found, object otherwise */ - public function getUser($user_id = null) { + public function getUser($user_id = null, $flush = false) { if($user_id == null) { if(!$this->isUserLoggedIn()) { return false; @@ -104,7 +106,7 @@ class User { } } - if(!isset($this->user_data[$user_id])) { + if(!isset($this->user_data[$user_id]) || $flush) { $query_user = $this->db_connection->prepare('SELECT * FROM '.PSM_DB_PREFIX.'users WHERE user_id = :user_id'); $query_user->bindValue(':user_id', $user_id, \PDO::PARAM_INT); $query_user->execute(); diff --git a/src/templates/main.tpl.html b/src/templates/main.tpl.html index 94c8482e..a824e4fc 100755 --- a/src/templates/main.tpl.html +++ b/src/templates/main.tpl.html @@ -75,6 +75,7 @@ diff --git a/src/templates/user/profile.tpl.html b/src/templates/user/profile.tpl.html new file mode 100644 index 00000000..b1ed1961 --- /dev/null +++ b/src/templates/user/profile.tpl.html @@ -0,0 +1,51 @@ + +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+
+
+ \ No newline at end of file