/** * Returns the path to the application's dispatcher. Optionally with the * domain prepended. * ie. http://domain.com/[web_root]/index.php/request * * @param boolean $WithDomain Should it include the domain with the WebRoot? Default is FALSE. * @return string */ public static function WebRoot($WithDomain = FALSE) { static $WebRoot = NULL; if (is_null($WebRoot)) { // Attempt to get the webroot from the configuration array $WebRoot = Gdn::Config('Garden.WebRoot'); // Attempt to get the webroot from the server if ($WebRoot === FALSE) { $WebRoot = explode('/', ArrayValue('PHP_SELF', $_SERVER, '')); // Look for index.php to figure out where the web root is. $Key = array_search('index.php', $WebRoot); if ($Key !== FALSE) { $WebRoot = implode('/', array_slice($WebRoot, 0, $Key)); } else { $WebRoot = ''; } } } if (is_string($WebRoot) && $WebRoot != '') { // Strip forward slashes from the beginning of webroot return ($WithDomain ? Gdn_Url::Domain() : '') . preg_replace('/(^\\/+)/', '', $WebRoot); } else { return $WithDomain ? Gdn_Url::Domain() : ''; } }
/** * Allows the configuration of basic setup information in Garden. This * should not be functional after the application has been set up. */ public function Configure($RedirectUrl = '') { $Config = Gdn::Factory(Gdn::AliasConfig); $ConfigFile = PATH_CONF . DS . 'config.php'; // Create a model to save configuration settings $Validation = new Gdn_Validation(); $ConfigurationModel = new Gdn_ConfigurationModel('Configuration', $ConfigFile, $Validation); $ConfigurationModel->SetField(array('Garden.Locale', 'Garden.Title', 'Garden.RewriteUrls', 'Garden.WebRoot', 'Garden.Cookie.Salt', 'Garden.Cookie.Domain', 'Database.Name', 'Database.Host', 'Database.User', 'Database.Password')); // Set the models on the forms. $this->Form->SetModel($ConfigurationModel); // Load the locales for the locale dropdown // $Locale = Gdn::Locale(); // $AvailableLocales = $Locale->GetAvailableLocaleSources(); // $this->LocaleData = array_combine($AvailableLocales, $AvailableLocales); // If seeing the form for the first time... if (!$this->Form->IsPostback()) { // Force the webroot using our best guesstimates $ConfigurationModel->Data['Database.Host'] = 'localhost'; $this->Form->SetData($ConfigurationModel->Data); } else { // Define some validation rules for the fields being saved $ConfigurationModel->Validation->AddRule('Connection', 'function:ValidateConnection'); $ConfigurationModel->Validation->ApplyRule('Database.Name', 'Connection'); $ConfigurationModel->Validation->ApplyRule('Garden.Title', 'Required'); $ConfigurationFormValues = $this->Form->FormValues(); if ($ConfigurationModel->Validate($ConfigurationFormValues) !== TRUE) { // Apply the validation results to the form(s) $this->Form->SetValidationResults($ConfigurationModel->ValidationResults()); } else { $Host = Gdn_Url::Host(); $Domain = Gdn_Url::Domain(); // Set up cookies now so that the user can be signed in. $ConfigurationFormValues['Garden.Cookie.Salt'] = RandomString(10); $ConfigurationFormValues['Garden.Cookie.Domain'] = strpos($Host, '.') === FALSE ? '' : $Host; // Don't assign the domain if it is a non .com domain as that will break cookies. $ConfigurationModel->Save($ConfigurationFormValues); // If changing locale, redefine locale sources: $NewLocale = 'en-CA'; // $this->Form->GetFormValue('Garden.Locale', FALSE); if ($NewLocale !== FALSE && Gdn::Config('Garden.Locale') != $NewLocale) { $ApplicationManager = new Gdn_ApplicationManager(); $PluginManager = Gdn::Factory('PluginManager'); $Locale = Gdn::Locale(); $Locale->Set($NewLocale, $ApplicationManager->EnabledApplicationFolders(), $PluginManager->EnabledPluginFolders(), TRUE); } // Set the instantiated config object's db params and make the database use them (otherwise it will use the default values from conf/config-defaults.php). $Config->Set('Database.Host', $ConfigurationFormValues['Database.Host']); $Config->Set('Database.Name', $ConfigurationFormValues['Database.Name']); $Config->Set('Database.User', $ConfigurationFormValues['Database.User']); $Config->Set('Database.Password', $ConfigurationFormValues['Database.Password']); $Config->ClearSaveData(); Gdn::FactoryInstall(Gdn::AliasDatabase, 'Gdn_Database', PATH_LIBRARY . DS . 'database' . DS . 'class.database.php', Gdn::FactorySingleton, array(Gdn::Config('Database'))); // Install db structure & basic data. $Database = Gdn::Database(); $Drop = FALSE; // Gdn::Config('Garden.Version') === FALSE ? TRUE : FALSE; $Explicit = FALSE; try { include PATH_APPLICATIONS . DS . 'garden' . DS . 'settings' . DS . 'structure.php'; } catch (Exception $ex) { $this->Form->AddError(strip_tags($ex->getMessage())); } if ($this->Form->ErrorCount() > 0) { return FALSE; } // Create the administrative user $UserModel = Gdn::UserModel(); $UserModel->DefineSchema(); $UserModel->Validation->ApplyRule('Name', 'Username', 'Admin username can only contain letters, numbers, and underscores.'); $UserModel->Validation->ApplyRule('Password', 'Required'); $UserModel->Validation->ApplyRule('Password', 'Match'); if (!$UserModel->SaveAdminUser($ConfigurationFormValues)) { $this->Form->SetValidationResults($UserModel->ValidationResults()); } else { // The user has been created successfully, so sign in now $Authenticator = Gdn::Authenticator(); $AuthUserID = $Authenticator->Authenticate(array('Name' => $this->Form->GetValue('Name'), 'Password' => $this->Form->GetValue('Password'), 'RememberMe' => TRUE)); } if ($this->Form->ErrorCount() > 0) { return FALSE; } // Assign some extra settings to the configuration file if everything succeeded. $ApplicationInfo = array(); include CombinePaths(array(PATH_APPLICATIONS . DS . 'garden' . DS . 'settings' . DS . 'about.php')); $Config->Load($ConfigFile, 'Save'); $Config->Set('Garden.Version', ArrayValue('Version', ArrayValue('Garden', $ApplicationInfo, array()), 'Undefined')); $Config->Set('Garden.WebRoot', Gdn_Url::WebRoot()); $Config->Set('Garden.RewriteUrls', function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules()) ? TRUE : FALSE); $Config->Set('Garden.Domain', $Domain); $Config->Set('Garden.CanProcessImages', function_exists('gd_info')); $Config->Set('Garden.Messages.Cache', 'arr:["Garden\\/Settings\\/Index"]'); // Make sure that the "welcome" message is cached for viewing $Config->Set('EnabledPlugins.HTMLPurifier', 'HtmlPurifier'); // Make sure html purifier is enabled so html has a default way of being safely parsed $Config->Save(); } } return $this->Form->ErrorCount() == 0 ? TRUE : FALSE; }