Revision [6326]

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

 

Register Action

Last edited by DarTar:
Adding links
Fri, 25 Feb 2005 16:46 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 (you'll need to logout to test it)

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



Implemented modifications





Much better... a few more comments:
  1. The variables are still not being initialized. If a user does not provide a value when submitting the form, the variable won't be set - and then you're trying to use the unset variable(s) as parameters to functions and values for form fields. Try not excluding E_NOTICE in php's error reporting and submit an empty form - and see what you get...
  1. What's the mysterious JavaScript for? Do we even need it?
  1. I don't think the submit button can do anything with a size attribute (missed that the first time)
--JavaWoman


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