]>
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 | ||
24 | protected function getApp($aid, $iid, $file, $class) { | |
25 | if (!is_file($this->config->item('app_prefix').$file)) { | |
c1828c8b | 26 | return $this->setError('App file does not exist'); |
3cf8917d | 27 | } |
536db703 | 28 | if (!(include_once $this->config->item('app_prefix').$file)) { |
c1828c8b | 29 | return $this->setError('Include error'); |
3cf8917d JR |
30 | } |
31 | ||
536db703 | 32 | return new $class($iid); |
3cf8917d JR |
33 | } |
34 | public function getAppInst($iid) { | |
35 | if ($this->apps[$iid]) { | |
36 | return $this->apps[$iid]; | |
37 | } else { | |
38 | $this->db->select('aid'); | |
39 | $this->db->where('iid', $iid); | |
40 | $q = $this->db->get('session_apps'); | |
41 | if ($q->num_rows() == 0) | |
42 | return FALSE; | |
43 | $row = $q->row(); | |
44 | $aid = $row->aid; | |
45 | ||
46 | $this->db->select('classname, filename'); | |
47 | $this->db->where('aid', $aid); | |
48 | $q = $this->db->get('apps'); | |
49 | if ($q->num_rows() == 0) | |
50 | return FALSE; | |
51 | $row = $q->row(); | |
52 | return $this->getApp($aid, $iid, $row->filename, $row->classname); | |
53 | } | |
54 | } | |
55 | ||
c1828c8b JR |
56 | public function openCoreApp($name) { |
57 | $idata = array( | |
58 | 'sid' => $this->user->sid(), | |
59 | 'aid' => -1, | |
60 | ); | |
61 | $this->db->insert('session_apps', $idata); | |
62 | $iid = $this->db->insert_id(); | |
63 | ||
64 | $app = $this->getApp(-1, $iid, 'core/'.$name.'.php', ucfirst($name)); | |
65 | if ($app) { | |
66 | $app->opening(); | |
67 | $this->apps[$iid] = $app; | |
68 | } | |
69 | return $app; | |
70 | } | |
3cf8917d JR |
71 | public function openApp($aid) { |
72 | $sid = $this->user->sid(); | |
73 | $level = $this->user->level(); | |
74 | ||
75 | $this->db->select('classname, filename, access'); | |
76 | $this->db->where('aid', $aid); | |
77 | $q = $this->db->get('apps'); | |
78 | if ($q->num_rows() == 0) | |
c1828c8b | 79 | return $this->setError('No such app'); |
3cf8917d JR |
80 | $row = $q->row(); |
81 | if (!$level) | |
c1828c8b | 82 | return $this->setError('No access'); |
3cf8917d | 83 | elseif ($row->access == 'operator' && $level == 'user') |
c1828c8b JR |
84 | return $this->setError('No access'); |
85 | elseif ($row->access == 'manager' && $level != 'manager') | |
86 | return $this->setError('No access'); | |
3cf8917d JR |
87 | // they have access, go on |
88 | ||
89 | $idata = array( | |
90 | 'sid' => $this->user->sid(), | |
91 | 'aid' => $aid, | |
92 | ); | |
93 | $this->db->insert('session_apps', $idata); | |
536db703 | 94 | $iid = $this->db->insert_id(); |
3cf8917d | 95 | |
536db703 | 96 | $app = $this->getApp($aid, $iid, $row->filename, $row->classname); |
3cf8917d JR |
97 | if ($app) { |
98 | $app->opening(); | |
99 | $this->apps[$iid] = $app; | |
100 | } | |
101 | return $app; | |
102 | } | |
103 | public function closeApp($iid) { | |
104 | $app = $this->getAppInst($iid); | |
105 | $app->closing(); | |
106 | ||
107 | $this->db->where('iid', $iid); | |
108 | $this->db->delete('session_apps'); | |
109 | } | |
110 | ||
111 | // UNUSED HERE, REMOVE | |
112 | public function do_login($uid) { | |
113 | $this->db->select('display_name, level'); | |
114 | $this->db->where('uid', $uid); | |
115 | $q = $this->db->get('users'); | |
116 | if ($q->num_rows() == 0) | |
117 | return FALSE; | |
118 | $row = $q->row(); | |
119 | $this->uid($uid); | |
120 | $this->display_name($row->display_name); | |
121 | $this->level($row->level); | |
122 | ||
123 | $sdata = array( | |
124 | 'uid' => $uid, | |
125 | 'started' => time(), | |
126 | 'last' => time(), | |
127 | 'lockip' => $this->input->ip_address(), | |
128 | ); | |
129 | $this->db->insert('sessions', $sdata); | |
130 | $this->sid($this->db->insert_id()); | |
131 | ||
132 | return $this->sid(); | |
133 | } | |
134 | ||
135 | public function sid($new=NULL) { | |
136 | if (!empty($new)) { | |
137 | $old = $this->cached_sid; | |
138 | $this->cached_sid = $new; | |
139 | return $old; | |
140 | } | |
141 | ||
142 | if (isset($this->cached_sid) && $this->cached_sid != 0) { | |
143 | return $this->cached_sid; | |
144 | } else { | |
145 | // TODO IP-lock checking... | |
146 | $this->cached_sid = $this->input->cookie('session_id'); | |
147 | if ($this->cached_sid != 0) { | |
148 | return $this->cached_sid; | |
149 | } | |
150 | } | |
151 | return FALSE; // fallback to this | |
152 | } | |
153 | } |