3 require_once('include/config.php');
4 require_once('include/network.php');
5 require_once('include/plugin.php');
6 require_once('include/text.php');
7 require_once('include/pgettext.php');
8 require_once('include/datetime.php');
11 function create_user($arr) {
13 // Required: { username, nickname, email } or { openid_url }
16 $result = array('success' => false, 'user' => null, 'password' => '', 'message' => '');
18 $using_invites = get_config('system','invitation_only');
19 $num_invites = get_config('system','number_invites');
22 $invite_id = ((x($arr,'invite_id')) ? notags(trim($arr['invite_id'])) : '');
23 $username = ((x($arr,'username')) ? notags(trim($arr['username'])) : '');
24 $nickname = ((x($arr,'nickname')) ? notags(trim($arr['nickname'])) : '');
25 $email = ((x($arr,'email')) ? notags(trim($arr['email'])) : '');
26 $openid_url = ((x($arr,'openid_url')) ? notags(trim($arr['openid_url'])) : '');
27 $photo = ((x($arr,'photo')) ? notags(trim($arr['photo'])) : '');
28 $password = ((x($arr,'password')) ? trim($arr['password']) : '');
29 $blocked = ((x($arr,'blocked')) ? intval($arr['blocked']) : 0);
30 $verified = ((x($arr,'verified')) ? intval($arr['verified']) : 0);
32 $publish = ((x($arr,'profile_publish_reg') && intval($arr['profile_publish_reg'])) ? 1 : 0);
33 $netpublish = ((strlen(get_config('system','directory_submit_url'))) ? $publish : 0);
35 $tmp_str = $openid_url;
39 $result['message'] .= t('An invitation is required.') . EOL;
42 $r = q("select * from register where `hash` = '%s' limit 1", dbesc($invite_id));
44 $result['message'] .= t('Invitation could not be verified.') . EOL;
49 if((! x($username)) || (! x($email)) || (! x($nickname))) {
51 if(! validate_url($tmp_str)) {
52 $result['message'] .= t('Invalid OpenID url') . EOL;
55 $_SESSION['register'] = 1;
56 $_SESSION['openid'] = $openid_url;
57 require_once('library/openid.php');
58 $openid = new LightOpenID;
59 $openid->identity = $openid_url;
60 $openid->returnUrl = $a->get_baseurl() . '/openid';
61 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
62 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
63 goaway($openid->authUrl());
67 notice( t('Please enter the required information.') . EOL );
71 if(! validate_url($tmp_str))
77 // collapse multiple spaces in name
78 $username = preg_replace('/ +/',' ',$username);
80 if(mb_strlen($username) > 48)
81 $result['message'] .= t('Please use a shorter name.') . EOL;
82 if(mb_strlen($username) < 3)
83 $result['message'] .= t('Name too short.') . EOL;
85 // I don't really like having this rule, but it cuts down
86 // on the number of auto-registrations by Russian spammers
88 // Using preg_match was completely unreliable, due to mixed UTF-8 regex support
89 // $no_utf = get_config('system','no_utf');
90 // $pat = (($no_utf) ? '/^[a-zA-Z]* [a-zA-Z]*$/' : '/^\p{L}* \p{L}*$/u' );
92 // So now we are just looking for a space in the full name.
94 $loose_reg = get_config('system','no_regfullname');
96 $username = mb_convert_case($username,MB_CASE_TITLE,'UTF-8');
97 if(! strpos($username,' '))
98 $result['message'] .= t("That doesn't appear to be your full \x28First Last\x29 name.") . EOL;
102 if(! allowed_email($email))
103 $result['message'] .= t('Your email domain is not among those allowed on this site.') . EOL;
105 if((! valid_email($email)) || (! validate_email($email)))
106 $result['message'] .= t('Not a valid email address.') . EOL;
108 // Disallow somebody creating an account using openid that uses the admin email address,
109 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
111 if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0) && strlen($openid_url)) {
112 $r = q("SELECT * FROM `user` WHERE `email` = '%s' LIMIT 1",
116 $result['message'] .= t('Cannot use that email.') . EOL;
119 $nickname = $arr['nickname'] = strtolower($nickname);
121 if(! preg_match("/^[a-z][a-z0-9\-\_]*$/",$nickname))
122 $result['message'] .= t('Your "nickname" can only contain "a-z", "0-9", "-", and "_", and must also begin with a letter.') . EOL;
123 $r = q("SELECT `uid` FROM `user`
124 WHERE `nickname` = '%s' LIMIT 1",
128 $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
130 // Check deleted accounts that had this nickname. Doesn't matter to us,
131 // but could be a security issue for federated platforms.
133 $r = q("SELECT * FROM `userd`
134 WHERE `username` = '%s' LIMIT 1",
138 $result['message'] .= t('Nickname was once registered here and may not be re-used. Please choose another.') . EOL;
140 if(strlen($result['message'])) {
144 $new_password = ((strlen($password)) ? $password : autoname(6) . mt_rand(100,9999));
145 $new_password_encoded = hash('whirlpool',$new_password);
147 $result['password'] = $new_password;
149 require_once('include/crypto.php');
151 $keys = new_keypair(4096);
153 if($keys === false) {
154 $result['message'] .= t('SERIOUS ERROR: Generation of security keys failed.') . EOL;
158 $default_service_class = get_config('system','default_service_class');
159 if(! $default_service_class)
160 $default_service_class = '';
163 $prvkey = $keys['prvkey'];
164 $pubkey = $keys['pubkey'];
168 * Create another keypair for signing/verifying
169 * salmon protocol messages. We have to use a slightly
170 * less robust key because this won't be using openssl
171 * but the phpseclib. Since it is PHP interpreted code
172 * it is not nearly as efficient, and the larger keys
173 * will take several minutes each to process.
177 $sres = new_keypair(512);
178 $sprvkey = $sres['prvkey'];
179 $spubkey = $sres['pubkey'];
181 $r = q("INSERT INTO `user` ( `guid`, `username`, `password`, `email`, `openid`, `nickname`,
182 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `register_date`, `verified`, `blocked`, `timezone`, `service_class` )
183 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 'UTC', '%s' )",
184 dbesc(generate_user_guid()),
186 dbesc($new_password_encoded),
194 dbesc(datetime_convert()),
197 dbesc($default_service_class)
201 $r = q("SELECT * FROM `user`
202 WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
204 dbesc($new_password_encoded)
206 if($r !== false && count($r)) {
208 $newuid = intval($r[0]['uid']);
212 $result['message'] .= t('An error occurred during registration. Please try again.') . EOL ;
217 * if somebody clicked submit twice very quickly, they could end up with two accounts
218 * due to race condition. Remove this one.
221 $r = q("SELECT `uid` FROM `user`
222 WHERE `nickname` = '%s' ",
225 if((count($r) > 1) && $newuid) {
226 $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
227 q("DELETE FROM `user` WHERE `uid` = %d LIMIT 1",
233 if(x($newuid) !== false) {
234 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb`, `publish`, `net-publish` )
235 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, %d ) ",
240 dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
241 dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
247 $result['message'] .= t('An error occurred creating your default profile. Please try again.') . EOL;
248 // Start fresh next time.
249 $r = q("DELETE FROM `user` WHERE `uid` = %d",
253 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`, `nurl`,
254 `request`, `notify`, `poll`, `confirm`, `poco`, `name-date`, `uri-date`, `avatar-date`, `closeness` )
255 VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0 ) ",
260 dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
261 dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
262 dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
263 dbesc($a->get_baseurl() . "/profile/$nickname"),
264 dbesc(normalise_link($a->get_baseurl() . "/profile/$nickname")),
265 dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
266 dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
267 dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
268 dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
269 dbesc($a->get_baseurl() . "/poco/$nickname"),
270 dbesc(datetime_convert()),
271 dbesc(datetime_convert()),
272 dbesc(datetime_convert())
275 // Create a group with no members. This allows somebody to use it
276 // right away as a default group for new contacts.
278 require_once('include/group.php');
279 group_add($newuid, t('Friends'));
281 $r = q("SELECT id FROM `group` WHERE uid = %d AND name = '%s'",
285 if($r && count($r)) {
286 $def_gid = $r[0]['id'];
288 q("UPDATE user SET def_gid = %d WHERE uid = %d",
294 if(get_config('system', 'newuser_private') && $def_gid) {
295 q("UPDATE user SET allow_gid = '%s' WHERE uid = %d",
296 dbesc("<" . $def_gid . ">"),
303 // if we have no OpenID photo try to look up an avatar
305 $photo = avatar_img($email);
307 // unless there is no avatar-plugin loaded
309 require_once('include/Photo.php');
310 $photo_failure = false;
312 $filename = basename($photo);
313 $img_str = fetch_url($photo,true);
314 // guess mimetype from headers or filename
315 $type = guess_image_type($photo,true);
318 $img = new Photo($img_str, $type);
319 if($img->is_valid()) {
321 $img->scaleImageSquare(175);
323 $hash = photo_new_resource();
325 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
328 $photo_failure = true;
330 $img->scaleImage(80);
332 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
335 $photo_failure = true;
337 $img->scaleImage(48);
339 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
342 $photo_failure = true;
344 if(! $photo_failure) {
345 q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
352 call_hooks('register_account', $newuid);
354 $result['success'] = true;
355 $result['user'] = $u;