Pman/Roo.php
[Pman.Base] / Pman.php
index 535fd86..66d6e83 100644 (file)
--- a/Pman.php
+++ b/Pman.php
@@ -16,7 +16,9 @@
  *  - authentication reset password ?? MOVEME?
  *  ?? arrayClean.. what's it doing here?!? ;)
  * 
- * 
+ * Usefull implemetors
+ * DB_DataObject*:*toEventString (for logging - this is generically prefixed to all database operations.)
+ *   - any data object where this method exists, the result will get prefixed to the log remarks
  */
 
 class Pman extends HTML_FlexyFramework_Page 
@@ -32,6 +34,8 @@ class Pman extends HTML_FlexyFramework_Page
     var $appModules = '';
     
     
+    var $authUser; // always contains the authenticated user..
+    
    
     
     /**
@@ -52,35 +56,26 @@ class Pman extends HTML_FlexyFramework_Page
     {
         if (isset($this->_hasInit)) {
             return;
-            
         }
         $this->_hasInit = true;
-        /*
-        if (method_exists('HTML_FlexyFramework', 'get')) {
-        */    
-            $boot = HTML_FlexyFramework::get();
-           // echo'<PRE>';print_R($boot);exit;
-            $this->appName= $boot->appName;
-            $this->appNameShort= $boot->appNameShort;
-            $this->appModules= $boot->enable;
-            $this->isDev = true; //empty($opts['isDev']) ? '' : $opts['isDev'];
-            $this->appDisable = $boot->disable;
-            $this->version = $boot->version;
-        /*    
-        } else {
-            // BC!!!
-           
-            $opts = PEAR::getStaticProperty('Pman', 'options');  
-            
-            $this->isDev = true; //empty($opts['isDev']) ? '' : $opts['isDev'];
-            
-            $this->appName= empty($opts['appName']) ? '' : $opts['appName'];
-            $this->appNameShort= empty($opts['appNameShort']) ? '' : $opts['appNameShort'];
-            $this->appModules= $opts['enable'];
-            $this->appDisable = $opts['disable'];
-            $this->version = isset($opts['version']) ? $this->version : $opts['version'];
+          
+        $boot = HTML_FlexyFramework::get();
+        // echo'<PRE>';print_R($boot);exit;
+        $this->appName= $boot->appName;
+        $this->appNameShort= $boot->appNameShort;
+        $this->appModules= $boot->enable;
+        $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
+        $this->appDisable = $boot->disable;
+        $this->version = $boot->version;
+        
+        if (!empty($ff->Pman['local_autoauth']) && 
+            ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
+            ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') 
+        ) {
+            $this->isDev = true;
         }
-        */
+        
+
     }
     
     function get($base) 
@@ -116,6 +111,9 @@ class Pman extends HTML_FlexyFramework_Page
         }
         // getting this to work with xhtml is a nightmare
         // = nbsp / <img> issues screw everyting up.
+         //var_dump($this->isDev);
+        // force regeneration on load for development enviroments..
+        HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
         
         //header('Content-type: application/xhtml+xml; charset=utf-8');
         header('Content-type: text/html; charset=utf-8');
@@ -173,22 +171,44 @@ class Pman extends HTML_FlexyFramework_Page
         return $au->hasPerm($name,$lvl);
         
     }
