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