Permalink
Browse files

sign valid

  • Loading branch information...
GongNation committed Feb 21, 2012
1 parent 8ed0e91 commit a52e6b2f542eaad442df204aa3d2fb9717381ee9
@@ -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();
@@ -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);
- }
- }
-}
Oops, something went wrong.

0 comments on commit a52e6b2

Please sign in to comment.