]> jfr.im git - z_archive/KronOS.git/commitdiff
Starting work on app framework. Changes to config.php
authorJohn Runyon <redacted>
Sat, 27 Oct 2012 13:29:35 +0000 (16:29 +0300)
committerJohn Runyon <redacted>
Sat, 27 Oct 2012 13:29:35 +0000 (16:29 +0300)
application/config/config.php.example
application/controllers/control.php [new file with mode: 0644]
application/models/session.php [new file with mode: 0644]
kosapps/common.php [new file with mode: 0644]
public/js/application.js

index f741122452e12f0d54bd1e5ac0da87ac011cc4b4..9fcb3b6152523daffde0a001e3ef74abd7fabac2 100644 (file)
@@ -1,5 +1,18 @@
 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
+/*
+|--------------------------------------------------------------------------
+| Base path to apps
+|--------------------------------------------------------------------------
+|
+| Path to apps, WITH trailing slash:
+| /var/www/kosapps/
+|
+| Must be absolute.
+|
+*/
+$config['app_prefix'] = '/var/www/kosapps/';
+
 /*
 |--------------------------------------------------------------------------
 | Base Site URL
diff --git a/application/controllers/control.php b/application/controllers/control.php
new file mode 100644 (file)
index 0000000..3ee3685
--- /dev/null
@@ -0,0 +1,35 @@
+<?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);
+       }
+}
+?>
diff --git a/application/models/session.php b/application/models/session.php
new file mode 100644 (file)
index 0000000..7e3c161
--- /dev/null
@@ -0,0 +1,135 @@
+<?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
+       }
+}
diff --git a/kosapps/common.php b/kosapps/common.php
new file mode 100644 (file)
index 0000000..524b29b
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+abstract class KOS_App {
+       abstract function __construct($iid);
+       abstract public function iid();
+       abstract public function appName();
+       abstract public function windowTitle();
+       abstract public function windowContents();
+
+       abstract public function opening();
+       abstract public function closing();
+
+       abstract public function act($action);
+}
+
+?>
index bf4bf4be2fdb1ea7431e7a78a3e86e8ce73472a1..382c1bb85d28bcb26228381cc18874c5da2ea8af 100644 (file)
@@ -1,6 +1,3 @@
-var state
-var wos
-
 (function( $ ) {
        $.fn.pageConstruct = function( initvar ) {
 
@@ -167,7 +164,43 @@ var wos
                        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);">&times;</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 );