/** * implement a singleton, create if needed * * @return mixed object|false */ public static function init() { if (empty(Anonymous_Statistics::$instance)) { Anonymous_Statistics::$instance = new Anonymous_Statistics(); } if (!Kohana::config('core.anonymous_statistics')) { self::$cache->delete_tag('anonymous_statistics'); return false; } return Anonymous_Statistics::$instance; }
/** * Validate form and test db connection * * @return bool true if db can connect and form is valid */ private function processConfigure() { // This both ensures that there is a / on both the front and back of siteDomain // but also if the is no actual domain the string remains empty (ie NOT //) $siteDomain = $this->session->get('installer.siteDomain', Kohana::config('core.site_domain')); $siteDomain = trim($siteDomain, '/'); $siteDomain = empty($siteDomain) ? '/' : '/' . $siteDomain . '/'; // This changes the log_threshold if it is not already changed by the user if (($log_threshold = Kohana::config('core.log_threshold')) == 0) { $log_threshold = $this->log_threshold; } // This array maps the database returns to the database config file $databaseOptions = array('type' => $this->session->get('installer.dbType'), 'host' => $this->session->get('installer.dbHostName'), 'port' => $this->session->get('installer.dbPortSelection'), 'user' => $this->session->get('installer.dbUserName'), 'pass' => $this->session->get('installer.dbUserPwd'), 'database' => $this->session->get('installer.dbName')); // This array maps the configuration returns to the config file $configOptions = array('site_domain' => $siteDomain, 'anonymous_statistics' => (bool) $this->session->get('installer.collectStatistics'), 'anonymous_id' => Anonymous_Statistics::getID(), 'log_threshold' => $log_threshold); // This array maps the configuration to the locale file $localeOptions = array('timezone' => $this->session->get('installer.defaultTimeZone')); // This array maps the configuration returns to the upload file $uploadOptions = array('directory' => $this->session->get('installer.uploadDir')); // Check if this will be using sqlite if (stristr($databaseOptions['type'], 'sqlite')) { $databaseOptions['host'] = $this->session->get('installer.dbPathName'); if (!file_exists($databaseOptions['host'])) { message::set('SQLite path does not exist or can not be read!'); return FALSE; } } // Ensure we have everything we need if (empty($databaseOptions['type']) || empty($databaseOptions['host']) || empty($databaseOptions['database'])) { message::set('Minimum database configuration not provided!'); return FALSE; } // If the user wants to use a custom port then set that up if (empty($databaseOptions['port'])) { $databaseOptions['port'] = 'FALSE'; } // anonymous statistics need to be a quoted bool for the config parser if (empty($configOptions['anonymous_statistics'])) { $configOptions['anonymous_statistics'] = 'FALSE'; } else { $configOptions['anonymous_statistics'] = 'TRUE'; } // database persistents need to be a quoted bool for the config parser if (empty($databaseOptions['persistent'])) { $databaseOptions['persistent'] = 'FALSE'; } else { $databaseOptions['persistent'] = 'TRUE'; } // Get the doctrine overlord $manager = Doctrine_Manager::getInstance(); // Close all current DB connections foreach ($manager as $conn) { $manager->closeConnection($conn); } foreach ($databaseOptions as $dbOption => $value) { Kohana::config_set('database.default.connection.' . $dbOption, $value); } // Re-initialize Doctrine call_user_func(array('DoctrineHook', 'bootstrapDoctrine'), FALSE); try { // See if we can connect to the db $manager->getCurrentConnection()->connect(); } catch (Doctrine_Connection_Exception $e) { // If we can not connect to the existing db and we are in upgrade mode // then error out $installMode = $this->session->get('installer.installMode', 'install'); if ($installMode != 'install') { // We cant resolve this issue without the user message::set('Unable to establish a connection to ' . $databaseOptions['database'] . '! <div class="error_details">' . $e->getMessage() . '</div>'); return FALSE; } try { // See if we can fix this problem by creating the DB, but only if we are in install mode! // If we are in upgrade then the user provided the wrong DB info... $response = $manager->createDatabases(); /** * TODO: Doctrine 1.2 does not seem to throw bad credentials for postgres * ....REPORT THIS TO DOCTRINE! */ if (!is_string($response['BlueboxDB'])) { $conn = $manager->getCurrentConnection(); $conn->rethrowException($response['BlueboxDB'], $conn); } } catch (Exception $e) { // We cant resolve this issue without the user message::set('Unable to establish a connection to ' . $databaseOptions['type'] . '! <div class="error_details">' . $e->getMessage() . '</div>'); return FALSE; } $this->session->set('installer.ensureInstall', $databaseOptions['type']); } $ensureInstall = $this->session->get('installer.ensureInstall', 'none'); // If this db exists, we are preforming a fresh install, and the user hasn't been warned. // Force them to go around again accepting this fact! if ($ensureInstall != $databaseOptions['type']) { message::set('The existing database will be permanently erased if you continue!'); message::set('Click continue again to proceed...', 'alert'); // This session var lets the user continue the second time around (after the warning) $this->session->set('installer.ensureInstall', $databaseOptions['type']); return FALSE; } // Write $configOptions to config.php if (!self::updateConfig($configOptions, 'config')) { return FALSE; } // Write $localeOptions to locale.php if (!self::updateConfig($localeOptions, 'locale')) { return FALSE; } // Write $databaseOptions to database.php if (!self::updateConfig($databaseOptions, 'database')) { return FALSE; } // Write $uploadOptions to upload.php if (!self::updateConfig($uploadOptions, 'upload')) { return FALSE; } // Go ahead and enable or disable anonymous stats based on the user, because we are about to try to use it if ($configOptions['anonymous_statistics'] == 'FALSE') { //Anonymous_Statistics::clear(); Kohana::config_set('core.anonymous_statistics', FALSE); } else { Kohana::config_set('core.anonymous_statistics', TRUE); // If the user has opted into anonymous_statistics then write out what we have stored in session previously $preStats = $this->session->get('installer.pre_stats'); if (!empty($preStats) && Kohana::config('core.anonymous_statistics')) { foreach ($preStats as $id => $preStat) { //Anonymous_Statistics::addMsg($preStat[0], $preStat[1], $preStat[2], $id); } $this->session->delete('installer.pre_stats'); } //Anonymous_Statistics::addMsg($databaseOptions['type'], 'db_type', 'installer', 'db_type'); } return TRUE; }