Revision [6321]

This is an old revision of RegisterAction made by DarTar2 on 2005-02-25 16:35:13.

 

Register Action

Last edited by DarTar2:
RegisterAction v.0.2
Fri, 25 Feb 2005 16:35 UTC [diff]


See also:
Documentation: RegisterActionInfo.
This is the development page for the Register action.
 


I've started working on a new version of an action for user registration. The motivation behind this is to replace the current usersetting action with three distinct actions:


[2005-02-25] action uploaded on this site as a beta feature: RegisterActionTest

The action

Current version: 0.2

Done:

To do:

The code


Save the following as ./actions/register.php and use it as {{register}}.

  1. <?php
  2. /**
  3.  * Display a form for user registration.
  4.  *
  5.  * This action allows new users to register an account, if user registration is enabled.
  6.  * All the required fields are validated before the new user is created.
  7.  *
  8.  * @package     Actions
  9.  * @name        Register
  10.  *
  11.  * @author      {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
  12.  * @version     0.2
  13.  * @since       Wikka 1.1.X.X
  14.  * @output      form for user registration
  15.  *
  16.  * @todo
  17.  *          - CSS to style form;
  18.  *          - (optionally) drop WikiName restriction on usernames;
  19.  *          - use core functions to validate fields;
  20.  *          - use central error handler for printing error messages;
  21.  *          - decide best strategy to link hardcoded login/logout page;
  22.  *          - define welcome page where new users must be redirected;
  23.  *          - (optionally) add option for email-confirmation of registered users.
  24.  */
  25.  
  26. print $this->Format('===== Registration page =====');
  27.  
  28. if ($this->GetConfigValue("allow_new_users") == "0") {
  29.     // user registration is disabled
  30.     print $this->Format('//User registration is disabled on this wiki//');
  31. } else {
  32.     if ($user = $this->GetUser()){
  33.         // user is logged in
  34.    
  35.         // is this the first time the user is logged in?
  36.         if ($_GET['reg'] == 'ok') {
  37.             // first login welcome stuff
  38.             // print $this->Format('--- **Registration successful!** --- --- You are currently logged in as '.$this->GetUserName());
  39.  
  40.             // ...or forward
  41.             $this->Redirect($this->href('','WelcomeUser'));
  42.         } else {
  43.             // print user information
  44.             print $this->Format('--- You are currently logged in as '.$this->GetUserName());
  45.         }
  46.     } else {
  47.         // user is not logged in
  48.         print "<script type=\"text/javascript\"><!-- \nfunction hov(loc,cls){ \n    if(loc.className) loc.className=cls;\n}\n //-->\n</script>\n";
  49.    
  50.         // is user trying to register?
  51.         if ($_POST) {
  52.  
  53.             // get POST values
  54.             if ($_POST['name']) $name = trim($_POST['name']);
  55.             if ($_POST['email']) $email = trim($_POST['email']);
  56.             if ($_POST['password']) $password = $_POST['password'];
  57.             if ($_POST['confpassword']) $confpassword = $_POST['confpassword'];
  58.    
  59.             // validate fields
  60.             // note: all these validation checks should use core functions to preserve consistency
  61.             // todo: add icons on non-valid fields
  62.  
  63.             if ($this->LoadUser($name)) {
  64.                 $error = 'Sorry, this username already exists. Please choose a different name.';
  65.                 $validname = $this->Action('failed');
  66.             } elseif ($this->ExistsPage($name)) {
  67.                 $error = 'Sorry, this username is reserved for a page. Please choose a different name.';
  68.                 $validname = $this->Action('failed');
  69.             } elseif (!$this->IsWikiName($name)) {
  70.                 $error = 'Please fill in a valid username (formatted as a ##""WikiName""##).';
  71.                 $validname = $this->Action('failed');
  72.             } elseif (!isset($email)) {
  73.                 $error = 'Please specify an email address.';
  74.                 $validname = $this->Action('done');
  75.                 $validemail = $this->Action('failed');
  76.             } elseif (!preg_match("/^.+?\@.+?\..+$/", $email)) {
  77.                 $error = 'That does not quite look like an email address.';
  78.                 $validname = $this->Action('done');
  79.                 $validemail = $this->Action('failed');
  80.             } elseif (!isset($password)) {
  81.                 $error = 'Please choose your password.';
  82.                 $validname = $this->Action('done');
  83.                 $validemail = $this->Action('done');
  84.                 $validpassword = $this->Action('failed');
  85.             } elseif (strlen($password) < 5) {
  86.                 $error = 'Sorry, password too short.';
  87.                 $validname = $this->Action('done');
  88.                 $validemail = $this->Action('done');
  89.                 $validpassword = $this->Action('failed');
  90.             } elseif (preg_match("/ /", $password)) {
  91.                 $error = 'Sorry, spaces are not allowed in passwords.';
  92.                 $validname = $this->Action('done');
  93.                 $validemail = $this->Action('done');
  94.                 $validpassword = $this->Action('failed');
  95.             } elseif (!isset($confpassword)) {
  96.                 $error = 'You need to confirm your password.';
  97.                 $validname = $this->Action('done');
  98.                 $validemail = $this->Action('done');
  99.                 $validpassword = $this->Action('failed');
  100.                 $validconfpassword = $this->Action('failed');
  101.             } elseif ($confpassword != $password) {
  102.                 $error = 'Sorry, passwords do not match.';
  103.                 $validname = $this->Action('done');
  104.                 $validemail = $this->Action('done');
  105.                 $validpassword = $this->Action('failed');
  106.                 $validconfpassword = $this->Action('failed');
  107.             } else {
  108.                 // all required fields are valid and non-empty
  109.  
  110.                 // create user
  111.                 $this->Query("insert into ".$this->config["table_prefix"]."users set ".
  112.                     "signuptime = now(), ".
  113.                     "name = '".mysql_real_escape_string($name)."', ".
  114.                     "email = '".mysql_real_escape_string($email)."', ".
  115.                     "password = md5('".mysql_real_escape_string($_POST["password"])."')");
  116.  
  117.                 // log in
  118.                 $this->SetUser($this->LoadUser($name));
  119.    
  120.                 // forward
  121.                 $this->Redirect($this->href('','','reg=ok'));
  122.             }
  123.         }
  124.        
  125.         $intro = $this->Format(' --- If you are a **new user** you can register an account using this form (if you already have an account, please go to the [[UserSettings login page]]). --- --- To register, the following fields are required:
  126. ~-your **username** (it must be formatted like a ##""WikiName""##, for example: ##""JuliusCaesar""##);
  127. ~-a **valid email address** (this will only be used to retrieve your password in case you lose it);
  128. ~-a **valid password** (min. 5 characters, no space allowed).
  129. --- ---');
  130.  
  131.         // build registration form
  132.         $form  = $this->FormOpen();
  133.         $form .= '  <table summary="Form to provide registration data: username, email and password">';
  134.         $form .= '  <caption>Registration form</caption>';
  135.         $form .= '  <tbody>';
  136.    
  137.         if (isset($error)) {
  138.             $form .= '<tr><td colspan="3" align="center"><span class="error">'.$this->Format($error).'</span></td></tr>';
  139.         }
  140.         $form .= '      <tr>';
  141.         $form .= '          <th align="right" scope="row"><label for="name">Your username:</label></th>';
  142.         $form .= '          <td><input name="name" id="name" size="40" value="'.$name.'" title="Choose a valid username (formatted as a WikiName)" /></td>';
  143.         $form .= '          <td>'.$validname.'</td>';
  144.         $form .= '      </tr>';
  145.         $form .= '      <tr>';
  146.         $form .= '          <th align="right" scope="row"><label for="email">Your email address:</label></th>';
  147.         $form .= '          <td><input name="email" id="email" size="40" value="'.$email.'" title="Fill in a valid email address"/></td>';
  148.         $form .= '          <td align="left">'.$validemail.'</td>';
  149.         $form .= '      </tr>';
  150.         $form .= '      <tr>';
  151.         $form .= '          <th align="right" scope="row"><label for="password">Your password:</label></th>';
  152.         $form .= '          <td><input type="password" name="password" id="password" size="40" title="Choose a valid password (min. 5 chars, no space)" /></td>';
  153.         $form .= '          <td align="left">'.$validpassword.'</td>';
  154.         $form .= '      </tr>';
  155.         $form .= '      <tr>';
  156.         $form .= '          <th align="right" scope="row"><label for="confpassword">Confirm password:</label></th>';
  157.         $form .= '          <td><input type="password" name="confpassword" id="confpassword" size="40" title="Type again your password for confirmation" /></td>';
  158.         $form .= '          <td align="left">'.$validconfpassword.'</td>';
  159.         $form .= '      </tr>';
  160.         $form .= '      <tr>';
  161.         $form .= '          <td></td>';
  162.         $form .= '          <td><input type="submit" value="Register" size="40" title="Register" /></td>';
  163.         $form .= '      </tr>';
  164.         $form .= '  </tbody>';
  165.         $form .= '  </table>';
  166.         $form .= $this->FormClose();
  167.  
  168.         // output intro and form
  169.         print $intro.$form;
  170.     }
  171. }
  172. ?>



Implemented modifications






CategoryDevelopment
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki