/** * @covers Cougar\Util\Arc4::loadParameters */ public function testLoadParameters() { file_put_contents("arc4_parameters_unit_test.conf", "key = 0123456789abcdef\n" . "magic = some_value\n" . "compress = yes"); Arc4::loadParameters("arc4_parameters_unit_test.conf"); unlink("arc4_parameters_unit_test.conf"); $this->assertTrue(true); }
/** * 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); } }