Exemplo n.º 1
0
 /**
  * @covers \Cougar\Util\StringObfuscator::getRandomString
  * @todo Implement testGetRandomString().
  */
 public function testGetRandomString()
 {
     foreach (array(0, 1, 5, 10, 15, 27, 39, 50, 100, 500, 1024) as $length) {
         $str = StringObfuscator::getRandomString($length);
         $this->assertEquals($length, strlen($str));
     }
 }
Exemplo n.º 2
0
 /**
  * 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));
 }
Exemplo n.º 3
0
 /**
  * Creates a new connection file with the parameters given. The username
  * and password will encoded either in ARC4 or via obfuscation. The key and
  * other pertinent values must be set up beforehand.
  *
  * @history
  * 2013.09.30:
  *   (AT)  Initial release
  * 2014.03.10:
  *   (AT)  Add support for persistent connections
  *
  * @version 2014.03.10
  * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**>
  *
  * @param string $name
  *   The name of the database connection
  * @param string $environment
  *   Connection environment
  * @param string $dsn
  *   PDO DSN connection string
  * @param string $username
  *   Username
  * @param string $password
  *   Password
  * @param string $encoding
  *   Encoding to use (Arc4 or Obfuscation)
  * @param string $arc4_config_file
  *   Configuration file with ARC4 parameters
  * @param bool $persistent
  *   Whether to establish a persistent connection
  * @return string Filename of created file
  * @throws \Cougar\Exceptions\Exception
  */
 public static function createConnectionFile($name, $environment, $dsn, $username, $password, $encoding = "Arc4", $arc4_config_file = null, $persistent = false)
 {
     # Make sure we have a name
     if (!$name) {
         throw new Exception("Connection name is required");
     }
     # Come up with the filename
     if ($environment) {
         $filename = strtolower($name . "." . $environment . ".conf");
         $env = $environment;
     } else {
         $filename = strtolower($name . ".conf");
         $env = "(default)";
     }
     # Make sure we have a DSN
     if (!$dsn) {
         throw new Exception("PDO DSN is required");
     }
     # Encode the username and password
     switch ($encoding) {
         case "Arc4":
             $enc_username = Arc4::encode($username);
             $enc_password = Arc4::encode($password);
             break;
         case "Obfuscation":
             $enc_username = StringObfuscator::encode($username);
             $enc_password = StringObfuscator::encode($password);
             break;
         default:
             throw new Exception("Invalid encoding: " . $encoding);
     }
     # See if we had an ARC4 encryption file
     $arc4_config_line = "";
     if ($arc4_config_file) {
         $arc4_config_line = "arc4 = " . $arc4_config_file;
     }
     # See if the connection is persistent
     if ($persistent) {
         $persistence_string = "yes";
     } else {
         $persistence_string = "no";
     }
     # Create the contents of the file
     $file = "# Begin Database Connection File\n\n# Connection name: " . $name . "\n# Environment: " . $env . "\n\ndsn = " . $dsn . "\nusername = "******"\npassword = "******"\npersistent = " . $persistence_string . "\n" . $arc4_config_line . "\n\n# End Database connection File\n";
     # Write out the contents
     file_put_contents($filename, $file);
     # Return the filename
     return $filename;
 }