Changeset 1401

Show
Ignore:
Timestamp:
07/03/09 12:21:22 (14 months 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)

Location:
trunk/wifidog-auth
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/CHANGELOG

    r1399 r1401  
    11# $Id$ 
     22009-07-03 Benoit Grégoire <benoitg@coeus.ca> 
     3        * 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) 
     4 
    252009-06-26 Benoit Grégoire <benoitg@coeus.ca> 
    36        * Updated greek translation by bill@e-texnologia.gr and strzol@gmail.com, still needs some work. 
  • trunk/wifidog-auth/wifidog/classes/Network.php

    r1393 r1401  
    243243     *                                    objects to select 
    244244     *  $userData['allowEmpty'] boolean Allow not selecting any object 
     245     *  $userData['onlyNetwoksAllowingSignup'] boolean Only list networks allowing user self-signup 
    245246     * @return string HTML markup 
    246247 
     
    257258        !empty($userData['nullCaptionString'])?$nullCaptionString=$userData['nullCaptionString']:$nullCaptionString=null; 
    258259        !empty($userData['onChange'])?$onChangeString=$userData['onChange']:$onChangeString=""; 
     260        !empty($userData['onlyNetwoksAllowingSignup'])?$onlyNetwoksAllowingSignup=$userData['onlyNetwoksAllowingSignup']:$onlyNetwoksAllowingSignup=false; 
    259261 
    260262        $db = AbstractDb::getObject(); 
     
    270272            $i = 0; 
    271273            foreach ($network_rows as $network_row) { 
    272                 $tab[$i][0] = $network_row['network_id']; 
    273                 $tab[$i][1] = $network_row['name']; 
    274                 $i ++; 
     274                 
     275                if($onlyNetwoksAllowingSignup==false || (self::getObject($network_row['network_id'])->getAuthenticator()->isRegistrationPermitted())==true){ 
     276                    $tab[$i][0] = $network_row['network_id']; 
     277                    $tab[$i][1] = $network_row['name']; 
     278                    $i ++; 
     279                } 
    275280            } 
    276281            $html .= _("Network:")." \n"; 
     
    371376        $network_id_str = $db->escapeString($p_network_id); 
    372377        if ($network_id_str == "") 
    373             $network_id_str = $db->escapeString(self::getDefaultNetwork()->getId()); 
     378        $network_id_str = $db->escapeString(self::getDefaultNetwork()->getId()); 
    374379 
    375380        $sql = "SELECT *, EXTRACT(EPOCH FROM validation_grace_time) as validation_grace_time_seconds FROM networks WHERE network_id='$network_id_str'"; 
     
    14231428    } 
    14241429 
    1425       /**  
    1426       * Are nodes allowed to redirect users to the original requested web page instead of  
    1427       * the portal?  
    1428       *  
    1429       * @return bool True or false  
    1430       *  
    1431       * @access public  
    1432       */  
    1433      public function getPortalOriginalUrlAllowed()  
    1434      {  
    1435          return (($this->_row['allow_original_url_redirect'] == 't') ? true : false);  
    1436      }  
    1437    
    1438      /**  
    1439       * Set if nodes are allowed to redirect users to the original requested web page  
    1440       * instead of the portal?  
    1441       *  
    1442       * @param bool $value The new value, true or false  
    1443       *  
    1444       * @return bool True on success, false on failure  
    1445       *  
    1446       * @access public  
    1447       */  
    1448      public function setPortalOriginalUrlAllowed($value)  
    1449      {  
    1450          // Init values  
    1451          $retval = true;  
    1452    
    1453          if ($value != $this->getPortalOriginalUrlAllowed()) {  
    1454              $db = AbstractDb::getObject();  
    1455              $value ? $value = 'TRUE' : $value = 'FALSE';  
    1456              $retval = $db->execSqlUpdate("UPDATE networks SET allow_original_url_redirect = {$value} WHERE network_id = '{$this->getId()}'", false);  
    1457              $this->refresh();  
    1458          }  
    1459    
    1460          return $retval;  
    1461      }  
     1430    /** 
     1431     * Are nodes allowed to redirect users to the original requested web page instead of 
     1432     * the portal? 
     1433     * 
     1434     * @return bool True or false 
     1435     * 
     1436     * @access public 
     1437     */ 
     1438    public function getPortalOriginalUrlAllowed() 
     1439    { 
     1440        return (($this->_row['allow_original_url_redirect'] == 't') ? true : false); 
     1441    } 
     1442 
     1443    /** 
     1444     * Set if nodes are allowed to redirect users to the original requested web page 
     1445     * instead of the portal? 
     1446     * 
     1447     * @param bool $value The new value, true or false 
     1448     * 
     1449     * @return bool True on success, false on failure 
     1450     * 
     1451     * @access public 
     1452     */ 
     1453    public function setPortalOriginalUrlAllowed($value) 
     1454    { 
     1455        // Init values 
     1456        $retval = true; 
     1457 
     1458        if ($value != $this->getPortalOriginalUrlAllowed()) { 
     1459            $db = AbstractDb::getObject(); 
     1460            $value ? $value = 'TRUE' : $value = 'FALSE'; 
     1461            $retval = $db->execSqlUpdate("UPDATE networks SET allow_original_url_redirect = {$value} WHERE network_id = '{$this->getId()}'", false); 
     1462            $this->refresh(); 
     1463        } 
     1464 
     1465        return $retval; 
     1466    } 
    14621467 
    14631468 
  • 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 ?>