/** * Loads ARC4 encryption parameters from a file * * @history * 2013.09.30: * (AT) Initial release * * @version 2013.09.30 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param string $filename Config file with ARC4 parameters */ public static function loadParameters($filename) { $config = new Config($filename); $key = $config->value("key"); if ($key) { self::setKey($key); } self::$magic = (string) $config->value("magic"); $compress = $config->value("compress"); Format::strToBool($compress); self::$compress = $compress; }
/** * Returns a database connection to the named database. * * The information on the database is stored in individual Config files. The * place were these files are stored is configured via Config's static * properties. * * The config file should contain the following values: * * dsn - The PDO dsn connection string * username - The username to use * password - The password used for connection * arc4 - Config file to ARC4 encryption parameters (optional) * * The username and passwords can be stored in either ARC4 encrypted form * (hex) or as obfuscated strings. * * The configuration files need to named using the following convention: * * connection_name.environment.conf * * Because Unix filenames are case sensitive, the database name and the * environment will be converted to lowercase. Since Oracle schema names * are case-insensitive, and MySQL should be configured to be * case-insensitive, this should be of little difference. If case- * sensitivity is required, the DSN will preserve the casing of the schema * name. * * If no suitable environment is found, the method will do a final attempt * to find the connection information in a file named connection_name.conf. * If that's not present, it will do a final search for database.conf. * * The environment value can be any value. If no environment is specified, * the value will be taken from the ENVIRONMENT constant. If it is not set, * it will revert to "local." You may not specify "production" as the * Production systems must have the ENVIRONMENT constant set. * * For best results, use the cougar-create-pdo-connection-file script * from a command prompt to generate your configuration files. * * @history * 2013.09.30: * (AT) Initial release * 2014.03.10: * (AT) Allow connection files to have a persistence flag * * @version 2014.03.10 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param string $name * Database connection name * @param string $environment * Environment to use (optional) * @return \Cougar\PDO\PDO Database connection * @throws \Cougar\Exceptions\Exception * @throws \Cougar\Exceptions\ConfigurationFileNotFoundException */ public static function getConnection($name, $environment = null) { # Make sure we have a database name $name = strtolower($name); if (!$name) { throw new Exception("Database name is required"); } # See if we have an environment if ($environment) { # Convert the environment to lowercase $environment = strtolower($environment); # Make sure we are not trying to set "production" as environment if ($environment == "production") { throw new Exception("You may not override the environment " . "\"production\""); } } else { # Grab the environment from the ENVIROMENT constant if (defined("ENVIRONMENT")) { $environment = strtolower(ENVIRONMENT); } else { $environment = "local"; } } # Define the list of filenames to look for $filenames = array($name . "." . $environment . ".conf", $name . ".conf", "database.conf"); # Try to load one of the configuration files $config = null; foreach ($filenames as $filename) { try { $config = new Config($filename); break; } catch (ConfigurationFileNotFoundException $e) { # Ignore file not found exceptions } } if (!$config) { throw new ConfigurationFileNotFoundException("Could not load configuration file for " . $name . " database for " . $environment); } # Get the values $dsn = $config->value("dsn"); $orig_username = $config->value("username"); $orig_password = $config->value("password"); $persistent = $config->value("persistent"); Format::strToBool($persistent); if (!$dsn) { throw new Exception("DSN is not declared in the configuration file"); } # Try to decode the username and password via obfuscation $username = StringObfuscator::decode($orig_username); $password = StringObfuscator::decode($orig_password); if ($username == $orig_username && $password == $orig_password) { # Try to decode with Arc4 $param_file = $config->value("arc4"); if (!$param_file) { $param_file = $config->value("encryption"); } if ($param_file) { Arc4::loadParameters($param_file); } $username = Arc4::decode($username); $password = Arc4::decode($password); } # See if we are establishing a persistent connection if ($persistent) { return new PDO($dsn, $username, $password, array(PDO::ATTR_PERSISTENT => true)); } else { # Return the new database connection return new PDO($dsn, $username, $password); } }
/** * @covers \Cougar\Util\Config::value */ public function testValue() { $this->assertEquals("value1", $this->object->value("name1")); $this->assertEquals("value2", $this->object->value("name2")); $this->assertEquals("value3", $this->object->value("name3")); $this->assertEquals("value4", $this->object->value("name4")); $this->assertEquals("value5", $this->object->value("name5")); $this->assertEquals("value6", $this->object->value("name6")); $this->assertEquals("value 7", $this->object->value("name7")); $this->assertEquals("value 8", $this->object->value("name8")); $this->assertEquals("value9", $this->object->value("name 9")); $this->assertEquals("value 10", $this->object->value("name 10")); $this->assertEquals("Indented value 11", $this->object->value("name11")); $this->assertEquals("Indented value 12", $this->object->value("name12")); $this->assertEquals("", $this->object->value("no_cache")); $this->assertEquals("mysql:hostname=localhost;dbname=UnitTest", $this->object->value("dsn")); $this->assertEquals("mysql", $this->object->value("db.type")); $this->assertEquals("localhost", $this->object->value("db.hostname")); $this->assertEquals("3306", $this->object->value("db.port")); $this->assertEquals("UnitTest", $this->object->value("db.schema")); $this->assertEquals("user", $this->object->value("db.username")); $this->assertEquals(":!:AQwo5+/yMbfoUPHjq+I2WC:!:", $this->object->value("db.password")); $this->assertNull($this->object->value("Some other setting")); }