]> jfr.im git - z_archive/KronOS.git/blame - application/models/user.php
Made JSON library; updated (some of?) the JSON output calls; made a login page.
[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
43 return TRUE;
44 }
45
46 public function sid($new=NULL) {
47 if (!empty($new)) {
48 $old = $this->cached_sid;
49 $this->cached_sid = $new;
50 return $old;
51 }
52
53 if (isset($this->cached_sid)) {
54 return $this->cached_sid;
55 } else {
56 return FALSE; // FIXME should we fetch SID somehow?
57 }
58 }
59 public function uid($new=NULL) {
60 if (!empty($new)) {
61 $old = $this->cached_uid;
62 $this->cached_uid = $new;
63 return $old;
64 }
65
66 if (isset($this->cached_uid)) {
67 return $this->cached_uid;
68 } else {
69 $sid = $this->sid();
70 if ($sid !== FALSE) {
71 $this->db->select('uid');
72 $this->db->where('sid', $sid);
73 $q = $this->db->get('sessions');
74 if ($q->num_rows() > 0) {
75 $row = $q->row();
76 return $this->cached_uid = $row->uid;
77 }
78 }
79 }
80 return FALSE;
81 }
82 public function display_name($new=NULL) {
83 if (!empty($new)) {
84 $old = $this->cached_display_name;
85 $this->cached_display_name = $new;
86 return $old;
87 }
88
89 if (isset($this->cached_display_name)) {
90 return $this->cached_display_name;
91 } else {
92 $uid = $this->uid();
93 if ($uid !== FALSE) {
94 $this->db->select('display_name');
95 $this->db->where('uid', $uid);
96 $q = $this->db->get('users');
97 if ($q->num_rows() > 0) {
98 $row = $q->row();
99 return $this->cached_display_name = $row->display_name;
100 }
101 }
102 }
103 return FALSE;
104 }
105 public function level($new=NULL) {
106 // TODO TODO TODO
107 $this->cached_level = $new;
108 }
109
110 public function is_logged_in() {
111 return $this->sid() > 0;
112 }
113
59c06b17
CS
114 public function check_login() {
115 if (!$this->logged_in)
116 redirect('account/login/');
117 }
9d2ed0ce 118}