UpdateProfile.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Login
  4. *
  5. * Copyright 2010 by Shaun McCormick <shaun+login@modx.com>
  6. *
  7. * Login is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Login is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package login
  21. */
  22. /**
  23. * Handles updating the profile of the active user
  24. *
  25. * @package login
  26. * @subpackage controllers
  27. */
  28. class LoginUpdateProfileController extends LoginController {
  29. /** @var boolean $hasPosted */
  30. public $hasPosted = false;
  31. /** @var modUser $user */
  32. public $user;
  33. /** @var modUserProfile $profile */
  34. public $profile;
  35. /**
  36. * Load default properties for this controller
  37. * @return void
  38. */
  39. public function initialize() {
  40. $this->modx->lexicon->load('login:updateprofile');
  41. $this->modx->lexicon->load('login:register');
  42. $this->setDefaultProperties(array(
  43. 'allowedExtendedFields' => '',
  44. 'emailField' => 'email',
  45. 'errTpl' => '<span class="error">[[+error]]</span>',
  46. 'excludeExtended' => '',
  47. 'placeholderPrefix' => '',
  48. 'postHooks' => '',
  49. 'preHooks' => '',
  50. 'redirectToLogin' => true,
  51. 'reloadOnSuccess' => true,
  52. 'submitVar' => 'login-updprof-btn',
  53. 'successKey' => 'updpsuccess',
  54. 'successMsg' => $this->modx->lexicon('login.profile_updated'),
  55. 'successMsgPlaceholder' => 'error.message',
  56. 'syncUsername' => false,
  57. 'useExtended' => true,
  58. 'user' => '',
  59. 'validate' => '',
  60. 'errorDelimited' => '<br>'
  61. ));
  62. }
  63. /**
  64. * Handle the UpdateProfile snippet business logic
  65. * @return string
  66. */
  67. public function process() {
  68. if (!$this->verifyAuthentication()) return '';
  69. if (!$this->getUser()) return '';
  70. if (!$this->getProfile()) return '';
  71. $this->checkForSuccessMessage();
  72. $validate = true;
  73. if ($this->hasPost()) {
  74. $this->loadDictionary();
  75. if ($this->validate()) {
  76. if ($this->runPreHooks()) {
  77. /* update the profile */
  78. $result = $this->runProcessor('UpdateProfile');
  79. if ($result !== true) {
  80. $this->modx->toPlaceholder('message',$result,'error');
  81. } else if ($this->getProperty('reloadOnSuccess',true,'isset')) {
  82. $url = $this->modx->makeUrl($this->modx->resource->get('id'),'',array(
  83. $this->getProperty('successKey','updpsuccess') => 1,
  84. ),'full');
  85. $this->modx->sendRedirect($url);
  86. } else {
  87. $this->modx->setPlaceholder('login.update_success',true);
  88. }
  89. } else {
  90. $validate = false;
  91. }
  92. } else {
  93. $validate = false;
  94. }
  95. }
  96. $this->setFieldPlaceholders();
  97. if ($validate === false) {
  98. $placeholderPrefix = rtrim($this->getProperty('placeholderPrefix'), '.');
  99. $fields = $this->dictionary->toArray();
  100. $fields = $this->escapePlaceholders($fields);
  101. $this->modx->toPlaceholders($fields, $placeholderPrefix);
  102. }
  103. return '';
  104. }
  105. /**
  106. * Verify the user is logged in; otherwise redirect or return false
  107. * @return boolean
  108. */
  109. public function verifyAuthentication() {
  110. $authenticated = true;
  111. if (!$this->modx->user->hasSessionContext($this->modx->context->get('key'))) {
  112. $authenticated = false;
  113. if ($this->getProperty('redirectToLogin',true,'isset')) {
  114. $this->modx->sendUnauthorizedPage();
  115. }
  116. }
  117. return $authenticated;
  118. }
  119. /**
  120. * Get the specified or active user
  121. * @return modUser
  122. */
  123. public function getUser() {
  124. $user = $this->getProperty('user',false);
  125. if (!empty($user)) {
  126. $c = intval($user) == 0 ? array('username' => $user) : intval($user);
  127. $this->user = $this->modx->getObject('modUser',$c);
  128. } else {
  129. $this->user =& $this->modx->user;
  130. }
  131. if (empty($this->user)) {
  132. $this->modx->log(modX::LOG_LEVEL_ERROR,'Could not find user: '.$user);
  133. }
  134. return $this->user;
  135. }
  136. /**
  137. * Get the Profile of the active user
  138. * @return modUserProfile
  139. */
  140. public function getProfile() {
  141. $this->profile = $this->user->getOne('Profile');
  142. if (empty($this->profile)) {
  143. $this->modx->log(modX::LOG_LEVEL_ERROR,'Could not find profile for user: '.$this->user->get('username'));
  144. }
  145. return $this->profile;
  146. }
  147. /**
  148. * Set the user data as placeholders
  149. * @return void
  150. */
  151. public function setFieldPlaceholders() {
  152. $placeholders = $this->profile->toArray();
  153. $placeholderPrefix = rtrim($this->getProperty('placeholderPrefix'), '.');
  154. /* add extended fields to placeholders */
  155. if ($this->getProperty('useExtended', true)) {
  156. $extended = $this->profile->get('extended');
  157. if (!empty($extended) && is_array($extended)) {
  158. $placeholders = array_merge($extended, $placeholders);
  159. }
  160. }
  161. $this->modx->toPlaceholders($placeholders, $placeholderPrefix);
  162. foreach ($placeholders as $k => $v) {
  163. if (is_array($v)) {
  164. $this->modx->toPlaceholder($k, json_encode($v), $placeholderPrefix);
  165. }
  166. }
  167. }
  168. /**
  169. * Look for a success message by the previous updating
  170. * @return void
  171. */
  172. public function checkForSuccessMessage() {
  173. if (!empty($_REQUEST[$this->getProperty('successKey','updpsuccess')])) {
  174. $this->modx->setPlaceholder('login.update_success',true);
  175. }
  176. }
  177. /**
  178. * See if the form has been submitted
  179. * @return boolean
  180. */
  181. public function hasPost() {
  182. $submitVar = $this->getProperty('submitVar');
  183. return (!empty($_POST) && (empty($submitVar) || !empty($_POST[$submitVar])));
  184. }
  185. /**
  186. * Validate the form submission
  187. *
  188. * @return boolean
  189. */
  190. public function validate() {
  191. $validated = false;
  192. $this->loadValidator();
  193. $fields = $this->validator->validateFields($this->dictionary,$this->getProperty('validate',''));
  194. foreach ($fields as $k => $v) {
  195. $fields[$k] = str_replace(array('[',']'),array('&#91;','&#93;'),$v);
  196. }
  197. $this->dictionary->fromArray($fields);
  198. $this->removeSubmitVar();
  199. $this->preventDuplicateEmails();
  200. if ($this->validator->hasErrors()) {
  201. $placeholders = $this->dictionary->toArray();
  202. $placeholderPrefix = rtrim($this->getProperty('placeholderPrefix'), '.');
  203. $errorPrefix = ($placeholderPrefix) ? $placeholderPrefix . '.error' : 'error';
  204. $this->modx->toPlaceholders($this->validator->getErrors(), $errorPrefix);
  205. $this->modx->toPlaceholders($placeholders, $placeholderPrefix);
  206. foreach ($placeholders as $k => $v) {
  207. if (is_array($v)) {
  208. $this->modx->toPlaceholder($k, json_encode($v), $placeholderPrefix);
  209. }
  210. }
  211. $errors = array();
  212. $es = $this->validator->getErrors();
  213. foreach ($es as $key => $error) {
  214. $errors['message'] .= $error . $this->getProperty('errorDelimited');
  215. }
  216. $this->modx->toPlaceholder('message', $errors['message'], $errorPrefix);
  217. } else {
  218. $validated = true;
  219. }
  220. return $validated;
  221. }
  222. /**
  223. * Remove the submitVar from the field list
  224. * @return void
  225. */
  226. public function removeSubmitVar() {
  227. $submitVar = $this->getProperty('submitVar');
  228. if (!empty($submitVar)) {
  229. $this->dictionary->remove($submitVar);
  230. }
  231. }
  232. /**
  233. * If allow_multiple_emails setting is false, prevent duplicate emails
  234. * @return void
  235. */
  236. public function preventDuplicateEmails() {
  237. $emailField = $this->getProperty('emailField','email');
  238. $email = $this->dictionary->get($emailField);
  239. if (!empty($email) && !$this->modx->getOption('allow_multiple_emails',null,false)) {
  240. $emailTaken = $this->modx->getObject('modUserProfile',array(
  241. 'email' => $email,
  242. 'internalKey:!=' => $this->user->get('id'),
  243. ));
  244. if ($emailTaken) {
  245. $this->validator->addError($emailField,$this->modx->lexicon('login.email_taken',array('email' => $email)));
  246. }
  247. }
  248. }
  249. /**
  250. * Run any preHooks for this snippet, that allow it to stop the form as submitted
  251. * @return boolean
  252. */
  253. public function runPreHooks() {
  254. $validated = true;
  255. $preHooks = $this->getProperty('preHooks','');
  256. if (!empty($preHooks)) {
  257. $this->loadHooks('preHooks');
  258. $this->preHooks->loadMultiple($preHooks,$this->dictionary->toArray(),array(
  259. 'submitVar' => $this->getProperty('submitVar'),
  260. 'redirectToLogin' => $this->getProperty('redirectToLogin',true,'isset'),
  261. 'reloadOnSuccess' => $this->getProperty('reloadOnSuccess',true,'isset'),
  262. ));
  263. $values = $this->preHooks->getValues();
  264. if (!empty($values)) {
  265. $this->dictionary->fromArray($values);
  266. }
  267. if ($this->preHooks->hasErrors()) {
  268. $errors = array();
  269. $es = $this->preHooks->getErrors();
  270. $errTpl = $this->getProperty('errTpl');
  271. foreach ($es as $key => $error) {
  272. $errors[$key] = str_replace('[[+error]]',$error,$errTpl);
  273. }
  274. $this->modx->toPlaceholders($errors,'error');
  275. $errorMsg = $this->preHooks->getErrorMessage();
  276. $this->modx->toPlaceholder('message',$errorMsg,'error');
  277. $validated = false;
  278. }
  279. }
  280. return $validated;
  281. }
  282. }
  283. return 'LoginUpdateProfileController';