fix image text
[pear] / HTML / FlexyFramework2.php
index 26f9d95..6698035 100644 (file)
@@ -68,7 +68,7 @@ error_reporting(E_ALL & ~E_STRICT );
 * it will try staging.ini
 *
 */
+#[AllowDynamicProperties]
 class HTML_FlexyFramework2 {
     
     /**
@@ -96,6 +96,7 @@ class HTML_FlexyFramework2 {
                 // use $this->run($_SERVER['REQUEST_URI'],false); to manually boot it..
 
     
+    var $isDev = false; // is development mode (can debug sql?)
     // derived.
     var $cli = false; // from cli 
     var $run = false; // from cli
@@ -116,7 +117,9 @@ class HTML_FlexyFramework2 {
     var $ext; // the striped extention.
     
     var $dataObjectsOriginalIni = ''; // 1 houre..
     
+    var $locale = 'en';
     // used to be $_GLOBALS[__CLASS__]
     
     static $singleton; 
@@ -196,11 +199,22 @@ class HTML_FlexyFramework2 {
     function loadModuleConfig($cfg)
     {
         
-        $mods = empty($cfg['enable']) ? array() : explode(',',$cfg['enable']);
-        array_unshift($mods,'');
+        
         $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);
+            }
+        }
+        
+        $mods = empty($cfg['enable']) ? array() : explode(',',$cfg['enable']);
+        array_unshift($mods,'');
+        
         foreach($mods as $m) {
             $cls = $proj. (strlen($m) ? '_'. $m  : '' ) . '_Config';
 
@@ -254,9 +268,8 @@ class HTML_FlexyFramework2 {
         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'])) {
-                
                 $this->baseURL = ''; // ??? this is if we replace top level...
             }
         }
@@ -376,7 +389,10 @@ class HTML_FlexyFramework2 {
         }
            
         $lang = isset($_COOKIE[$cfg['cookie']]) ?  $_COOKIE[$cfg['cookie']] : $default;
-
+         //print_r($_COOKIE); print_R($cfg['cookie']);print_R($lang);exit;
+        $lang = preg_replace('/^s:/','', $lang); // for some reason roo's library prefixes values with s:
         if (isset($_REQUEST[$cfg['param']])) {
             $lang = $_REQUEST[$cfg['param']];
         }
@@ -514,14 +530,19 @@ class HTML_FlexyFramework2 {
        
         $iniCache = $this->PDO_DataObject['schema_location'];
         
-        if ($force && file_exists($iniCache)) {
+        
+        $replace = array();
+        
+        if (file_exists($iniCache)) {
             $files = glob(dirname($iniCache).'/*.ini');
             foreach($files as $f) {
-                unlink($f);
-            } 
-            clearstatcache();
+                $replace[$f] = md5(file_get_contents($f)); // hash it..
+               
+            }
         }
         
+      
+        
         $iniCacheTmp = $iniCache . '.tmp' .md5(rand());  // random to stop two processes using the same file.
         // has it expired..
         $force = ($force ? $force : !file_exists($iniCache)) || !$this->dataObjectsCacheExpires;
@@ -562,7 +583,7 @@ class HTML_FlexyFramework2 {
         $generator->start();
         
         $this->debug('generateDataobjectsCache', 'dataObjectsCache  writing');
-        HTML_FlexyFramework2_Generator::writeCache($iniCacheTmp, $iniCache); 
+        HTML_FlexyFramework2_Generator::writeCache($iniCacheTmp, $iniCache, $replace); 
         // reset the cache to the correct lcoation.
         PDO_DataObject::config('schema_location',  $iniCache);
         $this->PDO_DataObject['schema_location'] = $iniCache;
@@ -571,7 +592,7 @@ class HTML_FlexyFramework2 {
         //$GLOBALS['_DB_DATAOBJECT']['INI'][$this->database] =   parse_ini_file($iniCache, true);
         //$GLOBALS['_DB_DATAOBJECT']['SEQUENCE']
         // clear any dataobject cache..
-         
+          
         
         //die("done");
         
@@ -800,7 +821,7 @@ class HTML_FlexyFramework2 {
     {
         /* have I been initialized */
         
-        
+        /* 
         if (get_magic_quotes_gpc() && !$this->cli) {
             $this->fatalError(
                 "magic quotes is enabled add the line<BR>
@@ -810,6 +831,7 @@ class HTML_FlexyFramework2 {
                    ");
                 
         }
+        */
         // set up error handling - 
         //$this->error = new HTML_FlexyFramework2_Error();
         
@@ -850,6 +872,20 @@ class HTML_FlexyFramework2 {
                      <BR> ".$e->getMessage());
         }
         
+          
+         if (preg_match('/^mysql/', $this->database)) {
+            $res = $x->PDO()->query("SELECT @@global.read_only as ro");
+           
+            $row = !is_a($res, 'PDOStatement') ? false : $res->fetch(PDO::FETCH_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>");
+                }
+                $this->database_is_readonly = true;
+            }
+        }
         
         
     }
