Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. // Requirments needed to build this function.
  2. defined('_JEXEC') or die('Restricted access');
  3. require_once ( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
  4. require_once ( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
  5. require_once ( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
  6. include_once JPATH_ROOT . '/components/com_community/libraries/core.php';
  7. include_once JPATH_ROOT . '/components/com_community/libraries/user.php';
  8.  
  9. /**
  10. * Creates a new Joomla! user. This method throws errors so you have to
  11. * wrap the call to this method in a try/catch. It will also set a default
  12. * $name, $username and $password if none is provided.
  13. *
  14. * @param type $email REQUIRED
  15. * @param string $name
  16. * @param type $username
  17. * @param type $password
  18. * @return boolean
  19. * @throws Exception
  20. */
  21. function createUser($email = null, $name = null, $username = null, $password = null) {
  22.  
  23. if (is_null($email)) {
  24. throw new Exception("Email is required to create new Joomla! user.");
  25. }
  26. $uniqid = uniqid();
  27. if (is_null($name)) {
  28. $name = 'member-' . $uniqid;
  29. }
  30. if (is_null($username)) {
  31. $username = $name;
  32. }
  33. if (is_null($password)) {
  34. $password = $uniqid;
  35. }
  36.  
  37. try {
  38. // Create new user.
  39. jimport('joomla.application.component.helper');
  40. $params = 'com_users';
  41. $usersParams = JComponentHelper::getParams($params);
  42. $new_user = JFactory::getUser(0);
  43. $config = JComponentHelper::getParams('com_users');
  44. $defaultUserGroup = $config->get('new_usertype', 2);
  45. $jdata = array(
  46. "name" => $name,
  47. "username" => $username,
  48. "password" => $password,
  49. "password2" => $password,
  50. "email" => $email,
  51. "sendEmail" => 0,
  52. "groups" => array($defaultUserGroup)
  53. );
  54.  
  55. // Automatically activate user. Comment this out if you use double
  56. // opt-in method.
  57. $useractivation = $usersParams->get('useractivation');
  58. if ($useractivation === 1) {
  59. jimport('joomla.user.helper');
  60. $jdata['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
  61. $jdata['block'] = 1; // block the user
  62. } else {
  63. $jdata['block'] = 0; // don't block the user
  64. }
  65.  
  66. // Write new user to the Joomla! database.
  67. if (!$new_user->bind($jdata)) {
  68. throw new Exception("Could not bind data. Error: " . $new_user->getError());
  69. return false;
  70. }
  71.  
  72. if (!$new_user->save()) {
  73. throw new Exception("Could not save user. Error: " . $new_user->getError());
  74. return false;
  75. }
  76.  
  77. // Finally, you can return the new user Id.
  78. $cuser = CFactory::getUser($new_user->id);
  79. return $cuser;
  80.  
  81. } catch (Exception $ex) {
  82. consoleLog("could not create user: " . $ex->getMessage());
  83. throw new Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
  84. return false;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement