]> git.mxchange.org Git - friendica.git/blob - mod/register.php
30666405c6a9866aba1f5c9d020c07025e8733b2
[friendica.git] / mod / register.php
1 <?php
2
3 if(! function_exists('register_post')) {
4 function register_post(&$a) {
5
6         $verified = 0;
7         $blocked  = 1;
8
9         switch($a->config['register_policy']) {
10
11         
12         case REGISTER_OPEN:
13                 $blocked = 0;
14                 $verified = 1;
15                 break;
16
17         case REGISTER_APPROVE:
18                 $blocked = 1;
19                 $verified = 0;
20                 break;
21
22         default:
23         case REGISTER_CLOSED:
24                 if((! x($_SESSION,'authenticated') && (! x($_SESSION,'administrator')))) {
25                         notice( t('Permission denied.') . EOL );
26                         return;
27                 }
28                 $blocked = 1;
29                 $verified = 0;
30                 break;
31         }
32
33         if(x($_POST,'username'))
34                 $username = notags(trim($_POST['username']));
35         if(x($_POST['nickname']))
36                 $nickname = notags(trim($_POST['nickname']));
37         if(x($_POST,'email'))
38                 $email = notags(trim($_POST['email']));
39
40         if((! x($username)) || (! x($email)) || (! x($nickname))) {
41                 notice( t('Please enter the required information.') . EOL );
42                 return;
43         }
44
45         $err = '';
46
47
48         if(strlen($username) > 48)
49                 $err .= t('Please use a shorter name.') . EOL;
50         if(strlen($username) < 3)
51                 $err .= t('Name too short.') . EOL;
52
53         // I don't really like having this rule, but it cuts down
54         // on the number of auto-registrations by Russian spammers
55         
56         $no_utf = get_config('system','no_utf');
57
58         $pat = (($no_utf) ? '/^[a-zA-Z]* [a-zA-Z]*$/' : '/^\p{L}* \p{L}*$/u' ); 
59
60         $loose_reg = get_config('system','no_regfullname');
61
62         if((! $loose_reg) && (! preg_match($pat,$username)))
63                 $err .= t('That doesn\'t appear to be your full name.') . EOL;
64
65         if(! allowed_email($email))
66                         $err .= t('Your email domain is not among those allowed on this site.') . EOL;
67
68         if((! valid_email($email)) || (! validate_email($email)))
69                 $err .= t('Not a valid email address.') . EOL;
70
71         $nickname = $_POST['nickname'] = strtolower($nickname);
72
73         if(! preg_match("/^[a-z][a-z0-9\-\_]*$/",$nickname))
74                 $err .= t('Your "nickname" can only contain "a-z", "0-9", "-", and "_", and must also begin with a letter.') . EOL;
75         $r = q("SELECT `uid` FROM `user`
76                 WHERE `nickname` = '%s' LIMIT 1",
77                 dbesc($nickname)
78         );
79         if(count($r))
80                 $err .= t('Nickname is already registered. Please choose another.') . EOL;
81
82         if(strlen($err)) {
83                 notice( $err );
84                 return;
85         }
86
87
88         $new_password = autoname(6) . mt_rand(100,9999);
89         $new_password_encoded = hash('whirlpool',$new_password);
90
91         $res=openssl_pkey_new(array(
92                 'digest_alg' => 'whirlpool',
93                 'private_key_bits' => 4096,
94                 'encrypt_key' => false ));
95
96         // Get private key
97
98         $prvkey = '';
99
100         openssl_pkey_export($res, $prvkey);
101
102         // Get public key
103
104         $pkey = openssl_pkey_get_details($res);
105         $pubkey = $pkey["key"];
106
107         $sres=openssl_pkey_new(array(
108                 'encrypt_key' => false ));
109
110         // Get private key
111
112         $sprvkey = '';
113
114         openssl_pkey_export($sres, $sprvkey);
115
116         // Get public key
117
118         $spkey = openssl_pkey_get_details($sres);
119         $spubkey = $spkey["key"];
120
121         $r = q("INSERT INTO `user` ( `username`, `password`, `email`, `nickname`,
122                 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `verified`, `blocked` )
123                 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d )",
124                 dbesc($username),
125                 dbesc($new_password_encoded),
126                 dbesc($email),
127                 dbesc($nickname),
128                 dbesc($pubkey),
129                 dbesc($prvkey),
130                 dbesc($spubkey),
131                 dbesc($sprvkey),
132                 intval($verified),
133                 intval($blocked)
134                 );
135
136         if($r) {
137                 $r = q("SELECT `uid` FROM `user` 
138                         WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
139                         dbesc($username),
140                         dbesc($new_password_encoded)
141                         );
142                 if($r !== false && count($r))
143                         $newuid = intval($r[0]['uid']);
144         }
145         else {
146                 notice( t('An error occurred during registration. Please try again.') . EOL );
147                 return;
148         }               
149
150         if(x($newuid) !== false) {
151                 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb` )
152                         VALUES ( %d, '%s', %d, '%s', '%s', '%s' ) ",
153                         intval($newuid),
154                         'default',
155                         1,
156                         dbesc($username),
157                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
158                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg")
159
160                 );
161                 if($r === false) {
162                         notice( t('An error occurred creating your default profile. Please try again.') . EOL );
163                         // Start fresh next time.
164                         $r = q("DELETE FROM `user` WHERE `uid` = %d",
165                                 intval($newuid));
166                         return;
167                 }
168                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`,
169                         `request`, `notify`, `poll`, `confirm`, `name-date`, `uri-date`, `avatar-date` )
170                         VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
171                         intval($newuid),
172                         datetime_convert(),
173                         dbesc($username),
174                         dbesc($nickname),
175                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
176                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
177                         dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
178                         dbesc($a->get_baseurl() . "/profile/$nickname"),
179                         dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
180                         dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
181                         dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
182                         dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
183                         dbesc(datetime_convert()),
184                         dbesc(datetime_convert()),
185                         dbesc(datetime_convert())
186                 );
187
188
189         }
190
191         require_once('include/Photo.php');
192
193         $photo = gravatar_img($email);
194         $photo_failure = false;
195
196         $filename = basename($photo);
197         $img_str = fetch_url($photo,true);
198         $img = new Photo($img_str);
199         if($img->is_valid()) {
200
201                 $img->scaleImageSquare(175);
202                                         
203                 $hash = photo_new_resource();
204
205                 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
206
207                 if($r === false)
208                         $photo_failure = true;
209
210                 $img->scaleImage(80);
211
212                 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
213
214                 if($r === false)
215                         $photo_failure = true;
216
217                 $img->scaleImage(48);
218
219                 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
220
221                 if($r === false)
222                         $photo_failure = true;
223
224                 if(! $photo_failure) {
225                         q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
226                                 dbesc($hash)
227                         );
228                 }
229         }
230
231         if( $a->config['register_policy'] == REGISTER_OPEN ) {
232                 $email_tpl = load_view_file("view/register_open_eml.tpl");
233                 $email_tpl = replace_macros($email_tpl, array(
234                                 '$sitename' => $a->config['sitename'],
235                                 '$siteurl' =>  $a->get_baseurl(),
236                                 '$username' => $username,
237                                 '$email' => $email,
238                                 '$password' => $new_password,
239                                 '$uid' => $newuid ));
240
241                 $res = mail($email, t('Registration details for ') . $a->config['sitename'],
242                         $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
243
244
245                 if($res) {
246                         notice( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
247                         goaway($a->get_baseurl());
248                 }
249                 else {
250                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
251                 }
252         }
253         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
254                 if(! strlen($a->config['admin_email'])) {
255                         notice( t('Your registration can not be processed.') . EOL);
256                         goaway($a->get_baseurl());
257                 }
258
259                 $hash = random_string();
260                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password` ) VALUES ( '%s', '%s', %d, '%s' ) ",
261                         dbesc($hash),
262                         dbesc(datetime_convert()),
263                         intval($newuid),
264                         dbesc($new_password)
265                 );
266
267                 $email_tpl = load_view_file("view/register_verify_eml.tpl");
268                 $email_tpl = replace_macros($email_tpl, array(
269                                 '$sitename' => $a->config['sitename'],
270                                 '$siteurl' =>  $a->get_baseurl(),
271                                 '$username' => $username,
272                                 '$email' => $email,
273                                 '$password' => $new_password,
274                                 '$uid' => $newuid,
275                                 '$hash' => $hash
276                  ));
277
278                 $res = mail($a->config['admin_email'], t('Registration request at ') . $a->config['sitename'],
279                         $email_tpl,'From: ' .  t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
280
281                 if($res) {
282                         notice( t('Your registration is pending approval by the site owner.') . EOL ) ;
283                         goaway($a->get_baseurl());
284                 }
285
286         }
287         
288         return;
289 }}
290
291
292
293
294
295
296 if(! function_exists('register_content')) {
297 function register_content(&$a) {
298
299         // logged in users can register others (people/pages/groups)
300         // even with closed registrations, unless specifically prohibited by site policy.
301         // 'block_extended_register' blocks all registrations, period.
302
303         $block = get_config('system','block_extended_register');
304
305         if((($a->config['register_policy'] == REGISTER_CLOSED) && (! getuid())) || ($block)) {
306                 notice("Permission denied." . EOL);
307                 return;
308         }
309
310         $username = ((x($_POST,'username')) ? $_POST['username'] : ((x($_GET,'username')) ? $_GET['username'] : ''));
311         $email    = ((x($_POST,'email'))    ? $_POST['email']    : ((x($_GET,'email'))    ? $_GET['email']    : ''));
312         $nickname = ((x($_POST,'nickname')) ? $_POST['nickname'] : ((x($_GET,'nickname')) ? $_GET['nickname'] : ''));
313
314         $o = load_view_file("view/register.tpl");
315         $o = replace_macros($o, array(
316                 '$regtitle'  => t('Registration'),
317                 '$registertext' =>((x($a->config,'register_text'))
318                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
319                         : "" ),
320                 '$namelabel' => t('Your Full Name ' . "\x28" . 'e.g. Joe Smith' . "\x29" . ': '),
321                 '$addrlabel' => t('Your Email Address: '),
322                 '$nickdesc'  => t('Choose a profile nickname. This must begin with a text character. Your global profile locator will then be \'<strong>nickname@$sitename</strong>\'.'),
323                 '$nicklabel' => t('Choose a nickname: '),
324                 '$regbutt'   => t('Register'),
325                 '$username'  => $username,
326                 '$email'     => $email,
327                 '$nickname'  => $nickname,
328                 '$sitename'  => $a->get_hostname()
329         ));
330         return $o;
331
332 }}
333