+    
+    function modules()
+    {
+        // appModules/appDisable contain a comma limited list of
+        // both modules and components that can be enabled/disabled..
+         $boot = HTML_FlexyFramework::get();
+
+        // the modules call just lists the modules
+        $enabled =  array('Core' => true);
+         $am = !empty($boot->enable) ? explode(',',  $boot->enable) : array();
+        foreach($am as $k) {
+            if (strpos( $k ,'.') ) {
+                continue;
+            }
+            $enabled[$k] = true;
+        }
+        
+        
+        $disabled =  !empty($boot->disable) ?  explode(',', $boot->disable) : array();
+        foreach($disabled as $k) {
+            if ( strpos( $k ,'.') ) {
+                continue;
+            }
+            if (isset($enabled[$k])) {
+                unset($enabled[$k]);
+            }   
+        }
+         //echo '<PRE>';       var_Dump($enabled);
+
+        return array_keys($enabled); 
+    }
+    
     function hasModule($name) 
     {
         $this->init();
         if (!strpos( $name,'.') ) {
             // use enable / disable..
-            
-            
-            $enabled =  array('Core') ;
-            $enabled = !empty($this->appModules) ? 
-                array_merge($enabled, explode(',',  $this->appModules)) : 
-                $enabled;
-            $disabled =  explode(',', $this->appDisable ? $this->appDisable: '');
-            
-            //print_R($opts);
-            
-            return in_array($name, $enabled) && !in_array($name, $disabled);
+            return in_array($name, $this->modules()); 
         }
         
         $x = DB_DataObject::factory('Group_Rights');
@@ -401,6 +421,9 @@ class Pman extends HTML_FlexyFramework_Page
         require_once 'Services/JSON.php';
         $json = new Services_JSON();
         
+        // log all errors!!!
+        $this->addEvent("ERROR", false, $str);
+        
         if (!empty($_REQUEST['returnHTML']) || 
             (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']))
         ) {
@@ -409,7 +432,7 @@ class Pman extends HTML_FlexyFramework_Page
             echo  $json->encodeUnsafe(array(
                     'success'=> false, 
                     'errorMsg' => $str,
-                     'message' => $str, // compate with exeption / loadexception.
+                    'message' => $str, // compate with exeption / loadexception.
 
                     'errors' => $errors ? $errors : true, // used by forms to flag errors.
                     'authFailure' => !empty($errors['authFailure']),
@@ -417,7 +440,7 @@ class Pman extends HTML_FlexyFramework_Page
             echo "</BODY></HTML>";
             exit;
         }
-       
+        
         echo $json->encode(array(
             'success'=> false, 
             'data'=> array(), 
@@ -441,7 +464,9 @@ class Pman extends HTML_FlexyFramework_Page
         ) {
             header('Content-type: text/html');
             echo "<HTML><HEAD></HEAD><BODY>";
-            echo  $json->encodeUnsafe(array('success'=> true, 'data' => $str));
+            // encode html characters so they can be read..
+            echo  str_replace(array('<','>'), array('\u003c','\u003e'),
+                        $json->encodeUnsafe(array('success'=> true, 'data' => $str)));
             echo "</BODY></HTML>";
             exit;
         }
@@ -491,11 +516,20 @@ class Pman extends HTML_FlexyFramework_Page
     {
         
         $mods = explode(',', $this->appModules);
-        array_unshift($mods,   'Core');
+        if (in_array('Core',$mods)) { // core has to be the first  modules loaded as it contains Pman.js
+            array_unshift($mods,   'Core');
+        }
+        
         $mods = array_unique($mods);
+         
+        $disabled =  explode(',', $this->appDisable ? $this->appDisable: '');
         
         foreach($mods as $mod) {
             // add the css file..
+            if (in_array($mod, $disabled)) {
+                continue;
+            }
+            
             
             $files = $this->moduleJavascriptList($mod.'/widgets');
             foreach($files as $f) {
@@ -557,10 +591,20 @@ class Pman extends HTML_FlexyFramework_Page
         if (file_exists($cfile)) {
            // $ctime = max(filemtime($cfile), filectime($cfile));
             // otherwise use compile dfile..
-            $cfile = array_pop(glob($cfile / '/' . $mod + '*.js'));
-            echo $cfile;exit;
+            $maxm = 0;
+            $ar = glob($cfile . '/' . $mod . '*.js');
+            // default to first..
+            $cfile = basename($ar[count($ar) -1]);
+            foreach($ar as $fn) {
+                if (filemtime($fn) > $maxm) {
+                    $cfile = basename($fn);
+                    $maxm = filemtime($fn);
+                }
+                
+            }
             
-            $files = array( $this->rootURL."/_compiled_/". basename($cfile));
+             
+            $files = array( $this->rootURL. "/_compiled_/".$mod . "/" . $cfile);
             if (file_exists($lfile)) {
                 array_push($files, $this->rootURL."/_translations_/$mod.js");
             }
@@ -601,26 +645,59 @@ class Pman extends HTML_FlexyFramework_Page
      * ---------------- Logging ---------------   
      */
     
+    /**
+     * addEventOnce:
+     * Log an action (only if it has not been logged already.
+     * 
+     * @param {String} action  - group/name of event
+     * @param {DataObject|false} obj - dataobject action occured on.
+     * @param {String} any remarks 
+     */
     
+    function addEventOnce($act, $obj = false, $remarks = '') 
+    {
+        $au = $this->getAuthUser();
+        $e = DB_DataObject::factory('Events');
+        $e->init($act,$obj,$remarks); 
+        if ($e->find(true)) {
+            return;
+        }
+        $this->addEvent($act, $obj, $remarks);
+    }
+    /**
+     * addEvent:
+     * Log an action.
+     * 
+     * @param {String} action  - group/name of event
+     * @param {DataObject|false} obj - dataobject action occured on.
+     * @param {String} any remarks
+     * @return {Number} id of event created.
+     */
     
-    
-    
-    function addEvent($act, $obj = false, $remarks = '') {
+    function addEvent($act, $obj = false, $remarks = '') 
+    {
         $au = $this->getAuthUser();
         $e = DB_DataObject::factory('Events');
+        $e->init($act,$obj,$remarks); 
+         
+        $e->event_when = date('Y-m-d H:i:s');
         
-        if (is_a($e, 'PEAR_Error')) {
-            return; // no event table!
+        $eid = $e->insert();
+        $ff  = HTML_FlexyFramework::get();
+        if (empty($ff->Pman['event_log_dir'])) {
+            return $eid;
         }
-        $e->person_name = $au ? $au->name : '';
-        $e->person_id = $au ? $au->id : '';
-        $e->event_when = date('Y-m-d H:i:s');
-        $e->ipaddr = $_SERVER["REMOTE_ADDR"];
-        $e->action = $act;
-        $e->on_table = $obj ? $obj->tableName() : '';
-        $e->on_id  = $obj ? $obj->id : 0;
-        $e->remarks = $remarks;
-        $e->insert();
+        $file = $ff->Pman['event_log_dir']. date('/Y/m/d/'). $eid . ".php";
+        if (!file_exists(dirname($file))) {
+            mkdir(dirname($file),0700,true);
+        }
+        file_put_contents($file, var_export(array(
+            'REQUEST_URI' => empty($_SERVER['REQUEST_URI']) ? 'cli' : $_SERVER['REQUEST_URI'],
+            'GET' => empty($_GET) ? array() : $_GET,
+            'POST' => empty($_POST) ? array() : $_POST,
+        ), true));
+        
+        return $eid;
         
     }