]>
jfr.im git - z_archive/KronOS.git/blob - application/models/msession.php
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
3 /* TODO: check if app was succesfully opened, if not delete the instance */
5 class Msession
extends CI_Model
{
6 public function __construct() {
7 $this->load
->model('user');
12 parent
::__construct();
15 protected function setError($error) {
16 trigger_error($error);
17 $this->lastError
= $error;
20 public function getError() {
21 return $this->lastError
;
24 protected function getApp($aid, $iid, $file, $class) {
25 if (!is_file($this->config
->item('app_prefix').$file)) {
26 return $this->setError('App file does not exist');
28 if (!(include_once $this->config
->item('app_prefix').$file)) {
29 return $this->setError('Include error');
32 return new $class($iid);
34 public function getAppInst($iid) {
35 if ($this->apps
[$iid]) {
36 return $this->apps
[$iid];
38 $this->db
->select('aid');
39 $this->db
->where('iid', $iid);
40 $q = $this->db
->get('session_apps');
41 if ($q->num_rows() == 0)
46 $this->db
->select('classname, filename');
47 $this->db
->where('aid', $aid);
48 $q = $this->db
->get('apps');
49 if ($q->num_rows() == 0)
52 return $this->getApp($aid, $iid, $row->filename
, $row->classname
);
56 public function openCoreApp($name) {
58 'sid' => $this->user
->sid(),
61 $this->db
->insert('session_apps', $idata);
62 $iid = $this->db
->insert_id();
64 $app = $this->getApp(-1, $iid, 'core/'.$name.'.php', ucfirst($name));
67 $this->apps
[$iid] = $app;
71 public function openApp($aid) {
72 $sid = $this->user
->sid();
73 $level = $this->user
->level();
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)
79 return $this->setError('No such app');
82 return $this->setError('No access');
83 elseif ($row->access
== 'operator' && $level == 'user')
84 return $this->setError('No access');
85 elseif ($row->access
== 'manager' && $level != 'manager')
86 return $this->setError('No access');
87 // they have access, go on
90 'sid' => $this->user
->sid(),
93 $this->db
->insert('session_apps', $idata);
94 $iid = $this->db
->insert_id();
96 $app = $this->getApp($aid, $iid, $row->filename
, $row->classname
);
99 $this->apps
[$iid] = $app;
103 public function closeApp($iid) {
104 $app = $this->getAppInst($iid);
107 $this->db
->where('iid', $iid);
108 $this->db
->delete('session_apps');
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)
120 $this->display_name($row->display_name
);
121 $this->level($row->level
);
127 'lockip' => $this->input
->ip_address(),
129 $this->db
->insert('sessions', $sdata);
130 $this->sid($this->db
->insert_id());
135 public function sid($new=NULL) {
137 $old = $this->cached_sid
;
138 $this->cached_sid
= $new;
142 if (isset($this->cached_sid
) && $this->cached_sid
!= 0) {
143 return $this->cached_sid
;
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
;
151 return FALSE; // fallback to this