Show SignupController.php syntax highlighted
<?php
/**
* Astrum Futura: Open Source Space Strategy Game
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@astrumfutura.com so we can send you a copy immediately.
*
* @category Astrum
* @package Astrum_Controller
* @copyright Copyright (c) 2006 Pádraic Brady (http://blog.quantum-star.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @version $Id$
*/
/**
* Signup Controller
*
* @category Astrum
* @package Astrum_Controller
* @copyright Copyright (c) 2006 Pádraic Brady (http://blog.quantum-star.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
*/
class SignupController extends Astrum_Controller_Action
{
/**
* Regex allowing a string containing alphanumeric characters,
* spaces and underscores. String must start with an alnum
* character.
*/
const REGEX_USERNAME = '/^[[:alnum:]\ \_]/';
/**
* Regex for email address format compatible with RFC 822
*/
const REGEX_EMAIL = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/';
public function indexAction()
{
$this->getResponse()->appendBody(
$this->_view->render('signup_index.tpl.html')
);
}
/**
* Process the form data for a new account.
*
* @access public
* @todo Utilise feyd's SHA256 class for hashing passwords
*/
public function processAction()
{
/*
* Check request type, and redisplay form if not POST
*/
if($this->getRequest()->getMethod() !== 'POST' || !isset($this->_post))
{
$this->_forward('signup', 'index');
return;
}
/*
* Get filtered input data or else exit current action
* The associated errorHandler will have setup
* additional instructions for handling bad input
*/
if(!$clean = $this->validateProcessInput())
{
return;
}
/*
* Process the signup with clean input data
*/
require_once 'Astrum/User.php';
$user = new Astrum_User;
$user->name = $clean['user'];
$user->password = sha1($clean['pass']);
$user->email = $clean['email'];
$user->joined = time();
$user->hashcode = sha1(uniqid(rand(), true));
$user->save();
/*
* Display confirmation
*/
$this->_view->user = $user->asArray();
$this->getResponse()->appendBody(
$this->_view->render('signup_process.tpl.html')
);
}
/**
* Validate input data passed to processAction method.
*
* @access private
* @return mixed Boolean false if invalid data, or clean input array
*/
private function validateProcessInput()
{
$clean = array();
/*
* Validate all expected input values
*/
if(!$clean['user'] = $this->_post->testRegex('astrum_form_signup_user', self::REGEX_USERNAME))
{
return $this->handleError('astrum_form_signup_user');
}
if(strlen($this->_post->getRaw('astrum_form_signup_pass1')) < 5)
{
return $this->handleError('astrum_form_signup_pass1');
}
$clean['pass'] = $this->_post->getRaw('astrum_form_signup_pass1');
if($this->_post->getRaw('astrum_form_signup_pass1') !== $this->_post->getRaw('astrum_form_signup_pass2'))
{
return $this->handleError('astrum_form_signup_pass2');
}
if(!$clean['email'] = $this->_post->testRegex('astrum_form_signup_email1', self::REGEX_EMAIL))
{
return $this->handleError('astrum_form_signup_email1');
}
if($this->_post->getRaw('astrum_form_signup_email1') !== $this->_post->getRaw('astrum_form_signup_email2'))
{
return $this->handleError('astrum_form_signup_email2');
}
return $clean;
}
/**
* Handle errors from validateProcessInput method in POST
* Currently appends an error message to Response, and
* forward to the indexAction
*
* @access private
* @return bool Returns false after setting forward action
*/
private function handleError($type)
{
$this->getResponse()->appendBody('<strong>' . $type . ' field is invalid</strong><br/>');
$this->_forward('signup', 'index');
return false;
}
}
See more files for this project here