Kohana::model

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Users extends Model {
public function signup_valid (array $data) {
$login = !empty($data['login']) ? $this->check_login($data['login']) : null;
$email = !empty($data['email']) ? $this->check_email($data['email']) : null;
$post = Validation::factory($data)
->rule('login', 'not_empty')
->rule('login', 'alpha_dash', array(':value', TRUE))
->rule('login', 'min_length', array(':value', 3))
->rule('login', 'max_length', array(':value', 20))
->rule('email', 'not_empty')
->rule('email', 'email')
->rule('login', 'uniq_data', array($login))
->rule('email', 'uniq_data', array($email))
->rule('password', 'not_empty')
->rule('password', 'min_length', array(':value', 8))
->rule('confirm', 'not_empty')
->rule('confirm', 'matches', array(':validation', 'confirm', 'password'))
->rule('captcha', 'not_empty')
->rule('captcha', 'Captcha::valid')
->rule('csrf', 'not_empty')
->rule('csrf', 'Security::check');
return $post;
}
public function check_logged ($email, $pass) {
$query = DB::select(DB::expr('COUNT(id) as count'))
->from('users')
->where('email', '=', $email)
->and_where('password', '=', $pass)
->execute()
->get('count');
if($query) return $query;
else return array();
}
public function check_auth ($data) {
$data = explode(':', $data);
$query = DB::select(DB::expr('COUNT(id) as count'))
->from('users')
->where('email', '=', $data[0])
->and_where('password', '=', $data[1])
->execute()
->get('count');
if($query) return $query;
else return array();
}
public function check_login ($login) {
$query = DB::select(DB::expr('COUNT(id) as count'))
->from('users')
->where('login', '=', $login)
->execute()
->get('count');
if($query) return $query;
else return array();
}
public function check_email ($email) {
$query = DB::select(DB::expr('COUNT(id) as count'))
->from('users')
->where('email', '=', $email)
->execute()
->get('count');
if($query) return $query;
else return array();
}
public function new_user ($login, $email, $password) {
$query = DB::insert('users',array('login','email', 'password', 'date_reg', 'date_last'))
->values(array($login, $email, $password, time(), time()))
->execute();
if($query) return $query;
else return array();
}
}