]> git.mxchange.org Git - friendica.git/blob - mod/register.php
a0a5e5c284f880ba7003d5732452184ea1472cee
[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         if( $a->config['register_policy'] == REGISTER_OPEN ) {
191                 $email_tpl = load_view_file("view/register_open_eml.tpl");
192                 $email_tpl = replace_macros($email_tpl, array(
193                                 '$sitename' => $a->config['sitename'],
194                                 '$siteurl' =>  $a->get_baseurl(),
195                                 '$username' => $username,
196                                 '$email' => $email,
197                                 '$password' => $new_password,
198                                 '$uid' => $newuid ));
199
200                 $res = mail($email, t('Registration details for ') . $a->config['sitename'],
201                         $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
202
203
204                 if($res) {
205                         notice( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
206                         goaway($a->get_baseurl());
207                 }
208                 else {
209                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
210                 }
211         }
212         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
213                 if(! strlen($a->config['admin_email'])) {
214                         notice( t('Your registration can not be processed.') . EOL);
215                         goaway($a->get_baseurl());
216                 }
217
218                 $hash = random_string();
219                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password` ) VALUES ( '%s', '%s', %d, '%s' ) ",
220                         dbesc($hash),
221                         dbesc(datetime_convert()),
222                         intval($newuid),
223                         dbesc($new_password)
224                 );
225
226                 $email_tpl = load_view_file("view/register_verify_eml.tpl");
227                 $email_tpl = replace_macros($email_tpl, array(
228                                 '$sitename' => $a->config['sitename'],
229                                 '$siteurl' =>  $a->get_baseurl(),
230                                 '$username' => $username,
231                                 '$email' => $email,
232                                 '$password' => $new_password,
233                                 '$uid' => $newuid,
234                                 '$hash' => $hash
235                  ));
236
237                 $res = mail($a->config['admin_email'], t('Registration request at ') . $a->config['sitename'],
238                         $email_tpl,'From: ' .  t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
239
240                 if($res) {
241                         notice( t('Your registration is pending approval by the site owner.') . EOL ) ;
242                         goaway($a->get_baseurl());
243                 }
244
245         }
246         
247         return;
248 }}
249
250
251
252
253
254
255 if(! function_exists('register_content')) {
256 function register_content(&$a) {
257
258         // logged in users can register others (people/pages/groups)
259         // even with closed registrations, unless specifically prohibited by site policy.
260         // 'block_extended_register' blocks all registrations, period.
261
262         $block = get_config('system','block_extended_register');
263
264         if((($a->config['register_policy'] == REGISTER_CLOSED) && (! getuid())) || ($block)) {
265                 notice("Permission denied." . EOL);
266                 return;
267         }
268
269         $o = load_view_file("view/register.tpl");
270         $o = replace_macros($o, array(
271                 '$registertext' =>((x($a->config,'register_text'))
272                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
273                         : "" ),
274                 '$sitename' => $a->get_hostname()
275         ));
276         return $o;
277
278 }}
279