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