Skip to content
Browse files

sign valid

  • Loading branch information...
1 parent 8ed0e91 commit a52e6b2f542eaad442df204aa3d2fb9717381ee9 @GongNation committed Feb 22, 2012
View
99 application/classes/controller/index.php
@@ -13,113 +13,103 @@ class Controller_Index extends Controller_Template {
// Routes
protected $media;
protected $index;
+ public $login_error = FALSE;
+ public $signup_error = FALSE;
public function before()
{
if (in_array($this->request->action(), array('media', 'ajax')))
{
- // Do not template media files
+ // media和ajax页面不需要页面模版
$this->auto_render = FALSE;
}
else
{
- // Grab the necessary routes
+ // 获取必要的route
$this->media = Route::get('media');
$this->index = Route::get('default');
}
-
+
+ // 是否已登录
+ $username = Model_User::is_login();
+ if ($username)
+ {
+ $this->request->redirect($username);
+ }
+
// I18n
$lang = $this->request->query('lang');
Model_Secure::set_lang($lang);
parent::before();
}
- public function action_index()
- {
- }
// Index
- public function action_index2()
+ public function action_index()
{
- $this->template->title = "index";
+ $this->template->title = "Kohana-Bootstrap";
- $is_login = Model_Secure::is_login();
- if ($is_login)
- {
- $this->request->redirect($is_login);
- }
- else
- {
- $this->template->view = View::factory('default/login');
- }
+ $this->template->login_error = $this->login_error;
+ $this->template->signup_error = $this->signup_error;
}
// Login
public function action_login()
{
- $this->template->title = "login";
if (count($this->request->post()) == 0)
{
- $this->template->view = View::factory('default/login');
+ $this->request->redirect();
}
else
{
- $username = $this->request->post('username');
+ $login = $this->request->post('login');
$password = $this->request->post('password');
- $login = Model_Secure::login($username, $password);
- if ($login)
- {
- $this->request->redirect($username);
- }
- else
+ $login = Model_User::login($login, $password);
+ if (!in_array($login, array(1, 2, 3)))
{
- $this->template->view = 'Login failed!'.$username.$password;
+ $this->request->redirect($login);
}
-
+ $this->login_error = $login;
+ $this->action_index();
}
}
// Logout
public function action_logout()
{
- Session::instance()->delete('username');
+ Model_User::logout();
$this->request->redirect();
}
// Signup
public function action_signup()
{
- $this->template->title = "Signup";
if (count($this->request->post()) == 0)
{
- $this->template->view = View::factory('default/signup');
+ $this->request->redirect();
}
else
{
$username = $this->request->post('username');
$password = $this->request->post('password');
- $mail = $this->request->post('mail');
+ $email = $this->request->post('email');
- $ModelSecure = Model::factory('secure');
- $signup = $ModelSecure->signup($username, $password, $mail);
- if ( ! $signup)
+ $signup = Model_User::signup($username, $password, $email);
+ if ($signup === FALSE)
{
- // Unkown error
- $this->template->view = 'Signup failed! Unkown error!'.$username.$password.$mail;
+ // 注册失败
+ $this->request->redirect();
}
- else if (is_array($signup))
+ else if ($signup > 0)
{
- // The user was signed up already
- $this->template->view = 'Signup failed! '.$username.' was signed up already!';
- }
- else if (is_numeric($signup))
- {
- // Insert success
- $ModelSecure->login($username, $password);
- $this->template->title = "Home";
- $this->template->view = View::factory('default/home')
- ->bind('username', $username);
+ // 注册成功
+ $session = Session::instance();
+ $session->set('username', $username);
+ $session->set('id', $signup);
+ $this->request->redirect($username);
}
+ $this->signup_error = $signup;
+ $this->action_index();
}
}
@@ -160,22 +150,11 @@ public function after()
// Get the media route
$media = Route::get('media');
- // Add styles
- $this->template->styles = array(
- $media->uri(array('file' => 'css/form.css')) => 'screen',
- $media->uri(array('file' => 'css/default.css')) => 'screen',
- );
-
// Add scripts
$this->template->scripts = array(
- $media->uri(array('file' => 'js/html5.js')),
+ $media->uri(array('file' => 'js/index.js')),
);
- // Add icon
- $this->template->icon = $media->uri(array('file' => 'images/favicon.ico'));
-
- // Add languages
- //$this->template->translations = Kohana::message('userguide', 'translations');
}
return parent::after();
View
156 application/classes/model/secure.php
@@ -1,156 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-class Model_Secure extends Model_Database {
-
-/**
-* Get user
-*
-*/
- static function get_user ($user_info, $is_id = TRUE)
- {
- if ($is_id)
- {
- $user_id = $user_info;
- $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE id=:id ');
- $query->param(':id', $user_id);
- }
- else
- {
- $username = $user_info;
- $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE username=:username');
- $query->param(':username', $username);
- }
- $result = $query->execute();
- if ($result->count() == 1)
- {
- // Select success
- return $result->as_array();
- }
- else
- {
- // The user is not in db
- return FALSE;
- }
- }
-
-/**
-* User singup
-*
-*/
- static function signup ($username, $password, $mail)
- {
- $user = $this->get_user($username, FALSE);
- if (!$user)
- {
- $password = md5($password);
- $query = DB::query(Database::INSERT, 'INSERT INTO user (username, password, mail) VALUES (:username, :password, :mail)')
- ->bind(':username', $username)
- ->bind(':password', $password)
- ->bind(':mail', $mail);
- list($insert_id, $affected_rows) = $query->execute();
- if ($affected_rows == 1)
- {
- // Insert success, the retuen value is a number
- return $insert_id;
- }
- else
- {
- // Unkown error, the return value is boolean
- return FALSE;
- }
- }
- else
- {
- // The user was signed up already, the return value is a array
- return $user;
- }
- }
-
-/**
-* User login
-*
-*/
- static function login ($username, $password)
- {
- $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE username=:user AND password=:pass');
- $query->parameters(array(
- ':user' => $username,
- ':pass' => md5($password),
- ));
-
- $result = $query->execute();
- if (count($result) == 1)
- {
- Session::instance()->set('username', $username);
-
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
-
-/**
-* User is_login
-*
-*/
- static function is_login ($username = null)
- {
- $session_username = Session::instance()->get('username');
- if ($username === NULL AND $session_username !== NULL)
- {
- return $session_username;
- }
- else if ( $username !== NULL AND $username == $session_username)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
-
-/**
-* Set language in cookie
-*
-*/
- static function set_lang ($lang)
- {
- $lang_cookie = Cookie::get('lang');
- if ($lang === NULL AND $lang_cookie !== NULL)
- {
- I18n::lang($lang_cookie);
- }
- elseif ($lang !== NULL)
- {
- Cookie::set('lang', $lang);
- I18n::lang($lang);
- }
- }
-
-/**
-* Set theme in cookie
-*
-*/
- static function set_theme ($theme)
- {
- $theme_cookie = Cookie::get('theme');
- if ($theme === NULL AND $theme_cookie === NULL)
- {
- Cookie::set('theme', 'default');
- }
- elseif ($theme !== NULL AND $theme_cookie === NULL)
- {
- Cookie::set('theme', $theme);
- }
- elseif ($theme === NULL AND $theme_cookie !== NULL)
- {
- Cookie::set('theme', $theme);
- }
- elseif ($theme !== NULL AND $theme_cookie !== NULL)
- {
- Cookie::set('theme', $theme);
- }
- }
-}
View
196 application/classes/model/user.php
@@ -0,0 +1,196 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+class Model_User extends Model_Database {
+/**
+* 通过Id获取user相关信息
+*
+*/
+ static function get_user_by_id ($id)
+ {
+ $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE id=:id ');
+ $query->param(':id', $id);
+ $result = $query->execute();
+ if ($result->count() == 1)
+ {
+ // 查询成功,返回user相关信息的数组
+ return $result->as_array();
+ }
+ // 查询失败
+ return FALSE;
+ }
+
+/**
+* 通过username获取user相关信息
+*
+*/
+ static function get_user_by_username ($username)
+ {
+ $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE username=:username');
+ $query->param(':username', $username);
+ $result = $query->execute();
+ if ($result->count() == 1)
+ {
+ // 查询成功,返回user相关信息的数组
+ return $result->as_array();
+ }
+ // 查询失败
+ return FALSE;
+ }
+
+/**
+* 通过email获取user相关信息
+*
+*/
+ static function get_user_by_email ($email)
+ {
+ $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE email=:email');
+ $query->param(':email', $email);
+ $result = $query->execute();
+ if ($result->count() == 1)
+ {
+ // 查询成功,返回user相关信息的数组
+ return $result->as_array();
+ }
+ // 查询失败
+ return FALSE;
+ }
+
+/**
+* 用户注册
+*
+*/
+ static function signup ($username, $password, $email)
+ {
+ $check_username = self::get_user_by_username($username);
+ $check_email = self::get_user_by_email($email);
+ if (!$check_username AND !$check_email)
+ {
+ $password = md5($password);
+ $query = DB::query(Database::INSERT, 'INSERT INTO user (username, password, email) VALUES (:username, :password, :email)')
+ ->bind(':username', $username)
+ ->bind(':password', $password)
+ ->bind(':email', $email);
+ list($insert_id, $affected_rows) = $query->execute();
+ if ($affected_rows == 1)
+ {
+ // 数据插入成功,返回user的Id
+ return $insert_id;
+ }
+ // 数据插入失败
+ return FALSE;
+ }
+ else if ($check_username AND !$check_email)
+ {
+ // username已经被注册,email可用,返回值 -1
+ return -1;
+ }
+ else if ($check_email AND !$check_username)
+ {
+ // email已经被注册,username可用,返回值 -2
+ return -2;
+ }
+ else
+ {
+ // username和email已经被注册,返回值 -3
+ return -3;
+ }
+ }
+
+/**
+* 用户登录
+*
+*/
+ static function login ($login, $password)
+ {
+ // $login为email
+ if (Valid::email($login))
+ {
+ $check_email = self::get_user_by_email($login);
+ if (!$check_email)
+ {
+ // 登录用的email不存在
+ return 1;
+ }
+ $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE email=:email AND password=:pass');
+ $query->parameters(array(
+ ':email' => $login,
+ ':pass' => md5($password),
+ ));
+ }
+ else
+ // $login为username
+ {
+ $check_username = self::get_user_by_username($login);
+ if (!$check_username)
+ {
+ // 登录用的username不存在
+ return 2;
+ }
+ $query = DB::query(Database::SELECT, 'SELECT * FROM user WHERE username=:user AND password=:pass');
+ $query->parameters(array(
+ ':user' => $login,
+ ':pass' => md5($password),
+ ));
+ }
+
+ $result = $query->execute();
+ if ($result->count() == 1)
+ {
+ $userinfo_array = $result->as_array();
+ $username = $userinfo_array[0]['username'];
+ $id = $userinfo_array[0]['id'];
+
+ $session = Session::instance();
+ $session->set('username', $username);
+ $session->set('id', $id);
+ // 登录成功
+ return $username;
+ }
+ // 密码错误
+ return 3;
+ }
+
+/**
+* 用户退出登录
+*
+*/
+ static function logout()
+ {
+ $session = Session::instance();
+ $session->delete('username');
+ $session->delete('id');
+ }
+
+/**
+* 判断用户是否登录
+*
+*/
+ static function is_login ()
+ {
+ $username = Session::instance()->get('username');
+ if ($username === NULL)
+ {
+ // 没有登录过
+ return FALSE;
+ }
+ // 已经登录,返回username
+ return $username;
+ }
+
+/**
+* 在cookie中保存用户的语言
+*
+*/
+ static function set_lang ($lang)
+ {
+ $lang_cookie = Cookie::get('lang');
+ if ($lang === NULL AND $lang_cookie !== NULL)
+ {
+ I18n::lang($lang_cookie);
+ }
+ elseif ($lang !== NULL)
+ {
+ Cookie::set('lang', $lang);
+ I18n::lang($lang);
+ }
+ }
+}
View
6 application/i18n/en.php
@@ -12,8 +12,8 @@
'Username' => 'Username',
'Password' => 'Password',
'Email' => 'Email',
- 'Login' => 'Login',
- 'Logout' => 'Logout',
+ 'Log in' => 'Log in',
+ 'Log out' => 'Log out',
'Sign up' => 'Sign up',
'Remember me' => 'Remember me',
'Forget password' => 'Forget password?',
@@ -22,5 +22,5 @@
'Welcome words' => 'Welcome to the Kohana-Bootstrap.',
// language
- 'Set language' => 'languageEnglish',
+ 'Set language' => 'language:English',
);
View
31 application/i18n/zh.php
@@ -3,24 +3,29 @@
return array
(
// nav
- 'Home' => '首页',
- 'About' => '关于',
- 'Contact' => '联系',
+ 'Home' => '首页',
+ 'About' => '关于',
+ 'Contact' => '联系',
// sign up and login
- 'Username or Email' => '用户名或邮箱',
- 'Username' => '用户名',
- 'Password' => '密码',
- 'Email' => '邮箱',
- 'Login' => '登录',
- 'Logout' => '退出',
- 'Sign up' => '注册',
- 'Remember me' => '记住我',
- 'Forget password' => '忘记密码?',
+ 'Username or Email' => '用户名或邮箱',
+ 'Username' => '用户名',
+ 'Password' => '密码',
+ 'Email' => '邮箱',
+ 'Log in' => '登录',
+ 'Log out' => '退出',
+ 'Sign up' => '注册',
+ 'Remember me' => '记住我',
+ 'Forget password' => '忘记密码?',
+ 'Username dose not exists!' => '用户名不存在!',
+ 'Username has been registered!' => '用户名已被注册!',
+ 'Email dose not exists!' => '邮箱不存在!',
+ 'Email has been registered!' => '邮箱已被注册!',
+ 'Password is incorrect!' => '忘记密码不正确!',
// welcome
'Welcome words' => '欢迎来到Kohana-Bootstrap.',
// language
- 'Set language' => '语言简体中文',
+ 'Set language' => '语言:简体中文',
);
View
6 application/media/css/style.css → application/media/css/base.css
@@ -18,6 +18,10 @@ a:focus,
outline-offset: 0px;
}
+a:hover {
+ _outline: 0;
+}
+
input[type="text"],
input[type="password"] {
outline: 0\9;
@@ -41,7 +45,7 @@ input[type="password"] {
border-radius: 4px 4px 4px 4px;
}
-.form-login, .form-register {
+.form-login, .form-signup {
-webkit-border-radius: 4px 4px 4px 4px;
-moz-border-radius: 4px 4px 4px 4px;
border-radius: 4px 4px 4px 4px;
View
160 application/views/default/index.php
@@ -16,37 +16,12 @@
<!-- bootstrap-responsive.css 用于在多平台设备的自适应
<link type="text/css" href="/media/plugin/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
-->
- <link type="text/css" href="/media/css/style.css" rel="stylesheet">
+ <link type="text/css" href="/media/css/base.css" rel="stylesheet">
<!-- icon -->
<link type="image/x-icon" href="/media/images/favicon.ico" rel="shortcut icon" />
<link href="/media/images/apple-touch-icon.png" rel="apple-touch-icon">
<link href="/media/images/apple-touch-icon-72x72.png" sizes="72x72" rel="apple-touch-icon" >
<link href="/media/images/apple-touch-icon-114x114.png" sizes="114x114" rel="apple-touch-icon" >
- <!--scripts-->
- <script type="text/javascript" src="/media/js/modernizr-2.5.2.custom.min.js"></script>
- <script>
- Modernizr.load([
- {
- load: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js',
- complete: function () {
- if ( !window.jQuery ) {
- Modernizr.load('/media/js/jquery-1.7.1.min.js');
- }
- }
- },
- {
- load: [
- '/media/js/jquery-ui-1.8.17.custom.min.js',
- '/media/plugin/bootstrap/js/bootstrap.js',
- '/media/plugin/jquery-ui-date-range-picker/js/date.js',
- '/media/plugin/jquery-ui-date-range-picker/js/daterangepicker.jquery.js',
- '/media/plugin/jquery-ui-fileinput/js/enhance.min.js',
- '/media/plugin/jquery-ui-fileinput/js/fileinput.jquery.js',
- '/media/js/base.js'
- ]
- }
- ]);
- </script>
</head>
<body>
<div class="navbar navbar-fixed-top">
@@ -82,38 +57,38 @@
</div>
</div>
<div class="span4 pull-right">
- <form methond="post" action="<?php echo Kohana::$base_url.Route::get('login')->uri() ?>" class="form-horizontal span3 form-actions form-login pull-right">
+ <form method="post" action="<?php echo Kohana::$base_url.Route::get('login')->uri() ?>" class="form-horizontal span3 form-actions form-login pull-right">
<div class="control-group">
- <div class="input-prepend">
- <span class="add-on"><i class="icon-envelope"></i></span><input type="text" name="login" placeholder="<?php echo __('Username or Email')?>">
+ <div class="input-prepend" id="login-input-login">
+ <span class="add-on"><i class="icon-envelope"></i></span><input type="text" name="login" autocomplete="off" placeholder="<?php echo __('Username or Email')?>">
</div>
</div>
<div class="control-group">
- <div class="input-prepend">
- <span class="add-on"><i class="icon-lock"></i></span><input type="password" name="password" placeholder="<?php echo __('Password')?>">
+ <div class="input-prepend" id="login-input-password">
+ <span class="add-on"><i class="icon-lock"></i></span><input type="password" name="password" autocomplete="off" placeholder="<?php echo __('Password')?>">
</div>
</div>
<div class="control-group">
<div class="pull-left">
<label class="checkbox pull-left"><input type="checkbox" name="remember" value="1"><?php echo __('Remember me')?></label>&nbsp;·&nbsp;<a href="#"><?php echo __('Forget password')?></a>
</div>
- <button type="submit" class="btn btn-primary pull-right"><?php echo __('Login')?></button>
+ <button type="submit" class="btn btn-primary pull-right"><?php echo __('Log in')?></button>
</div>
</form>
- <form methond="post" action="" class="form-horizontal span3 form-actions form-register pull-right">
+ <form method="post" action="<?php echo Kohana::$base_url.Route::get('signup')->uri() ?>" class="form-horizontal span3 form-actions form-signup pull-right">
<div class="control-group">
- <div class="input-prepend">
- <span class="add-on"><i class="icon-user"></i></span><input type="text" name="username" placeholder="<?php echo __('Username')?>">
+ <div class="input-prepend" id="signup-input-username">
+ <span class="add-on"><i class="icon-user"></i></span><input type="text" name="username" autocomplete="off" placeholder="<?php echo __('Username')?>">
</div>
</div>
<div class="control-group">
- <div class="input-prepend">
- <span class="add-on"><i class="icon-envelope"></i></span><input type="text" name="email" placeholder="<?php echo __('Email')?>">
+ <div class="input-prepend" id="signup-input-email">
+ <span class="add-on"><i class="icon-envelope"></i></span><input type="text" name="email" autocomplete="off" placeholder="<?php echo __('Email')?>">
</div>
</div>
<div class="control-group">
- <div class="input-prepend">
- <span class="add-on"><i class="icon-lock"></i></span><input type="password" name="password" placeholder="<?php echo __('Password')?>">
+ <div class="input-prepend" id="signup-input-password">
+ <span class="add-on"><i class="icon-lock"></i></span><input type="password" name="password" autocomplete="off" placeholder="<?php echo __('Password')?>">
</div>
</div>
<div class="control-group"><button type="submit" class="btn btn-success pull-right"><?php echo __('Sign up')?></button></div>
@@ -126,6 +101,111 @@
<p>&copy; Kohana-Bootstrap 2012</p>
</footer>
</div><!-- /container -->
-
+
+ <!--scripts-->
+ <script type="text/javascript" src="/media/js/jquery-1.7.1.min.js"></script>
+ <script type="text/javascript" src="/media/js/jquery-ui-1.8.17.custom.min.js"></script>
+ <script type="text/javascript" src="/media/plugin/bootstrap/js/bootstrap.js"></script>
+ <?php foreach ($scripts as $script) echo HTML::script($script, NULL, NULL, TRUE), "\n" ?>
+ <script type="text/javascript" src="/media/js/modernizr-2.5.2.custom.min.js"></script>
+ <script>
+ $(function(){
+<?php
+// 登录出错提示
+if ($login_error !== FALSE)
+{
+ switch ($login_error)
+ {
+ case 1:
+ $login_error_id = "#login-input-login";
+ $login_error_title = __('Email dose not exists!');
+ $login_error_input = ".form-login input[name='login']";
+ break;
+
+ case 2:
+ $login_error_id = "#login-input-login";
+ $login_error_title = __('Username dose not exists!');
+ $login_error_input = ".form-login input[name='login']";
+ break;
+
+ case 3:
+ $login_error_id = "#login-input-password";
+ $login_error_title = __('Password is incorrect!');
+ $login_error_input = ".form-login input[name='password']";
+ break;
+ }
+?>
+ $("<?php echo $login_error_id ?>").tooltip({
+ placement: "left",
+ title: "<?php echo $login_error_title ?>",
+ trigger: 'manual',
+ });
+ $("<?php echo $login_error_id ?>").tooltip('show');
+ $("<?php echo $login_error_input ?>").focus(function(){
+ $("#<?php echo $login_error_id ?>").tooltip('hide');
+ });
+<?php
+}
+// 注册出错提示
+if ($signup_error !== FALSE)
+{
+ switch ($signup_error)
+ {
+ case -1:
+?>
+ $("#signup-input-username").tooltip({
+ placement: "left",
+ title: "<?php echo __('Username has been registered!') ?>",
+ trigger: 'manual',
+ });
+ $("#signup-input-username").tooltip('show');
+ $(".form-signup input[name='username']").focus(function(){
+ $("#signup-input-username").tooltip('hide');
+ });
+<?php
+ break;
+
+ case -2:
+?>
+ $("#signup-input-email").tooltip({
+ placement: "left",
+ title: "<?php echo __('Email has been registered!') ?>",
+ trigger: 'manual',
+ });
+ $("#signup-input-email").tooltip('show');
+ $(".form-signup input[name='email']").focus(function(){
+ $("#signup-input-email").tooltip('hide');
+ });
+<?php
+ break;
+
+ case -3:
+?>
+ $("#signup-input-username").tooltip({
+ placement: "left",
+ title: "<?php echo __('Username has been registered!') ?>",
+ trigger: 'manual',
+ });
+ $("#signup-input-username").tooltip('show');
+ $(".form-signup input[name='username']").focus(function(){
+ $("#signup-input-username").tooltip('hide');
+ });
+
+ $("#signup-input-email").tooltip({
+ placement: "left",
+ title: "<?php echo __('Email has been registered!') ?>",
+ trigger: 'manual',
+ });
+ $("#signup-input-email").tooltip('show');
+ $(".form-signup input[name='email']").focus(function(){
+ $("#signup-input-email").tooltip('hide');
+ });
+<?php
+ break;
+ }
+}
+?>
+ });
+ </script>
</body>
</html>
View
2 sql.tex → sql.txt
@@ -2,6 +2,6 @@
id int(10) unsigned auto_increment not null,
username varchar(20) not null unique,
password varchar(20) not null,
-mail varchar(50) not null,
+email varchar(50) not null,
primary key(id)
)

0 comments on commit a52e6b2

Please sign in to comment.
Something went wrong with that request. Please try again.