<?php
/**
* Display a form for user registration.
*
* This action allows new users to register an account, if user registration is enabled.
* All the required fields are validated before the new user is created.
*
* @package Actions
* @name Register
*
* @author {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
* @version 0.3
* @since Wikka 1.1.X.X
* @output form for user registration
*
* @todo
* - CSS to style form;
* - (optionally) drop WikiName restriction on usernames;
* - use core functions to validate fields;
* - use central error handler for printing error messages;
* - decide best strategy to link hardcoded login/logout page;
* - define welcome page where new users must be redirected;
* - (optionally) add option for email-confirmation of registered users.
*/
// constants
define('MIN_PASSW_LENGTH',
'5');
define('DEFAULT_REDIRECT_TO',
'WelcomeUser');
print $this->
Format('===== Registration page =====');
if ($this->GetConfigValue('allow_new_users') == '0')
{
// user registration is disabled
print $this->
Format('//User registration is disabled on this wiki//');
} else
{
if ($user = $this->GetUser())
{
// user is logged in
// initializing variables
$name = '';
$email = '';
$password = '';
$confpassword = '';
$error = '';
// is this the first time the user logs in?
if ((isset($_GET['reg'])) &&
($_GET['reg'] ==
'1'))
{
switch ($this->GetConfigValue('allow_new_users'))
{
default:
case 0:
// print first login welcome screen
print $this->
Format('--- **Registration successful!** --- --- You are currently logged in as '.
$this->
GetUserName());
break;
case 1:
// redirect to welcome page
$this->Redirect($this->href('', DEFAULT_REDIRECT_TO));
break;
case 2:
// redirect to referrer page
$this->Redirect($this->href('', DEFAULT_REDIRECT_TO));
break;
}
} else
{
// user is already logged in: print user information
print $this->
Format('--- You are currently logged in as '.
$this->
GetUserName());
}
} else
{
// user is not logged in
// is user trying to register?
if ($_POST)
{
// get POST values
if (isset($_POST['name'])) $name =
trim($_POST['name']);
if (isset($_POST['email'])) $email =
trim($_POST['email']);
if (isset($_POST['password'])) $password =
$_POST['password'];
if (isset($_POST['confpassword'])) $confpassword =
$_POST['confpassword'];
// validate fields
// note: all these validation checks should use core functions to preserve consistency
if ($this->LoadUser($name))
{
$error = 'Sorry, this username already exists. Please choose a different name.';
$validname = $this->Action('failed');
} elseif ($this->ExistsPage($name))
{
$error = 'Sorry, this username is reserved for a page. Please choose a different name.';
$validname = $this->Action('failed');
} elseif (!$this->IsWikiName($name))
{
$error = 'Please fill in a valid username (formatted as a ##""WikiName""##).';
$validname = $this->Action('failed');
} elseif (!$email)
{
$error = 'Please specify an email address.';
$validname = $this->Action('done');
$validemail = $this->Action('failed');
} elseif (!
preg_match("/^.+?\@.+?\..+$/",
$email))
{
$error = 'That does not quite look like an email address.';
$validname = $this->Action('done');
$validemail = $this->Action('failed');
} elseif (!$password)
{
$error = 'Please choose your password.';
$validname = $this->Action('done');
$validemail = $this->Action('done');
$validpassword = $this->Action('failed');
} elseif (strlen($password) < MIN_PASSW_LENGTH
)
{
$error = 'Sorry, password too short (min. '.MIN_PASSW_LENGTH.' chars).';
$validname = $this->Action('done');
$validemail = $this->Action('done');
$validpassword = $this->Action('failed');
$error = 'Sorry, spaces are not allowed in passwords.';
$validname = $this->Action('done');
$validemail = $this->Action('done');
$validpassword = $this->Action('failed');
} elseif (!$confpassword)
{
$error = 'You need to confirm your password.';
$validname = $this->Action('done');
$validemail = $this->Action('done');
$validpassword = $this->Action('failed');
$validconfpassword = $this->Action('failed');
} elseif ($confpassword != $password)
{
$error = 'Sorry, passwords do not match.';
$validname = $this->Action('done');
$validemail = $this->Action('done');
$validpassword = $this->Action('failed');
$validconfpassword = $this->Action('failed');
} else
{
// all required fields are valid and non-empty
// create user
$this->Query("insert into ".$this->config["table_prefix"]."users set ".
"signuptime = now(), ".
"name = '".mysql_real_escape_string($name)."', ".
"email = '".mysql_real_escape_string($email)."', ".
"password = md5('".mysql_real_escape_string($password)."')");
// log in
$this->SetUser($this->LoadUser($name));
// forward
$this->Redirect($this->href('','','reg=1'));
}
}
$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:
~-your **username** (it must be formatted like a ##""WikiName""##, for example: ##""JuliusCaesar""##);
~-a **valid email address** (this will only be used to retrieve your password in case you lose it);
~-a **valid password** (min. '.MIN_PASSW_LENGTH.' characters, no space allowed).
--- ---');
// build registration form
$form = $this->FormOpen();
$form .= ' <table summary="Form to provide registration data: username, email and password">';
$form .= ' <caption>Registration form</caption>';
$form .= ' <tbody>';
{
$form .= '<tr><td colspan="3" align="center"><span class="error">'.$this->Format($error).'</span></td></tr>';
}
$form .= ' <tr>';
$form .= ' <th align="right" scope="row"><label for="name">Your username:</label></th>';
$form .= ' <td><input name="name" id="name" size="40" value="'.$name.'" title="Choose a valid username (formatted as a WikiName)" /></td>';
$form .= ' <td>'.$validname.'</td>';
$form .= ' </tr>';
$form .= ' <tr>';
$form .= ' <th align="right" scope="row"><label for="email">Your email address:</label></th>';
$form .= ' <td><input name="email" id="email" size="40" value="'.$email.'" title="Fill in a valid email address"/></td>';
$form .= ' <td align="left">'.$validemail.'</td>';
$form .= ' </tr>';
$form .= ' <tr>';
$form .= ' <th align="right" scope="row"><label for="password">Your password:</label></th>';
$form .= ' <td><input type="password" name="password" id="password" size="40" title="Choose a valid password (min. '.MIN_PASSW_LENGTH.' chars, no space)" /></td>';
$form .= ' <td align="left">'.$validpassword.'</td>';
$form .= ' </tr>';
$form .= ' <tr>';
$form .= ' <th align="right" scope="row"><label for="confpassword">Confirm password:</label></th>';
$form .= ' <td><input type="password" name="confpassword" id="confpassword" size="40" title="Type again your password for confirmation" /></td>';
$form .= ' <td align="left">'.$validconfpassword.'</td>';
$form .= ' </tr>';
$form .= ' <tr>';
$form .= ' <td></td>';
$form .= ' <td><input type="submit" value="Register" title="Register" /></td>';
$form .= ' </tr>';
$form .= ' </tbody>';
$form .= ' </table>';
$form .= $this->FormClose();
// output intro and form
}
}
?>