]> git.mxchange.org Git - friendica.git/blob - mod/register.php
progress on photos
[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         if(!eregi('[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,6}',$email))
48                 $err .= t(' Not a valid email address.');
49         if(strlen($username) > 48)
50                 $err .= t(' Please use a shorter name.');
51         if(strlen($username) < 3)
52                 $err .= t(' Name too short.');
53         $r = q("SELECT `uid` FROM `user` 
54                 WHERE `email` = '%s' LIMIT 1",
55                 dbesc($email)
56         );
57
58         if($r !== false && count($r))
59                 $err .= t(' Your email address is already registered on this system.') ;
60
61         if(! preg_match("/^[a-zA-Z][a-zA-Z0-9\-\_]*$/",$nickname))
62                 $err .= t(' Nickname <strong>must</strong> start with a letter and contain only letters, numbers, dashes, or underscore.') ;
63         $r = q("SELECT `uid` FROM `user`
64                 WHERE `nickname` = '%s' LIMIT 1",
65                 dbesc($nickname)
66         );
67         if(count($r))
68                 $err .= t(' Nickname is already registered. Please choose another.');
69
70         if(strlen($err)) {
71                 notice( $err . EOL );
72                 return;
73         }
74
75
76         $new_password = autoname(6) . mt_rand(100,9999);
77         $new_password_encoded = hash('whirlpool',$new_password);
78
79         $res=openssl_pkey_new(array(
80                 'digest_alg' => 'whirlpool',
81                 'private_key_bits' => 4096,
82                 'encrypt_key' => false ));
83
84         // Get private key
85
86         $prvkey = '';
87
88         openssl_pkey_export($res, $prvkey);
89
90         // Get public key
91
92         $pkey = openssl_pkey_get_details($res);
93         $pubkey = $pkey["key"];
94
95         $r = q("INSERT INTO `user` ( `username`, `password`, `email`, `nickname`,
96                 `pubkey`, `prvkey`, `verified`, `blocked` )
97                 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', %d, %d )",
98                 dbesc($username),
99                 dbesc($new_password_encoded),
100                 dbesc($email),
101                 dbesc($nickname),
102                 dbesc($pubkey),
103                 dbesc($prvkey),
104                 intval($verified),
105                 intval($blocked)
106                 );
107
108         if($r) {
109                 $r = q("SELECT `uid` FROM `user` 
110                         WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
111                         dbesc($username),
112                         dbesc($new_password_encoded)
113                         );
114                 if($r !== false && count($r))
115                         $newuid = intval($r[0]['uid']);
116         }
117         else {
118                 notice( t('An error occurred during registration. Please try again.') . EOL );
119                 return;
120         }               
121
122         if(x($newuid) !== false) {
123                 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb` )
124                         VALUES ( %d, '%s', %d, '%s', '%s', '%s' ) ",
125                         intval($newuid),
126                         'default',
127                         1,
128                         dbesc($username),
129                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
130                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg")
131
132                 );
133                 if($r === false) {
134                         notice( t('An error occurred creating your default profile. Please try again.') . EOL );
135                         // Start fresh next time.
136                         $r = q("DELETE FROM `user` WHERE `uid` = %d",
137                                 intval($newuid));
138                         return;
139                 }
140                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `photo`, `thumb`, `blocked`, `pending`, `url`,
141                         `request`, `notify`, `poll`, `confirm` )
142                         VALUES ( %d, '%s', 1, '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s' ) ",
143                         intval($newuid),
144                         datetime_convert(),
145                         dbesc($username),
146                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
147                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
148                         dbesc($a->get_baseurl() . "/profile/$nickname"),
149                         dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
150                         dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
151                         dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
152                         dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname")
153
154                 );
155
156
157         }
158
159         if( $a->config['register_policy'] == REGISTER_OPEN ) {
160                 $email_tpl = file_get_contents("view/register_open_eml.tpl");
161                 $email_tpl = replace_macros($email_tpl, array(
162                                 '$sitename' => $a->config['sitename'],
163                                 '$siteurl' =>  $a->get_baseurl(),
164                                 '$username' => $username,
165                                 '$email' => $email,
166                                 '$password' => $new_password,
167                                 '$uid' => $newuid ));
168
169                 $res = mail($email, t('Registration details for ') . $a->config['sitename'],
170                         $email_tpl, 'From: ' . t('Administrator@') . $_SERVER[SERVER_NAME]);
171
172
173                 if($res) {
174                         notice( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
175                         goaway($a->get_baseurl());
176                 }
177                 else {
178                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
179                 }
180         }
181         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
182                 if(! strlen($a->config['admin_email'])) {
183                         notice( t('Your registration can not be processed.') . EOL);
184                         goaway($a->get_baseurl());
185                 }
186
187                 $hash = random_string();
188                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password` ) VALUES ( '%s', '%s', %d, '%s' ) ",
189                         dbesc($hash),
190                         dbesc(datetime_convert()),
191                         intval($newuid),
192                         dbesc($new_password)
193                 );
194
195                 $email_tpl = file_get_contents("view/register_verify_eml.tpl");
196                 $email_tpl = replace_macros($email_tpl, array(
197                                 '$sitename' => $a->config['sitename'],
198                                 '$siteurl' =>  $a->get_baseurl(),
199                                 '$username' => $username,
200                                 '$email' => $email,
201                                 '$password' => $new_password,
202                                 '$uid' => $newuid,
203                                 '$hash' => $hash
204                  ));
205
206                 $res = mail($a->config['admin_email'], t('Registration request at ') . $a->config['sitename'],
207                         $email_tpl,'From: ' .  t('Administrator@') . $_SERVER[SERVER_NAME]);
208
209                 if($res) {
210                         notice( t('Your registration is pending approval by the site owner.') . EOL ) ;
211                         goaway($a->get_baseurl());
212                 }
213
214         }
215         
216         return;
217 }}
218
219
220
221
222
223
224 if(! function_exists('register_content')) {
225 function register_content(&$a) {
226
227         if($a->config['register_policy'] == REGISTER_CLOSED) {
228                 notice("Permission denied." . EOL);
229                 return;
230         }
231
232         $o = file_get_contents("view/register.tpl");
233         $o = replace_macros($o, array(
234                 '$registertext' =>((x($a->config,'register_text'))
235                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
236                         : "" ),
237                 '$sitename' => $a->get_hostname()
238         ));
239         return $o;
240
241 }}
242