leaderbin/modules/user/create.php
2013-12-16 15:37:07 -05:00

67 lines
1.8 KiB
PHP

<?php
class user_create extends CustomModule
{
// protected $ajax = true;
// protected $method = 'POST';
// protected $validate = array(
// 'email' => array(
// 'length:>:100' => 'Email addresses may not be more than 100 characters.',
// 'filter:email' => 'Your email address is invalid.',
// ),
// 'username' => array(
// 'length:<:4' => 'Usernames may not be less than 4 characters.',
// 'length:>:30' => 'Usernames may not be more than 50 characters.',
// 'regex:is:/[^a-z0-9]+/i' => 'Usernames may only contain letters and numbers.',
// ),
// 'password' => array(
// 'length:<:8' => 'Passwords may not be less than 8 characters.',
// ),
// );
public function __default()
{
try
{
$user = new User();
// Checks if the email or username is already in use
foreach (array('email', 'username') as $field)
{
if ($user->getMapping($field, $_POST[$field]))
{
return array('error' => 'The ' . $field . ' is already in use.');
}
}
// Grabs the next UID
$uid = $user->nextUID();
// Generates the auth token
$auth_token = sha1(mt_rand() . microtime());
// Writes the user data
$user->set($uid, array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'),
'auth' => $auth_token,
));
// Sets the UID mappings
$user->setMapping('username', $_POST['username'], $uid);
$user->setMapping('email', $_POST['email'], $uid);
// Sets a cookie with the UID and auth token
setcookie('auth', $uid . '|' . $auth_token, Time::YEAR);
return array('status' => 'success', 'url' => '/');
}
catch (RedisException $e)
{
return array('error' => $e->getMessage());
}
}
}
?>