PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/_archive/joomla/components/com_user/controller.php

https://github.com/kuzin/website-2008
PHP | 537 lines | 328 code | 96 blank | 113 comment | 55 complexity | 3f592ca61a372953309c6ab87b6f7d7d MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: controller.php 11215 2008-10-26 02:25:51Z ian $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is included in Joomla!
  15. defined('_JEXEC') or die( 'Restricted access' );
  16. jimport('joomla.application.component.controller');
  17. /**
  18. * User Component Controller
  19. *
  20. * @package Joomla
  21. * @subpackage Weblinks
  22. * @since 1.5
  23. */
  24. class UserController extends JController
  25. {
  26. /**
  27. * Method to display a view
  28. *
  29. * @access public
  30. * @since 1.5
  31. */
  32. function display()
  33. {
  34. parent::display();
  35. }
  36. function edit()
  37. {
  38. global $mainframe, $option;
  39. $db =& JFactory::getDBO();
  40. $user =& JFactory::getUser();
  41. if ( $user->get('guest')) {
  42. JError::raiseError( 403, JText::_('Access Forbidden') );
  43. return;
  44. }
  45. JRequest::setVar('layout', 'form');
  46. parent::display();
  47. }
  48. function save()
  49. {
  50. // Check for request forgeries
  51. JRequest::checkToken() or jexit( 'Invalid Token' );
  52. $user =& JFactory::getUser();
  53. $userid = JRequest::getVar( 'id', 0, 'post', 'int' );
  54. // preform security checks
  55. if ($user->get('id') == 0 || $userid == 0 || $userid <> $user->get('id')) {
  56. JError::raiseError( 403, JText::_('Access Forbidden') );
  57. return;
  58. }
  59. //clean request
  60. $post = JRequest::get( 'post' );
  61. $post['username'] = JRequest::getVar('username', '', 'post', 'username');
  62. $post['password'] = JRequest::getVar('password', '', 'post', 'string', JREQUEST_ALLOWRAW);
  63. $post['password2'] = JRequest::getVar('password2', '', 'post', 'string', JREQUEST_ALLOWRAW);
  64. // get the redirect
  65. $return = JURI::base();
  66. // do a password safety check
  67. if(strlen($post['password']) || strlen($post['password2'])) { // so that "0" can be used as password e.g.
  68. if($post['password'] != $post['password2']) {
  69. $msg = JText::_('PASSWORDS_DO_NOT_MATCH');
  70. // something is wrong. we are redirecting back to edit form.
  71. // TODO: HTTP_REFERER should be replaced with a base64 encoded form field in a later release
  72. $return = @$_SERVER['HTTP_REFERER'];
  73. if (empty($return) || !JURI::isInternal($return)) {
  74. $return = JURI::base();
  75. }
  76. $this->setRedirect($return, $msg, 'error');
  77. return false;
  78. }
  79. }
  80. // we don't want users to edit certain fields so we will unset them
  81. unset($post['gid']);
  82. unset($post['block']);
  83. unset($post['usertype']);
  84. unset($post['registerDate']);
  85. unset($post['activation']);
  86. // store data
  87. $model = $this->getModel('user');
  88. if ($model->store($post)) {
  89. $msg = JText::_( 'Your settings have been saved.' );
  90. } else {
  91. //$msg = JText::_( 'Error saving your settings.' );
  92. $msg = $model->getError();
  93. }
  94. $this->setRedirect( $return, $msg );
  95. }
  96. function cancel()
  97. {
  98. $this->setRedirect( 'index.php' );
  99. }
  100. function login()
  101. {
  102. // Check for request forgeries
  103. JRequest::checkToken('request') or jexit( 'Invalid Token' );
  104. global $mainframe;
  105. if ($return = JRequest::getVar('return', '', 'method', 'base64')) {
  106. $return = base64_decode($return);
  107. if (!JURI::isInternal($return)) {
  108. $return = '';
  109. }
  110. }
  111. $options = array();
  112. $options['remember'] = JRequest::getBool('remember', false);
  113. $options['return'] = $return;
  114. $credentials = array();
  115. $credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
  116. $credentials['password'] = JRequest::getString('passwd', '', 'post', JREQUEST_ALLOWRAW);
  117. //preform the login action
  118. $error = $mainframe->login($credentials, $options);
  119. if(!JError::isError($error))
  120. {
  121. // Redirect if the return url is not registration or login
  122. if ( ! $return ) {
  123. $return = 'index.php?option=com_user';
  124. }
  125. $mainframe->redirect( $return );
  126. }
  127. else
  128. {
  129. // Facilitate third party login forms
  130. if ( ! $return ) {
  131. $return = 'index.php?option=com_user&view=login';
  132. }
  133. // Redirect to a login form
  134. $mainframe->redirect( $return );
  135. }
  136. }
  137. function logout()
  138. {
  139. global $mainframe;
  140. //preform the logout action
  141. $error = $mainframe->logout();
  142. if(!JError::isError($error))
  143. {
  144. if ($return = JRequest::getVar('return', '', 'method', 'base64')) {
  145. $return = base64_decode($return);
  146. if (!JURI::isInternal($return)) {
  147. $return = '';
  148. }
  149. }
  150. // Redirect if the return url is not registration or login
  151. if ( $return && !( strpos( $return, 'com_user' )) ) {
  152. $mainframe->redirect( $return );
  153. }
  154. } else {
  155. parent::display();
  156. }
  157. }
  158. /**
  159. * Prepares the registration form
  160. * @return void
  161. */
  162. function register()
  163. {
  164. $usersConfig = &JComponentHelper::getParams( 'com_users' );
  165. if (!$usersConfig->get( 'allowUserRegistration' )) {
  166. JError::raiseError( 403, JText::_( 'Access Forbidden' ));
  167. return;
  168. }
  169. $user =& JFactory::getUser();
  170. if ( $user->get('guest')) {
  171. JRequest::setVar('view', 'register');
  172. } else {
  173. $this->setredirect('index.php?option=com_user&task=edit',JText::_('You are already registered.'));
  174. }
  175. parent::display();
  176. }
  177. /**
  178. * Save user registration and notify users and admins if required
  179. * @return void
  180. */
  181. function register_save()
  182. {
  183. global $mainframe;
  184. // Check for request forgeries
  185. JRequest::checkToken() or jexit( 'Invalid Token' );
  186. // Get required system objects
  187. $user = clone(JFactory::getUser());
  188. $pathway =& $mainframe->getPathway();
  189. $config =& JFactory::getConfig();
  190. $authorize =& JFactory::getACL();
  191. $document =& JFactory::getDocument();
  192. // If user registration is not allowed, show 403 not authorized.
  193. $usersConfig = &JComponentHelper::getParams( 'com_users' );
  194. if ($usersConfig->get('allowUserRegistration') == '0') {
  195. JError::raiseError( 403, JText::_( 'Access Forbidden' ));
  196. return;
  197. }
  198. // Initialize new usertype setting
  199. $newUsertype = $usersConfig->get( 'new_usertype' );
  200. if (!$newUsertype) {
  201. $newUsertype = 'Registered';
  202. }
  203. // Bind the post array to the user object
  204. if (!$user->bind( JRequest::get('post'), 'usertype' )) {
  205. JError::raiseError( 500, $user->getError());
  206. }
  207. // Set some initial user values
  208. $user->set('id', 0);
  209. $user->set('usertype', '');
  210. $user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
  211. $date =& JFactory::getDate();
  212. $user->set('registerDate', $date->toMySQL());
  213. // If user activation is turned on, we need to set the activation information
  214. $useractivation = $usersConfig->get( 'useractivation' );
  215. if ($useractivation == '1')
  216. {
  217. jimport('joomla.user.helper');
  218. $user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
  219. $user->set('block', '1');
  220. }
  221. // If there was an error with registration, set the message and display form
  222. if ( !$user->save() )
  223. {
  224. JError::raiseWarning('', JText::_( $user->getError()));
  225. $this->register();
  226. return false;
  227. }
  228. // Send registration confirmation mail
  229. $password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
  230. $password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
  231. UserController::_sendMail($user, $password);
  232. // Everything went fine, set relevant message depending upon user activation state and display message
  233. if ( $useractivation == 1 ) {
  234. $message = JText::_( 'REG_COMPLETE_ACTIVATE' );
  235. } else {
  236. $message = JText::_( 'REG_COMPLETE' );
  237. }
  238. $this->setRedirect('index.php', $message);
  239. }
  240. function activate()
  241. {
  242. global $mainframe;
  243. // Initialize some variables
  244. $db =& JFactory::getDBO();
  245. $user =& JFactory::getUser();
  246. $document =& JFactory::getDocument();
  247. $pathway =& $mainframe->getPathWay();
  248. $usersConfig = &JComponentHelper::getParams( 'com_users' );
  249. $userActivation = $usersConfig->get('useractivation');
  250. $allowUserRegistration = $usersConfig->get('allowUserRegistration');
  251. // Check to see if they're logged in, because they don't need activating!
  252. if ($user->get('id')) {
  253. // They're already logged in, so redirect them to the home page
  254. $mainframe->redirect( 'index.php' );
  255. }
  256. if ($allowUserRegistration == '0' || $userActivation == '0') {
  257. JError::raiseError( 403, JText::_( 'Access Forbidden' ));
  258. return;
  259. }
  260. // create the view
  261. require_once (JPATH_COMPONENT.DS.'views'.DS.'register'.DS.'view.html.php');
  262. $view = new UserViewRegister();
  263. $message = new stdClass();
  264. // Do we even have an activation string?
  265. $activation = JRequest::getVar('activation', '', '', 'alnum' );
  266. $activation = $db->getEscaped( $activation );
  267. if (empty( $activation ))
  268. {
  269. // Page Title
  270. $document->setTitle( JText::_( 'REG_ACTIVATE_NOT_FOUND_TITLE' ) );
  271. // Breadcrumb
  272. $pathway->addItem( JText::_( 'REG_ACTIVATE_NOT_FOUND_TITLE' ));
  273. $message->title = JText::_( 'REG_ACTIVATE_NOT_FOUND_TITLE' );
  274. $message->text = JText::_( 'REG_ACTIVATE_NOT_FOUND' );
  275. $view->assign('message', $message);
  276. $view->display('message');
  277. return;
  278. }
  279. // Lets activate this user
  280. jimport('joomla.user.helper');
  281. if (JUserHelper::activateUser($activation))
  282. {
  283. // Page Title
  284. $document->setTitle( JText::_( 'REG_ACTIVATE_COMPLETE_TITLE' ) );
  285. // Breadcrumb
  286. $pathway->addItem( JText::_( 'REG_ACTIVATE_COMPLETE_TITLE' ));
  287. $message->title = JText::_( 'REG_ACTIVATE_COMPLETE_TITLE' );
  288. $message->text = JText::_( 'REG_ACTIVATE_COMPLETE' );
  289. }
  290. else
  291. {
  292. // Page Title
  293. $document->setTitle( JText::_( 'REG_ACTIVATE_NOT_FOUND_TITLE' ) );
  294. // Breadcrumb
  295. $pathway->addItem( JText::_( 'REG_ACTIVATE_NOT_FOUND_TITLE' ));
  296. $message->title = JText::_( 'REG_ACTIVATE_NOT_FOUND_TITLE' );
  297. $message->text = JText::_( 'REG_ACTIVATE_NOT_FOUND' );
  298. }
  299. $view->assign('message', $message);
  300. $view->display('message');
  301. }
  302. /**
  303. * Password Reset Request Method
  304. *
  305. * @access public
  306. */
  307. function requestreset()
  308. {
  309. // Check for request forgeries
  310. JRequest::checkToken() or jexit( 'Invalid Token' );
  311. // Get the input
  312. $email = JRequest::getVar('email', null, 'post', 'string');
  313. // Get the model
  314. $model = &$this->getModel('Reset');
  315. // Request a reset
  316. if ($model->requestReset($email) === false)
  317. {
  318. $message = JText::sprintf('PASSWORD_RESET_REQUEST_FAILED', $model->getError());
  319. $this->setRedirect('index.php?option=com_user&view=reset', $message);
  320. return false;
  321. }
  322. $this->setRedirect('index.php?option=com_user&view=reset&layout=confirm');
  323. }
  324. /**
  325. * Password Reset Confirmation Method
  326. *
  327. * @access public
  328. */
  329. function confirmreset()
  330. {
  331. // Check for request forgeries
  332. JRequest::checkToken() or jexit( 'Invalid Token' );
  333. // Get the input
  334. $token = JRequest::getVar('token', null, 'post', 'alnum');
  335. // Get the model
  336. $model = &$this->getModel('Reset');
  337. // Verify the token
  338. if ($model->confirmReset($token) === false)
  339. {
  340. $message = JText::sprintf('PASSWORD_RESET_CONFIRMATION_FAILED', $model->getError());
  341. $this->setRedirect('index.php?option=com_user&view=reset&layout=confirm', $message);
  342. return false;
  343. }
  344. $this->setRedirect('index.php?option=com_user&view=reset&layout=complete');
  345. }
  346. /**
  347. * Password Reset Completion Method
  348. *
  349. * @access public
  350. */
  351. function completereset()
  352. {
  353. // Check for request forgeries
  354. JRequest::checkToken() or jexit( 'Invalid Token' );
  355. // Get the input
  356. $password1 = JRequest::getVar('password1', null, 'post', 'string', JREQUEST_ALLOWRAW);
  357. $password2 = JRequest::getVar('password2', null, 'post', 'string', JREQUEST_ALLOWRAW);
  358. // Get the model
  359. $model = &$this->getModel('Reset');
  360. // Reset the password
  361. if ($model->completeReset($password1, $password2) === false)
  362. {
  363. $message = JText::sprintf('PASSWORD_RESET_FAILED', $model->getError());
  364. $this->setRedirect('index.php?option=com_user&view=reset&layout=complete', $message);
  365. return false;
  366. }
  367. $message = JText::_('PASSWORD_RESET_SUCCESS');
  368. $this->setRedirect('index.php?option=com_user&view=login', $message);
  369. }
  370. /**
  371. * Username Reminder Method
  372. *
  373. * @access public
  374. */
  375. function remindusername()
  376. {
  377. // Check for request forgeries
  378. JRequest::checkToken() or jexit( 'Invalid Token' );
  379. // Get the input
  380. $email = JRequest::getVar('email', null, 'post', 'string');
  381. // Get the model
  382. $model = &$this->getModel('Remind');
  383. // Send the reminder
  384. if ($model->remindUsername($email) === false)
  385. {
  386. $message = JText::sprintf('USERNAME_REMINDER_FAILED', $model->getError());
  387. $this->setRedirect('index.php?option=com_user&view=remind', $message);
  388. return false;
  389. }
  390. $message = JText::sprintf('USERNAME_REMINDER_SUCCESS', $email);
  391. $this->setRedirect('index.php?option=com_user&view=login', $message);
  392. }
  393. function _sendMail(&$user, $password)
  394. {
  395. global $mainframe;
  396. $db =& JFactory::getDBO();
  397. $name = $user->get('name');
  398. $email = $user->get('email');
  399. $username = $user->get('username');
  400. $usersConfig = &JComponentHelper::getParams( 'com_users' );
  401. $sitename = $mainframe->getCfg( 'sitename' );
  402. $useractivation = $usersConfig->get( 'useractivation' );
  403. $mailfrom = $mainframe->getCfg( 'mailfrom' );
  404. $fromname = $mainframe->getCfg( 'fromname' );
  405. $siteURL = JURI::base();
  406. $subject = sprintf ( JText::_( 'Account details for' ), $name, $sitename);
  407. $subject = html_entity_decode($subject, ENT_QUOTES);
  408. if ( $useractivation == 1 ){
  409. $message = sprintf ( JText::_( 'SEND_MSG_ACTIVATE' ), $name, $sitename, $siteURL."index.php?option=com_user&task=activate&activation=".$user->get('activation'), $siteURL, $username, $password);
  410. } else {
  411. $message = sprintf ( JText::_( 'SEND_MSG' ), $name, $sitename, $siteURL);
  412. }
  413. $message = html_entity_decode($message, ENT_QUOTES);
  414. //get all super administrator
  415. $query = 'SELECT name, email, sendEmail' .
  416. ' FROM #__users' .
  417. ' WHERE LOWER( usertype ) = "super administrator"';
  418. $db->setQuery( $query );
  419. $rows = $db->loadObjectList();
  420. // Send email to user
  421. if ( ! $mailfrom || ! $fromname ) {
  422. $fromname = $rows[0]->name;
  423. $mailfrom = $rows[0]->email;
  424. }
  425. JUtility::sendMail($mailfrom, $fromname, $email, $subject, $message);
  426. // Send notification to all administrators
  427. $subject2 = sprintf ( JText::_( 'Account details for' ), $name, $sitename);
  428. $subject2 = html_entity_decode($subject2, ENT_QUOTES);
  429. // get superadministrators id
  430. foreach ( $rows as $row )
  431. {
  432. if ($row->sendEmail)
  433. {
  434. $message2 = sprintf ( JText::_( 'SEND_MSG_ADMIN' ), $row->name, $sitename, $name, $email, $username);
  435. $message2 = html_entity_decode($message2, ENT_QUOTES);
  436. JUtility::sendMail($mailfrom, $fromname, $row->email, $subject2, $message2);
  437. }
  438. }
  439. }
  440. }
  441. ?>