@@ -941,6 +977,7 @@ class HTML_FlexyFramework2 {
         if (!$classname) {
             return false;
         }
+       
         
         // make page data/object accessable at anypoint in time using  this
         // not sure if this is used anymore - or even works..?
@@ -987,6 +1024,8 @@ class HTML_FlexyFramework2 {
         }
         
         $classobj  = $this->initPage($request,$isRedirect,$args);
+        
+        
         $classobj->cli = $this->cli;
         
         if ($this->cli && !$isRedirect ) { // redirect always just takes redirect args..
@@ -1096,7 +1135,8 @@ class HTML_FlexyFramework2 {
         }
         
         $startRequest = $request;
-        $request =@ array_shift(explode('?', $request));
+        $rq = explode('?', $request);
+        $request =  array_shift($rq);
         $this->debug("INPUT REQUEST $request<BR>");
         if (!$isRedirect) {
             // check that request forms contains baseurl????
@@ -1129,7 +1169,22 @@ class HTML_FlexyFramework2 {
         
         // REDIRECT ROO to index.php! for example..
         
+        // this has a slight issue when we are using 'hidden index.php' mod_rewrite..
+        
+        /*
+         normal (non-mod_rewrite)
+         $_SERVER['REQUEST_URI'] start with baseURL..
+         */
+        if (isset($_SERVER['REQUEST_URI']) && substr($_SERVER['REQUEST_URI'], 0, strlen($this->baseURL)) != $this->baseURL) {
+            // then we are rewriting?
+            $this->baseURL = $this->rootURL;
+            return $request;
+        }
+        
+        //var_dump(array($this->baseURL, $this->rootURL, $request ,$isRedirect, $_SERVER['REQUEST_URI'])); phpinfo();exit;
         if (!$request && !$isRedirect) {
+            
+            
             if ($this->baseURL && (strlen($startRequest) < strlen($this->baseURL))) {
                 
                 // needs to handle https + port
@@ -1219,21 +1274,28 @@ class HTML_FlexyFramework2 {
             $this->debug("baseDIR = {$this->baseDir}");
             
             $floc = "{$this->baseDir}/$location";
-            $this->debug("CHECK LOCATION = $location");
+            $this->debug("CHECK LOCATION = $floc");
             
             
             
             if (!empty($location) && $location != '.php' && @file_exists($floc )) {             // hide? error???
                 require_once $floc ;
                 $classname = $this->classPrefix . implode('_',$request_array);
-                $this->debug("FOUND FILE - SET CLASS = $classname <BR>");
-                break;
+                 
+                if (class_exists($classname) &&
+                        (is_a($classname, 'HTML_FlexyFramework2_Page', true) ||
+                         is_a($classname, 'HTML_FlexyFramework_Page', true))
+                ) {
+                         
+                    $this->debug("FOUND FILE - SET CLASS = $classname <BR>");
+                    break;
+                }
             } 
             
             // in here check the 'projectExtends' versions..?
             
             if(!empty($this->projectExtends)){
-                $this->debug("Trying project Extends<BR>");
+                $this->debug("Trying project Extends <BR>");
                 $has_extend_class = false;
                 
                 foreach ($this->projectExtends as $e){
@@ -1242,9 +1304,14 @@ class HTML_FlexyFramework2 {
                     if (!empty($location) && @file_exists($floc)) {             // hide? error???
                         require_once $floc ;
                         $classname = $e . '_' . implode('_',$request_array);
-                        $has_extend_class = true;
-                        $this->debug("FOUND FILE - SET CLASS = $classname <BR>");
-                        break;
+                         
+                        if (class_exists($classname) &&
+                            (is_a($classname, 'HTML_FlexyFramework2_Page', true) ||
+                             is_a($classname, 'HTML_FlexyFramework_Page', true))  ) {
+                            $has_extend_class = true;
+                            $this->debug("FOUND FILE - SET CLASS = $classname <BR>");
+                            break;
+                        }
                     } 
                 }
                 
@@ -1267,11 +1334,14 @@ class HTML_FlexyFramework2 {
         $classname = preg_replace('/[^a-z0-9]/i','_',$classname);
         $this->debug("CLASSNAME is '$classname'");
         // got it ok.
-        if ($classname && class_exists($classname)) {
+        if ($classname && class_exists($classname) &&
+                (is_a($classname, 'HTML_FlexyFramework2_Page', true) ||
+                 is_a($classname, 'HTML_FlexyFramework_Page', true))) {
             $this->debug("using $classname");
             //print_r($sub_request_array);
             return array($classname,implode('/',array_reverse($sub_request_array)));
         }
+        $this->debug("$classname did not exist - using base");        
         // stop looping..
         if ($showError) {
             $this->fatalError("INVALID REQUEST: \n $request FILE:".$this->baseDir. "/{$location}  CLASS:{$classname}");
@@ -1297,7 +1367,7 @@ class HTML_FlexyFramework2 {
             }
         }
         // got projectname.php
-        if ($classname && class_exists($classname)) {
+        if ($classname && class_exists($classname)  ) {
             $this->debug("using $classname");
             //print_r($sub_request_array);
              
@@ -1351,7 +1421,8 @@ class HTML_FlexyFramework2 {
             return true;
         }
         // file exists, but process might not be the same..
-        $name = array_pop(explode('_', get_class($class)));
+        $ca = explode('_', get_class($class));
+        $name = array_pop($ca);
         $cmd = file_get_contents('/proc/' . $oldpid.'/cmdline');
         if (!preg_match('/php/i',$cmd) || !preg_match('/'.$name.'/i',$cmd)) {
             file_put_contents($lock, getmypid());
@@ -1425,7 +1496,9 @@ class HTML_FlexyFramework2 {
             HTML_FlexyFramework2::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) {