aboutsummaryrefslogtreecommitdiff
path: root/lib/helpers.php
blob: 765fc0a61aa2d99bd4e36040954c2596199313f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
 * Helper functions for the OpenID client plugin
 */

/**
 * Serves a page to the new user to determine account values
 *
 * This should only be called after validating the OpenID response.
 *
 * @param array $data Key value pairs extracted from the response
 * @return bool
 */
function openid_client_registration_page_handler(array $data) {

	if (!is_array($data)) {
		return false;
	}

	$title = 'register';

	$vars = openid_client_prepare_registration_vars($data);
	$content = elgg_view('openid_client/register', $vars);

	$body = elgg_view_layout('one_column', array('content' => $content));
	echo elgg_view_page($title, $body);

	return true;
}

/**
 * Create the form vars for registration
 *
 * @param array $data
 * @return array
 */
function openid_client_prepare_registration_vars(array $data) {
	$vars = array();

	$vars['openid_identifier'] = $data['openid_identifier'];

	// username
	if (isset($data['username'])) {
		$vars['username'] = $data['username'];
	} else if (isset($data['email'])) {
		$vars['username'] = array_shift(explode('@', $data['email']));
	} else {
		$vars['username'] = null;
	}

	// is the username available
	$vars['is_username_available'] = true;

	// is the username valid
	try {
		$vars['is_username_valid'] = validate_username($vars['username']);
	} catch (RegistrationException $e) {
		$vars['is_username_valid'] = false;
	}

	// the rest
	$vars['email'] = elgg_extract('email', $data);
	$vars['name'] = elgg_extract('name', $data);

	return $vars;
}