root/wifidogadmin/wifidog/signup.php

Revision 479, 12.0 kB (checked in by insultant, 8 months ago)

--

Line 
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 // +-------------------------------------------------------------------+
6 // | WiFiDog Authentication Server                                     |
7 // | =============================                                     |
8 // |                                                                   |
9 // | The WiFiDog Authentication Server is part of the WiFiDog captive  |
10 // | portal suite.                                                     |
11 // +-------------------------------------------------------------------+
12 // | PHP version 5 required.                                           |
13 // +-------------------------------------------------------------------+
14 // | Homepage:     http://www.wifidog.org/                             |
15 // | Source Forge: http://sourceforge.net/projects/wifidog/            |
16 // +-------------------------------------------------------------------+
17 // | This program is free software; you can redistribute it and/or     |
18 // | modify it under the terms of the GNU General Public License as    |
19 // | published by the Free Software Foundation; either version 2 of    |
20 // | the License, or (at your option) any later version.               |
21 // |                                                                   |
22 // | This program is distributed in the hope that it will be useful,   |
23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
25 // | GNU General Public License for more details.                      |
26 // |                                                                   |
27 // | You should have received a copy of the GNU General Public License |
28 // | along with this program; if not, contact:                         |
29 // |                                                                   |
30 // | Free Software Foundation           Voice:  +1-617-542-5942        |
31 // | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
32 // | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
33 // |                                                                   |
34 // +-------------------------------------------------------------------+
35
36 /**
37  * Sign up page
38  *
39  * @package    WiFiDogAuthServer
40  * @author     Philippe April
41  * @author     Benoit Grégoire <bock@step.polymtl.ca>
42  * @author     Max Horváth <max.horvath@freenet.de>
43  * @copyright  2004-2006 Philippe April
44  * @copyright  2004-2006 Benoit Grégoire, Technologies Coeus inc.
45  * @copyright  2006 Max Horváth, Horvath Web Consulting
46  * @version    Subversion $Id: signup.php 1249 2007-07-12 20:05:42Z benoitg $
47  * @link       http://www.wifidog.org/
48  */
49
50 /**
51  * Load required files
52  */
53 require_once(dirname(__FILE__) . '/include/common.php');
54
55 require_once('classes/User.php');
56 require_once('classes/Security.php');
57 require_once('classes/MainUI.php');
58 require_once('classes/Mail.php');
59 $smarty = SmartyWifidog::getObject();
60 /**
61  * Load custom signup URL if it has been defined in config.php
62  */
63 if (defined("CUSTOM_SIGNUP_URL")) {
64     header("Location: " . CUSTOM_SIGNUP_URL . "?gw=" . base64_encode($_SERVER['REQUEST_URI']));
65     exit;
66 }
67
68 /**
69  * Validates the format of an username
70  *
71  * @param string $username The username
72  *
73  * @return void
74  *
75  * @throws Exeption if no username was given or if the username contains
76  *         invalid characters
77  */
78 function validate_username($username)
79 {
80     if (!isset ($username) || !$username) {
81         throw new Exception(_('Username is required.'));
82     }
83
84     if (!ereg("^[0-9a-zA-Z_]*$", $username)) {
85         throw new Exception(_('Username contains invalid characters.'));
86     }
87 }
88
89 /**
90  * Validates the format of an email address
91  *
92  * @param string $email The email address
93  *
94  * @return void
95  *
96  * @throws Exeption if no email address was given or if the format of the email
97  *         address is invalid characters or if the domain of the email address
98  *         is black-listed
99  */
100 function validate_email($email)
101 {
102     if (!isset ($email) || !$email) {
103         throw new Exception(_("A valid email address is required."));
104     }
105
106        if (Mail::validateEmailAddress($email) === false) {
107         throw new Exception(_("The email address must be valid (i.e. user@domain.com). Please understand that we also black-listed various temporary-email-address providers."));
108        }
109 }
110
111 /**
112  * Validates the format of a password
113  *
114  * @param string $password       The password
115  * @param string $password_again Copy of password
116  *
117  * @return void
118  *
119  * @throws Exeption if no password was given or if the password contains
120  *         invalid characters or if the two given passwords don't match or
121  *         if the password is too short
122  */
123 function validate_passwords($password, $password_again)
124 {
125     if (!isset ($password) || !$password) {
126         throw new Exception(_("A password of at least 6 characters is required."));
127     }
128
129     if (!ereg("^[0-9a-zA-Z]*$", $password)) {
130         throw new Exception(_("Password contains invalid characters.  Allowed characters are 0-9, a-z and A-Z"));
131     }
132
133     if (!isset ($password_again)) {
134         throw new Exception(_("You must type your password twice."));
135     }
136
137     if ($password != $password_again) {
138         throw new Exception(_("Passwords do not match."));
139     }
140
141     if (strlen($password) < 6) {
142         throw new Exception(_("Password is too short, it must be 6 characters minimum."));
143     }
144 }
145
146 /**
147  * Process signing up
148  */
149
150 // Init ALL smarty SWITCH values
151 $smarty->assign('sectionTOOLCONTENT', false);
152 $smarty->assign('sectionMAINCONTENT', false);
153
154 // Init ALL smarty values
155 $smarty->assign('username', "");
156 $smarty->assign('email', "");
157 $smarty->assign('error', "");
158 $smarty->assign('auth_sources', "");
159 $smarty->assign('selected_auth_source', "");
160 $smarty->assign('SelectNetworkUI', "");
161
162 if (isset ($_REQUEST["form_request"]) && $_REQUEST["form_request"] == "signup") {
163     // Secure entered values
164     $username = trim($_REQUEST['username']);
165     $email = trim($_REQUEST['email']);
166     $password = trim($_REQUEST['password']);
167     $password_again = trim($_REQUEST['password_again']);
168
169     $smarty->assign('username', $username);
170     $smarty->assign('email', $email);
171
172     $network = Network::getObject($_REQUEST['auth_source']);
173
174     try {
175         /*
176          * Tool content
177          */
178
179         // Set section of Smarty template
180         $smarty->assign('sectionTOOLCONTENT', true);
181
182         // Compile HTML code
183         $html = $smarty->fetch("templates/sites/signup.tpl");
184
185         /*
186          * Main content
187          */
188
189         // Reset ALL smarty SWITCH values
190         $smarty->assign('sectionTOOLCONTENT', false);
191         $smarty->assign('sectionMAINCONTENT', false);
192
193         // Set section of Smarty template
194         $smarty->assign('sectionMAINCONTENT', true);
195
196         if (!isset($network)) {
197             throw new Exception(_("Sorry, this network does not exist !"));
198         }
199
200         if (!$network->getAuthenticator()->isRegistrationPermitted()) {
201             throw new Exception(_("Sorry, this network does not accept new user registration !"));
202         }
203
204         // Validate entered values
205         validate_username($username);
206         validate_email($email);
207         validate_passwords($password, $password_again);
208
209         // Check if user exists
210         if (User::getUserByUsernameAndOrigin($username, $network)) {
211             throw new Exception(_("Sorry, a user account is already associated to this username. Pick another one."));
212         }
213
214         if (User::getUserByEmailAndOrigin($email, $network)) {
215             throw new Exception(_("Sorry, a user account is already associated to this email address."));
216         }
217
218         // Create user and send him the validation email
219         $created_user = User::createUser(get_guid(), $username, $network, $email, $password);
220         $created_user->sendValidationEmail();
221
222         // Authenticate this new user automatically
223         $errmsg = "";
224         $authenticated_user = $network->getAuthenticator()->login($username, $password, $errmsg);
225
226         // While in validation period, alert user that he should validate his account ASAP
227         $validationMsgHtml = "<div id='warning_message_area'>\n";
228         $validationMsgHtml .= _("An email with confirmation instructions was sent to your email address.");
229         $validationMsgHtml .= sprintf(_("Your account has been granted %s minutes of access to retrieve your email and validate your account."), ($network->getValidationGraceTime() / 60));
230         $validationMsgHtml .= _('You may now open a browser window or start your email client and go to any remote Internet address to obtain the validation email.');
231         $validationMsgHtml .= "</div>\n";
232
233         // If the user is at a REAL hotspot, give him his sign-up minutes right away
234         $session = Session::getObject();
235         $gw_id = $session->get(SESS_GW_ID_VAR);
236         $gw_address = $session->get(SESS_GW_ADDRESS_VAR);
237         $gw_port = $session->get(SESS_GW_PORT_VAR);
238
239         if ($gw_id && $gw_address && $gw_port) {
240             // Make sure the user IDs match
241             if(($created_user->getId() == $authenticated_user->getId())) {
242                 $token = $created_user->generateConnectionToken();
243
244                 $redirURL = "http://" . $gw_address . ":" . $gw_port . "/wifidog/auth?token=" . $token;
245             } else {
246                 $redirURL = BASE_NON_SSL_PATH;
247             }
248
249             MainUI::redirect($redirURL, 0);
250         }
251
252         // Compile HTML code
253         $html_body = $smarty->fetch("templates/sites/signup.tpl");
254
255         /*
256          * Render output
257          */
258         $ui = MainUI::getObject();
259
260         $ui->addContent('left_area_middle', $html);
261         $ui->addContent('main_area_middle', $html_body);
262
263         // $ui->addContent('page_header', $validationMsgHtml);
264         $ui->addContent('main_area_top', $validationMsgHtml);
265
266         $ui->display();
267
268         // We're done ...
269         exit;
270     }
271
272     catch (Exception $e) {
273         $smarty->assign('error', $e->getMessage());
274
275         // Reset HTML output
276         $html = "";
277         $html_body = "";
278
279         // Reset ALL smarty SWITCH values
280         $smarty->assign('sectionTOOLCONTENT', false);
281         $smarty->assign('sectionMAINCONTENT', false);
282     }
283 }
284
285 /*
286  * Tool content
287  */
288
289 if (isset ($_REQUEST["form_request"]) && $_REQUEST["form_request"] == "login") {
290     $username = trim($_REQUEST['username']);
291     if (strpos($username, "@") === false)
292         $smarty->assign('username', $username);
293     else {
294         $email = $username;
295         $username = "";
296         $smarty->assign('email', $email);
297     }
298 }
299
300 // Set section of Smarty template
301 $smarty->assign('sectionTOOLCONTENT', true);
302
303 // Compile HTML code
304 $html = $smarty->fetch("templates/sites/signup.tpl");
305
306 /*
307  * Main content
308  */
309
310 // Reset ALL smarty SWITCH values
311 $smarty->assign('sectionTOOLCONTENT', false);
312 $smarty->assign('sectionMAINCONTENT', false);
313
314 // Set section of Smarty template
315 $smarty->assign('sectionMAINCONTENT', true);
316
317 // Add the auth servers list to smarty variables
318 $sources = array ();
319
320 // Preserve keys
321 $network_array = Network::getAllNetworks();
322
323 foreach ($network_array as $network) {
324     if ($network->getAuthenticator()->isRegistrationPermitted()) {
325         $sources[$network->getId()] = $network->getName();
326     }
327 }
328
329 if (isset($sources)) {
330     $smarty->assign('auth_sources', $sources);
331 }
332
333 // Pass the account_origin along, if it's set
334 if (isset($_REQUEST["auth_source"])) {
335     $smarty->assign('selected_auth_source', $_REQUEST["auth_source"]);
336 }
337
338 $smarty->assign('SelectNetworkUI', Network::getSelectUI('auth_source'));
339
340 // Compile HTML code
341 $html_body = $smarty->fetch("templates/sites/signup.tpl");
342
343 /*
344  * Render output
345  */
346 $ui = MainUI::getObject();
347 $ui->addContent('left_area_middle', $html);
348 $ui->addContent('main_area_middle', $html_body);
349 $ui->display();
350
351 /*
352  * Local variables:
353  * tab-width: 4
354  * c-basic-offset: 4
355  * c-hanging-comment-ender-p: nil
356  * End:
357  */
358
359 ?>
360
Note: See TracBrowser for help on using the browser.