fix image text
[pear] / HTML / FlexyFramework.php
index 5ae3460..8cad346 100755 (executable)
@@ -71,7 +71,7 @@ error_reporting(E_ALL & ~E_STRICT );
 * it will try staging.ini
 *
 */
+#[AllowDynamicProperties]
 class HTML_FlexyFramework {
     
     /**
@@ -95,7 +95,8 @@ class HTML_FlexyFramework {
     var $dataObjectsCacheExpires = 72000; // 20 hours..
     var $languages = false; // language settings -- see _handlelanguage
     var $projectExtends = false; // if this is an array, it's a fallback of 'Projects' that can be called
-    
+    var $database_is_readonly = false;
+     
 
     
     // derived.
@@ -153,7 +154,7 @@ class HTML_FlexyFramework {
             $this->$k = $v;
         }
         $this->_parseConfig();
-        
+
         // echo '<PRE>'; print_r($this);exit;
         if ($this->cli) {
             $args = $_SERVER['argv'];
@@ -162,7 +163,7 @@ class HTML_FlexyFramework {
             $this->_run($this->run,false,$args);
             return;
         }
-    
+
         // handle apache mod_rewrite..
         // it looks like this might not work anymore..
         
@@ -178,14 +179,18 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
 </IfModule>
 */ 
         
-        if (empty($_SERVER['REDIRECT_STATUS'])  && !empty($_SERVER['REDIRECT_URL'])) {
-            
-            $this->_run($_SERVER['SCRIPT_NAME'] . $_SERVER['REQUEST_URI'],false);
+        if (!empty($_SERVER['REDIRECT_STATUS'])  && !empty($_SERVER['REDIRECT_URL'])) {
+          // phpinfo();exit;
+            $sn = $_SERVER['SCRIPT_NAME'];
+            $sublen = strlen(substr($sn , 0,  strlen($sn) - strlen(basename($sn)) -1));
+            //var_dump(array($sn,$subdir,basename($sn)));exit;
+          
+            //var_dump($_SERVER['SCRIPT_NAME'] . substr($_SERVER['REDIRECT_URL'],$sublen));
+            $this->_run($_SERVER['SCRIPT_NAME'] .  substr($_SERVER['REDIRECT_URL'], $sublen),false);
             return ;
         }
-        
-        
-        $this->_run($_SERVER['REQUEST_URI'],false);
+        // eg... /web.hpasite/index.local.php/Projects
+         $this->_run($_SERVER['REQUEST_URI'],false);
             
         
     }
@@ -204,12 +209,24 @@ 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';
 
@@ -242,7 +259,6 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
             }
         }
         
-        $this->_handleLanguages();
         
         // enable modules.
         if (!empty($this->enable)) {
@@ -263,21 +279,30 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         if (!$this->cli) {
             $bits[0] = str_replace('%2F','/',urlencode($bits[0]));
             $this->baseURL = $bits[0] . basename($_SERVER["SCRIPT_FILENAME"]);
-            //phpinfo();exit;
-            if (empty($_SERVER['SCRIPT_NAME'])) {
+            // however this is not correct if we are using rewrite..
+            if (!empty($_SERVER['REDIRECT_STATUS'])  && !empty($_SERVER['REDIRECT_URL'])) {
+                $this->baseURL = substr($bits[0],0,-1); // without the trailing '/' ??
+                $this->rootURL = $bits[0] == '/' ? '' : $bits[0];
+                //$this->baseURL = $this->baseURL == '' ? '/' : $this->baseURL;
                 
-                $this->baseURL = ''; // ??? this is if we replace top level...
             }
+            //phpinfo();exit;
+            // is this bit used??
+            //if (empty($_SERVER['SCRIPT_NAME'])) {
+                
+            //    $this->baseURL = ''; // ??? this is if we replace top level...
+            //}
         }
         // if cli - you have to have set baseURL...
         
         
         $this->rootDir = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
         $this->baseDir = $this->rootDir .'/'. $this->project;
-        $this->rootURL = dirname($this->baseURL);
-        $this->rootURL = ($this->rootURL == '/') ? '' : $this->rootURL;
-        
-        
+        if (empty($this->rootURL)) {
+            $this->rootURL = dirname($this->baseURL); 
+            $this->rootURL = ($this->rootURL == '/') ? '' : $this->rootURL;
+        }
+         
       
         //var_dump($this->baseURL);
         
@@ -286,8 +311,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);
@@ -304,7 +328,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
             $res = $fcli->parseDefaultOpts();
             if ($res === true) {
                 $ishelp = true;
-            } 
+            }
              
         }
         
@@ -332,7 +356,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         }
 
         $this->_validateDatabase();
+
         $this->_validateTemplate();
         
     }
@@ -352,13 +376,13 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         ),
     */
     
-    function _handleLanguages()
+    function _handleLanguages($request)
     {
         if (
-                empty($this->languages) ||
-                (
-                        !isset($this->languages['cookie']) && !isset($this->languages['default'])
-                )
+            empty($this->languages) ||
+            (
+                    !isset($this->languages['cookie']) && !isset($this->languages['default'])
+            )
         ) {
             return;
         }
@@ -387,6 +411,17 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
            
         $lang = isset($_COOKIE[$cfg['cookie']]) ?  $_COOKIE[$cfg['cookie']] : $default;
 
+        // handle languages in request..
+        $bits = explode('/', $request);
+        $redirect_to = false;
+        if (count($bits) && in_array($bits[0],$cfg['avail'])) {
+            // redirect..
+            $lang = array_shift($bits);
+            $redirect_to = implode('/', $bits);
+        }
+        
+         
+        
         if (isset($_REQUEST[$cfg['param']])) {
             $lang = $_REQUEST[$cfg['param']];
         }
@@ -404,7 +439,11 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         if (!empty($this->HTML_Template_Flexy)) {
             $this->HTML_Template_Flexy['locale'] = $lang;   //set a language for template engine
         }
+        if ($redirect_to !== false) {
+            header('Location: ' . $this->rootURL . '/'.$redirect_to );
+            exit;
          
+        }
     }
     
     function parseDefaultLanguage($http_accept, $deflang = "en") 
