PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/user/joomla/joomla.php

https://github.com/3den/J-MediaGalleries
PHP | 271 lines | 141 code | 36 blank | 94 comment | 22 complexity | 0eebbfa5c5bd958bffc57c5f41184428 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: joomla.php 18522 2010-08-18 05:22:14Z 3dentech $
  4. * @package Joomla
  5. * @subpackage JFramework
  6. * @copyright Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.plugin.plugin');
  12. /**
  13. * Joomla User plugin
  14. *
  15. * @package Joomla
  16. * @subpackage JFramework
  17. * @since 1.5
  18. */
  19. class plgUserJoomla extends JPlugin
  20. {
  21. /**
  22. * Remove all sessions for the user name
  23. *
  24. * Method is called after user data is deleted from the database
  25. *
  26. * @param array $user Holds the user data
  27. * @param boolean $succes True if user was succesfully stored in the database
  28. * @param string $msg Message
  29. *
  30. * @return boolean
  31. * @since 1.6
  32. */
  33. public function onUserAfterDelete($user, $succes, $msg)
  34. {
  35. if (!$succes) {
  36. return false;
  37. }
  38. $db = JFactory::getDbo();
  39. $db->setQuery(
  40. 'DELETE FROM `#__session`' .
  41. ' WHERE `userid` = '.(int) $user['id']
  42. );
  43. $db->Query();
  44. return true;
  45. }
  46. /**
  47. * Utility method to act on a user after it has been saved.
  48. *
  49. * This method sends a registration email to new users created in the backend.
  50. *
  51. * @param array $user Holds the new user data.
  52. * @param boolean $isnew True if a new user is stored.
  53. * @param boolean $success True if user was succesfully stored in the database.
  54. * @param string $msg Message.
  55. *
  56. * @return void
  57. * @since 1.6
  58. */
  59. public function onUserAfterSave($user, $isnew, $success, $msg)
  60. {
  61. // Initialise variables.
  62. $app = JFactory::getApplication();
  63. $config = JFactory::getConfig();
  64. if ($isnew) {
  65. // TODO: Suck in the frontend registration emails here as well. Job for a rainy day.
  66. if ($app->isAdmin()) {
  67. // Load user_joomla plugin language (not done automatically).
  68. $lang = JFactory::getLanguage();
  69. $lang->load('plg_user_joomla', JPATH_ADMINISTRATOR);
  70. // Compute the mail subject.
  71. $emailSubject = JText::sprintf(
  72. 'PLG_USER_JOOMLA_NEW_USER_EMAIL_SUBJECT',
  73. $user['name'],
  74. $config->get('sitename')
  75. );
  76. // Compute the mail body.
  77. $emailBody = JText::sprintf(
  78. 'PLG_USER_JOOMLA_NEW_USER_EMAIL_BODY',
  79. $user['name'],
  80. $config->get('sitename'),
  81. JUri::root(),
  82. $user['username'],
  83. $user['password_clear']
  84. );
  85. // Assemble the email data...the sexy way!
  86. $mail = JFactory::getMailer()
  87. ->setSender(
  88. array(
  89. $config->get('mailfrom'),
  90. $config->get('fromname')
  91. )
  92. )
  93. ->addRecipient($user['email'])
  94. ->setSubject($emailSubject)
  95. ->setBody($emailBody);
  96. if (!$mail->Send()) {
  97. // TODO: Probably should raise a plugin error but this event is not error checked.
  98. JError::raiseWarning(500, JText::_('ERROR_SENDING_EMAIL'));
  99. }
  100. }
  101. }
  102. else {
  103. // Existing user - nothing to do...yet.
  104. }
  105. }
  106. /**
  107. * This method should handle any login logic and report back to the subject
  108. *
  109. * @param array $user Holds the user data
  110. * @param array $options Array holding options (remember, autoregister, group)
  111. *
  112. * @return boolean True on success
  113. * @since 1.5
  114. */
  115. public function onUserLogin($user, $options = array())
  116. {
  117. jimport('joomla.user.helper');
  118. $instance = $this->_getUser($user, $options);
  119. // If _getUser returned an error, then pass it back.
  120. if (JError::isError($instance)) {
  121. return $instance;
  122. }
  123. // If the user is blocked, redirect with an error
  124. if ($instance->get('block') == 1) {
  125. return JError::raiseWarning('SOME_ERROR_CODE', JText::_('JERROR_NOLOGIN_BLOCKED'));
  126. }
  127. // Authorise the user based on the group information
  128. if (!isset($options['group'])) {
  129. $options['group'] = 'USERS';
  130. }
  131. // Chek the user can login.
  132. $result = $instance->authorise($options['action']);
  133. if (!$result) {
  134. return JError::raiseWarning(401, JText::_('JERROR_LOGIN_DENIED'));
  135. }
  136. // Mark the user as logged in
  137. $instance->set('guest', 0);
  138. // Register the needed session variables
  139. $session = JFactory::getSession();
  140. $session->set('user', $instance);
  141. // Update the user related fields for the Joomla sessions table.
  142. $db = JFactory::getDBO();
  143. $db->setQuery(
  144. 'UPDATE `#__session`' .
  145. ' SET `guest` = '.$db->quote($instance->get('guest')).',' .
  146. ' `username` = '.$db->quote($instance->get('username')).',' .
  147. ' `userid` = '.(int) $instance->get('id') .
  148. ' WHERE `session_id` = '.$db->quote($session->getId())
  149. );
  150. $db->query();
  151. // Hit the user last visit field
  152. $instance->setLastVisit();
  153. return true;
  154. }
  155. /**
  156. * This method should handle any logout logic and report back to the subject
  157. *
  158. * @param array $user Holds the user data.
  159. * @param array $options Array holding options (client, ...).
  160. *
  161. * @return object True on success
  162. * @since 1.5
  163. */
  164. public function onUserLogout($user, $options = array())
  165. {
  166. $my = JFactory::getUser();
  167. $session = JFactory::getSession();
  168. $app = JFactory::getApplication();
  169. // Make sure we're a valid user first
  170. if ($user['id'] == 0 && !$my->get('tmp_user')) {
  171. return true;
  172. }
  173. // Check to see if we're deleting the current session
  174. if ($my->get('id') == $user['id'] && $options['clientid'] == $app->getClientId()) {
  175. // Hit the user last visit field
  176. $my->setLastVisit();
  177. // Destroy the php session for this user
  178. $session->destroy();
  179. }
  180. else {
  181. // Force logout all users with that userid
  182. $db = JFactory::getDBO();
  183. $db->setQuery(
  184. 'DELETE FROM `#__session`' .
  185. ' WHERE `userid` = '.(int) $user['id'] .
  186. ' AND `client_id` = '.(int) $options['clientid']
  187. );
  188. $db->query();
  189. }
  190. return true;
  191. }
  192. /**
  193. * This method will return a user object
  194. *
  195. * If options['autoregister'] is true, if the user doesn't exist yet he will be created
  196. *
  197. * @param array $user Holds the user data.
  198. * @param array $options Array holding options (remember, autoregister, group).
  199. *
  200. * @return object A JUser object
  201. * @since 1.5
  202. */
  203. protected function &_getUser($user, $options = array())
  204. {
  205. $instance = JUser::getInstance();
  206. if ($id = intval(JUserHelper::getUserId($user['username']))) {
  207. $instance->load($id);
  208. return $instance;
  209. }
  210. //TODO : move this out of the plugin
  211. jimport('joomla.application.component.helper');
  212. $config = JComponentHelper::getParams('com_users');
  213. // Default to Registered.
  214. $usertype = $config->get('new_usertype', 2);
  215. $acl = JFactory::getACL();
  216. $instance->set('id' , 0);
  217. $instance->set('name' , $user['fullname']);
  218. $instance->set('username' , $user['username']);
  219. $instance->set('password_clear' , $user['password_clear']);
  220. $instance->set('email' , $user['email']); // Result should contain an email (check)
  221. $instance->set('usertype' , $usertype);
  222. //If autoregister is set let's register the user
  223. $autoregister = isset($options['autoregister']) ? $options['autoregister'] : $this->params->get('autoregister', 1);
  224. if ($autoregister) {
  225. if (!$instance->save()) {
  226. return JError::raiseWarning('SOME_ERROR_CODE', $instance->getError());
  227. }
  228. }
  229. else {
  230. // No existing user and autoregister off, this is a temporary user.
  231. $instance->set('tmp_user', true);
  232. }
  233. return $instance;
  234. }
  235. }