PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_users/models/registration.php

https://github.com/joebushi/joomla
PHP | 292 lines | 148 code | 47 blank | 97 comment | 15 complexity | 7c6e20b47e72ed9103bbdb64c2a1f50f MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Site
  5. * @subpackage com_users
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. jimport('joomla.application.component.modelform');
  11. jimport('joomla.event.dispatcher');
  12. jimport('joomla.plugin.helper');
  13. /**
  14. * Registration model class for Users.
  15. *
  16. * @package Joomla.Site
  17. * @subpackage com_users
  18. * @version 1.0
  19. */
  20. class UsersModelRegistration extends JModelForm
  21. {
  22. /**
  23. * Method to auto-populate the model state.
  24. *
  25. * @since 1.6
  26. */
  27. protected function _populateState()
  28. {
  29. // Get the application object.
  30. $app = &JFactory::getApplication();
  31. $params = &$app->getParams('com_users');
  32. // Load the parameters.
  33. $this->setState('params', $params);
  34. }
  35. /**
  36. * Method to get the registration form.
  37. *
  38. * The base form is loaded from XML and then an event is fired
  39. * for users plugins to extend the form with extra fields.
  40. *
  41. * @access public
  42. * @return mixed JForm object on success, false on failure.
  43. * @since 1.0
  44. */
  45. function getForm()
  46. {
  47. // Get the form.
  48. $form = parent::getForm('registration', 'com_users.registration', array('array' => 'jform', 'event' => 'onPrepareForm'));
  49. // Check for an error.
  50. if (JError::isError($form)) {
  51. $this->setError($form->getMessage());
  52. return false;
  53. }
  54. // Get the dispatcher and load the users plugins.
  55. $dispatcher = &JDispatcher::getInstance();
  56. JPluginHelper::importPlugin('users');
  57. // Trigger the form preparation event.
  58. $results = $dispatcher->trigger('onPrepareUserRegistrationForm', array(&$form));
  59. // Check for errors encountered while preparing the form.
  60. if (count($results) && in_array(false, $results, true)) {
  61. $this->setError($dispatcher->getError());
  62. return false;
  63. }
  64. return $form;
  65. }
  66. /**
  67. * Method to get the registration form data.
  68. *
  69. * The base form data is loaded and then an event is fired
  70. * for users plugins to extend the data.
  71. *
  72. * @access public
  73. * @return mixed Data object on success, false on failure.
  74. * @since 1.0
  75. */
  76. function &getData()
  77. {
  78. $false = false;
  79. $data = new stdClass();
  80. $app = &JFactory::getApplication();
  81. $params = &JComponentHelper::getParams('com_users');
  82. // Override the base user data with any data in the session.
  83. $temp = (array)$app->getUserState('com_users.registration.data', array());
  84. foreach ($temp as $k => $v) {
  85. $data->$k = $v;
  86. }
  87. // Get the groups the user should be added to after registration.
  88. $data->groups = isset($data->groups) ? array_unique($data->groups) : array();
  89. // Get the default new user group, Registered if not specified.
  90. $system = $params->get('new_usertype', 2);
  91. $data->usertype = $system;
  92. // TODO: Not sure we need all this stuff anymore. Just need to add the group to the list and we are golden.
  93. // Handle the system default group.
  94. if (!in_array($system, $data->groups)) {
  95. // Add the system group to the first position.
  96. array_unshift($data->groups, $system);
  97. } else {
  98. // Make sure the system group is the first item.
  99. unset($data->groups[array_search($system, $data->groups)]);
  100. array_unshift($data->groups, $system);
  101. }
  102. // Unset the passwords.
  103. unset($data->password1);
  104. unset($data->password2);
  105. // Get the dispatcher and load the users plugins.
  106. $dispatcher = &JDispatcher::getInstance();
  107. JPluginHelper::importPlugin('users');
  108. // Trigger the data preparation event.
  109. $results = $dispatcher->trigger('onPrepareUserRegistrationData', array(&$data));
  110. // Check for errors encountered while preparing the data.
  111. if (count($results) && in_array(false, $results, true)) {
  112. $this->setError($dispatcher->getError());
  113. return $false;
  114. }
  115. return $data;
  116. }
  117. /**
  118. * Method to save the form data.
  119. *
  120. * @access public
  121. * @param array $data The form data.
  122. * @return mixed The user id on success, false on failure.
  123. * @since 1.0
  124. */
  125. function register($temp)
  126. {
  127. $config = &JFactory::getConfig();
  128. $params = &JComponentHelper::getParams('com_users');
  129. // Initialise the table with JUser.
  130. JUser::getTable('User', 'JTable');
  131. $user = new JUser();
  132. $data = (array)$this->getData();
  133. // Merge in the registration data.
  134. foreach ($data as $k => $v) {
  135. $temp[$k] = $v;
  136. }
  137. $data = $temp;
  138. // Prepare the data for the user object.
  139. $data['email'] = $data['email1'];
  140. $data['password'] = $data['password1'];
  141. // Check if the user needs to activate their account.
  142. if ($params->get('useractivation')) {
  143. jimport('joomla.user.helper');
  144. $data['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
  145. $data['block'] = 1;
  146. }
  147. // Bind the data.
  148. if (!$user->bind($data)) {
  149. $this->setError(JText::sprintf('USERS REGISTRATION BIND FAILED', $user->getError()));
  150. return false;
  151. }
  152. // Load the users plugin group.
  153. JPluginHelper::importPlugin('users');
  154. // Store the data.
  155. if (!$user->save()) {
  156. $this->setError($user->getError());
  157. return false;
  158. }
  159. // Compile the notification mail values.
  160. $data = $user->getProperties();
  161. $data['fromname'] = $config->getValue('fromname');
  162. $data['mailfrom'] = $config->getValue('mailfrom');
  163. $data['sitename'] = $config->getValue('sitename');
  164. // Handle account activation/confirmation e-mails.
  165. if ($params->get('useractivation'))
  166. {
  167. // Set the link to activate the user account.
  168. $uri = &JURI::getInstance();
  169. $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
  170. $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
  171. // Get the registration activation e-mail.
  172. $message = 'com_users.registration.activate';
  173. }
  174. else
  175. {
  176. // Get the registration confirmation e-mail.
  177. $message = 'com_users.registration.confirm';
  178. }
  179. // Load the message template and bind the data.
  180. jimport('joomla.utilities.simpletemplate');
  181. $template = JxSimpleTemplate::getInstance($message);
  182. $template->bind($data);
  183. // Send the registration e-mail.
  184. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $data['email'], $template->getTitle(), $template->getBody());
  185. // Check for an error.
  186. if ($return !== true) {
  187. $this->setError(JText::_('USERS REGISTRATION SEND MAIL FAILED'));
  188. return false;
  189. }
  190. return $user->id;
  191. }
  192. /**
  193. * Method to activate a user account.
  194. *
  195. * @access public
  196. * @param string $token The activation token.
  197. * @return boolean True on success, false on failure.
  198. * @since 1.0
  199. */
  200. function activate($token)
  201. {
  202. $config = &JFactory::getConfig();
  203. // Get the user id based on the token.
  204. $this->_db->setQuery(
  205. 'SELECT `id` FROM `#__users`' .
  206. ' WHERE `activation` = '.$this->_db->Quote($token) .
  207. ' AND `block` = 1' .
  208. ' AND `lastvisitDate` = '.$this->_db->Quote($this->_db->getNullDate())
  209. );
  210. $userId = (int)$this->_db->loadResult();
  211. // Check for a valid user id.
  212. if (!$userId) {
  213. $this->setError(JText::_('USERS_ACTIVATION_TOKEN_NOT_FOUND'));
  214. return false;
  215. }
  216. // Load the users plugin group.
  217. JPluginHelper::importPlugin('users');
  218. // Activate the user.
  219. $user = &JFactory::getUser($userId);
  220. $user->set('activation', '');
  221. $user->set('block', '0');
  222. // Store the user object.
  223. if (!$user->save()) {
  224. $this->setError($user->getError());
  225. return false;
  226. }
  227. // Compile the notification mail values.
  228. $data = $user->getProperties();
  229. $data['fromname'] = $config->getValue('fromname');
  230. $data['mailfrom'] = $config->getValue('mailfrom');
  231. $data['sitename'] = $config->getValue('sitename');
  232. // Load the message template and bind the data.
  233. jimport('joomla.utilities.simpletemplate');
  234. $template = JxSimpleTemplate::getInstance('com_users.registration.confirm');
  235. $template->bind($data);
  236. // Send the registration e-mail.
  237. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $data['email'], $template->getTitle(), $template->getBody());
  238. // Check for an error.
  239. if ($return !== true) {
  240. $this->setError(JText::_('USERS ACTIVATION SEND MAIL FAILED'));
  241. return false;
  242. }
  243. return true;
  244. }
  245. }