@@ -437,7 +476,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;
@@ -468,7 +508,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'];
@@ -479,7 +525,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) {
@@ -488,9 +534,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'])) {
@@ -508,15 +552,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;
         }
@@ -537,7 +580,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;
         }
         
@@ -546,10 +589,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..
@@ -578,19 +629,27 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         }
         
         $this->DB_DataObject[$dbini] = $iniCacheTmp;
-        $this->_exposeToPear();
         
+        $dl = DB_DataObject::DebugLevel();
+        $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..
@@ -811,7 +870,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>
@@ -856,9 +915,22 @@ 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());
+        }
+        // only applies to mysql...
+        
+        if (preg_match('/^mysql/', $this->database)) {
+            $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'])) {
+                if (empty($options['skip-read-only-check'])) {
+                    $this->fatalError("Database is configured to be read-only - please check database<BR> ".$err->toString());
+                }
+                $this->database_is_readonly = true;
+            }
         }
         // reset dont die!
         $options['dont_die'] = $dd ;
@@ -872,10 +944,9 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         if (empty($this->HTML_Template_Flexy['compileDir'])) {
             return;
         }
-        
+        clearstatcache();
         if ( !file_exists($this->HTML_Template_Flexy['compileDir']))  {
-            mkdir($this->HTML_Template_Flexy['compileDir'], 0700);
-            @mkdir($this->HTML_Template_Flexy['compileDir'], 0700, true);
+            mkdir($this->HTML_Template_Flexy['compileDir'], 0700, true);
             clearstatcache();
              
             if ( !file_exists($this->HTML_Template_Flexy['compileDir']))  {
@@ -962,6 +1033,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         
         $newRequest = $this->_getRequest($request,$isRedirect);
         
+         
         // find the class/file to load
         list($classname,$subRequest) = $this->requestToClassName($newRequest,FALSE);
         
@@ -986,6 +1058,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..
@@ -998,6 +1071,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");
@@ -1025,6 +1099,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)))  {
@@ -1088,27 +1163,43 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
     function _getRequest($request, $isRedirect) 
     {
         
-        
-        
+         
         if ($this->cli) {
             return $request;
         }
         
         $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????
+            if (!empty($_SERVER['REDIRECT_STATUS'])  && !empty($_SERVER['REDIRECT_URL'])) {
+               // phpinfo();exit;
+                $sn = $_SERVER['SCRIPT_NAME'];
+                $sublen = strlen(substr($sn , 0,  strlen($sn) - strlen(basename($sn)) -1 ));
+                 //var_dump(array($sn,$subdir,basename($sn)));exit;
+                $subreq =  $_SERVER['SCRIPT_NAME'];
+                $request = substr($_SERVER['REDIRECT_URL'],$sublen);
+                
+                 
+            } else {
+                  
              
-            $subreq = substr($request,0,strlen($this->baseURL));
-            if ($subreq != substr($this->baseURL,0,strlen($subreq))) {
-                $this->fatalError(
-                    "Configuration error: Got base of $subreq which does not 
-                        match configuration of: $this->baseURL} ");
+                $subreq = substr($request,0, strlen($this->baseURL));
+                if ($subreq != substr($this->baseURL,0,strlen($subreq))) {
+                    $this->fatalError(
+                        "Configuration error: Got base of $subreq which does not 
+                            match configuration of: $this->baseURL} ");
+                }
+                $request = substr($request,strlen($this->baseURL));
+                
             }
-            $request = substr($request,strlen($this->baseURL));
+            
              
         }
+       // var_Dump(array('req'=>$request,'subreq'=>$subreq));
+        
         // strip front
         // echo "REQUEST WAS: $request<BR>";
         // $request = preg_replace('/^'.preg_quote($base_url,'/').'/','',trim($request));
@@ -1140,14 +1231,23 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
                         $sp .= ':'.((int) $_SERVER['SERVER_PORT']);
                     }
                 }
-                $host = !empty($_SERVER["HTTP_X_FORWARDED_HOST"]) ? $_SERVER["HTTP_X_FORWARDED_HOST"] : $_SERVER["HTTP_HOST"];
+                // not sure why http host would not be set?
+                $host = !empty($_SERVER["HTTP_X_FORWARDED_HOST"]) ? $_SERVER["HTTP_X_FORWARDED_HOST"] :
+                    (!isset($_SERVER["HTTP_HOST"]) ? 'localhost' : $_SERVER["HTTP_HOST"]);
+                    
                 header('Location: '.$http.'://'.$host .$sp . $this->baseURL);
  
                 exit;
             }
             $request = "";
         }
+       // var_dump(array($startRequest,$request, $this->baseRequest));
+        
         $this->debug("OUTPUT REQUEST $request<BR>");
+        
+        $this->_handleLanguages($request);
+
+        
         return $request;
     }
     
@@ -1184,7 +1284,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;
@@ -1348,7 +1448,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());
@@ -1422,7 +1523,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) {