[CS-Project-svn_notify] SF.net SVN: cs-project: [859] trunk/1.2/lib
Brought to you by:
crazedsanity
From: <cra...@us...> - 2008-03-30 05:44:21
|
Revision: 859 http://cs-project.svn.sourceforge.net/cs-project/?rev=859&view=rev Author: crazedsanity Date: 2008-03-29 22:44:19 -0700 (Sat, 29 Mar 2008) Log Message: ----------- Implemented config::check_site_status() to determine if site is okay, locked, going through setup by another user, or if the current user has locked the setup. /lib/config.class.php: * MAIN::: -- NEW PROPERTY: siteStatus -- NEW PROPERTY: setupRequired=FALSE -- NEW PROPERTY: config * __construct(): -- run check to ensure the CONFIG_DIRECTORY (rw) is writable; if it's not, throw an exception. -- read the config file into $this->config * do_setup_redirect() -- only do a redirect if site status is okay, setup is required, and the current URL isn't /setup. -- call create_setup_config() to create /rw/setup.xml, so we know who is running the setup. * check_site_status(): -- updated header. -- always check if there's an old config; if there is, copy it to the rw directory & delete the old file. -- intelligent checks: if there's a setup file, site status is only good if the current user is the owner. -- if the config file exists, make sure the site isn't locked. -- if there's no config, site status is good & setupRequired is set. * is_workingonit_set() [NEW]: -- determines if the "WORKINGONIT" section of the site config has caused the site to be locked or not. * setup_config_exists() [NEW]: -- determines if setup is already being run, optionally saying if the current user owns the setup process. -- NOTE: the check for ownership should probably be moved to another method for sanity's sake. * is_setup_required() [NEW]: -- returns the value of the private property "setupRequired". * create_setup_config() [NEW]: -- create a setup config file (rw/setup.xml). * get_site_status() [NEW]: -- return the value of the private property "siteStatus". /lib/site_config.php: * define where and what the setup config file is. * turn on error reporting. * when creating config{}, pass CONFIG_FILE_LOCATION instead of deriving it. * use config::check_site_status() to determine if it needs to be setup, has already been setup, setup running by other users, etc. * removed an old global SITE_ROOT setting for CLI scripts. Modified Paths: -------------- trunk/1.2/lib/config.class.php trunk/1.2/lib/site_config.php Modified: trunk/1.2/lib/config.class.php =================================================================== --- trunk/1.2/lib/config.class.php 2008-03-30 04:08:45 UTC (rev 858) +++ trunk/1.2/lib/config.class.php 2008-03-30 05:44:19 UTC (rev 859) @@ -8,8 +8,10 @@ private $gf; private $fileExists; - + private $siteStatus; + private $setupRequired = FALSE; private $fileName; + private $config; //------------------------------------------------------------------------- public function __construct($fileName=NULL) { @@ -27,6 +29,11 @@ else { $this->fileExists = TRUE; } + + if(!$this->fs->is_writable(CONFIG_DIRECTORY)) { + throw new exception(__METHOD__ .": the config directory (". CONFIG_DIRECTORY .") isn't writable!"); + } + $this->config = $this->get_config_contents(TRUE); }//end __construct() //------------------------------------------------------------------------- @@ -93,23 +100,17 @@ //------------------------------------------------------------------------- public function do_setup_redirect() { - if(!preg_match('/^setup/', $_SERVER['REQUEST_URI']) && !$_SESSION[SESSION_SETUP_KEY]) { - - //set something in the session so we know. - if(!isset($_SESSION[SESSION_SETUP_KEY])) { - $_SESSION[SESSION_SETUP_KEY]++; + if($this->check_site_status() && $this->setupRequired) { + if(!($_SERVER['SCRIPT_NAME'] == '/setup')) { + $this->gf->debug_print("script_name check=(". ($_SERVER['script_name'] != '/setup') .")", 1); + $goHere = '/setup'; + if(strlen($_SERVER['REQUEST_URI']) > 1 && !isset($_SESSION['setup__viewed'])) { + $goHere .= '?from='. urlencode($_SERVER['REQUEST_URI']); + } + $_SESSION['setup_redirect'] = time(); + $this->create_setup_config(); + $this->gf->conditional_header($goHere); } - else { - throw new exception(__METHOD__ .": setup key (". SESSION_SETUP_KEY .") found in session already"); - } - - - $goHere = '/setup'; - - if(strlen($_SERVER['REQUEST_URI']) > 1) { - $goHere .= '?from='. urlencode($_SERVER['REQUEST_URI']); - } - $this->gf->conditional_header($goHere); } }//end do_setup_redirect() //------------------------------------------------------------------------- @@ -129,86 +130,145 @@ * TODO: instead of a ton of returns, just set true/false for return, and have internal message explaining what's up. * * @return TRUE OK: display normal page (no upgrade/setup needed/running) - * @return 1 OK: setup required (perform redirect) - * @return 2 OK: setup initiated by current user (display setup page) - * @return 3 ERROR: setup initiated by OTHER user (show "setup running" error) - * @return 4 OK: upgrade required (starts upgrade process, then displays normal page) - * @return 5 ERROR: upgrade started by other user (show temporary "upgrade in progress" message, set reload timer) + * @return FALSE FAIL: somebody else is running setup, or the site is otherwise locked. */ public function check_site_status() { - //============================================================= - //BEGIN pseudo code: - //-------------------------------------------------- + //check for the OLD config file. + /** + * WHY THE CHECK IS HERE::: + * + * The setup system expects that the config file exists in the location + * specified by CONFIG_FILE_LOCATION; if it's not there, the site_config + * will assume setup must be run... so we have to circumvent that + * behaviour right here. + */ + if(file_exists(OLD_CONFIG_FILE_LOCATION)) { + //copy old file to new location... + if(!$this->fs->copy_file(OLD_CONFIG_FILE_LOCATION, CONFIG_FILE_LOCATION)) { + throw new exception(__METHOD__ .": failed to copy existing config into new location"); + } + $this->fs->rm(OLD_CONFIG_FILE_LOCATION); + } - if($this->configFileExists()) { + if($this->setup_config_exists()) { + if($this->setup_config_exists(TRUE)) { + //the currently logged-in user is actually running the setup, no worries. + $this->siteStatus = 'You are running setup... please continue.'; + $this->setupRequired = TRUE; + $retval = TRUE; + } + else { + //tell 'em somebody is working on setup and to WAIT. + $this->siteStatus = 'Setup is in progress by another user. Please wait.'; + $retval = FALSE; + } + } + elseif($this->fileExists) { //got an existing config file. - if($this->isWorkingOnItSet()) { - //site access is locked (probably an upgrade); get the message and show 'em. - $retval = $this->isWorkingOnItSet(TRUE); - $this->showFatalError($retval); + if($this->is_workingonit_set()) { + //site access is locked; get the message and show 'em. + $this->siteStatus = $this->is_workingonit_set(TRUE); + $retval = $this->siteStatus; } - elseif($this->setupConfigExists()) { - if($this->setupConfigExists() === 'current_user') { - //the currently logged-in user is actually running the setup, no worries. - $retval = 'undergoing setup by current user'; - } - else { - //tell 'em somebody is working on setup and to WAIT. - $retval = $this->showSetupMessage(); - } - } else { //config exists, site not locked... GOOD TO GO! - $retval = $this->setOkay(); + $this->siteStatus = 'Normal (site is setup).'; + $retval = TRUE; } } else { - //check for the OLD config file. - if($this->oldConfigFileExists()) { - //copy old file to new location... - $this->copyOldConfigFile(); + //good to go! + $this->siteStatus = 'No existing config or setup file: you may initiate the setup process now.'; + $this->setupRequired = TRUE; + $retval = TRUE; + } + + return($retval); + + }//end check_site_status() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + private function is_workingonit_set($giveValue=FALSE) { + if((is_numeric($this->config['WORKINGONIT']) && $this->config['WORKINGONIT'] == 0) || ($this->config['WORKINGONIT'] === FALSE)) { + $retval = FALSE; + } + else { + $retval = TRUE; + } + + if($giveValue === TRUE) { + $retval = $this->config['WORKINGONIT']; + } + + return($retval); + }//end is_workingonit_set() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function setup_config_exists($checkOwnership=FALSE) { + $retval = FALSE; + + $dirContents = $this->fs->ls(CONFIG_DIRECTORY); + if($dirContents[SETUP_FILENAME]) { + $retval = TRUE; + + if($checkOwnership === TRUE) { + //read the object. + $xmlParser = new xmlParser($this->fs->read(SETUP_FILE_LOCATION)); + $configData = $xmlParser->get_tree(TRUE); + $configData = $configData['CONFIG']; - //now check if the site is locked. - if($this->isWorkingOnItSet()) { - //upgrade running. Show 'em the message. - $retval = $this->isWorkingOnItSet(TRUE); - $this->showFatalError($retval); + //now that we've got the data, determine if the current user is the owner. + if($configData['OWNER_SESSION'] === session_id()) { + $retval = TRUE; } - elseif($this->setupConfigExists()) { - //SETUP IN PROGRESS... - - if($this->setupConfigExists() === 'current_user') { - //the currently logged-in user is actually running the setup, no worries. - $retval = 'undergoing setup by current user'; - } - else { - //tell 'em somebody is working on setup and to WAIT. - $retval = $this->showSetupMessage(); - } - } else { - //good to go! - $retval=$this->setOkay(); + $retval = FALSE; } } - else { - //okay, no config file (new or old), no setup running, so current user has option - // to run setup (viewing the page not enough; must click something to create the - // setup config file which locks setup to their session) - $this->do_setup_redirect(); - } } return($retval); - //-------------------------------------------------- - //END pseudo code; - //============================================================= + }//end setup_config_exists() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function is_setup_required() { + return($this->setupRequired); + }//end is_setup_required() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + private function create_setup_config() { + $xmlCreator = new xmlCreator('config'); + $attributes = array( + 'creation' => time() + ); + $xmlCreator->add_tag('/config/owner_session', session_id(), $attributes); - }//end check_site_status() + $this->fs->create_file(SETUP_FILE_LOCATION); + $this->fs->write($xmlCreator->create_xml_string()); + }//end create_setup_config() //------------------------------------------------------------------------- + + + //------------------------------------------------------------------------- + public function get_site_status() { + return($this->siteStatus); + }//end get_site_status() + //------------------------------------------------------------------------- }//end config{} ?> \ No newline at end of file Modified: trunk/1.2/lib/site_config.php =================================================================== --- trunk/1.2/lib/site_config.php 2008-03-30 04:08:45 UTC (rev 858) +++ trunk/1.2/lib/site_config.php 2008-03-30 05:44:19 UTC (rev 859) @@ -25,9 +25,10 @@ require_once(dirname(__FILE__) .'/config.class.php'); define(CONFIG_FILENAME, 'config.xml'); +define(SETUP_FILENAME, 'setup.xml'); define(CONFIG_DIRECTORY, 'rw'); define(CONFIG_FILE_LOCATION, CONFIG_DIRECTORY .'/'. CONFIG_FILENAME); -define(SESSION_SETUP_KEY, '___setup_key___'); +define(SETUP_FILE_LOCATION, CONFIG_DIRECTORY .'/'. SETUP_FILENAME); //location where the config file USED to be, for the purpose of upgrading from previous versions. define(OLD_CONFIG_DIRECTORY, 'lib'); @@ -39,6 +40,7 @@ //TODO: turn off if it's not a dev site, but NOT if setup is running (so they can see problems). ini_set('error_reporting', 'On'); ini_set('display_errors', 'On'); +error_reporting(E_ALL && ~E_NOTICE); //########################################################################## function exception_handler($exception) { $exceptionMessage = $exception->getMessage(); @@ -72,14 +74,24 @@ //########################################################################## -$configObj = new config(dirname(__FILE__) .'/'. CONFIG_FILENAME, FALSE); -$configObj->read_config_file(TRUE, TRUE); +$configObj = new config(CONFIG_FILE_LOCATION, FALSE); check_external_lib_versions(); //call a method to see if setup should run. -$configObj->check_site_status(); +unset($_SESSION['setup_redirect']); +if($configObj->check_site_status()) { + if($configObj->is_setup_required()) { + $configObj->do_setup_redirect(); + } +} +else { + //tell 'em what the site's status is. + //TODO: make this look nicer. + echo($configObj->get_site_status()); + exit; +} if($_SERVER['DOCUMENT_ROOT']) { @@ -89,7 +101,7 @@ } else { //called from the command line. - $GLOBALS['SITE_ROOT'] = $_SERVER['HOME'] ."/partslogistics2002"; + $GLOBALS['SITE_ROOT'] = $_SERVER['HOME']; } $GLOBALS['LIBDIR']=$GLOBALS['SITE_ROOT'] . "/lib"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |