Elgg  Version 3.0
register.php
Go to the documentation of this file.
1 <?php
6 /* @var $request \Elgg\Request */
7 
8 elgg_make_sticky_form('register');
9 
10 if (!elgg_get_config('allow_registration')) {
11  return elgg_error_response(elgg_echo('registerdisabled'));
12 }
13 
14 // Get variables
15 $username = $request->getParam('username');
16 $password = $request->getParam('password', null, false);
17 $password2 = $request->getParam('password2', null, false);
18 $email = $request->getParam('email');
19 $name = $request->getParam('name');
20 
21 $username = trim($username);
22 $name = trim(strip_tags($name));
23 $email = trim($email);
24 
25 try {
27  $failures = $validation->getFailures();
28  if ($failures) {
29  $messages = array_map(function (\Elgg\Validation\ValidationResult $e) {
30  return $e->getError();
31  }, $failures);
32 
33  throw new RegistrationException(implode(PHP_EOL, $messages));
34  }
35 
37  if ($guid === false) {
38  throw new RegistrationException(elgg_echo('registerbad'));
39  }
40 
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  } catch (\Exception $e) {
61  // Catch all exception to make sure there are no incomplete user entities left behind
62  $fail();
63  throw $e;
64  }
65 
66  elgg_clear_sticky_form('register');
67 
69  'user' => $new_user,
70  ];
71 
72  if (!$new_user->isEnabled()) {
73  // Plugins can alter forwarding URL by registering for 'response', 'action:register' hook
75  }
76 
77  try {
79  // set forward url
81  $response_message = elgg_echo('registerok', [elgg_get_site_entity()->getDisplayName()]);
82 
84  } catch (LoginException $e) {
85  // if exception thrown, this probably means there is a validation
86  // plugin that has disabled the user
87  return elgg_error_response($e->getMessage(), REFERRER, $e->getCode() ? : ELGG_HTTP_UNAUTHORIZED);
88  }
89 } catch (RegistrationException $r) {
90  return elgg_error_response($r->getMessage(), REFERRER, $r->getCode() ? : ELGG_HTTP_BAD_REQUEST);
91 }
$messages
Definition: admin.php:15
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:1176
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:222
$password2
Definition: register.php:17
_elgg_get_login_forward_url(\Elgg\Request $request,\ElggUser $user)
Determine which URL the user should be forwarded to upon successful login.
Definition: sessions.php:345
if($failures) $guid
Definition: register.php:36
get_user($guid)
Get a user object from a GUID.
Definition: users.php:40
$forward_url
Definition: register.php:80
$request
Page handler for autocomplete endpoint.
Definition: livesearch.php:9
$name
Definition: register.php:19
const ELGG_HTTP_UNAUTHORIZED
Definition: constants.php:80
$email
Definition: register.php:18
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$fail
Definition: register.php:43
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:156
$password
Definition: register.php:16
Configuration exception.
const REFERRER
Definition: constants.php:42
elgg_error_response($error= '', $forward_url=REFERRER, $status_code=ELGG_HTTP_OK)
Prepare an error response to be returned by a page or an action handler.
elgg_make_sticky_form($form_name)
Save form submission data (all GET and POST vars) into a session cache.
Definition: input.php:120
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
register_user($username, $password, $name, $email, $allow_multiple_emails=false, $subtype=null)
Registers a user, returning false if the username already exists.
Definition: users.php:205
$failures
Definition: register.php:27
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
$params['user']
Definition: register.php:55
elgg_ok_response($content= '', $message= '', $forward_url=null, $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
if(!elgg_get_config('allow_registration')) $username
Definition: register.php:15
const ELGG_HTTP_BAD_REQUEST
Definition: constants.php:79
login(\ElggUser $user, $persistent=false)
Logs in a specified .
Definition: sessions.php:273
$response_data
Definition: register.php:68
elgg_clear_sticky_form($form_name)
Remove form submission data from the session.
Definition: input.php:136
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
$response_message
Definition: register.php:81
if($guid===false) $new_user
Definition: register.php:41