]>
Commit | Line | Data |
---|---|---|
3cf8917d JR |
1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
2 | ||
c1828c8b JR |
3 | /* TODO: check if app was succesfully opened, if not delete the instance */ |
4 | ||
536db703 | 5 | class Msession extends CI_Model { |
3cf8917d JR |
6 | public function __construct() { |
7 | $this->load->model('user'); | |
8 | ||
9 | $this->apps = array(); | |
10 | ||
11 | # Required | |
12 | parent::__construct(); | |
13 | } | |
14 | ||
15 | protected function setError($error) { | |
c1828c8b | 16 | trigger_error($error); |
3cf8917d JR |
17 | $this->lastError = $error; |
18 | return FALSE; | |
19 | } | |
20 | public function getError() { | |
21 | return $this->lastError; | |
22 | } | |
23 | ||
906f0f92 JR |
24 | protected function includeApp($file) { |
25 | $f = $this->config->item('app_prefix').$file; | |
26 | if (!is_file($f)) { | |
27 | return $this->setError('App file does not exist '.$f); | |
3cf8917d | 28 | } |
906f0f92 JR |
29 | if (!(include_once $f)) { |
30 | return $this->setError('Include error '.$f); | |
3cf8917d | 31 | } |
906f0f92 JR |
32 | } |
33 | ||
34 | protected function getApp($aid, $iid, $file, $class) { | |
35 | $this->includeApp($file); | |
36 | return new $class($iid, $aid); | |
37 | } | |
3cf8917d | 38 | |
906f0f92 JR |
39 | public function getAppClass($aid) { |
40 | $this->db->select('classname, filename'); | |
41 | $this->db->where('aid', $aid); | |
42 | $q = $this->db->get('apps'); | |
43 | if ($q->num_rows() == 0) | |
44 | return FALSE; | |
45 | $row = $q->row(); | |
46 | $filename = $row->filename; | |
47 | $classname = $row->classname; | |
48 | $this->includeApp($filename); | |
49 | return $classname; | |
3cf8917d | 50 | } |
906f0f92 | 51 | |
3cf8917d | 52 | public function getAppInst($iid) { |
906f0f92 | 53 | if (isset($this->apps[$iid])) { |
3cf8917d JR |
54 | return $this->apps[$iid]; |
55 | } else { | |
906f0f92 | 56 | $this->db->select('aid, corename'); |
3cf8917d JR |
57 | $this->db->where('iid', $iid); |
58 | $q = $this->db->get('session_apps'); | |
59 | if ($q->num_rows() == 0) | |
60 | return FALSE; | |
61 | $row = $q->row(); | |
62 | $aid = $row->aid; | |
63 | ||
906f0f92 JR |
64 | if ($aid == -1) { |
65 | $filename = 'core/'.$row->corename.'.php'; | |
66 | $classname = ucfirst($row->corename); | |
67 | } else { | |
68 | $this->db->select('classname, filename'); | |
69 | $this->db->where('aid', $aid); | |
70 | $q = $this->db->get('apps'); | |
71 | if ($q->num_rows() == 0) | |
72 | return FALSE; | |
73 | $row = $q->row(); | |
74 | $filename = $row->filename; | |
75 | $classname = $row->classname; | |
76 | } | |
77 | return $this->getApp($aid, $iid, $filename, $classname); | |
3cf8917d JR |
78 | } |
79 | } | |
80 | ||
c1828c8b JR |
81 | public function openCoreApp($name) { |
82 | $idata = array( | |
83 | 'sid' => $this->user->sid(), | |
84 | 'aid' => -1, | |
906f0f92 | 85 | 'corename' => $name, |
c1828c8b JR |
86 | ); |
87 | $this->db->insert('session_apps', $idata); | |
88 | $iid = $this->db->insert_id(); | |
89 | ||
90 | $app = $this->getApp(-1, $iid, 'core/'.$name.'.php', ucfirst($name)); | |
91 | if ($app) { | |
92 | $app->opening(); | |
93 | $this->apps[$iid] = $app; | |
94 | } | |
95 | return $app; | |
96 | } | |
3cf8917d JR |
97 | public function openApp($aid) { |
98 | $sid = $this->user->sid(); | |
99 | $level = $this->user->level(); | |
100 | ||
101 | $this->db->select('classname, filename, access'); | |
102 | $this->db->where('aid', $aid); | |
103 | $q = $this->db->get('apps'); | |
104 | if ($q->num_rows() == 0) | |
c1828c8b | 105 | return $this->setError('No such app'); |
3cf8917d JR |
106 | $row = $q->row(); |
107 | if (!$level) | |
c1828c8b | 108 | return $this->setError('No access'); |
3cf8917d | 109 | elseif ($row->access == 'operator' && $level == 'user') |
c1828c8b JR |
110 | return $this->setError('No access'); |
111 | elseif ($row->access == 'manager' && $level != 'manager') | |
112 | return $this->setError('No access'); | |
3cf8917d JR |
113 | // they have access, go on |
114 | ||
115 | $idata = array( | |
116 | 'sid' => $this->user->sid(), | |
117 | 'aid' => $aid, | |
118 | ); | |
119 | $this->db->insert('session_apps', $idata); | |
536db703 | 120 | $iid = $this->db->insert_id(); |
3cf8917d | 121 | |
536db703 | 122 | $app = $this->getApp($aid, $iid, $row->filename, $row->classname); |
3cf8917d JR |
123 | if ($app) { |
124 | $app->opening(); | |
125 | $this->apps[$iid] = $app; | |
126 | } | |
127 | return $app; | |
128 | } | |
129 | public function closeApp($iid) { | |
130 | $app = $this->getAppInst($iid); | |
131 | $app->closing(); | |
132 | ||
133 | $this->db->where('iid', $iid); | |
134 | $this->db->delete('session_apps'); | |
135 | } | |
136 | ||
137 | // UNUSED HERE, REMOVE | |
138 | public function do_login($uid) { | |
139 | $this->db->select('display_name, level'); | |
140 | $this->db->where('uid', $uid); | |
141 | $q = $this->db->get('users'); | |
142 | if ($q->num_rows() == 0) | |
143 | return FALSE; | |
144 | $row = $q->row(); | |
145 | $this->uid($uid); | |
146 | $this->display_name($row->display_name); | |
147 | $this->level($row->level); | |
148 | ||
149 | $sdata = array( | |
150 | 'uid' => $uid, | |
151 | 'started' => time(), | |
152 | 'last' => time(), | |
153 | 'lockip' => $this->input->ip_address(), | |
154 | ); | |
155 | $this->db->insert('sessions', $sdata); | |
156 | $this->sid($this->db->insert_id()); | |
157 | ||
158 | return $this->sid(); | |
159 | } | |
160 | ||
161 | public function sid($new=NULL) { | |
162 | if (!empty($new)) { | |
163 | $old = $this->cached_sid; | |
164 | $this->cached_sid = $new; | |
165 | return $old; | |
166 | } | |
167 | ||
168 | if (isset($this->cached_sid) && $this->cached_sid != 0) { | |
169 | return $this->cached_sid; | |
170 | } else { | |
171 | // TODO IP-lock checking... | |
172 | $this->cached_sid = $this->input->cookie('session_id'); | |
173 | if ($this->cached_sid != 0) { | |
174 | return $this->cached_sid; | |
175 | } | |
176 | } | |
177 | return FALSE; // fallback to this | |
178 | } | |
179 | } |