/** * 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; }
/** * Gets information on the columns in the given table. * * @history * 2013.09.30: * (JPK) Initial release * 2013.11.19: * (AT) Work around OCI driver limitations to obtain column information * * @version 2013.11.19 * @author (JPK) Jillian Koontz, Brigham Young Univ. <*****@*****.**> * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param string $table Table name * @throws \Cougar\Exceptions\Exception * @return array List of column properties */ public function getColumns($table) { $columns = array(); switch ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) { case "sqlite": # TODO: SQLite driver doesn't support getColumnMeta $statement = $this->pdo->prepare("SELECT * FROM " . $table . " LIMIT 1"); $statement->execute(); $result = $statement->fetch(); for ($i = 0; $i < count($result); $i++) { $columns[] = $statement->getColumnMeta($i); } break; case "mysql": $statement = $this->pdo->prepare("SELECT * FROM " . $table . " LIMIT 1"); $statement->execute(); $result = $statement->fetch(); for ($i = 0; $i < count($result); $i++) { $columns[] = $statement->getColumnMeta($i); } break; case "oci": $statement = $this->pdo->prepare("SELECT COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_SCALE,\n\t\t\t\t\t\t\tNULLABLE\n\t\t\t\t\t\tFROM dba_tab_columns WHERE table_name = :table_name"); $statement->execute(array(":table_name" => $table)); $result = $statement->fetchAll(\PDO::FETCH_ASSOC); foreach ($result as $raw_column) { $column = array("name" => "", "flags" => array(), "native_type" => ""); $column["name"] = $raw_column["COLUMN_NAME"]; switch ($raw_column["DATA_TYPE"]) { case "NUMBER": case "LONG": case "LONG RAW": if ($raw_column["DATA_SCALE"] > 0) { $column["native_type"] = "float"; } else { $column["native_type"] = "int"; } break; case "FLOAT": $column["native_type"] = "float"; break; case "DATE": case "TIMESTAMP": $column["native_type"] = "datetime"; break; default: if (substr($raw_column["DATA_TYPE"], 0, 9) == "TIMESTAMP") { $column["native_type"] = "datetime"; } else { $column["native_type"] = "string"; } break; } Format::strToBool($raw_column["NULLABLE"]); if (!$raw_column["NULLABLE"]) { $column["floags"][] = "not_null"; } $columns[] = $column; } break; default: throw new Exception("Unsupported driver"); } return $columns; }
/** * Returns the value of the given POST variable. If it does not exist, * return the default value (null by default). * * If the method is not post, and the request content type is application/ * x-www-form-urlencoded, the body will be parsed and the key/value pairs * will be put into the $_POST array. * * The value can be cast to one of the PHP scalar values (string, integer, * float or boolean). Additionally, a "set" type can be used to return true * if the variable is defined irregardless of its value, and false if it is * not. This is userful when GET variables are used as flags to the request * (for example, key=value&sort_asc). * * @history * 2013.09.30: * (AT) Initial release * * @version 2013.09.30 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @param int $variable POST variable name * @param string $type Cast to the specified type (string|int|float|bool) * @param mixed $default Default value * @return mixed POST variable value */ public function postValue($variable, $type = "string", $default = null) { # See if the header exists if (array_key_exists($variable, $_POST)) { # See what we will be returning switch ($type) { case "string": default: return $_POST[$variable]; break; case "int": return (int) $_POST[$variable]; break; case "float": return (double) $_POST[$variable]; break; case "bool": $value = $_POST[$variable]; Format::strToBool($value, true); return $value; break; case "set": return true; break; } } else { # Return the default value (always false for set type) if ($type == "set") { return false; } else { return $default; } } }
/** * 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\Format::removeNullWord */ public function testRemoveNullWord() { $string1 = "Hello World!"; Format::removeNullWord($string1); $this->assertEquals("Hello World!", $string1); $string2 = " Hello World! null"; Format::removeNullWord($string2); $this->assertEquals("Hello World!", $string2); $array = array("Hello World!", " Hello World!null"); Format::removeNullWord($array); $this->assertEquals(array("Hello World!", "Hello World!"), $array); $jagged_array = array("Hello World!", " Hello World! NULL", array("Hello World!", " Hello World! Null")); Format::removeNullWord($jagged_array); $this->assertEquals(array("Hello World!", "Hello World!", array("Hello World!", "Hello World!")), $jagged_array); $object = new \stdClass(); $object->value1 = "Hello World!"; $object->value2 = " Hello World!NULL"; Format::removeNullWord($object); $this->assertEquals("Hello World!", $object->value1); $this->assertEquals("Hello World!", $object->value2); $embedded_object = new \stdClass(); $embedded_object->value1 = "Hello World!"; $embedded_object->value2 = " Hello World!NULL"; $embedded_object->object = new \stdClass(); $embedded_object->object->value1 = "Hello World!"; $embedded_object->object->value2 = " Hello World! null"; Format::removeNullWord($embedded_object); $this->assertEquals("Hello World!", $embedded_object->value1); $this->assertEquals("Hello World!", $embedded_object->value2); $this->assertEquals("Hello World!", $embedded_object->object->value1); $this->assertEquals("Hello World!", $embedded_object->object->value2); }