Created
March 13, 2012 20:13
-
-
Save Medalink/2031263 to your computer and use it in GitHub Desktop.
Register & Email validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Validate registration data for our system | |
* | |
* @param array | |
* @param boolean | |
* @return Redirect with array | |
*/ | |
public function post_register( $data = NULL, $email_confirm = true ) { | |
if( !empty( $data ) ) // Directly set the data so we can use it as normal | |
Input::$input = $data; | |
else // If this is a non-system call require CSRF | |
$this->filter( 'before', 'csrf' ); | |
$rules = array( | |
'first_name' => 'required|alpha|max:80', | |
'last_name' => 'required|alpha|max:120', | |
'gender' => 'required', | |
'email' => 'required|email|unique:users', | |
); | |
// Handle Facebook accounts a little different | |
if( ! Input::get( 'is_facebook_account' ) ) | |
$rules['password'] = 'required|confirmed|min:6'; | |
// Validate all input | |
$validator = Validator::make( Input::all(), $rules ); | |
// Send them back with errors | |
if( ! $validator->valid() ) | |
return Redirect::to( 'home' ) | |
->with( 'errors', $validator->errors ) | |
->with_input( 'except', array( 'password', 'password_confirm' ) ); // Add errors to the view | |
// Set and clean up some input | |
$user_data = Input::all(); | |
$user_data['name'] = $user_data['first_name'] . ' ' . $user_data['last_name']; | |
$user_data['is_valid_email'] = 0; | |
if( ! Input::get( 'is_facebook_account' ) ) | |
$user_data['password'] = Hash::make( Input::get( 'password' ) ); | |
// Handle dates. | |
$user_data['birthday'] = date( 'Y-m-d H:i:s', strtotime( $user_data['birthday'] ) ); | |
// Save the data | |
$user = new User( $user_data ); | |
$user->save(); | |
$ev = new Evalid(); | |
// Handle email confirmation | |
if( $email_confirm ) | |
{ | |
$code = sha1( Str::random( 40 ) ); | |
$ev->email = $user->email; | |
$ev->user_id = $user->id; | |
$ev->code = $code; | |
$ev->save(); | |
$mailer = IoC::resolve( 'mailer' ); | |
$message = Swift_Message::newInstance( 'Welcome to example.com' ) | |
->setFrom( array( Config::get( 'email.from' ) => Config::get( 'email.name' ) ) ) | |
->setTo( array( $user_data['email'] => $name ) ) | |
->setBody( | |
'<html>' . | |
'<body>' . | |
'<h2>Welcome to example.com</h2>' . | |
'<p>You need to confirm your e-mail in order to participate.</p>' . | |
'<p>Please <a href="http://www.example.com/auth/validate/' . | |
$user_id . | |
'/' . | |
htmlentities( $code ) . | |
'">click here</a>.</p>' . | |
'</body>' . | |
'</html>', | |
'text/html' // Mark the content-type as HTML | |
) | |
; | |
$result = $mailer->send( $message ); | |
} | |
else | |
{ | |
// Flag as valid | |
$user->is_valid_email = 1; | |
$user->save(); | |
// Remove entries from the validation table | |
$ev->delete; | |
} | |
// Try to authenticate by passing the data to the login object. | |
$this->post_login( $user_data ); | |
return Redirect::to( 'home' ); | |
} | |
/** | |
* Validate email | |
* | |
* @return redirect | |
*/ | |
public function get_validate( $user_id, $key ) { | |
$ev = Evalid::find( $user_id ); | |
if( $ev->code == html_entity_decode( $key ) ) | |
{ | |
// Flag as valid | |
$user = User::find( $user_id ); | |
$user->is_valid_account = 1; | |
$user->save(); | |
// Remove entries from the validation table | |
$ev->delete(); | |
} | |
else | |
return Redirect::to( 'home' ); | |
// Notify the user it worked | |
Session::set( 'notice', 'Your E-mail address has been confirmed.' ); | |
return Redirect::to( 'home' ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment