Elgg  Version 4.3
register.php
Go to the documentation of this file.
1 <?php
8 
9 /* @var $request \Elgg\Request */
10 
11 elgg_make_sticky_form('register', ['password', 'password2']);
12 
13 // Get variables
14 $username = $request->getParam('username');
15 $password = $request->getParam('password', null, false);
16 $password2 = $request->getParam('password2', null, false);
17 $email = $request->getParam('email', '');
18 $name = $request->getParam('name', '');
19 
21 $name = trim(strip_tags($name));
22 $email = trim($email);
23 
24 try {
26  $failures = $validation->getFailures();
27  if ($failures) {
28  $messages = array_map(function (\Elgg\Validation\ValidationResult $e) {
29  return $e->getError();
30  }, $failures);
31 
32  throw new RegistrationException(implode(PHP_EOL, $messages));
33  }
34 
36  'username' => $username,
37  'password' => $password,
38  'name' => $name,
39  'email' => $email,
40  'validated' => false,
41  ]);
42 
43  $fail = function () use ($new_user) {
44  elgg_call(ELGG_IGNORE_ACCESS, function () use ($new_user) {
45  $new_user->delete();
46  });
47  };
48 
49  try {
50  // allow plugins to respond to self registration
51  // note: To catch all new users, even those created by an admin,
52  // register for the create, user event instead.
53  // only passing vars that aren't in ElggUser.
54  $params = $request->getParams();
55  $params['user'] = $new_user;
56 
57  if (!elgg_trigger_plugin_hook('register', 'user', $params, true)) {
58  throw new RegistrationException(elgg_echo('registerbad'));
59  }
60 
61  if ($new_user->isValidated() === null) {
62  // no hook decided to set validation status, so it will become validated
63  $new_user->setValidationStatus(true, 'register_action');
64  }
65  } catch (\Exception $e) {
66  // Catch all exception to make sure there are no incomplete user entities left behind
67  $fail();
68  throw $e;
69  }
70 
71  elgg_clear_sticky_form('register');
72 
74  'user' => $new_user,
75  ];
76 
77  if (!$new_user->isEnabled()) {
78  // Plugins can alter forwarding URL by registering for 'response', 'action:register' hook
80  }
81 
82  try {
84  // set forward url
86  $response_message = elgg_echo('registerok', [elgg_get_site_entity()->getDisplayName()]);
87 
89  } catch (LoginException $e) {
90  // if exception thrown, this probably means there is a validation
91  // plugin that has disabled the user
92  return elgg_error_response($e->getMessage(), REFERRER, $e->getCode() ? : ELGG_HTTP_UNAUTHORIZED);
93  }
94 } catch (RegistrationException $r) {
95  return elgg_error_response($r->getMessage(), REFERRER, $r->getCode() ? : ELGG_HTTP_BAD_REQUEST);
96 }
$messages
Definition: admin.php:12
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:592
elgg_validate_registration_data($username, $password, $name, $email, $allow_multiple_emails=false)
Assert that given registration details are valid and can be used to register the user.
Definition: users.php:128
$password2
Definition: register.php:16
$forward_url
Definition: register.php:85
Elgg registration action.
Generic parent class for login exceptions.
$request
Definition: livesearch.php:11
elgg_register_user(array $params=[])
Registers a user.
Definition: users.php:111
$name
Definition: register.php:18
const ELGG_HTTP_UNAUTHORIZED
Definition: constants.php:80
$email
Definition: register.php:17
Could not register a new user for whatever reason.
if($failures) $new_user
Definition: register.php:35
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$fail
Definition: register.php:43
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:146
$password
Definition: register.php:15
elgg_make_sticky_form($form_name, array $ignored_field_names=[])
Save form submission data (all GET and POST vars) into a session cache.
Definition: input.php:107
const REFERRER
Definition: constants.php:42
elgg_get_login_forward_url(\ElggUser $user)
Determine which URL the user should be forwarded to upon successful login.
Definition: sessions.php:141
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:380
$failures
Definition: register.php:26
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
$params['user']
Definition: register.php:55
$username
Definition: register.php:14
const ELGG_HTTP_BAD_REQUEST
Definition: constants.php:79
elgg_login(\ElggUser $user, bool $persistent=false)
Log in a user.
Definition: sessions.php:81
elgg_error_response($message= '', $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
elgg_ok_response($content= '', $message= '', $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
$response_data
Definition: register.php:73
Login as the specified user.
elgg_clear_sticky_form($form_name)
Remove form submission data from the session.
Definition: input.php:123
$response_message
Definition: register.php:86