Esempio n. 1
0
 /**
  * Fetch the cryptkey from the database and store it in the new GD_Crypt obj
  *
  * @throws GD_Exception
  */
 public function __construct()
 {
     $raw_cryptkey = GD_Config::get("cryptkey");
     if (!isset($raw_cryptkey) || $raw_cryptkey == "") {
         throw new GD_Exception("The 'cryptkey' value must be specified in config.ini.");
     }
     $this->_key = md5($raw_cryptkey);
 }
Esempio n. 2
0
 /**
  * Should be called before doing any get/set operation to "connect" to the
  * database
  */
 private static function initialise()
 {
     if (!isset(self::$_db)) {
         self::$_db_table = 'configuration';
         self::$_db = Zend_Db_Table::getDefaultAdapter();
         if (!self::$_db) {
             return false;
         }
     }
     return true;
 }
Esempio n. 3
0
 public function testCanGetAndSetConfig()
 {
     $this->loadDatabaseFromConfig();
     $checkKey = "gd_unit_test_value";
     $checkValue = time();
     $retval = GD_Config::set($checkKey, $checkValue);
     if (is_numeric($retval)) {
         $this->assertGreaterThan(0, $retval);
     } else {
         $this->assertTrue($retval);
     }
     $retval2 = GD_Config::get($checkKey);
     $this->assertEquals($checkValue, $retval2);
 }
Esempio n. 4
0
 protected function _initConfig()
 {
     // Default languages
     $use_lang = false;
     if (isset($setup_session->language)) {
         $use_lang = $setup_session->language;
     }
     // Config file
     $cfg_file = APPLICATION_PATH . '/configs/config.ini';
     // Load version
     $version_conf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/version.ini', 'version');
     Zend_Registry::set("gd.version", $version_conf->gd->version);
     // Initialise language to english in case of a "fallback" mode
     // Mainly for using _e and _r if something else fails.
     GD_Translate::init("english");
     // Set default database adapter
     if (file_exists($cfg_file)) {
         $db_conf = new Zend_Config_Ini($cfg_file, 'database');
         $adapter = Zend_Db::factory($db_conf->adapter, $db_conf->toArray());
         Zend_Db_Table::setDefaultAdapter($adapter);
         Zend_Registry::set("db", $db_conf);
         // If we're not on the /error/database page, do a DB test, else
         // we return out to ensure no DB errors later in this Bootstrap fn.
         if (stripos($_SERVER['REQUEST_URI'], '/error/database') === false) {
             // Do a database test to ensure we can run queries on the DB. If not
             // redirect to the error controller in a hacky way.
             try {
                 $adapter->query("SELECT 1");
             } catch (Zend_Db_Adapter_Exception $ex) {
                 header("Location: /error/database");
                 die;
             }
         } else {
             return;
         }
         $lang = GD_Config::get("language");
         if ($lang !== false) {
             $use_lang = $lang;
         }
     }
     if (!$use_lang) {
         $use_lang = "english";
     }
     $translate = GD_Translate::init($use_lang);
 }
Esempio n. 5
0
 /**
  * Set the current debug level or default. If debugging enabled then open
  * the file handle and allow logging.
  */
 private static function initialise()
 {
     try {
         self::$_current_debug_level = GD_Config::get("debug_level");
     } catch (Zend_Db_Adapter_Exception $ex) {
         self::$_current_debug_level = self::DEBUG_NONE;
     }
     if (!self::$_current_debug_level || self::$_current_debug_level == "0") {
         return false;
     }
     if (!self::$_fh) {
         $logfile = sys_get_temp_dir() . "/godeploy_log";
         self::$_fh = fopen($logfile, "a");
         //chmod($logfile, 0755);
         if (!self::$_fh) {
             return false;
         }
     }
     return true;
 }
Esempio n. 6
0
 /**
  * Set the current debug level or default. If debugging enabled then open
  * the file handle and allow logging.
  */
 private static function initialise()
 {
     try {
         self::$_current_debug_level = GD_Config::get("debug_level");
     } catch (Exception $ex) {
         error_log("Failed to get debug_level setting, error was: {$ex->getMessage()}", 0);
         self::$_current_debug_level = self::DEBUG_NONE;
     }
     if (!self::$_current_debug_level || self::$_current_debug_level == "0") {
         return false;
     }
     if (!self::$_fh) {
         $logfile = GD_Config::get('logfile');
         self::$_fh = @fopen($logfile, "a");
         if (!self::$_fh) {
             return false;
         }
     }
     return true;
 }
