--- /dev/null
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+
+/* TODO: error handling */
+
+class Control extends CI_Controller {
+
+ public function __construct() {
+ parent::__construct();
+ $this->load->model('user');
+ $this->load->model('session');
+ }
+
+ public function index() {
+ }
+
+ public function open($aid) {
+ if ($this->user->is_logged_in()) {
+ $instance = $this->session->openApp($aid);
+
+ $repl = array('id' => $instance->iid(), 'name' => $instance->appName(), 'title' => $instance->windowTitle(), 'interior' => $instance->windowContents());
+ $this->json->reply($this->user->is_logged_in());
+ } else {
+ $this->json->error('Not logged in.');
+ }
+ }
+
+ public function act($iid, $action) {
+// return $this->session->getAppInst($iid)->act($action);
+ }
+
+ public function close($iid) {
+ $this->session->closeApp($iid);
+ }
+}
+?>
--- /dev/null
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+
+class Session extends CI_Model {
+ public function __construct() {
+ $this->load->model('user');
+
+ $this->apps = array();
+
+ # Required
+ parent::__construct();
+ }
+
+ protected function setError($error) {
+ $this->lastError = $error;
+ return FALSE;
+ }
+ public function getError() {
+ return $this->lastError;
+ }
+
+ protected function getApp($aid, $iid, $file, $class) {
+ if (!is_file($this->config->item('app_prefix').$file)) {
+ return setError('App file does not exist');
+ }
+ if (!(@include_once $this->config->item('app_prefix').$file)) {
+ return setError('Include error');
+ }
+
+ return new ($class)($iid)
+ }
+ public function getAppInst($iid) {
+ if ($this->apps[$iid]) {
+ return $this->apps[$iid];
+ } else {
+ $this->db->select('aid');
+ $this->db->where('iid', $iid);
+ $q = $this->db->get('session_apps');
+ if ($q->num_rows() == 0)
+ return FALSE;
+ $row = $q->row();
+ $aid = $row->aid;
+
+ $this->db->select('classname, filename');
+ $this->db->where('aid', $aid);
+ $q = $this->db->get('apps');
+ if ($q->num_rows() == 0)
+ return FALSE;
+ $row = $q->row();
+ return $this->getApp($aid, $iid, $row->filename, $row->classname);
+ }
+ }
+
+ public function openApp($aid) {
+ $sid = $this->user->sid();
+ $level = $this->user->level();
+
+ $this->db->select('classname, filename, access');
+ $this->db->where('aid', $aid);
+ $q = $this->db->get('apps');
+ if ($q->num_rows() == 0)
+ return setError('No such app');
+ $row = $q->row();
+ if (!$level)
+ return setError('No access');
+ elseif ($row->access == 'operator' && $level == 'user')
+ return setError('No access');
+ elseif ($row->access == 'manager' && $level != 'maneger')
+ return setError('No access');
+ // they have access, go on
+
+ $idata = array(
+ 'sid' => $this->user->sid(),
+ 'aid' => $aid,
+ );
+ $this->db->insert('session_apps', $idata);
+
+
+ $app = $this->getApp($aid, $this->db->insert_id(), $row->filename, $row->classname);
+ if ($app) {
+ $app->opening();
+ $this->apps[$iid] = $app;
+ }
+ return $app;
+ }
+ public function closeApp($iid) {
+ $app = $this->getAppInst($iid);
+ $app->closing();
+
+ $this->db->where('iid', $iid);
+ $this->db->delete('session_apps');
+ }
+
+ // UNUSED HERE, REMOVE
+ public function do_login($uid) {
+ $this->db->select('display_name, level');
+ $this->db->where('uid', $uid);
+ $q = $this->db->get('users');
+ if ($q->num_rows() == 0)
+ return FALSE;
+ $row = $q->row();
+ $this->uid($uid);
+ $this->display_name($row->display_name);
+ $this->level($row->level);
+
+ $sdata = array(
+ 'uid' => $uid,
+ 'started' => time(),
+ 'last' => time(),
+ 'lockip' => $this->input->ip_address(),
+ );
+ $this->db->insert('sessions', $sdata);
+ $this->sid($this->db->insert_id());
+
+ return $this->sid();
+ }
+
+ public function sid($new=NULL) {
+ if (!empty($new)) {
+ $old = $this->cached_sid;
+ $this->cached_sid = $new;
+ return $old;
+ }
+
+ if (isset($this->cached_sid) && $this->cached_sid != 0) {
+ return $this->cached_sid;
+ } else {
+ // TODO IP-lock checking...
+ $this->cached_sid = $this->input->cookie('session_id');
+ if ($this->cached_sid != 0) {
+ return $this->cached_sid;
+ }
+ }
+ return FALSE; // fallback to this
+ }
+}
-var state
-var wos
-
(function( $ ) {
$.fn.pageConstruct = function( initvar ) {
this.loadDefaults();
}
- return this;
+ this.openApp = function(appid) {
+ $.getJSON("/control/open/"+appid, function(resp) {
+ if (resp.success) {
+ var repl = resp.contents
+ var target = 'div#'+repl.name;
+
+ if ($(target).length != 0) {
+ this.closeApp(target);
+ }
+
+ this.apps[repl.name] = {aid: appid, instance: repl.id, title: repl.title, target: target};
+
+ $('body').append('<div id="'+repl.name+'" class="modal hide fade"></div>');
+
+ $(target).append('<div class="modal-header"></div>');
+ $(target).append('<div class="modal-body"></div>');
+ $(target).append('<div class="modal-footer"></div>');
+
+ $(target+'>.modal-header').append('<button class="btn appclose" aria-hidden="true" onClick="wos.closeApp(\'#'+repl.name+'\');void(0);">×</button>');
+ $(target+'>.modal-header').append('<h3 class="appLabel">'+repl.title+'</h3>');
+
+ $(target+'>.modal-body').append(repl.interior);
+ } else {
+ throwError(resp.error, 'error', '#desktop');
+ }
+ });
+ }
+
+ this.openCoreApp = function(appname) {
+ }
+
+ this.closeApp = function(target) {
+ $(target).modal('hide');
+ $(target).remove();
+ }
+
+ return this;
};
})( jQuery );