]>
Commit | Line | Data |
---|---|---|
3cf8917d JR |
1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
2 | ||
3 | class Session extends CI_Model { | |
4 | public function __construct() { | |
5 | $this->load->model('user'); | |
6 | ||
7 | $this->apps = array(); | |
8 | ||
9 | # Required | |
10 | parent::__construct(); | |
11 | } | |
12 | ||
13 | protected function setError($error) { | |
14 | $this->lastError = $error; | |
15 | return FALSE; | |
16 | } | |
17 | public function getError() { | |
18 | return $this->lastError; | |
19 | } | |
20 | ||
21 | protected function getApp($aid, $iid, $file, $class) { | |
22 | if (!is_file($this->config->item('app_prefix').$file)) { | |
23 | return setError('App file does not exist'); | |
24 | } | |
25 | if (!(@include_once $this->config->item('app_prefix').$file)) { | |
26 | return setError('Include error'); | |
27 | } | |
28 | ||
29 | return new ($class)($iid) | |
30 | } | |
31 | public function getAppInst($iid) { | |
32 | if ($this->apps[$iid]) { | |
33 | return $this->apps[$iid]; | |
34 | } else { | |
35 | $this->db->select('aid'); | |
36 | $this->db->where('iid', $iid); | |
37 | $q = $this->db->get('session_apps'); | |
38 | if ($q->num_rows() == 0) | |
39 | return FALSE; | |
40 | $row = $q->row(); | |
41 | $aid = $row->aid; | |
42 | ||
43 | $this->db->select('classname, filename'); | |
44 | $this->db->where('aid', $aid); | |
45 | $q = $this->db->get('apps'); | |
46 | if ($q->num_rows() == 0) | |
47 | return FALSE; | |
48 | $row = $q->row(); | |
49 | return $this->getApp($aid, $iid, $row->filename, $row->classname); | |
50 | } | |
51 | } | |
52 | ||
53 | public function openApp($aid) { | |
54 | $sid = $this->user->sid(); | |
55 | $level = $this->user->level(); | |
56 | ||
57 | $this->db->select('classname, filename, access'); | |
58 | $this->db->where('aid', $aid); | |
59 | $q = $this->db->get('apps'); | |
60 | if ($q->num_rows() == 0) | |
61 | return setError('No such app'); | |
62 | $row = $q->row(); | |
63 | if (!$level) | |
64 | return setError('No access'); | |
65 | elseif ($row->access == 'operator' && $level == 'user') | |
66 | return setError('No access'); | |
67 | elseif ($row->access == 'manager' && $level != 'maneger') | |
68 | return setError('No access'); | |
69 | // they have access, go on | |
70 | ||
71 | $idata = array( | |
72 | 'sid' => $this->user->sid(), | |
73 | 'aid' => $aid, | |
74 | ); | |
75 | $this->db->insert('session_apps', $idata); | |
76 | ||
77 | ||
78 | $app = $this->getApp($aid, $this->db->insert_id(), $row->filename, $row->classname); | |
79 | if ($app) { | |
80 | $app->opening(); | |
81 | $this->apps[$iid] = $app; | |
82 | } | |
83 | return $app; | |
84 | } | |
85 | public function closeApp($iid) { | |
86 | $app = $this->getAppInst($iid); | |
87 | $app->closing(); | |
88 | ||
89 | $this->db->where('iid', $iid); | |
90 | $this->db->delete('session_apps'); | |
91 | } | |
92 | ||
93 | // UNUSED HERE, REMOVE | |
94 | public function do_login($uid) { | |
95 | $this->db->select('display_name, level'); | |
96 | $this->db->where('uid', $uid); | |
97 | $q = $this->db->get('users'); | |
98 | if ($q->num_rows() == 0) | |
99 | return FALSE; | |
100 | $row = $q->row(); | |
101 | $this->uid($uid); | |
102 | $this->display_name($row->display_name); | |
103 | $this->level($row->level); | |
104 | ||
105 | $sdata = array( | |
106 | 'uid' => $uid, | |
107 | 'started' => time(), | |
108 | 'last' => time(), | |
109 | 'lockip' => $this->input->ip_address(), | |
110 | ); | |
111 | $this->db->insert('sessions', $sdata); | |
112 | $this->sid($this->db->insert_id()); | |
113 | ||
114 | return $this->sid(); | |
115 | } | |
116 | ||
117 | public function sid($new=NULL) { | |
118 | if (!empty($new)) { | |
119 | $old = $this->cached_sid; | |
120 | $this->cached_sid = $new; | |
121 | return $old; | |
122 | } | |
123 | ||
124 | if (isset($this->cached_sid) && $this->cached_sid != 0) { | |
125 | return $this->cached_sid; | |
126 | } else { | |
127 | // TODO IP-lock checking... | |
128 | $this->cached_sid = $this->input->cookie('session_id'); | |
129 | if ($this->cached_sid != 0) { | |
130 | return $this->cached_sid; | |
131 | } | |
132 | } | |
133 | return FALSE; // fallback to this | |
134 | } | |
135 | } |