示例#1
0
 /**
  * @covers \Cougar\Util\StringObfuscator::decode
  * @todo Implement testDecode().
  */
 public function testDecode()
 {
     # Test decoding the obfuscated strings
     foreach ($this->obfuscatedStrings as $index => $enc_string) {
         $dec_string = StringObfuscator::decode($enc_string);
         $this->assertEquals($this->strings[$index], $dec_string);
     }
     # Test decoding the original strings (should always return as equal)
     foreach ($this->strings as $index => $string) {
         $dec_string = StringObfuscator::decode($string);
         $this->assertEquals($this->strings[$index], $dec_string);
     }
 }
 /**
  * Adds a Basic authorization header to the request. The header consists of
  * the keyword "Basic" followed by the username and password, separated by
  * a colon, and encoded as base64.
  *
  * @history
  * 2014.01.23:
  *   (AT)  Initial release
  * 2014.01.27:
  *   (AT)  Added $content_type parameter to match interface
  *
  * @version 2014.01.27
  * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**>
  *
  * @param string $method
  *   Request's HTTP method
  * @param string $url
  *   Request URL
  * @param array $headers
  *   Request headers as key/value pairs
  * @param array $cookies
  *   Request cookies as key/value pairs
  * @param mixed $body
  *   Either an assoc. array of POST parameters or raw body content
  * @param string $content_type
  *   The body's content type (optional)
  */
 public function addCredentials($method, &$url, array &$headers, array &$cookies, &$body, $content_type = null)
 {
     // Add the authorization header
     $headers["Authorization"] = "Basic " . base64_encode(StringObfuscator::decode($this->username) . ":" . StringObfuscator::decode($this->password));
 }
示例#3
0
 /**
  * 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);
     }
 }