doc/developer/ClosedNetwork: manual_useradd.php

File manual_useradd.php, 10.3 kB (added by Andrew Hodel, 9 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  * Manual Useradd Page
38  *
39  * @package    WiFiDogAuthServer
40  * @author     Andrew Hodel
41  * @author     Philippe April
42  * @author     Benoit Grégoire <bock@step.polymtl.ca>
43  * @author     Max Horváth <max.horvath@freenet.de>
44  * @copyright  2004-2006 Philippe April
45  * @copyright  2004-2006 Benoit Grégoire, Technologies Coeus inc.
46  * @copyright  2006 Max Horváth, Horvath Web Consulting
47  * @version    Subversion $Id: manual_useradd.php 1249 2008-03-01 20:05:42Z benoitg $
48  * @link       http://www.wifidog.org/
49  */
50
51 /**
52  * Load required files
53  */
54 require_once(dirname(__FILE__) . '/include/common.php');
55
56 require_once('classes/User.php');
57 require_once('classes/Security.php');
58 require_once('classes/MainUI.php');
59 require_once('classes/Mail.php');
60 $smarty = SmartyWifidog::getObject();
61 /**
62  * Load custom signup URL if it has been defined in config.php
63  */
64 if (defined("CUSTOM_SIGNUP_URL")) {
65     header("Location: " . CUSTOM_SIGNUP_URL . "?gw=" . base64_encode($_SERVER['REQUEST_URI']));
66     exit;
67 }
68
69 /**
70  * Validates the format of an username
71  *
72  * @param string $username The username
73  *
74  * @return void
75  *
76  * @throws Exeption if no username was given or if the username contains
77  *         invalid characters
78  */
79 function validate_username($username)
80 {
81     if (!isset ($username) || !$username) {
82         throw new Exception(_('Username is required.'));
83     }
84
85     if (!ereg("^[0-9a-zA-Z_]*$", $username)) {
86         throw new Exception(_('Username contains invalid characters.'));
87     }
88 }
89
90 /**
91  * Validates the format of an email address
92  *
93  * @param string $email The email address
94  *
95  * @return void
96  *
97  * @throws Exeption if no email address was given or if the format of the email
98  *         address is invalid characters or if the domain of the email address
99  *         is black-listed
100  */
101 function validate_email($email)
102 {
103     if (!isset ($email) || !$email) {
104         throw new Exception(_("A valid email address is required."));
105     }
106
107        if (Mail::validateEmailAddress($email) === false) {
108         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."));
109        }
110 }
111
112 /**
113  * Validates the format of a password
114  *
115  * @param string $password       The password
116  * @param string $password_again Copy of password
117  *
118  * @return void
119  *
120  * @throws Exeption if no password was given or if the password contains
121  *         invalid characters or if the two given passwords don't match or
122  *         if the password is too short
123  */
124 function validate_passwords($password, $password_again)
125 {
126     if (!isset ($password) || !$password) {
127         throw new Exception(_("A password of at least 6 characters is required."));
128     }
129
130     if (!ereg("^[0-9a-zA-Z]*$", $password)) {
131         throw new Exception(_("Password contains invalid characters.  Allowed characters are 0-9, a-z and A-Z"));
132     }
133
134     if (!isset ($password_again)) {
135         throw new Exception(_("You must type your password twice."));
136     }
137
138     if ($password != $password_again) {
139         throw new Exception(_("Passwords do not match."));
140     }
141
142     if (strlen($password) < 6) {
143         throw new Exception(_("Password is too short, it must be 6 characters minimum."));
144     }
145 }
146
147 /**
148  * Process signing up
149  */
150
151 // Init ALL smarty SWITCH values
152 $smarty->assign('sectionTOOLCONTENT', false);
153 $smarty->assign('sectionMAINCONTENT', false);
154
155 // Init ALL smarty values
156 $smarty->assign('username', "");
157 $smarty->assign('email', "");
158 $smarty->assign('error', "");
159 $smarty->assign('auth_sources', "");
160 $smarty->assign('selected_auth_source', "");
161 $smarty->assign('SelectNetworkUI', "");
162
163 if (isset ($_REQUEST["form_request"]) && $_REQUEST["form_request"] == "signup") {
164     // Secure entered values
165     $username = trim($_REQUEST['username']);
166     $email = trim($_REQUEST['email']);
167     $password = trim($_REQUEST['password']);
168     $password_again = trim($_REQUEST['password_again']);
169
170     $smarty->assign('username', $username);
171     $smarty->assign('email', $email);
172
173     $network = Network::getObject($_REQUEST['auth_source']);
174
175     try {
176         /*
177          * Tool content
178          */
179
180         // Set section of Smarty template
181         $smarty->assign('sectionTOOLCONTENT', true);
182
183         // Compile HTML code
184         $html = $smarty->fetch("templates/sites/manual_useradd.tpl");
185
186         /*
187          * Main content
188          */
189
190         // Reset ALL smarty SWITCH values
191         $smarty->assign('sectionTOOLCONTENT', false);
192         $smarty->assign('sectionMAINCONTENT', false);
193
194         // Set section of Smarty template
195         $smarty->assign('sectionMAINCONTENT', true);
196
197         if (!isset($network)) {
198             throw new Exception(_("Sorry, this network does not exist !"));
199         }
200
201         if (!$network->getAuthenticator()->isRegistrationPermitted()) {
202             throw new Exception(_("Sorry, this network does not accept new user registration !"));
203         }
204
205         // Validate entered values
206         validate_username($username);
207         validate_email($email);
208         validate_passwords($password, $password_again);
209
210         // Check if user exists
211         if (User::getUserByUsernameAndOrigin($username, $network)) {
212             throw new Exception(_("Sorry, a user account is already associated to this username. Pick another one."));
213         }
214
215         if (User::getUserByEmailAndOrigin($email, $network)) {
216             throw new Exception(_("Sorry, a user account is already associated to this email address."));
217         }
218
219         // Create user
220     $tempguid = get_guid();
221         $created_user = User::createUser($tempguid, $username, $network, $email, $password);
222
223     $validated_user = User::getObject($tempguid);
224
225     $validated_user->SetAccountStatus(ACCOUNT_STATUS_ALLOWED);
226
227         // Compile HTML code
228         $html_body = $smarty->fetch("templates/sites/manual_useradd.tpl");
229
230         /*
231          * Render output
232          */
233
234     $usercomplete = "User: $username has been added!";
235
236         $ui = MainUI::getObject();
237
238         $ui->addContent('left_area_middle', $html);
239         $ui->addContent('main_area_middle', $html_body);
240
241         $ui->addContent('main_area_top', $usercomplete);
242
243         $ui->display();
244
245         // We're done ...
246         exit;
247     }
248
249     catch (Exception $e) {
250         $smarty->assign('error', $e->getMessage());
251
252         // Reset HTML output
253         $html = "";
254         $html_body = "";
255
256         // Reset ALL smarty SWITCH values
257         $smarty->assign('sectionTOOLCONTENT', false);
258         $smarty->assign('sectionMAINCONTENT', false);
259     }
260 }
261
262 /*
263  * Tool content
264  */
265
266 if (isset ($_REQUEST["form_request"]) && $_REQUEST["form_request"] == "login") {
267     $username = trim($_REQUEST['username']);
268     if (strpos($username, "@") === false)
269         $smarty->assign('username', $username);
270     else {
271         $email = $username;
272         $username = "";
273         $smarty->assign('email', $email);
274     }
275 }
276
277 // Set section of Smarty template
278 $smarty->assign('sectionTOOLCONTENT', true);
279
280 // Compile HTML code
281 $html = $smarty->fetch("templates/sites/manual_useradd.tpl");
282
283 /*
284  * Main content
285  */
286
287 // Reset ALL smarty SWITCH values
288 $smarty->assign('sectionTOOLCONTENT', false);
289 $smarty->assign('sectionMAINCONTENT', false);
290
291 // Set section of Smarty template
292 $smarty->assign('sectionMAINCONTENT', true);
293
294 // Add the auth servers list to smarty variables
295 $sources = array ();
296
297 // Preserve keys
298 $network_array = Network::getAllNetworks();
299
300 foreach ($network_array as $network) {
301     if ($network->getAuthenticator()->isRegistrationPermitted()) {
302         $sources[$network->getId()] = $network->getName();
303     }
304 }
305
306 if (isset($sources)) {
307     $smarty->assign('auth_sources', $sources);
308 }
309
310 // Pass the account_origin along, if it's set
311 if (isset($_REQUEST["auth_source"])) {
312     $smarty->assign('selected_auth_source', $_REQUEST["auth_source"]);
313 }
314
315 $smarty->assign('SelectNetworkUI', Network::getSelectUI('auth_source'));
316
317 // Compile HTML code
318 $html_body = $smarty->fetch("templates/sites/manual_useradd.tpl");
319
320 /*
321  * Render output
322  */
323 $ui = MainUI::getObject();
324 $ui->addContent('left_area_middle', $html);
325 $ui->addContent('main_area_middle', $html_body);
326 $ui->display();
327
328 /*
329  * Local variables:
330  * tab-width: 4
331  * c-basic-offset: 4
332  * c-hanging-comment-ender-p: nil
333  * End:
334  */
335
336 ?>
337