use md5sum on ini files to determine if they need re-writing
[pear] / HTML / FlexyFramework.php
index 04928a7..b9297b7 100755 (executable)
@@ -153,7 +153,7 @@ class HTML_FlexyFramework {
             $this->$k = $v;
         }
         $this->_parseConfig();
-        
+
         // echo '<PRE>'; print_r($this);exit;
         if ($this->cli) {
             $args = $_SERVER['argv'];
@@ -162,7 +162,7 @@ class HTML_FlexyFramework {
             $this->_run($this->run,false,$args);
             return;
         }
-    
+
         // handle apache mod_rewrite..
         // it looks like this might not work anymore..
         
@@ -208,12 +208,21 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
      */
     function loadModuleConfig($cfg)
     {
+         
+        $proj = $cfg['project'];
+        $rootDir = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
+        
+        $cls = $proj.'_Config';
+         if (file_exists($rootDir . '/'.str_replace('_','/', $cls). '.php')) {
+            require_once str_replace('_','/', $cls). '.php';
+            $c = new $cls();
+            if (method_exists($c,'init')) {
+                $cfg = $c->init($this,$cfg);
+            }
+        }
         if (empty($cfg['enable'])) {
             return $cfg;
         }
-        $proj = $cfg['project'];
-        $rootDir = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
-
         foreach(explode(',',$cfg['enable']) as $m) {
             $cls = $proj.'_'. $m . '_Config';
 
@@ -298,8 +307,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         }
         
          $this->classPrefix   = str_replace('/', '_', $this->project) . '_';
-        
-        // list the available options..
+         // list the available options..
         if ($this->cli && empty($_SERVER['argv'][1])) {
             require_once 'HTML/FlexyFramework/Cli.php';
             $fcli = new HTML_FlexyFramework_Cli($this);
@@ -344,7 +352,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         }
 
         $this->_validateDatabase();
+
         $this->_validateTemplate();
         
     }
@@ -464,7 +472,8 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
      * overlay array properties..
      */
     
-    function applyIf($prop, $ar) {
+    function applyIf($prop, $ar)
+    {
         if (!isset($this->$prop)) {
             $this->$prop = $ar;
             return;
@@ -495,7 +504,13 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
     function _configDataObjectsCache()
     {
         // cli works under different users... it may cause problems..
+        
         $this->debug(__METHOD__);
+        
+        if ($this->database === false) {
+            return;
+        }
+        
         if (function_exists('posix_getpwuid')) {
             $uinfo = posix_getpwuid( posix_getuid () ); 
             $user = $uinfo['name'];
@@ -506,7 +521,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         
 
         $iniCache = ini_get('session.save_path') .'/' . 
-               'dbcfg-' . $user . '/'. str_replace('/', '_', $this->project) ;
+               'dbcfg-' . $user . '/'. str_replace('/', '_', $this->project);
         
         
         if ($this->appNameShort) {
@@ -515,9 +530,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         if ($this->version) {
             $iniCache .= '.' . $this->version;
         }
-        if ($this->database === false) {
-            return;
-        }
+       
         
         $dburl = parse_url($this->database);
         if (!empty($dburl['path'])) {
@@ -535,15 +548,14 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
             $this->dataObjectsOriginalIni = $this->DB_DataObject[$dbini];
             ///print_r($this->DB_DataObject);exit;
         }
-        // 
-        
-        
-        
+          
+         
         $this->DB_DataObject[$dbini] =   $iniCache;
         // we now have the configuration file name..
         
         
         if (!file_exists($iniCache) || empty( $this->dataObjectsCacheExpires)) {
+            $this->debug(__METHOD__ . ':calling generate do cache');
             $this->generateDataobjectsCache(true);
             return;
         }
@@ -564,7 +576,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
     {
         //$this->debug('generateDataobjectsCache: force=' . ($force ? 'yes' : 'no'));
         if (!$this->dataObjectsCache) { // does not use dataObjects Caching..
-            $this->debug('generateDataobjectsCache', 'dataObjectsCache - empty');
+            $this->debug('generateDataobjectsCachedataObjectsCache - empty');
             return;
         }
         
@@ -573,10 +585,18 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         
         
         $iniCache = $this->DB_DataObject[$dbini];
-        if ($force && file_exists($iniCache)) {
-            unlink($iniCache);
-            clearstatcache();
+        $this->debug('generateDataobjectsCache:' .dirname($iniCache).'/*.ini');
+        
+        $replace = array();
+        
+        if (file_exists($iniCache)) {
+            $files = glob(dirname($iniCache).'/*.ini');
+            foreach($files as $f) {
+                $replace[$f] = md5(file_get_contents($f)); // hash it..
+               
+            }
         }
+        $this->debug('generateDataobjectsCache: DONE ini delete');
         
         $iniCacheTmp = $iniCache . '.tmp' .md5(rand());  // random to stop two processes using the same file.
         // has it expired..
@@ -610,16 +630,22 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         $this->_exposeToPear(); // this will reset the debug level...
         DB_DataObject::DebugLevel($dl);
         
+        $this->debug('generateDataobjectsCache: running generator');
         // DB_DataObject::debugLevel(1);      
         require_once 'HTML/FlexyFramework/Generator.php';
         $generator = new HTML_FlexyFramework_Generator();
         $generator->start();
-        
-        HTML_FlexyFramework_Generator::writeCache($iniCacheTmp, $iniCache); 
+        $this->debug('generateDataobjectsCache: done generator');
+
+        HTML_FlexyFramework_Generator::writeCache($iniCacheTmp, $iniCache, $replace); 
         // reset the cache to the correct lcoation.
         $this->DB_DataObject[$dbini] = $iniCache;
-        $this->_exposeToPear();
         
+         
+
+        $this->_exposeToPear();
+        DB_DataObject::DebugLevel($dl);
+
         //$GLOBALS['_DB_DATAOBJECT']['INI'][$this->database] =   parse_ini_file($iniCache, true);
         //$GLOBALS['_DB_DATAOBJECT']['SEQUENCE']
         // clear any dataobject cache..
@@ -840,7 +866,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         /* have I been initialized */
         
         
-        if (get_magic_quotes_gpc() && !$this->cli) {
+        if (version_compare(PHP_VERSION, '7.0.0') < 0  && get_magic_quotes_gpc() && !$this->cli) {
             $this->fatalError(
                 "magic quotes is enabled add the line<BR>
                    php_value magic_quotes_gpc 0<BR>
@@ -885,9 +911,15 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         if (PEAR::isError($err = $x->getDatabaseConnection())) {
                                 
 
-                $this->fatalError("Configuration or Database Error: could not connect to Database, <BR>
-                    Please check the value given to HTML_FlexyFramework, or run with debug on!<BR>
-                     <BR> ".$err->toString());
+            $this->fatalError("Configuration or Database Error: could not connect to Database, <BR>
+                Please check the value given to HTML_FlexyFramework, or run with debug on!<BR>
+                 <BR> ".$err->toString());
+        }
+        $res = $err->query("SELECT @@global.read_only as ro");
+        
+        $row = is_a($res, 'DB_Error') ? false : $res->fetchRow(DB_FETCHMODE_ASSOC);
+        if (!$row || (!empty($row['ro']) && empty($options['skip-read-only-check']))) {
+            $this->fatalError("Database is configured to be read-only - please check database<BR> ".$err->toString());
         }
         // reset dont die!
         $options['dont_die'] = $dd ;
@@ -1016,6 +1048,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         $classobj->bootLoader  = $this;
         $classobj->request = $newRequest;
         $classobj->timer = &$this->timer;
+        $classobj->cli = $this->cli;
         
         $this->page = $classobj;
         if ($this->cli && !$isRedirect ) { // redirect always just takes redirect args..
@@ -1028,6 +1061,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         
         // echo '<PRE>'; print_r($this);exit;
         // echo "CHECK GET AUTH?";
+
         if (!method_exists($classobj, 'getAuth')) {
         //    echo "NO GET AUTH?";
             $this->fatalError("class $classname does not have a getAuth Method");
@@ -1055,6 +1089,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
                 return $result;
             }
         }
+                    
         /* allow redirect from start */
         if (method_exists($classobj,"start")) {
             if (is_string($redirect = $classobj->start($subRequest,$isRedirect,$args)))  {
@@ -1124,7 +1159,8 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         }
         
         $startRequest = $request;
-        $request =@ array_shift(explode('?', $request));
+        $ra = explode('?', $request);
+        $request =  array_shift($ra);
         $this->debug("INPUT REQUEST $request<BR>");
         if (!$isRedirect) {
             // check that request forms contains baseurl????
@@ -1235,7 +1271,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         }
         
         
-        $request_array=explode("/",$request);
+        $request_array = explode("/",$request);
         $original_request_array = $request_array;
         $sub_request_array = array();
         $l = count($request_array)-1;
@@ -1399,7 +1435,8 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
             return true;
         }
         // file exists, but process might not be the same..
-        $name = array_pop(explode('_', get_class($class)));
+        $ea = explode('_', get_class($class));
+        $name = array_pop($ea);
         $cmd = file_get_contents('/proc/' . $oldpid.'/cmdline');
         if (!preg_match('/php/i',$cmd) || !preg_match('/'.$name.'/i',$cmd)) {
             file_put_contents($lock, getmypid());
@@ -1473,7 +1510,9 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
             HTML_FlexyFramework::run($this->fatalAction,$msg);
             exit;
         }
-        
+        header('HTTP/1.1 503 Service Temporarily Unavailable');
+        header('Status: 503 Service Temporarily Unavailable');
+        header('Retry-After: 300');
         echo $this->cli ? $msg ."\n" : "<H1>$msg</H1>configuration information<PRE>";
         if ($showConfig) {