Revision [6318]

This is an old revision of RegisterAction made by DarTar on 2005-02-25 16:11:58.

 

Register Action

Last edited by DarTar:
Register action v.0.2
Fri, 25 Feb 2005 16:11 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:



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.         } else {
  40.             // print user information
  41.             print $this->Format('--- You are currently logged in as '.$this->GetUserName());
  42.         }
  43.     } else {
  44.         // user is not logged in
  45.         print "<script type=\"text/javascript\"><!-- \nfunction hov(loc,cls){ \n    if(loc.className) loc.className=cls;\n}\n //-->\n</script>\n";
  46.    
  47.         // is user trying to register?
  48.         if ($_POST) {
  49.  
  50.             // get POST values
  51.             if ($_POST['name']) $name = trim($_POST['name']);
  52.             if ($_POST['email']) $email = trim($_POST['email']);
  53.             if ($_POST['password']) $password = $_POST['password'];
  54.             if ($_POST['confpassword']) $confpassword = $_POST['confpassword'];
  55.    
  56.             // validate fields
  57.             // note: all these validation checks should use core functions to preserve consistency
  58.             // todo: add icons on non-valid fields
  59.  
  60.             if ($this->LoadUser($name)) {
  61.                 $error = 'Sorry, this username already exists. Please choose a different name.';
  62.                 $validname = $this->Action('failed');
  63.             } elseif ($this->ExistsPage($name)) {
  64.                 $error = 'Sorry, this username is reserved for a page. Please choose a different name.';
  65.                 $validname = $this->Action('failed');
  66.             } elseif (!$this->IsWikiName($name)) {
  67.                 $error = 'Please fill in a valid username (formatted as a ##""WikiName""##).';
  68.                 $validname = $this->Action('failed');
  69.             } elseif (!isset($email)) {
  70.                 $error = 'Please specify an email address.';
  71.                 $validname = $this->Action('done');
  72.                 $validemail = $this->Action('failed');
  73.             } elseif (!preg_match("/^.+?\@.+?\..+$/", $email)) {
  74.                 $error = 'That does not quite look like an email address.';
  75.                 $validname = $this->Action('done');
  76.                 $validemail = $this->Action('failed');
  77.             } elseif (!isset($password)) {
  78.                 $error = 'Please choose your password.';
  79.                 $validname = $this->Action('done');
  80.                 $validemail = $this->Action('done');
  81.                 $validpassword = $this->Action('failed');
  82.             } elseif (strlen($password) < 5) {
  83.                 $error = 'Sorry, password too short.';
  84.                 $validname = $this->Action('done');
  85.                 $validemail = $this->Action('done');
  86.                 $validpassword = $this->Action('failed');
  87.             } elseif (preg_match("/ /", $password)) {
  88.                 $error = 'Sorry, spaces are not allowed in passwords.';
  89.                 $validname = $this->Action('done');
  90.                 $validemail = $this->Action('done');
  91.                 $validpassword = $this->Action('failed');
  92.             } elseif (!isset($confpassword)) {
  93.                 $error = 'You need to confirm your password.';
  94.                 $validname = $this->Action('done');
  95.                 $validemail = $this->Action('done');
  96.                 $validpassword = $this->Action('failed');
  97.                 $validconfpassword = $this->Action('failed');
  98.             } elseif ($confpassword != $password) {
  99.                 $error = 'Sorry, passwords do not match.';
  100.                 $validname = $this->Action('done');
  101.                 $validemail = $this->Action('done');
  102.                 $validpassword = $this->Action('failed');
  103.                 $validconfpassword = $this->Action('failed');
  104.             } else {
  105.                 // all required fields are valid and non-empty
  106.  
  107.                 // create user
  108.                 $this->Query("insert into ".$this->config["table_prefix"]."users set ".
  109.                     "signuptime = now(), ".
  110.                     "name = '".mysql_real_escape_string($name)."', ".
  111.                     "email = '".mysql_real_escape_string($email)."', ".
  112.                     "password = md5('".mysql_real_escape_string($_POST["password"])."')");
  113.  
  114.                 // log in
  115.                 $this->SetUser($this->LoadUser($name));
  116.    
  117.                 // forward
  118.                 $this->Redirect($this->href('','','reg=ok'));
  119.             }
  120.         }
  121.        
  122.         $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:
  123. ~-your **username** (it must be formatted like a ##""WikiName""##, for example: ##""JuliusCaesar""##);
  124. ~-a **valid email address** (this will only be used to retrieve your password in case you lose it);
  125. ~-a **valid password** (min. 5 characters, no space allowed).
  126. --- ---');
  127.  
  128.         // build registration form
  129.         $form  = $this->FormOpen();
  130.         $form .= '  <table summary="Form to provide registration data: username, email and password">';
  131.         $form .= '  <caption>Registration form</caption>';
  132.         $form .= '  <tbody>';
  133.    
  134.         if (isset($error)) {
  135.             $form .= '<tr><td colspan="3" align="center"><span class="error">'.$this->Format($error).'</span></td></tr>';
  136.         }
  137.         $form .= '      <tr>';
  138.         $form .= '          <th align="right" scope="row"><label for="name">Your username:</label></th>';
  139.         $form .= '          <td><input name="name" id="name" size="40" value="'.$name.'" title="Choose a valid username (formatted as a WikiName)" /></td>';
  140.         $form .= '          <td>'.$validname.'</td>';
  141.         $form .= '      </tr>';
  142.         $form .= '      <tr>';
  143.         $form .= '          <th align="right" scope="row"><label for="email">Your email address:</label></th>';
  144.         $form .= '          <td><input name="email" id="email" size="40" value="'.$email.'" title="Fill in a valid email address"/></td>';
  145.         $form .= '          <td align="left">'.$validemail.'</td>';
  146.         $form .= '      </tr>';
  147.         $form .= '      <tr>';
  148.         $form .= '          <th align="right" scope="row"><label for="password">Your password:</label></th>';
  149.         $form .= '          <td><input type="password" name="password" id="password" size="40" title="Choose a valid password (min. 5 chars, no space)" /></td>';
  150.         $form .= '          <td align="left">'.$validpassword.'</td>';
  151.         $form .= '      </tr>';
  152.         $form .= '      <tr>';
  153.         $form .= '          <th align="right" scope="row"><label for="confpassword">Confirm password:</label></th>';
  154.         $form .= '          <td><input type="password" name="confpassword" id="confpassword" size="40" title="Type again your password for confirmation" /></td>';
  155.         $form .= '          <td align="left">'.$validconfpassword.'</td>';
  156.         $form .= '      </tr>';
  157.         $form .= '      <tr>';
  158.         $form .= '          <td></td>';
  159.         $form .= '          <td><input type="submit" value="Register" size="40" title="Register" /></td>';
  160.         $form .= '      </tr>';
  161.         $form .= '  </tbody>';
  162.         $form .= '  </table>';
  163.         $form .= $this->FormClose();
  164.  
  165.         // output intro and form
  166.         print $intro.$form;
  167.     }
  168. }
  169. ?>



Implemented modifications






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