]> jfr.im git - z_archive/KronOS.git/blame - application/models/user.php
Moved Core (models -> libraries); changed more things to Json lib
[z_archive/KronOS.git] / application / models / user.php
CommitLineData
59c06b17
CS
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3class User extends CI_Model {
f255c3e2 4 protected $cached_sid, $cached_uid, $cached_level, $cached_display_name;
59c06b17
CS
5 public function __construct() {
6 # Required
55433e7f 7 parent::__construct();
59c06b17 8 }
9d2ed0ce 9
f255c3e2
JR
10 public function try_login($user, $pass) {
11 $this->db->select('uid, password, salt');
12 $this->db->where('username', $user);
13 $q = $this->db->get('users');
14 if ($q->num_rows() > 0) {
15 $row = $q->row();
16 $pwdigest = sha1($row->salt.$pass);
17 if ($pwdigest == $row->password) {
18 return $this->do_login($row->uid);
19 }
20 }
21 return FALSE;
22 }
23 public function do_login($uid) {
24 $this->db->select('display_name, level');
25 $this->db->where('uid', $uid);
26 $q = $this->db->get('users');
27 if ($q->num_rows() == 0)
28 return FALSE;
29 $row = $q->row();
30 $this->uid($uid);
31 $this->display_name($row->display_name);
32 $this->level($row->level);
33
34 $sdata = array(
35 'uid' => $uid,
36 'started' => time(),
37 'last' => time(),
38 'lockip' => $this->input->ip_address(),
39 );
40 $this->db->insert('sessions', $sdata);
41 $this->sid($this->db->insert_id());
42
4d35980c
JR
43 $this->input->set_cookie('session_id', $this->sid());
44
f255c3e2
JR
45 return TRUE;
46 }
47
48 public function sid($new=NULL) {
49 if (!empty($new)) {
50 $old = $this->cached_sid;
51 $this->cached_sid = $new;
52 return $old;
53 }
54
55 if (isset($this->cached_sid)) {
56 return $this->cached_sid;
57 } else {
4d35980c
JR
58 // FIXME needs IP-lock checking...
59 return $this->cached_sid = $this->input->cookie('session_id');
f255c3e2
JR
60 }
61 }
62 public function uid($new=NULL) {
63 if (!empty($new)) {
64 $old = $this->cached_uid;
65 $this->cached_uid = $new;
66 return $old;
67 }
68
69 if (isset($this->cached_uid)) {
70 return $this->cached_uid;
71 } else {
72 $sid = $this->sid();
73 if ($sid !== FALSE) {
74 $this->db->select('uid');
75 $this->db->where('sid', $sid);
76 $q = $this->db->get('sessions');
77 if ($q->num_rows() > 0) {
78 $row = $q->row();
79 return $this->cached_uid = $row->uid;
80 }
81 }
82 }
83 return FALSE;
84 }
85 public function display_name($new=NULL) {
86 if (!empty($new)) {
87 $old = $this->cached_display_name;
88 $this->cached_display_name = $new;
89 return $old;
90 }
91
92 if (isset($this->cached_display_name)) {
93 return $this->cached_display_name;
94 } else {
95 $uid = $this->uid();
96 if ($uid !== FALSE) {
97 $this->db->select('display_name');
98 $this->db->where('uid', $uid);
99 $q = $this->db->get('users');
100 if ($q->num_rows() > 0) {
101 $row = $q->row();
102 return $this->cached_display_name = $row->display_name;
103 }
104 }
105 }
106 return FALSE;
107 }
108 public function level($new=NULL) {
109 // TODO TODO TODO
110 $this->cached_level = $new;
111 }
112
113 public function is_logged_in() {
114 return $this->sid() > 0;
115 }
116
59c06b17
CS
117 public function check_login() {
118 if (!$this->logged_in)
119 redirect('account/login/');
120 }
9d2ed0ce 121}