Profile.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * Displays the profile of a specific user
  24. *
  25. * @package login
  26. * @subpackage controllers
  27. */
  28. class LoginProfileController extends LoginController {
  29. /** @var modUser $user */
  30. public $user;
  31. /** @var modUserProfile $profile */
  32. public $profile;
  33. public function initialize() {
  34. $this->setDefaultProperties(array(
  35. 'prefix' => '',
  36. 'user' => false,
  37. ));
  38. $this->modx->lexicon->load('login:profile');
  39. }
  40. /**
  41. * Process the controller
  42. * @return string
  43. */
  44. public function process() {
  45. if (!$this->getUser()) {
  46. return '';
  47. }
  48. if (!$this->getProfile()) {
  49. return '';
  50. }
  51. $this->setToPlaceholders();
  52. return '';
  53. }
  54. /**
  55. * Set the user data to placeholders
  56. *
  57. * @return array
  58. */
  59. public function setToPlaceholders() {
  60. $placeholders = array_merge($this->profile->toArray(),$this->user->toArray());
  61. $placeholderPrefix = rtrim($this->getProperty('prefix', ''), '.');
  62. $extended = $this->getExtended();
  63. $placeholders = array_merge($extended,$placeholders);
  64. $placeholders = $this->removePasswordPlaceholders($placeholders);
  65. $this->modx->toPlaceholders($placeholders, $placeholderPrefix);
  66. foreach ($placeholders as $k => $v) {
  67. if (is_array($v)) {
  68. $this->modx->toPlaceholder($k, json_encode($v), $placeholderPrefix);
  69. }
  70. }
  71. return $placeholders;
  72. }
  73. /**
  74. * Remove the password fields from the outputted placeholders
  75. * @param array $placeholders
  76. * @return array
  77. */
  78. public function removePasswordPlaceholders(array $placeholders = array()) {
  79. unset($placeholders['password'],$placeholders['cachepwd'],$placeholders['hash_class'],$placeholders['salt'],$placeholders['sessionid']);
  80. return $placeholders;
  81. }
  82. /**
  83. * Get extended fields for a user
  84. * @return array
  85. */
  86. public function getExtended() {
  87. $extended = array();
  88. if ($this->getProperty('useExtended',true,'isset')) {
  89. $extended = $this->profile->get('extended');
  90. }
  91. return (array) $extended;
  92. }
  93. /**
  94. * Get the profile for the user
  95. *
  96. * @return bool|modUserProfile
  97. */
  98. public function getProfile() {
  99. $this->profile = $this->user->getOne('Profile');
  100. if (empty($this->profile)) {
  101. $this->modx->log(modX::LOG_LEVEL_ERROR,'Could not find profile for user: '.$this->user->get('username'));
  102. return false;
  103. }
  104. return $this->profile;
  105. }
  106. /**
  107. * Get the specified or active user
  108. * @return boolean|modUser
  109. */
  110. public function getUser() {
  111. $user = $this->getProperty('user',false,'isset');
  112. /* verify authenticated status if no user specified */
  113. if (empty($user) && !$this->modx->user->hasSessionContext($this->modx->context->get('key'))) {
  114. $this->user = false;
  115. }
  116. /* specifying a specific user, so try and get it */
  117. if (!empty($user)) {
  118. $username = $user;
  119. $userNum = (int)$user;
  120. $c = array();
  121. if (!empty($userNum)) {
  122. $c['id'] = $userNum;
  123. } else {
  124. $c['username'] = $username;
  125. }
  126. $this->user = $this->modx->getObject('modUser',$c);
  127. if (!$this->user) {
  128. $this->modx->log(modX::LOG_LEVEL_ERROR,'Could not find user: '.$username);
  129. $this->user = false;
  130. }
  131. /* just use current user if user is logged in */
  132. } else {
  133. if (!$this->modx->user->hasSessionContext($this->modx->context->get('key'))) {
  134. $this->user = false;
  135. } else {
  136. $this->user =& $this->modx->user;
  137. }
  138. }
  139. return $this->user;
  140. }
  141. }
  142. return 'LoginProfileController';