PageRenderTime 27ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_users/models/registration.php

https://github.com/sengann/iks-school.com
PHP | 443 lines | 271 code | 61 blank | 111 comment | 34 complexity | ab7b265648e28e899e01c7fb4767e46b MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: registration.php 18935 2010-09-17 14:27:40Z infograf768 $
  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. * @since 1.6
  19. */
  20. class UsersModelRegistration extends JModelForm
  21. {
  22. /**
  23. * @var object The user registration data.
  24. * @since 1.6
  25. */
  26. protected $data;
  27. /**
  28. * Method to activate a user account.
  29. *
  30. * @param string The activation token.
  31. * @return mixed False on failure, user object on success.
  32. * @since 1.6
  33. */
  34. public function activate($token)
  35. {
  36. $config = JFactory::getConfig();
  37. $userParams = JComponentHelper::getParams('com_users');
  38. $db = $this->getDbo();
  39. // Get the user id based on the token.
  40. $db->setQuery(
  41. 'SELECT `id` FROM `#__users`' .
  42. ' WHERE `activation` = '.$db->Quote($token) .
  43. ' AND `block` = 1' .
  44. ' AND `lastvisitDate` = '.$db->Quote($db->getNullDate())
  45. );
  46. $userId = (int) $db->loadResult();
  47. // Check for a valid user id.
  48. if (!$userId) {
  49. $this->setError(JText::_('COM_USERS_ACTIVATION_TOKEN_NOT_FOUND'));
  50. return false;
  51. }
  52. // Load the users plugin group.
  53. JPluginHelper::importPlugin('users');
  54. // Activate the user.
  55. $user = JFactory::getUser($userId);
  56. // Admin activation is on and user is verifying their email
  57. if (($userParams->get('useractivation') == 2) && !$user->getParam('activate', 0))
  58. {
  59. $uri = JURI::getInstance();
  60. jimport('joomla.user.helper');
  61. // Compile the admin notification mail values.
  62. $data = $user->getProperties();
  63. $data['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
  64. $user->set('activation', $data['activation']);
  65. $data['siteurl'] = JUri::base();
  66. $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
  67. $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
  68. $data['fromname'] = $config->get('fromname');
  69. $data['mailfrom'] = $config->get('mailfrom');
  70. $data['sitename'] = $config->get('sitename');
  71. $user->setParam('activate', 1);
  72. $emailSubject = JText::sprintf(
  73. 'COM_USERS_EMAIL_ACTIVATE_WITH_ADMIN_ACTIVATION_SUBJECT',
  74. $data['name'],
  75. $data['sitename']
  76. );
  77. $emailBody = JText::sprintf(
  78. 'COM_USERS_EMAIL_ACTIVATE_WITH_ADMIN_ACTIVATION_BODY',
  79. $data['sitename'],
  80. $data['name'],
  81. $data['email'],
  82. $data['username'],
  83. $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation']
  84. );
  85. // get all admin users
  86. $query = 'SELECT name, email, sendEmail' .
  87. ' FROM #__users' .
  88. ' WHERE sendEmail=1';
  89. $db->setQuery( $query );
  90. $rows = $db->loadObjectList();
  91. // Send mail to all superadministrators id
  92. foreach( $rows as $row )
  93. {
  94. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $row->email, $emailSubject, $emailBody);
  95. // Check for an error.
  96. if ($return !== true) {
  97. $this->setError(JText::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED'));
  98. return false;
  99. }
  100. }
  101. }
  102. //Admin activation is on and admin is activating the account
  103. else if (($userParams->get('useractivation') == 2) && $user->getParam('activate', 0))
  104. {
  105. $user->set('activation', '');
  106. $user->set('block', '0');
  107. $uri = JURI::getInstance();
  108. jimport('joomla.user.helper');
  109. // Compile the user activated notification mail values.
  110. $data = $user->getProperties();
  111. $user->setParam('activate', 0);
  112. $data['fromname'] = $config->get('fromname');
  113. $data['mailfrom'] = $config->get('mailfrom');
  114. $data['sitename'] = $config->get('sitename');
  115. $data['siteurl'] = JUri::base();
  116. $emailSubject = JText::sprintf(
  117. 'COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_SUBJECT',
  118. $data['name'],
  119. $data['sitename']
  120. );
  121. $emailBody = JText::sprintf(
  122. 'COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_BODY',
  123. $data['name'],
  124. $data['siteurl'],
  125. $data['username']
  126. );
  127. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $data['email'], $emailSubject, $emailBody);
  128. // Check for an error.
  129. if ($return !== true) {
  130. $this->setError(JText::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED'));
  131. return false;
  132. }
  133. }
  134. else
  135. {
  136. $user->set('activation', '');
  137. $user->set('block', '0');
  138. }
  139. // Store the user object.
  140. if (!$user->save()) {
  141. $this->setError(JText::sprintf('COM_USERS_REGISTRATION_ACTIVATION_SAVE_FAILED', $user->getError()));
  142. return false;
  143. }
  144. return $user;
  145. }
  146. /**
  147. * Method to get the registration form data.
  148. *
  149. * The base form data is loaded and then an event is fired
  150. * for users plugins to extend the data.
  151. *
  152. * @return mixed Data object on success, false on failure.
  153. * @since 1.6
  154. */
  155. public function getData()
  156. {
  157. if ($this->data === null) {
  158. $this->data = new stdClass();
  159. $app = JFactory::getApplication();
  160. $params = JComponentHelper::getParams('com_users');
  161. // Override the base user data with any data in the session.
  162. $temp = (array)$app->getUserState('com_users.registration.data', array());
  163. foreach ($temp as $k => $v) {
  164. $this->data->$k = $v;
  165. }
  166. // Get the groups the user should be added to after registration.
  167. $this->data->groups = isset($this->data->groups) ? array_unique($this->data->groups) : array();
  168. // Get the default new user group, Registered if not specified.
  169. $system = $params->get('new_usertype', 2);
  170. $this->data->groups[$system] = null;
  171. // Unset the passwords.
  172. unset($this->data->password1);
  173. unset($this->data->password2);
  174. // Get the dispatcher and load the users plugins.
  175. $dispatcher = JDispatcher::getInstance();
  176. JPluginHelper::importPlugin('users');
  177. // Trigger the data preparation event.
  178. $results = $dispatcher->trigger('onContentPrepareData', array('com_users.registration', $this->data));
  179. // Check for errors encountered while preparing the data.
  180. if (count($results) && in_array(false, $results, true)) {
  181. $this->setError($dispatcher->getError());
  182. $this->data = false;
  183. }
  184. }
  185. return $this->data;
  186. }
  187. /**
  188. * Method to get the registration form.
  189. *
  190. * The base form is loaded from XML and then an event is fired
  191. * for users plugins to extend the form with extra fields.
  192. *
  193. * @param array $data An optional array of data for the form to interogate.
  194. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  195. * @return JForm A JForm object on success, false on failure
  196. * @since 1.6
  197. */
  198. public function getForm($data = array(), $loadData = true)
  199. {
  200. // Get the form.
  201. $form = $this->loadForm('com_users.registration', 'registration', array('control' => 'jform', 'load_data' => $loadData));
  202. if (empty($form)) {
  203. return false;
  204. }
  205. return $form;
  206. }
  207. /**
  208. * Method to get the data that should be injected in the form.
  209. *
  210. * @return mixed The data for the form.
  211. * @since 1.6
  212. */
  213. protected function loadFormData()
  214. {
  215. return $this->getData();
  216. }
  217. /**
  218. * Override preprocessForm to load the user plugin group instead of content.
  219. *
  220. * @param object A form object.
  221. * @param mixed The data expected for the form.
  222. * @throws Exception if there is an error in the form event.
  223. * @since 1.6
  224. */
  225. protected function preprocessForm(JForm $form, $data)
  226. {
  227. parent::preprocessForm($form, $data, 'user');
  228. }
  229. /**
  230. * Method to auto-populate the model state.
  231. *
  232. * Note. Calling getState in this method will result in recursion.
  233. *
  234. * @since 1.6
  235. */
  236. protected function populateState()
  237. {
  238. // Get the application object.
  239. $app = JFactory::getApplication();
  240. $params = $app->getParams('com_users');
  241. // Load the parameters.
  242. $this->setState('params', $params);
  243. }
  244. /**
  245. * Method to save the form data.
  246. *
  247. * @param array The form data.
  248. * @return mixed The user id on success, false on failure.
  249. * @since 1.6
  250. */
  251. public function register($temp)
  252. {
  253. $config = JFactory::getConfig();
  254. $params = JComponentHelper::getParams('com_users');
  255. // Initialise the table with JUser.
  256. JUser::getTable('User', 'JTable');
  257. $user = new JUser();
  258. $data = (array)$this->getData();
  259. // Merge in the registration data.
  260. foreach ($data as $k => $v) {
  261. $temp[$k] = $v;
  262. }
  263. $data = $temp;
  264. // Prepare the data for the user object.
  265. $data['email'] = $data['email1'];
  266. $data['password'] = $data['password1'];
  267. $useractivation = $params->get('useractivation');
  268. // Check if the user needs to activate their account.
  269. if (($useractivation == 1) || ($useractivation == 2)) {
  270. jimport('joomla.user.helper');
  271. $data['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
  272. $data['block'] = 1;
  273. }
  274. // Bind the data.
  275. if (!$user->bind($data)) {
  276. $this->setError(JText::sprintf('COM_USERS_REGISTRATION_BIND_FAILED', $user->getError()));
  277. return false;
  278. }
  279. // Load the users plugin group.
  280. JPluginHelper::importPlugin('users');
  281. // Store the data.
  282. if (!$user->save()) {
  283. $this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
  284. return false;
  285. }
  286. // Compile the notification mail values.
  287. $data = $user->getProperties();
  288. $data['fromname'] = $config->get('fromname');
  289. $data['mailfrom'] = $config->get('mailfrom');
  290. $data['sitename'] = $config->get('sitename');
  291. $data['siteurl'] = JUri::base();
  292. // Handle account activation/confirmation emails.
  293. if ($useractivation == 2)
  294. {
  295. // Set the link to confirm the user email.
  296. $uri = JURI::getInstance();
  297. $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
  298. $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
  299. $emailSubject = JText::sprintf(
  300. 'COM_USERS_EMAIL_ACCOUNT_DETAILS',
  301. $data['name'],
  302. $data['sitename']
  303. );
  304. $emailBody = JText::sprintf(
  305. 'COM_USERS_EMAIL_REGISTERED_WITH_ADMIN_ACTIVATION_BODY',
  306. $data['name'],
  307. $data['sitename'],
  308. $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation'],
  309. $data['siteurl'],
  310. $data['username'],
  311. $data['password_clear']
  312. );
  313. }
  314. else if ($useractivation == 1)
  315. {
  316. // Set the link to activate the user account.
  317. $uri = JURI::getInstance();
  318. $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
  319. $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
  320. $emailSubject = JText::sprintf(
  321. 'COM_USERS_EMAIL_ACCOUNT_DETAILS',
  322. $data['name'],
  323. $data['sitename']
  324. );
  325. $emailBody = JText::sprintf(
  326. 'COM_USERS_EMAIL_REGISTERED_WITH_ACTIVATION_BODY',
  327. $data['name'],
  328. $data['sitename'],
  329. $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation'],
  330. $data['siteurl'],
  331. $data['username'],
  332. $data['password_clear']
  333. );
  334. } else {
  335. $emailSubject = JText::sprintf(
  336. 'COM_USERS_EMAIL_ACCOUNT_DETAILS',
  337. $data['name'],
  338. $data['sitename']
  339. );
  340. $emailBody = JText::sprintf(
  341. 'COM_USERS_EMAIL_REGISTERED_BODY',
  342. $data['name'],
  343. $data['sitename'],
  344. $data['siteurl']
  345. );
  346. }
  347. // Send the registration email.
  348. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $data['email'], $emailSubject, $emailBody);
  349. // Check for an error.
  350. if ($return !== true) {
  351. $this->setError(JText::_('COM_USERS_REGISTRATION_SEND_MAIL_FAILED'));
  352. // Send a system message to administrators receiving system mails
  353. $db = JFactory::getDBO();
  354. $q = "SELECT id
  355. FROM #__users
  356. WHERE block = 0
  357. AND sendEmail = 1";
  358. $db->setQuery($q);
  359. $sendEmail = $db->loadResultArray();
  360. if (count($sendEmail) > 0) {
  361. $jdate = new JDate();
  362. // Build the query to add the messages
  363. $q = "INSERT INTO `#__messages` (`user_id_from`, `user_id_to`, `date_time`, `subject`, `message`)
  364. VALUES ";
  365. $messages = array();
  366. foreach ($sendEmail as $userid) {
  367. $messages[] = "(".$userid.", ".$userid.", '".$jdate->toMySQL()."', '".JText::_('COM_USERS_MAIL_SEND_FAILURE_SUBJECT')."', '".JText::sprintf('COM_USERS_MAIL_SEND_FAILURE_BODY', $return, $data['username'])."')";
  368. }
  369. $q .= implode(',', $messages);
  370. $db->setQuery($q);
  371. $db->query();
  372. }
  373. return false;
  374. }
  375. if ($useractivation == 1)
  376. return "useractivate";
  377. else if ($useractivation == 2)
  378. return "adminactivate";
  379. else
  380. return $user->id;
  381. }
  382. }