]> git.mxchange.org Git - friendica.git/blob - mod/register.php
turn registration code into a standalone function for re-use
[friendica.git] / mod / register.php
1 <?php
2
3 if(! function_exists('register_post')) {
4 function register_post(&$a) {
5
6         global $lang;
7
8         $verified = 0;
9         $blocked  = 1;
10
11         $arr = array('post' => $_POST);
12         call_hooks('register_post', $arr);
13
14         $max_dailies = intval(get_config('system','max_daily_registrations'));
15         if($max_dailes) {
16                 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
17                 if($r && $r[0]['total'] >= $max_dailies) {
18                         return;
19                 }
20         }
21
22         switch($a->config['register_policy']) {
23
24         
25         case REGISTER_OPEN:
26                 $blocked = 0;
27                 $verified = 1;
28                 break;
29
30         case REGISTER_APPROVE:
31                 $blocked = 1;
32                 $verified = 0;
33                 break;
34
35         default:
36         case REGISTER_CLOSED:
37                 if((! x($_SESSION,'authenticated') && (! x($_SESSION,'administrator')))) {
38                         notice( t('Permission denied.') . EOL );
39                         return;
40                 }
41                 $blocked = 1;
42                 $verified = 0;
43                 break;
44         }
45
46         require_once('include/user.php');
47
48         $result = create_user($_POST);
49
50         if(! $result['success']) {
51                 notice($result['message']);
52                 return;
53         }
54
55         $user = $result['user'];
56  
57         if($netpublish && $a->config['register_policy'] != REGISTER_APPROVE) {
58                 $url = $a->get_baseurl() . '/profile/' . $user['nickname'];
59                 proc_run('php',"include/directory.php","$url");
60         }
61
62         $using_invites = get_config('system','invitation_only');
63         $num_invites   = get_config('system','number_invites');
64         $invite_id  = ((x($_POST,'invite_id'))  ? notags(trim($_POST['invite_id']))  : '');
65
66
67         if( $a->config['register_policy'] == REGISTER_OPEN ) {
68
69                 if($using_invites && $invite_id) {
70                         q("delete * from register where hash = '%s' limit 1", dbesc($invite_id));
71                         set_pconfig($user['uid'],'system','invites_remaining',$num_invites);
72                 }
73
74                 $email_tpl = get_intltext_template("register_open_eml.tpl");
75                 $email_tpl = replace_macros($email_tpl, array(
76                                 '$sitename' => $a->config['sitename'],
77                                 '$siteurl' =>  $a->get_baseurl(),
78                                 '$username' => $user['username'],
79                                 '$email' => $user['email'],
80                                 '$password' => $result['password'],
81                                 '$uid' => $user['uid'] ));
82
83                 $res = mail($user['email'], sprintf(t('Registration details for %s'), $a->config['sitename']),
84                         $email_tpl, 
85                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
86                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
87                                 . 'Content-transfer-encoding: 8bit' );
88
89
90                 if($res) {
91                         info( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
92                         goaway(z_root());
93                 }
94                 else {
95                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
96                 }
97         }
98         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
99                 if(! strlen($a->config['admin_email'])) {
100                         notice( t('Your registration can not be processed.') . EOL);
101                         goaway(z_root());
102                 }
103
104                 $hash = random_string();
105                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password`, `language` ) VALUES ( '%s', '%s', %d, '%s', '%s' ) ",
106                         dbesc($hash),
107                         dbesc(datetime_convert()),
108                         intval($user['uid']),
109                         dbesc($result['password']),
110                         dbesc($lang)
111                 );
112
113                 $r = q("SELECT `language` FROM `user` WHERE `email` = '%s' LIMIT 1",
114                         dbesc($a->config['admin_email'])
115                 );
116                 if(count($r))
117                         push_lang($r[0]['language']);
118                 else
119                         push_lang('en');
120
121                 if($using_invites && $invite_id) {
122                         q("delete * from register where hash = '%s' limit 1", dbesc($invite_id));
123                         set_pconfig($user['uid'],'system','invites_remaining',$num_invites);
124                 }
125
126                 $email_tpl = get_intltext_template("register_verify_eml.tpl");
127                 $email_tpl = replace_macros($email_tpl, array(
128                                 '$sitename' => $a->config['sitename'],
129                                 '$siteurl' =>  $a->get_baseurl(),
130                                 '$username' => $user['username'],
131                                 '$email' => $user['email'],
132                                 '$password' => $result['password'],
133                                 '$uid' => $user['uid'],
134                                 '$hash' => $hash
135                  ));
136
137                 $res = mail($a->config['admin_email'], sprintf(t('Registration request at %s'), $a->config['sitename']),
138                         $email_tpl,
139                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
140                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
141                                 . 'Content-transfer-encoding: 8bit' );
142
143                 pop_lang();
144
145                 if($res) {
146                         info( t('Your registration is pending approval by the site owner.') . EOL ) ;
147                         goaway(z_root());
148                 }
149
150         }
151
152         return;
153 }}
154
155
156
157
158
159
160 if(! function_exists('register_content')) {
161 function register_content(&$a) {
162
163         // logged in users can register others (people/pages/groups)
164         // even with closed registrations, unless specifically prohibited by site policy.
165         // 'block_extended_register' blocks all registrations, period.
166
167         $block = get_config('system','block_extended_register');
168
169         if(local_user() && ($block)) {
170                 notice("Permission denied." . EOL);
171                 return;
172         }
173
174         if((! local_user()) && ($a->config['register_policy'] == REGISTER_CLOSED)) {
175                 notice("Permission denied." . EOL);
176                 return;
177         }
178
179         $max_dailies = intval(get_config('system','max_daily_registrations'));
180         if($max_dailes) {
181                 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
182                 if($r && $r[0]['total'] >= $max_dailies) {
183                         logger('max daily registrations exceeded.');
184                         notice( t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
185                         return;
186                 }
187         }
188
189         if(x($_SESSION,'theme'))
190                 unset($_SESSION['theme']);
191
192
193         $username     = ((x($_POST,'username'))     ? $_POST['username']     : ((x($_GET,'username'))     ? $_GET['username']              : ''));
194         $email        = ((x($_POST,'email'))        ? $_POST['email']        : ((x($_GET,'email'))        ? $_GET['email']                 : ''));
195         $openid_url   = ((x($_POST,'openid_url'))   ? $_POST['openid_url']   : ((x($_GET,'openid_url'))   ? $_GET['openid_url']            : ''));
196         $nickname     = ((x($_POST,'nickname'))     ? $_POST['nickname']     : ((x($_GET,'nickname'))     ? $_GET['nickname']              : ''));
197         $photo        = ((x($_POST,'photo'))        ? $_POST['photo']        : ((x($_GET,'photo'))        ? hex2bin($_GET['photo'])        : ''));
198         $invite_id    = ((x($_POST,'invite_id'))    ? $_POST['invite_id']    : ((x($_GET,'invite_id'))    ? $_GET['invite_id']             : ''));
199
200         $noid = get_config('system','no_openid');
201
202         if($noid) {
203                 $oidhtml = '';
204                 $fillwith = '';
205                 $fillext = '';
206                 $oidlabel = '';
207         }
208         else {
209                 $oidhtml = '<label for="register-openid" id="label-register-openid" >$oidlabel</label><input type="text" maxlength="60" size="32" name="openid_url" class="openid" id="register-openid" value="$openid" >';
210                 $fillwith = t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
211                 $fillext =  t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
212                 $oidlabel = t("Your OpenID \x28optional\x29: ");
213         }
214
215         // I set this and got even more fake names than before...
216
217         $realpeople = ''; // t('Members of this network prefer to communicate with real people who use their real names.');
218
219         if(get_config('system','publish_all')) {
220                 $profile_publish_reg = '<input type="hidden" name="profile_publish_reg" value="1" />';
221         }
222         else {
223                 $publish_tpl = get_markup_template("profile_publish.tpl");
224                 $profile_publish = replace_macros($publish_tpl,array(
225                         '$instance'     => 'reg',
226                         '$pubdesc'      => t('Include your profile in member directory?'),
227                         '$yes_selected' => ' checked="checked" ',
228                         '$no_selected'  => '',
229                         '$str_yes'      => t('Yes'),
230                         '$str_no'       => t('No')
231                 ));
232         }
233
234
235         $license = '';
236
237         $o = get_markup_template("register.tpl");
238
239         $arr = array('template' => $o);
240
241         call_hooks('register_form',$arr);
242
243         $o = replace_macros($o, array(
244                 '$oidhtml' => $oidhtml,
245                 '$invitations' => get_config('system','invitation_only'),
246                 '$invite_desc' => t('Membership on this site is by invitation only.'),
247                 '$invite_label' => t('Your invitation ID: '),
248                 '$invite_id' => $invite_id,
249                 '$realpeople' => $realpeople,
250                 '$regtitle'  => t('Registration'),
251                 '$registertext' =>((x($a->config,'register_text'))
252                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
253                         : "" ),
254                 '$fillwith'  => $fillwith,
255                 '$fillext'   => $fillext,
256                 '$oidlabel'  => $oidlabel,
257                 '$openid'    => $openid_url,
258                 '$namelabel' => t('Your Full Name ' . "\x28" . 'e.g. Joe Smith' . "\x29" . ': '),
259                 '$addrlabel' => t('Your Email Address: '),
260                 '$nickdesc'  => t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be \'<strong>nickname@$sitename</strong>\'.'),
261                 '$nicklabel' => t('Choose a nickname: '),
262                 '$photo'     => $photo,
263                 '$publish'   => $profile_publish,
264                 '$regbutt'   => t('Register'),
265                 '$username'  => $username,
266                 '$email'     => $email,
267                 '$nickname'  => $nickname,
268                 '$license'   => $license,
269                 '$sitename'  => $a->get_hostname()
270         ));
271         return $o;
272
273 }}
274