| 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 | // Validate entered values |
|---|
| 202 | validate_username($username); |
|---|
| 203 | validate_email($email); |
|---|
| 204 | validate_passwords($password, $password_again); |
|---|
| 205 | |
|---|
| 206 | // Check if user exists |
|---|
| 207 | if (User::getUserByUsernameAndOrigin($username, $network)) { |
|---|
| 208 | throw new Exception(_("Sorry, a user account is already associated to this username. Pick another one.")); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | if (User::getUserByEmailAndOrigin($email, $network)) { |
|---|
| 212 | throw new Exception(_("Sorry, a user account is already associated to this email address.")); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | // Create user |
|---|
| 216 | $tempguid = get_guid(); |
|---|
| 217 | $created_user = User::createUser($tempguid, $username, $network, $email, $password); |
|---|
| 218 | |
|---|
| 219 | $validated_user = User::getObject($tempguid); |
|---|
| 220 | |
|---|
| 221 | $validated_user->SetAccountStatus(ACCOUNT_STATUS_ALLOWED); |
|---|
| 222 | |
|---|
| 223 | // Compile HTML code |
|---|
| 224 | $html_body = $smarty->fetch("templates/sites/manual_useradd.tpl"); |
|---|
| 225 | |
|---|
| 226 | /* |
|---|
| 227 | * Render output |
|---|
| 228 | */ |
|---|
| 229 | |
|---|
| 230 | $usercomplete = "User: $username has been added!"; |
|---|
| 231 | |
|---|
| 232 | $ui = MainUI::getObject(); |
|---|
| 233 | |
|---|
| 234 | $ui->addContent('left_area_middle', $html); |
|---|
| 235 | $ui->addContent('main_area_middle', $html_body); |
|---|
| 236 | |
|---|
| 237 | $ui->addContent('main_area_top', $usercomplete); |
|---|
| 238 | |
|---|
| 239 | $ui->display(); |
|---|
| 240 | |
|---|
| 241 | // We're done ... |
|---|
| 242 | exit; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | catch (Exception $e) { |
|---|
| 246 | $smarty->assign('error', $e->getMessage()); |
|---|
| 247 | |
|---|
| 248 | // Reset HTML output |
|---|
| 249 | $html = ""; |
|---|
| 250 | $html_body = ""; |
|---|
| 251 | |
|---|
| 252 | // Reset ALL smarty SWITCH values |
|---|
| 253 | $smarty->assign('sectionTOOLCONTENT', false); |
|---|
| 254 | $smarty->assign('sectionMAINCONTENT', false); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /* |
|---|
| 259 | * Tool content |
|---|
| 260 | */ |
|---|
| 261 | |
|---|
| 262 | if (isset ($_REQUEST["form_request"]) && $_REQUEST["form_request"] == "login") { |
|---|
| 263 | $username = trim($_REQUEST['username']); |
|---|
| 264 | if (strpos($username, "@") === false) |
|---|
| 265 | $smarty->assign('username', $username); |
|---|
| 266 | else { |
|---|
| 267 | $email = $username; |
|---|
| 268 | $username = ""; |
|---|
| 269 | $smarty->assign('email', $email); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | // Set section of Smarty template |
|---|
| 274 | $smarty->assign('sectionTOOLCONTENT', true); |
|---|
| 275 | |
|---|
| 276 | // Compile HTML code |
|---|
| 277 | $html = $smarty->fetch("templates/sites/manual_useradd.tpl"); |
|---|
| 278 | |
|---|
| 279 | /* |
|---|
| 280 | * Main content |
|---|
| 281 | */ |
|---|
| 282 | |
|---|
| 283 | // Reset ALL smarty SWITCH values |
|---|
| 284 | $smarty->assign('sectionTOOLCONTENT', false); |
|---|
| 285 | $smarty->assign('sectionMAINCONTENT', false); |
|---|
| 286 | |
|---|
| 287 | // Set section of Smarty template |
|---|
| 288 | $smarty->assign('sectionMAINCONTENT', true); |
|---|
| 289 | |
|---|
| 290 | // Add the auth servers list to smarty variables |
|---|
| 291 | $sources = array (); |
|---|
| 292 | |
|---|
| 293 | // Preserve keys |
|---|
| 294 | $network_array = Network::getAllNetworks(); |
|---|
| 295 | |
|---|
| 296 | foreach ($network_array as $network) { |
|---|
| 297 | if ($network->getAuthenticator()->isRegistrationPermitted()) { |
|---|
| 298 | $sources[$network->getId()] = $network->getName(); |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | if (isset($sources)) { |
|---|
| 303 | $smarty->assign('auth_sources', $sources); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | // Pass the account_origin along, if it's set |
|---|
| 307 | if (isset($_REQUEST["auth_source"])) { |
|---|
| 308 | $smarty->assign('selected_auth_source', $_REQUEST["auth_source"]); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | $smarty->assign('SelectNetworkUI', Network::getSelectUI('auth_source')); |
|---|
| 312 | |
|---|
| 313 | // Compile HTML code |
|---|
| 314 | $html_body = $smarty->fetch("templates/sites/manual_useradd.tpl"); |
|---|
| 315 | |
|---|
| 316 | /* |
|---|
| 317 | * Render output |
|---|
| 318 | */ |
|---|
| 319 | $ui = MainUI::getObject(); |
|---|
| 320 | $ui->addContent('left_area_middle', $html); |
|---|
| 321 | $ui->addContent('main_area_middle', $html_body); |
|---|
| 322 | $ui->display(); |
|---|
| 323 | |
|---|
| 324 | /* |
|---|
| 325 | * Local variables: |
|---|
| 326 | * tab-width: 4 |
|---|
| 327 | * c-basic-offset: 4 |
|---|
| 328 | * c-hanging-comment-ender-p: nil |
|---|
| 329 | * End: |
|---|
| 330 | */ |
|---|
| 331 | |
|---|
| 332 | ?> |
|---|