Esempio n. 7
0
 /**
  * Load version, database settings and language configurations
  *
  * (non-PHPdoc)
  * @see Zend_Controller_Plugin_Abstract::preDispatch()
  */
 public function preDispatch(Zend_Controller_Request_Abstract $request)
 {
     // If a language is in session (from setup), set that first
     // May get overridden later in this file
     $use_lang = false;
     if (isset($setup_session->language)) {
         $use_lang = $setup_session->language;
     }
     // Load version
     $version_conf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/version.ini', 'version');
     Zend_Registry::set("gd.version", $version_conf->gd->version);
     // Set default database adapter
     if (file_exists($this->_config_ini)) {
         $db_conf = new Zend_Config_Ini($this->_config_ini, 'database');
         $adapter = Zend_Db::factory($db_conf->adapter, $db_conf->toArray());
         Zend_Db_Table::setDefaultAdapter($adapter);
         Zend_Registry::set("db", $db_conf);
         // If we're not on the /error/database page, do a DB test, else
         // we return out to ensure no DB errors later in this Bootstrap fn.
         if ($request->controller != "error") {
             try {
                 $adapter->query("SELECT 1");
                 // If we can get the language from the database, use that language
                 $lang = GD_Config::get("language");
                 if ($lang !== false) {
                     $use_lang = $lang;
                 }
             } catch (Exception $ex) {
                 $this->_response->setRedirect('/error/database?e=' . $ex->getCode());
                 $this->_response->sendResponse();
             }
         }
     }
     // If we can't set a language at all, default to english
     if (!$use_lang) {
         $use_lang = "english";
     }
     // Initialise translations now we should have a language to use
     GD_Translate::init($use_lang);
 }
Esempio n. 8
0
 public function dosetupAction()
 {
     $this->view->headTitle('Configuration');
     $_user_config_file = APPLICATION_PATH . '/configs/config.ini';
     // Create the config ini from session
     $setup_session = new Zend_Session_Namespace('gd_setup_session');
     if (!$setup_session->complete) {
         $config = new Zend_Config(array(), true);
         $config->database = array();
         $config->database->adapter = "PDO_MYSQL";
         $config->database->host = $setup_session->database->host;
         $config->database->username = $setup_session->database->username;
         $config->database->password = $setup_session->database->password;
         $config->database->dbname = $setup_session->database->dbname;
         $writer_opts = array('config' => $config, 'filename' => $_user_config_file);
         $writer = new Zend_Config_Writer_Ini($writer_opts);
         try {
             $writer->write();
         } catch (Exception $ex) {
             if (strpos($ex->getMessage(), 'Could not write to file') !== false) {
                 $setup_session->ini_string = $writer->render();
             }
         }
         // Load the database manually
         Zend_Db_Table::setDefaultAdapter(Zend_Db::factory($config->database->adapter, $config->database->toArray()));
         // Run the appropriate database setup script
         $db_adm = new GD_Db_Admin($config->database->host, $config->database->username, $config->database->password, $config->database->dbname);
         $db_adm->installDatabase();
         // Set the other config values into database
         GD_Config::set("language", $setup_session->language ? $setup_session->language : "english");
         GD_Config::set("setup_complete", "1");
         GD_Config::set("cryptkey", md5(microtime() . $setup_session->admin->username . $setup_session->admin->password));
         GD_Config::set("install_date", date("d/m/Y H:i:s"));
         // Create the first user in the database
         $userMapper = new GD_Model_UsersMapper();
         $crypt = new GD_Crypt();
         $user = new GD_Model_User();
         $user->setName($setup_session->admin->username)->setPassword($crypt->makeHash($setup_session->admin->password))->setDateAdded(date('Y-m-d H:i:s'))->setAdmin(1)->enableUser();
         $userMapper->save($user);
         // Setup the SSH keypair
         $ssh_key = new GD_Model_SSHKey();
         $ssh_key->setSSHKeyTypesId(1);
         $ssh_key->generateKeyPair();
         //$ssh_key->setId(1);
         $ssh_keys_map = new GD_Model_SSHKeysMapper();
         $ssh_key_id = $ssh_keys_map->save($ssh_key);
         GD_Config::set("ssh_key_id", $ssh_key_id);
         $setup_session->complete = true;
     }
     if (isset($setup_session->ini_string)) {
         $this->view->ini = $setup_session->ini_string;
     } else {
         $this->_redirect("/setup/complete");
     }
 }
Esempio n. 9
0
 /**
  * Check the database version is the version we are expecting. If it isn't,
  * attempt to automatically upgrade the database.
  */
 protected function checkDatabaseVersion()
 {
     $version_conf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/version.ini', 'version');
     $expected_db_version = (int) $version_conf->gd->expect_db_version;
     try {
         $current_db_version = (int) GD_Config::get("db_version");
         if ($current_db_version < $expected_db_version) {
             $session = new Zend_Session_Namespace('gd_session');
             if ($session->upgrade_attempts >= 1) {
                 $session->upgrade_attempts = 0;
                 die("Database upgrade failed.");
             } else {
                 $cfg = Zend_Db_Table::getDefaultAdapter()->getConfig();
                 $db_adm = new GD_Db_Admin($cfg["host"], $cfg["username"], $cfg["password"], $cfg["dbname"]);
                 $db_adm->upgradeDatabase($current_db_version, $expected_db_version);
                 if (isset($session->upgrade_attempts)) {
                     $session->upgrade_attempts++;
                 } else {
                     $session->upgrade_attempts = 1;
                 }
                 $this->_response->setRedirect('/');
                 $this->_response->sendResponse();
             }
         } else {
             if ($current_db_version > $expected_db_version) {
                 die("Database version was too new??? Expected '{$expected_db_version}' and it is currently at '{$current_db_version}'.");
             }
         }
     } catch (Exception $ex) {
         $this->_request->setParam('db_error_detail', $ex->getMessage());
         $this->_request->setModuleName('default');
         $this->_request->setControllerName('error');
         $this->_request->setActionName('database');
         return;
     }
 }