Show
Ignore:
Timestamp:
07/03/09 12:21:22 (3 years ago)
Author:
benoitg
Message:

* Refactor signup.php. The code flow is still annoyingly unclear complex, but the file is shorter and hopefully easier to debug (and hopefully fixes #593)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/wifidog/signup.php

    r1399 r1401  
    4242 * @author     Max Horváth <max.horvath@freenet.de> 
    4343 * @copyright  2004-2006 Philippe April 
    44  * @copyright  2004-2006 Benoit Grégoire, Technologies Coeus inc. 
     44 * @copyright  2004-2009 Benoit Grégoire, Technologies Coeus inc. 
    4545 * @copyright  2006 Max Horváth, Horvath Web Consulting 
    4646 * @version    Subversion $Id$ 
     
    147147 * Process signing up 
    148148 */ 
    149  
    150 // Init ALL smarty SWITCH values 
    151 $smarty->assign('sectionTOOLCONTENT', false); 
    152 $smarty->assign('sectionMAINCONTENT', false); 
    153149 
    154150// Init ALL smarty values 
     
    160156$smarty->assign('SelectNetworkUI', ""); 
    161157 
     158/* Signup was requested */ 
    162159if (isset ($_REQUEST["form_request"]) && $_REQUEST["form_request"] == "signup") { 
    163     // Secure entered values 
     160    // Sanitize user-entered values 
    164161    $username = trim($_REQUEST['username']); 
    165162    $email = trim($_REQUEST['email']); 
     
    170167    $smarty->assign('email', $email); 
    171168 
    172     $network = Network::getObject($_REQUEST['auth_source']); 
     169    $selectedNetwork = Network::getObject($_REQUEST['auth_source']); 
    173170 
    174171    try { 
     
    177174         */ 
    178175 
    179         // Set section of Smarty template 
     176        // Retrieve the TOOLCONTENT section of the Smarty template 
    180177        $smarty->assign('sectionTOOLCONTENT', true); 
    181  
    182         // Compile HTML code 
    183         $html = $smarty->fetch("templates/sites/signup.tpl"); 
     178        $smarty->assign('sectionMAINCONTENT', false); 
     179        $htmlToolContent = $smarty->fetch("templates/sites/signup.tpl"); 
    184180 
    185181        /* 
     
    187183         */ 
    188184 
    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)) { 
     185        if (!isset($selectedNetwork)) { 
    197186            throw new Exception(_("Sorry, this network does not exist !")); 
    198187        } 
    199188 
    200         if (!$network->getAuthenticator()->isRegistrationPermitted()) { 
     189        if (!$selectedNetwork->getAuthenticator()->isRegistrationPermitted()) { 
    201190            throw new Exception(_("Sorry, this network does not accept new user registration !")); 
    202191        } 
     
    208197 
    209198        // Check if user exists 
    210         if (User::getUserByUsernameAndOrigin($username, $network)) { 
     199        if (User::getUserByUsernameAndOrigin($username, $selectedNetwork)) { 
    211200            throw new Exception(_("Sorry, a user account is already associated to this username. Pick another one.")); 
    212201        } 
    213202 
    214         if (User::getUserByEmailAndOrigin($email, $network)) { 
     203        if (User::getUserByEmailAndOrigin($email, $selectedNetwork)) { 
    215204            throw new Exception(_("Sorry, a user account is already associated to this email address.")); 
    216205        } 
    217206 
    218207        // Create user and send him the validation email 
    219         $created_user = User::createUser(get_guid(), $username, $network, $email, $password); 
     208        $created_user = User::createUser(get_guid(), $username, $selectedNetwork, $email, $password); 
    220209        $created_user->sendValidationEmail(); 
    221210 
    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(); 
     211        // Authenticate this new user automatically 
     212        $errmsg = ""; 
     213        $authenticated_user = $selectedNetwork->getAuthenticator()->login($username, $password, $errmsg); 
     214 
     215        // While in validation period, alert user that he should validate his account ASAP 
     216        $validationMsgHtml = "<div id='warning_message_area'>\n"; 
     217        $validationMsgHtml .= _("An email with confirmation instructions was sent to your email address."); 
     218        $validationMsgHtml .= sprintf(_("Your account has been granted %s minutes of access to retrieve your email and validate your account."), ($selectedNetwork->getValidationGraceTime() / 60)); 
     219        $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.'); 
     220        $validationMsgHtml .= "</div>\n"; 
     221 
     222        // If the user is at a REAL hotspot, login the user and give him his sign-up minutes right away 
     223        $session = Session::getObject(); 
    235224        $gw_id = $session->get(SESS_GW_ID_VAR); 
    236225        $gw_address = $session->get(SESS_GW_ADDRESS_VAR); 
     
    247236            } 
    248237 
    249                         MainUI::redirect($redirURL, 0); 
    250         } 
    251  
     238            MainUI::redirect($redirURL, 0); 
     239        } 
     240        // Set section of Smarty template 
     241        $smarty->assign('sectionTOOLCONTENT', false); 
     242        $smarty->assign('sectionMAINCONTENT', true); 
    252243        // Compile HTML code 
    253         $html_body = $smarty->fetch("templates/sites/signup.tpl"); 
     244        $htmlMainContent = $smarty->fetch("templates/sites/signup.tpl"); 
    254245 
    255246        /* 
    256          * Render output 
     247         * Render output (siggess message) 
    257248         */ 
    258249        $ui = MainUI::getObject(); 
    259250 
    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); 
     251        $ui->addContent('left_area_middle', $htmlToolContent); 
     252        $ui->addContent('main_area_middle', $htmlMainContent); 
     253 
     254        // $ui->addContent('page_header', $validationMsgHtml); 
     255        $ui->addContent('main_area_top', $validationMsgHtml); 
    265256 
    266257        $ui->display(); 
     
    269260        exit; 
    270261    } 
    271  
    272262    catch (Exception $e) { 
    273263        $smarty->assign('error', $e->getMessage()); 
    274264 
    275265        // 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); 
     266        $htmlToolContent = ""; 
     267        $htmlMainContent = ""; 
    282268    } 
    283269} 
     
    287273 */ 
    288274 
    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  
    300275// Set section of Smarty template 
    301276$smarty->assign('sectionTOOLCONTENT', true); 
     277$smarty->assign('sectionMAINCONTENT', false); 
    302278 
    303279// Compile HTML code 
    304 $html = $smarty->fetch("templates/sites/signup.tpl"); 
     280$htmlToolContent = $smarty->fetch("templates/sites/signup.tpl"); 
    305281 
    306282/* 
     
    308284 */ 
    309285 
    310 // Reset ALL smarty SWITCH values 
     286// Use the account_origin along, if it was set (it may be set in case there was an error processing the form). 
     287if (isset($_REQUEST["auth_source"])) { 
     288    $selectedNetwork = Network::getObject($_REQUEST['auth_source']); 
     289} 
     290else { 
     291    $selectedNetwork = Network::getDefaultNetwork(); 
     292} 
     293 
     294if (Server::getServer()->getUseGlobalUserAccounts()){ 
     295    $smarty->assign('SelectNetworkUI', "<input type=\"hidden\" name=\"auth_source\" value='".$selectedNetwork->getId()."' />"); 
     296} 
     297else { 
     298    //Make sure to only list networks whose authenticator allows user self-signup 
     299    $smarty->assign('SelectNetworkUI', Network::getSelectUI('auth_source', array('preSelectedObject' => $selectedNetwork, 'onlyNetwoksAllowingSignup' => true)) ); 
     300} 
     301 
     302// Set section of Smarty template 
    311303$smarty->assign('sectionTOOLCONTENT', false); 
    312 $smarty->assign('sectionMAINCONTENT', false); 
    313  
    314 // Set section of Smarty template 
    315304$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 $default_network = Network::getDefaultNetwork(); 
    323  
    324 foreach ($network_array as $networkObject) { 
    325     if ($networkObject->getAuthenticator()->isRegistrationPermitted()) { 
    326         $sources[$networkObject->getId()] = $networkObject->getName(); 
    327     } 
    328 } 
    329  
    330 if (isset($sources)) { 
    331     $smarty->assign('auth_sources', $sources); 
    332 } 
    333  
    334 // Pass the account_origin along, if it's set 
    335 if (isset($_REQUEST["auth_source"])) { 
    336     $smarty->assign('selected_auth_source', $_REQUEST["auth_source"]); 
    337 } 
    338  
    339 if (Server::getServer()->getUseGlobalUserAccounts()) 
    340         $smarty->assign('SelectNetworkUI', "<input type=\"hidden\" name=\"auth_source\" value='".$default_network->getId()."' />"); 
    341 else 
    342         $smarty->assign('SelectNetworkUI', Network::getSelectUI('auth_source', array('preSelectedObject' => $network)) ); 
    343  
    344305// Compile HTML code 
    345 $html_body = $smarty->fetch("templates/sites/signup.tpl"); 
     306$htmlMainContent = $smarty->fetch("templates/sites/signup.tpl"); 
    346307 
    347308/* 
    348  * Render output 
     309 * Render final output 
    349310 */ 
    350311$ui = MainUI::getObject(); 
    351 $ui->addContent('left_area_middle', $html); 
    352 $ui->addContent('main_area_middle', $html_body); 
     312$ui->addContent('left_area_middle', $htmlToolContent); 
     313$ui->addContent('main_area_middle', $htmlMainContent); 
    353314$ui->display(); 
    354315 
     
    360321 * End: 
    361322 */ 
    362  
    363 ?>