2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Alan Knowles <alan@akbkhome.com> |
17 // +----------------------------------------------------------------------+
19 // $Id: FlexyFramework.php,v 1.8 2003/02/22 01:52:50 alan Exp $
22 // A Page (URL) to Object Mapper
23 // Cleaned up version.. - for use on new projects -- not BC!! beware!!!
26 //-----------------------------------------------------------
27 // Debian APACHE - some idiot disabled AcceptPathInfo - it needs adding back.
28 //-----------------------------------------------------------
32 // Initialize Static Options
33 require_once 'HTML/FlexyFramework2/Page.php';
36 // To be removed ?? or made optional or something..
41 error_reporting(E_ALL & ~E_STRICT );
42 //ini_set('display_errors','off');
43 //ini_set('log_errors','off');
51 * The URL to Object Mapper
54 * Create a index.php and add these lines.
56 * ini_set("include_path", "/path/to/application");
57 * require_once 'HTML/FlexyFramework.php';
58 * HTML_FlexyFramework::factory(array("dir"=>"/where/my/config/dir/is");
61 * the path could include pear's path, if you dont install all the pear
62 * packages into the development directory.
64 * It attempts to load a the config file from the includepath,
65 * looks for ConfigData/default.ini
66 * or ConfigData/{hostname}.ini
67 * if your file is called staging.php rather than index.php
68 * it will try staging.ini
72 class HTML_FlexyFramework2 {
75 * Confirgurable items..
76 * If we set them to 'true', they must be set, otherwise they are optional.
78 var $project; // base class name
79 var $database; // set to true even if nodatabase=true
83 var $enable = false; // modules
84 var $disable = false; // modules or permissions
86 var $appNameShort = false; // appname (which has templates)
87 var $version = false; // give it a version name. (appended to compile dir)
88 var $nodatabase = false; // set to true to block db config and testing.
89 var $fatalAction = false; // page to redirct to on failure. (eg. databse down etc.)
90 var $charset = false; // default UTF8
91 var $dataObjectsCache = true; // use dataobjects ini cache.. - let's try this as the default behaviour...
92 var $dataObjectsCacheExpires = 72000; // 20 hours..
93 var $languages = false; // language settings -- see _handlelanguage
94 var $projectExtends = false; // if this is an array, it's a fallback of 'Projects' that can be called
95 var $initOnly = false; // use to prevent run() being called...
96 // use $this->run($_SERVER['REQUEST_URI'],false); to manually boot it..
100 var $cli = false; // from cli
101 var $run = false; // from cli
102 var $enableArray = false; // from enable.
103 var $classPrefix = false; // from prject.
104 var $baseDir = false ; // (directory+project)
105 var $rootDir = false ; // (directory that index.php is in!)
107 var $baseURL = false;
108 var $rootURL = false ; // basename($baseURL)
110 var $page = false; // active page..
111 var $timer = false; // the debug timer
112 var $calls = false; // the number of calls made to run!
113 var $start = false; // the start tiem.
115 var $baseRequest = '';
116 var $ext; // the striped extention.
118 var $dataObjectsOriginalIni = ''; // 1 houre..
120 // used to be $_GLOBALS[__CLASS__]
127 * Constructor - with assoc. array of props as option
128 * called by index.php usually, and runs the app code,
130 * uses 'universal construcor' format, so the argument relates directly to properties of this object.
135 function __construct($config)
137 if (isset(self::$singleton)) {
138 trigger_error("FlexyFramework Construct called twice!", E_ERROR);
141 self::$singleton = $this;
145 $m = explode(' ',microtime());
146 $this->start = $m[0] + $m[1];
148 $config = $this->loadModuleConfig($config);
150 foreach($config as $k=>$v) {
153 $this->_parseConfig();
155 // echo '<PRE>'; print_r($this);exit;
157 $args = $_SERVER['argv'];
160 $this->_run($this->run,false,$args);
164 // handle apache mod_rewrite..
165 // it looks like this might not work anymore..
167 if ($this->initOnly) {
172 if (!empty($_SERVER['REDIRECT_URL'])) {
174 $this->_run($_SERVER['SCRIPT_NAME'] . $_SERVER['REQUEST_URI'],false);
179 $this->_run($_SERVER['REQUEST_URI'],false);
184 * This is the standard way to get information about the application settings.
185 * $ff = HTML_FlexyFramework::get();
186 * if ($ff->SomeVar[...])....
189 static function get()
191 return self::$singleton;
194 * looks for files in the path and load up the default values for config?
196 function loadModuleConfig($cfg)
200 $proj = $cfg['project'];
201 $rootDir = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
203 $cls = $proj.'_Config';
204 if (file_exists($rootDir . '/'.str_replace('_','/', $cls). '.php')) {
205 require_once str_replace('_','/', $cls). '.php';
207 if (method_exists($c,'init')) {
208 $cfg = $c->init($this,$cfg);
212 $mods = empty($cfg['enable']) ? array() : explode(',',$cfg['enable']);
213 array_unshift($mods,'');
215 foreach($mods as $m) {
216 $cls = $proj. (strlen($m) ? '_'. $m : '' ) . '_Config';
218 if (!file_exists($rootDir . '/'.str_replace('_','/', $cls). '.php')) {
221 require_once str_replace('_','/', $cls). '.php';
223 if (method_exists($c,'init')) {
224 $cfg = $c->init($this,$cfg);
232 * parse the configuration set by the constructor.
237 function _parseConfig()
240 // make sure required values are set.. (anything that is not defaulted to false..)
241 foreach(get_class_vars(__CLASS__) as $k =>$v) {
242 if ($v === false && !isset($this->$k)) {
243 die("$k is not set");
247 $this->_handleLanguages();
250 if (!empty($this->enable)) {
251 $this->enableArray = explode(',', $this->enable);
253 if (!in_array('Core',$this->enableArray ) &&
254 !in_array('Core', explode(',', $this->disable ? $this->disable : '')))
256 $this->enable = 'Core,'. $this->enable ;
257 $this->enableArray = explode(',', $this->enable);
260 // are we running cli?
261 $this->cli = php_sapi_name() == 'cli';
263 // will these work ok with cli?
264 $bits = explode(basename($_SERVER["SCRIPT_FILENAME"]), $_SERVER["SCRIPT_NAME"]);
266 $bits[0] = str_replace('%2F','/',urlencode($bits[0]));
267 $this->baseURL = $bits[0] . basename($_SERVER["SCRIPT_FILENAME"]);
269 if (empty($_SERVER['SCRIPT_NAME'])) {
270 $this->baseURL = ''; // ??? this is if we replace top level...
273 // if cli - you have to have set baseURL...
276 $this->rootDir = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
277 $this->baseDir = $this->rootDir .'/'. $this->project;
278 $this->rootURL = dirname($this->baseURL);
279 $this->rootURL = ($this->rootURL == '/') ? '' : $this->rootURL;
282 //var_dump($this->baseURL);
284 if (!isset($this->database) && isset($this->PDO_DataObject['database'])) {
285 $this->database = $this->PDO_DataObject['database'];
288 $this->classPrefix = str_replace('/', '_', $this->project) . '_';
290 // list the available options..
291 if ($this->cli && empty($_SERVER['argv'][1])) {
292 require_once 'HTML/FlexyFramework2/Cli.php';
293 $fcli = new HTML_FlexyFramework2_Cli($this);
299 // see if it's a framework assignment.
302 require_once 'HTML/FlexyFramework2/Cli.php';
303 $fcli = new HTML_FlexyFramework2_Cli($this);
304 $res = $fcli->parseDefaultOpts();
312 $this->run = $this->cli ? $_SERVER['argv'][1] : false;
315 $this->_parseConfigDataObjects();
316 if ($this->dataObjectsCache && !$this->nodatabase) {
317 $this->_configDataObjectsCache();
320 $this->_parseConfigTemplate();
321 $this->_parseConfigMail();
323 //echo '<PRE>';print_r($this);exit;
325 //$this->_exposeToPear();
328 $this->_validateEnv();
334 $this->_validateDatabase();
336 $this->_validateTemplate();
342 *'languages' => array(
344 'avail' => array('en','zh_HK', 'zh_CN'),
346 'cookie' => 'TalentPricing_lang',
347 'localemap' => array(
348 'en' => 'en_US.utf8',
349 'zh_HK' => 'zh_TW.utf8',
350 'zh_CN' => 'zh_CN.utf8',
355 function _handleLanguages()
358 empty($this->languages) ||
360 !isset($this->languages['cookie']) && !isset($this->languages['default'])
366 $cfg = $this->languages;
368 $default = $cfg['default'];
370 if(!empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])){
372 $brower_langs = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
374 foreach ($brower_langs as $bl) {
375 $l = preg_replace('/;(.*)/', '', $bl);
377 $l = str_replace('-', '_', $l);
379 if(!in_array($l, $cfg['avail'])){
388 $lang = isset($_COOKIE[$cfg['cookie']]) ? $_COOKIE[$cfg['cookie']] : $default;
390 if (isset($_REQUEST[$cfg['param']])) {
391 $lang = $_REQUEST[$cfg['param']];
394 if (!in_array($lang, $cfg['avail'])) {
395 $lang = $cfg['default'];
397 if (isset($cfg['localemap'][$lang])) {
398 setlocale(LC_ALL, $cfg['localemap'][$lang]);
400 setcookie($cfg['cookie'], $lang, 0, '/');
402 $this->locale = $lang;
404 if (!empty($this->HTML_Template_Flexy)) {
405 $this->HTML_Template_Flexy['locale'] = $lang; //set a language for template engine
411 * overlay array properties..
414 function applyIf($prop, $ar) {
415 if (!isset($this->$prop)) {
419 // add only things that where not set!!!.
420 $this->$prop = array_merge($ar,$this->$prop);
423 //foreach($ar as $k=>$v) {
424 // if (!isset($this->$prop->$k)) {
425 // $this->$prop->$k = $v;
432 * - if turned on (dataObjectsCache = true) then
433 * a) ini file points to a parsed version of the structure.
434 * b) links.ini is a merged version of the configured link files.
436 * This only will force a generation if no file exists at all.. - after that it has to be called manually
437 * from the core page.. - which uses the Expires time to determine if regeneration is needed..
442 function _configDataObjectsCache()
444 // cli works under different users... it may cause problems..
445 $this->debug(__METHOD__);
446 if (function_exists('posix_getpwuid')) {
447 $uinfo = posix_getpwuid( posix_getuid () );
448 $user = $uinfo['name'];
450 $user = getenv('USERNAME'); // windows.
455 $iniCache = ini_get('session.save_path') .'/' .
456 'pdocfg-' . $user . '/'. str_replace('/', '_', $this->project) ;
459 if ($this->appNameShort) {
460 $iniCache .= '_' . $this->appNameShort;
462 if ($this->version) {
463 $iniCache .= '.' . $this->version;
465 if ($this->database === false) {
469 $dburl = parse_url($this->database);
470 if (!empty($dburl['path'])) {
471 $iniCache .= '-'.ltrim($dburl['path'],'/');
475 $this->debug(__METHOD__ . " : ini cache : $iniCache");
477 $dburl = parse_url($this->database);
478 //override ini setting... - store original..
480 if (isset($this->PDO_DataObject['schema_location'])) {
481 $this->dataObjectsOriginalIni = $this->PDO_DataObject['schema_location'];
482 ///print_r($this->DB_DataObject);exit;
485 $this->PDO_DataObject['schema_location'] = $iniCache;
486 PDO_DataObject::config($this->PDO_DataObject);
488 // we now have the configuration file name..
490 if (!file_exists($iniCache) || empty( $this->dataObjectsCacheExpires)) {
491 $this->generateDataobjectsCache(true);
499 * generateDataobjectsCache:
501 * if the 'cache file' does not exist, then
502 * create it using HTML_FlexyFramework2_Generator
504 * create xxx.ini and xxx.links.ini
506 * @arg force (boolean) force generation - default false;
510 function generateDataobjectsCache($force = false)
512 //$this->debug('generateDataobjectsCache: force=' . ($force ? 'yes' : 'no'));
513 if (!$this->dataObjectsCache) { // does not use dataObjects Caching..
514 PDO_DataObject::config('schema_location', $this->dataObjectsOriginalIni );
516 $this->debug('generateDataobjectsCache', 'dataObjectsCache - empty');
520 $dburl = parse_url($this->database);
521 $dbnick = basename($dburl['path']);
525 $iniCache = $this->PDO_DataObject['schema_location'];
527 if ($force && file_exists($iniCache)) {
528 $files = glob(dirname($iniCache).'/*.ini');
529 foreach($files as $f) {
535 $iniCacheTmp = $iniCache . '.tmp' .md5(rand()); // random to stop two processes using the same file.
537 $force = ($force ? $force : !file_exists($iniCache)) || !$this->dataObjectsCacheExpires;
538 // $this->debug('generateDataobjectsCache: after check : force=' . ($force ? 'yes' : 'no'));
539 // not force or not expired, do not bother..
541 if ((filemtime($iniCache) + $this->dataObjectsCacheExpires) >time()) {
547 $this->debug('generateDataobjectsCache', 'dataObjectsCache generating');
550 // force quoting of column names..
551 // unless it forced off..
552 if (!isset($this->PDO_DataObject['quote_identifiers'] )) {
553 $this->PDO_DataObject['quote_identifiers'] = true;
555 if (!file_exists(dirname($iniCache))) {
556 mkdir(dirname($iniCache),0700, true);
559 $this->PDO_DataObject['schema_location'] = $iniCacheTmp;
561 PDO_DataObject::reset();
563 PDO_DataObject::config($this->PDO_DataObject);
567 // DB_DataObject::debugLevel(1);
568 require_once 'HTML/FlexyFramework2/Generator.php';
570 PDO_DataObject::config('database', $this->database);
571 $generator = new HTML_FlexyFramework2_Generator();
574 $this->debug('generateDataobjectsCache', 'dataObjectsCache writing');
575 HTML_FlexyFramework2_Generator::writeCache($iniCacheTmp, $iniCache);
576 // reset the cache to the correct lcoation.
577 PDO_DataObject::config('schema_location', $iniCache);
578 $this->PDO_DataObject['schema_location'] = $iniCache;
579 //$this->_exposeToPear();
581 //$GLOBALS['_DB_DATAOBJECT']['INI'][$this->database] = parse_ini_file($iniCache, true);
582 //$GLOBALS['_DB_DATAOBJECT']['SEQUENCE']
583 // clear any dataobject cache..
590 * DataObject Configuration:
591 * Always in Project/DataObjects
592 * unless enableArray is available...
597 function _parseConfigDataObjects()
599 if ($this->nodatabase && !$this->database) {
603 // better done here.. ?? only include if we have database configuration?
604 require_once 'PDO/DataObject.php';
606 $dburl = parse_url($this->database);
607 $dbini = 'ini_'. basename($dburl['path']);
609 $dbinis = array(); //array(dirname(__FILE__) . '/Pman/DataObjects/pman.ini');
610 $dbreq = array(); //array( dirname(__FILE__) . '/Pman/DataObjects/');
611 $dbcls = array(); //array('Pman_DataObjects_');
613 $project = explode('/',$this->project)[0];
615 if (!empty($this->enableArray)) {
617 $tops = array_merge( array($project), empty($this->projectExtends) ? array() : $this->projectExtends);
619 foreach($tops as $td) {
620 $bd = $this->rootDir .'/'.$td;
621 foreach($this->enableArray as $m) {
622 // look in Pman/MODULE/DataObjects/*
623 if (file_exists($bd.'/'.$m.'/DataObjects')) {
624 $dbinis[] = $bd.'/'.$m.'/DataObjects/'. strtolower($td).'.ini';
625 $dbcls[] = $td.'_'. $m . '_DataObjects_';
626 $dbreq[] = $bd.'/'.$m.'/DataObjects';
629 // look in MODULE/DataObjects ?? DO WE SUPPORT THIS ANYMORE???
630 if (file_exists($bd.'/../'.$m.'/DataObjects')) {
631 $dbinis[] = $bd.'/../'.$m.'/DataObjects/'. strtolower($td).'.ini';
632 $dbcls[] = $td. '_DataObjects_';
633 $dbreq[] = $bd.'/../'.$m.'/DataObjects';
642 if (isset($this->PDO_DataObject['schema_location'])) {
643 $dbinis[] = $this->PDO_DataObject['schema_location'] .'/'.basename($dburl['path']).'.ini';
645 $dbinis[] = $this->baseDir.'/DataObjects/'.basename($dburl['path']).'.ini';
649 $dbcls[] = $project .'_DataObjects_';
650 $dbreq[] = $this->baseDir.'/DataObjects';
654 $this->applyIf('PDO_DataObject', array(
656 'class_location' => implode(PATH_SEPARATOR,$dbreq),
657 'class_prefix' => implode(PATH_SEPARATOR,$dbcls),
658 'database' => $this->database,
659 ///'require_prefix' =>
660 // 'schema_location' => dirname(__FILE__) . '/Pman/DataObjects/',
661 'schema_location' => implode(PATH_SEPARATOR,$dbinis),
665 PDO_DataObject::config($this->PDO_DataObject);
672 function _parseConfigTemplate()
676 if (function_exists('posix_getpwuid')) {
677 $uinfo = posix_getpwuid( posix_getuid () );
679 $user = $uinfo['name'];
681 $user = getenv('USERNAME'); // windows.
684 $compileDir = ini_get('session.save_path') .'/' .
685 $user . '_compiled_templates_' . $this->project;
687 if ($this->appNameShort) {
688 $compileDir .= '_' . $this->appNameShort;
690 if ($this->version) {
691 $compileDir .= '.' . $this->version;
694 // templates. -- all this should be cached!!!
698 if ($this->appNameShort && !in_array('Core', explode(',', $this->disable ? $this->disable : ''))) {
699 // in app based version, template directory is in Core
702 $this->baseDir . '/Core/templates'
706 if(!empty($this->projectExtends)){
707 foreach ($this->projectExtends as $e){
708 $add = $this->rootDir . '/' . $e .'/templates';
709 if (!in_array($add,$src) && file_exists($add)) {
715 $src[] = $this->baseDir . '/templates';
719 if (!empty($this->enableArray)) {
722 foreach($this->enableArray as $m) {
723 $add = $this->baseDir . '/' . $m .'/templates';
724 if (!in_array($add,$src) && file_exists($add) && $this->appNameShort != $m) {
729 if (!empty($this->projectExtends) ) {
730 foreach ($this->projectExtends as $extend){
731 foreach($this->enableArray as $m) {
732 $add = $this->rootDir . '/' . $extend . '/' . $m .'/templates';
733 if (!in_array($add,$src) && file_exists($add) && $this->appNameShort != $m) {
743 if ($this->appNameShort) {
744 $src[] = $this->baseDir . '/'. $this->appNameShort. '/templates';
747 // images may come from multiple places: - if we have multiple template directories.?
748 // how do we deal with this..?
749 // images/ << should always be mapped to master!
750 // for overridden appdir ones we will have to se rootURL etc.
752 $url_rewrite = 'images/:'. $this->rootURL . '/'. $this->project. '/templates/images/';
754 $this->applyIf('HTML_Template_Flexy', array(
755 'templateDir' => implode(PATH_SEPARATOR, $src),
756 'compileDir' => $compileDir,
757 'multiSource' => true,
759 'url_rewrite' => $url_rewrite,
760 'filters' => 'Php,SimpleTags', /// for non-tokenizer version?
761 'debug' => $this->debug ? 1 : 0,
770 function _parseConfigMail()
772 $this->applyIf('HTML_Template_Flexy', array(
775 'host' => 'localhost',
781 * exposes to PEAR::getStaticProperty..
782 * at present, only FlexyFramework uses this..
783 * // we should really stop it..
784 * ctor for Flexy - should read from config...
787 function _exposeToPear()
789 $cls = array_keys(get_class_vars(__CLASS__));
792 // anything that get's set, that's not in our default properties
793 // is assumed to be an option set .
794 foreach(get_object_vars($this) as $k=>$v) {
795 if (in_array($k,$cls)) {
799 $options = &PEAR::getStaticProperty($k,'options');
802 // $options = &PEAR::getStaticProperty('HTML_FlexyFramework2','options');
809 function _validateEnv()
811 /* have I been initialized */
814 if (get_magic_quotes_gpc() && !$this->cli) {
816 "magic quotes is enabled add the line<BR>
817 php_value magic_quotes_gpc 0<BR>
818 to your .htaccess file <BR>
819 (Apache has to be configured to "AllowOverride Options AuthConfig" for the directory)
824 // set up error handling -
825 //$this->error = new HTML_FlexyFramework2_Error();
827 /// fudge work around bugs in PEAR::setErrorHandling(,)
828 //$GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_CALLBACK;
829 //$GLOBALS['_PEAR_default_error_options'] = array($this->error,'raiseError');
834 require_once 'Benchmark/Timer.php';
835 $this->timer = new BenchMark_Timer(true);
836 register_shutdown_function(function() { echo $this->timer->getOutput(); });
841 function _validateDatabase()
843 //echo "<PRE>"; print_r($this);
845 if ($this->nodatabase) {
849 // database is the only setting - we dont support mult databses?
852 $x = new PDO_Dataobject();
853 PDO_Dataobject::config('database', $this->database);
856 } catch (Exception $e) {
859 $this->fatalError("Configuration or Database Error: could not connect to Database, <BR>
860 Please check the value given to HTML_FlexyFramework2, or run with debug on!<BR>
861 <BR> ".$e->getMessage());
867 function _validateTemplate()
869 // check that we have a writeable directory for flexy's compiled templates.
871 if (empty($this->HTML_Template_Flexy['compileDir'])) {
875 if ( !file_exists($this->HTML_Template_Flexy['compileDir'])) {
876 mkdir($this->HTML_Template_Flexy['compileDir'], 0700, true);
879 if ( !file_exists($this->HTML_Template_Flexy['compileDir'])) {
881 $this->fatalError("Configuration Error: you specified a directory that does not exist for<BR>
882 HTML_Template_Flexy => compileDir {$this->HTML_Template_Flexy['compileDir']}<BR>\n"
887 if (!is_writeable($this->HTML_Template_Flexy['compileDir'])) {
888 $this->fatalError("Configuration Error: Please make sure the template cache directory is writeable<BR>
890 chmod 700 {$this->HTML_Template_Flexy['compileDir']}<BR>
891 chgrp apache_user {$this->HTML_Template_Flexy['compileDir']}<BR>\n"
894 //echo "<PRE>";print_R($config);
912 * HTML_FlexyFramework::run('someurl/someother',array('somearg'=>'xxx'));
914 * exit; <- dont cary on!!!!
918 * @param string redirect to url
919 * @param array Args Optional any data you want to send to the next page..
928 static function run($request,$args=array())
930 self::$singleton->_run($request,true,$args);
935 * initPage - load the page up, and set the variables, but do not actually run it.
936 * - used where we want features from this framework, buy may actually be running another one..
938 * call with ($_SERVER["SCRIPT_NAME"]) for root page...
941 public function initPage($request, $isRedirect=false, $args=array())
943 $newRequest = $this->_getRequest($request,$isRedirect);
945 // find the class/file to load
946 list($classname,$subRequest) = $this->requestToClassName($newRequest,FALSE);
949 $this->debug("requestToClassName return = CLASSNAME: $classname SUB REQUEST: $subRequest");
951 // assume that this was handled by getclassname ?????
956 // make page data/object accessable at anypoint in time using this
957 // not sure if this is used anymore - or even works..?
959 $classobj = new $classname(); // normally do not have constructors.
961 $classobj->baseURL = $this->baseURL;
962 $classobj->rootURL = $this->rootURL;
963 $classobj->rootDir = $this->rootDir;
964 $classobj->bootLoader = $this;
965 $classobj->request = $newRequest;
966 $classobj->timer = &$this->timer;
967 $classobj->subrequest = $subRequest;
969 $this->page = $classobj;
975 * The main execution loop
977 * recursivly self called if redirects (eg. return values from page start methods)
979 * @param string from $_REQUEST or redirect from it'self.
980 * @param boolean isRedirect = is the request a redirect TRUE = always handle as 'GET', 1 = handle as POST/GET depending on method, 0 = handle as initial request.
983 * @return false || other false indicates no page was served!
984 * @access public|private
985 * @see see also methods.....
988 public function _run($request,$isRedirect = false,$args = array())
991 // clean the request up.
994 if ($this->calls > 5) {
995 // to many redirections...
996 trigger_error("FlexyFramework:: too many redirects - backtrace me!",E_USER_ERROR);
1000 $classobj = $this->initPage($request,$isRedirect,$args);
1001 $classobj->cli = $this->cli;
1003 if ($this->cli && !$isRedirect ) { // redirect always just takes redirect args..
1004 require_once 'HTML/FlexyFramework2/Cli.php';
1005 $fcli = new HTML_FlexyFramework2_Cli($this);
1006 $nargs = $fcli->cliParse(get_class($classobj));
1007 $args = $nargs === false ? $args : $nargs; /// replace if found.
1008 $classobj->cli_args = $nargs;
1011 // echo '<PRE>'; print_r($this);exit;
1012 // echo "CHECK GET AUTH?";
1013 if (!method_exists($classobj, 'getAuth')) {
1014 // echo "NO GET AUTH?";
1015 $this->fatalError("class ". get_class($classobj) . " does not have a getAuth Method");
1019 /* check auth on the page */
1020 if (is_string($redirect = $classobj->getAuth())) {
1021 $this->debug("GOT AUTH REDIRECT".$redirect);
1022 return $this->_run($redirect,TRUE);
1024 // used HTML_FlexyFramework::run();
1027 if ($redirect === false) {
1028 $this->debug("GOT AUTH FALSE");
1029 return false; /// Access deined!!! -
1032 // allow the page to implement caching (for the full page..)
1033 // although normally it should implement caching on the outputBody() method.
1035 if (method_exists($classobj,"getCache")) {
1036 if ($result = $classobj->getCache()) {
1040 /* allow redirect from start */
1041 if (method_exists($classobj,"start")) {
1042 if (is_string($redirect = $classobj->start($classobj->subrequest,$isRedirect,$args))) {
1043 $this->debug("REDIRECT $redirect <BR>");
1044 return $this->_run($redirect,TRUE);
1046 if ($redirect === false) {
1052 // used HTML_FlexyFramework::run();
1055 * Modules are common page components like navigation headers etc.
1056 * that can have dynamic code.
1057 * Code has been removed now..
1062 $this->timer->setMarker("After $request loadModules Modules");
1065 /* output it - (our base page does not implement output for cli. */
1067 if ( method_exists($classobj,'output')) {
1068 $classobj->output();
1073 $this->timer->setMarker("After $request output");
1074 $this->timer->stop(); //?? really - yes...
1084 exit; /// die here...
1089 * map the request into an object and run the page.
1091 * The core of the work is done here.
1094 * @param request the request string
1095 * @param boolean isRedirect - indicates that it should not attempt to strip the .../index.php from the request.
1100 function _getRequest($request, $isRedirect)
1109 $startRequest = $request;
1110 $rq = explode('?', $request);
1111 $request = array_shift($rq);
1112 $this->debug("INPUT REQUEST $request<BR>");
1114 // check that request forms contains baseurl????
1116 $subreq = substr($request,0,strlen($this->baseURL));
1117 if ($subreq != substr($this->baseURL,0,strlen($subreq))) {
1119 "Configuration error: Got base of $subreq which does not
1120 match configuration of: $this->baseURL} ");
1122 $request = substr($request,strlen($this->baseURL));
1126 // echo "REQUEST WAS: $request<BR>";
1127 // $request = preg_replace('/^'.preg_quote($base_url,'/').'/','',trim($request));
1128 // echo "IS NOW: $request<BR>";
1130 // strip valid html stuff
1131 //$request = preg_replace('/\/[.]+/','',$request);
1134 $request = preg_replace('/^[\/]*/','',$request);
1135 $request = preg_replace('/\?.*$/','',$request);
1136 $request = preg_replace('/[\/]*$/','',$request);
1137 $this->baseRequest = $request;
1138 $request = str_replace('&','',$request); // any other invalid characters???
1139 $request = preg_replace('/\.([a-z]+)$/','',$request);
1140 $this->ext = substr($this->baseRequest , strlen($request));
1142 // REDIRECT ROO to index.php! for example..
1144 // this has a slight issue when we are using 'hidden index.php' mod_rewrite..
1147 normal (non-mod_rewrite)
1148 $_SERVER['REQUEST_URI'] start with baseURL..
1150 if (isset($_SERVER['REQUEST_URI']) && substr($_SERVER['REQUEST_URI'], 0, strlen($this->baseURL)) != $this->baseURL) {
1151 // then we are rewriting?
1152 $this->baseURL = $this->rootURL;
1156 //var_dump(array($this->baseURL, $this->rootURL, $request ,$isRedirect, $_SERVER['REQUEST_URI'])); phpinfo();exit;
1157 if (!$request && !$isRedirect) {
1160 if ($this->baseURL && (strlen($startRequest) < strlen($this->baseURL))) {
1162 // needs to handle https + port
1163 $http = ((!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on')) ? 'https' : 'http';
1165 if (!empty($_SERVER['SERVER_PORT'])) {
1166 if ((($http == 'http') && ($_SERVER['SERVER_PORT'] == 80)) || (($http == 'https') && ($_SERVER['SERVER_PORT'] == 443))) {
1169 $sp .= ':'.((int) $_SERVER['SERVER_PORT']);
1172 //var_dump($startRequest); echo '<PRE>'; print_R($this); phpinfo();exit;
1173 $host = !empty($_SERVER["HTTP_X_FORWARDED_HOST"]) ? $_SERVER["HTTP_X_FORWARDED_HOST"] : $_SERVER["HTTP_HOST"];
1174 header('Location: '.$http.'://'.$host .$sp . $this->baseURL);
1180 $this->debug("OUTPUT REQUEST $request<BR>");
1188 * get the Class name and filename to load
1190 * Parses the request and converts that into a File + Classname
1191 * if the class doesnt exist it will attempt to find a file below it, and
1192 * call that one with the data.
1193 * Used by the module loader to determine the location of the modules
1195 * @param request the request string
1196 * @param boolean showError - if false, allows you to continue if the class doesnt exist.
1199 * @return array classname, filepath
1204 function requestToClassName($request,$showError=TRUE)
1206 // if ($request == "error") {
1207 // return array("HTML_FlexyFramework_Error","");
1210 // special classes ::
1211 if ($this->cli && in_array($request, array('DataObjects'))) {
1212 require_once 'HTML/FlexyFramework2/'. $request . '.php';
1213 return array('HTML_FlexyFramework2_'. $request,'');
1217 $request_array=explode("/",$request);
1218 $original_request_array = $request_array;
1219 $sub_request_array = array();
1220 $l = count($request_array)-1;
1221 if ($l > 10) { // ?? configurable?
1222 //PEAR::raiseError("Request To Long");
1223 $this->fatalError("Request To Long - " . $request);
1228 // tidy up request array
1230 if ($request_array) {
1231 foreach(array_keys($request_array) as $i) {
1232 $request_array[$i] = preg_replace('/[^a-z0-9]/i','_',urldecode($request_array[$i]));
1235 //echo "<PRE>"; print_r($request_array);
1236 // technically each module should do a check here... similar to this..
1239 for ($i=$l;$i >-1;$i--) {
1240 $location = implode('/',$request_array) . ".php";
1241 if ($location == '.php') {
1242 $this->debug("SKIP first path check, as request str is empty");
1246 $this->debug("baseDIR = {$this->baseDir}");
1248 $floc = "{$this->baseDir}/$location";
1249 $this->debug("CHECK LOCATION = $location");
1253 if (!empty($location) && $location != '.php' && @file_exists($floc )) { // hide? error???
1254 require_once $floc ;
1255 $classname = $this->classPrefix . implode('_',$request_array);
1256 $this->debug("FOUND FILE - SET CLASS = $classname <BR>");
1260 // in here check the 'projectExtends' versions..?
1262 if(!empty($this->projectExtends)){
1263 $this->debug("Trying project Extends<BR>");
1264 $has_extend_class = false;
1266 foreach ($this->projectExtends as $e){
1267 $floc = "{$this->rootDir}/{$e}/$location";
1268 $this->debug("Trying file: $floc");
1269 if (!empty($location) && @file_exists($floc)) { // hide? error???
1270 require_once $floc ;
1271 $classname = $e . '_' . implode('_',$request_array);
1272 $has_extend_class = true;
1273 $this->debug("FOUND FILE - SET CLASS = $classname <BR>");
1278 if(!empty($has_extend_class)){
1285 $this->debug("$floc - !!FOUND NOT FILE!!");
1287 $sub_request_array[] = $original_request_array[$i];
1288 unset($request_array[$i]);
1289 unset($original_request_array[$i]);
1292 // is this really needed here!
1294 $classname = preg_replace('/[^a-z0-9]/i','_',$classname);
1295 $this->debug("CLASSNAME is '$classname'");
1297 if ($classname && class_exists($classname)) {
1298 $this->debug("using $classname");
1299 //print_r($sub_request_array);
1300 return array($classname,implode('/',array_reverse($sub_request_array)));
1304 $this->fatalError("INVALID REQUEST: \n $request FILE:".$this->baseDir. "/{$location} CLASS:{$classname}");
1309 $this->debug("Try base {$this->baseDir}.php");
1310 // try {project name}.php
1311 // this used to be silenced @ - if this fails we are usually pretty fried..
1313 if (file_exists($this->baseDir.'.php')) {
1316 $classname = str_replace('/', '_', $this->project); // basename($this->baseDir);
1318 $this->debug("FOUND {$this->baseDir} requring and checking class $classname");
1319 require_once $this->baseDir.'.php';
1320 $this->debug("require success");
1322 if (!class_exists($classname)) {
1323 $this->fatalError( "{$this->baseDir}.php did not contain class $classname");
1326 // got projectname.php
1327 if ($classname && class_exists($classname)) {
1328 $this->debug("using $classname");
1329 //print_r($sub_request_array);
1331 return array($classname,implode('/',array_reverse($sub_request_array)));
1335 $this->fatalError( "can not find {$this->baseDir}.php"); // dies..
1341 * ensure Single CLi process
1343 * HTML_FlexyFramework2::ensureSingle(__FILE__, $this);
1344 * @param string filename of running class
1345 * @param object class
1348 static function ensureSingle($sig, $class)
1350 //echo "check single: $sig / ". get_class($class) ."\n";
1351 $ff = HTML_FlexyFramework2::get();
1352 if (function_exists('posix_getpwuid')) {
1353 $uinfo = posix_getpwuid( posix_getuid () );
1354 $user = $uinfo['name'];
1356 $user = getenv('USERNAME'); // windows.
1358 $fdir = ini_get('session.save_path') .'/' .
1359 $user . '_cli_' . $ff->project ;
1362 if (!file_exists($fdir)) {
1366 $lock = $fdir.'/'. md5($sig) . '.' . get_class($class);
1367 //echo "check single: lock : $lock\n";
1368 if (!file_exists($lock)) {
1369 file_put_contents($lock, getmypid());
1370 //echo "check single: lock : DOES NOT EXIST\n";
1373 $oldpid = file_get_contents($lock);
1374 if (!file_exists('/proc/' . $oldpid)) {
1376 file_put_contents($lock, getmypid());
1377 // echo "check single: lock : PROC NOT EXIST\n";
1380 // file exists, but process might not be the same..
1381 $ca = explode('_', get_class($class));
1382 $name = array_pop($ca);
1383 $cmd = file_get_contents('/proc/' . $oldpid.'/cmdline');
1384 if (!preg_match('/php/i',$cmd) || !preg_match('/'.$name.'/i',$cmd)) {
1385 file_put_contents($lock, getmypid());
1386 //echo "check single: lock : CMDLINE !have PHP \n";
1389 die("process " . $sig . " already running\n");
1393 * removes the lock for the applicaiton - use with care...
1397 static function ensureSingleClear($sig, $class)
1399 $ff = HTML_FlexyFramework2::get();
1400 if (function_exists('posix_getpwuid')) {
1401 $uinfo = posix_getpwuid( posix_getuid () );
1402 $user = $uinfo['name'];
1404 $user = getenv('USERNAME'); // windows.
1406 $fdir = ini_get('session.save_path') .'/' .
1407 $user . '_cli_' . $ff->project ;
1410 if (!file_exists($fdir)) {
1413 $lock = $fdir.'/'. md5($sig);
1414 if (!file_exists($lock)) {
1425 * @param string text to output.
1429 function debug($output) {
1431 if (empty($this->debug)) {
1435 "HTML_FlexyFramework2::debug - ".$output."\n"
1436 : "<B>HTML_FlexyFramework2::debug</B> - ".$output."<BR>\n";
1440 * Raises a fatal error. - normally only used when setting up to help get the config right.
1442 * can redirect to fatal Action page.. - hoepfully not issued before basic vars are set up..
1444 * @param string text to output.
1448 function fatalError($msg,$showConfig = 0)
1452 if ($this->fatalAction) {
1453 HTML_FlexyFramework2::run($this->fatalAction,$msg);
1456 header('HTTP/1.1 503 Service Temporarily Unavailable');
1457 header('Status: 503 Service Temporarily Unavailable');
1458 header('Retry-After: 300');
1459 echo $this->cli ? $msg ."\n" : "<H1>$msg</H1>configuration information<PRE>";
1464 $ff = HTML_FlexyFramework2::get();