diff options
| -rw-r--r-- | .htaccess | 3 | ||||
| -rw-r--r-- | index.php | 2 | ||||
| -rw-r--r-- | private/controller/Controller.php | 2 | ||||
| -rw-r--r-- | private/model/Connection.php | 23 | ||||
| -rw-r--r-- | private/model/creds.php | 103 | ||||
| -rw-r--r-- | private/view/Result.php | 4 |
6 files changed, 135 insertions, 2 deletions
diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..611f7cc --- /dev/null +++ b/.htaccess @@ -0,0 +1,3 @@ +RewriteEngine On +RewriteRule ^/api/ index.php +RedirectMatch 403 ^/private/.*$ @@ -1,5 +1,7 @@ <?php +declare(strict_types = On); + namespace AugustOffensive; include 'private/model/Connection.php'; diff --git a/private/controller/Controller.php b/private/controller/Controller.php index 84bd4e7..eda5e13 100644 --- a/private/controller/Controller.php +++ b/private/controller/Controller.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = On); + namespace AugustOffensive\controller; use AugustOffensive\model; diff --git a/private/model/Connection.php b/private/model/Connection.php index 26632f7..31b58c3 100644 --- a/private/model/Connection.php +++ b/private/model/Connection.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = On); + namespace AugustOffensive\model; /** @@ -7,6 +9,8 @@ namespace AugustOffensive\model; */ class Connection { + /** @var PDO $_conn PDO connection to database. */ + private $_conn; /** * Initiates connection to PostGreSQL database. * @@ -14,6 +18,23 @@ class Connection */ public function __construct () { - // + // Establish connection to db + include './creds.php'; + + try { + $_conn = new PDO( + "pgsql: host=" . $cred->getHost() . + (($cred->getPort() !== '') ? ";port=" . $cred->getPort() : '') . + ";dbname=" . $cred->getDBName(), + $cred->getLogin(), + $cred->getPassword() + ); + // we destroy $cred as quickly as possible + $cred = null; + } catch (PDOException as $e) { + // we destroy $cred as quickly as possible + $cred = null; + die(json_encode(array("Result-Type" => "Error", "Content" => array($e)))); + } } } diff --git a/private/model/creds.php b/private/model/creds.php new file mode 100644 index 0000000..8ec06ad --- /dev/null +++ b/private/model/creds.php @@ -0,0 +1,103 @@ +<?php + +declare(strict_types = On); + +/** + * Holds the credentials for connecting to the database + */ +class Cred +{ + /** @var string $host The destination host that holds the database. */ + private $host; + /** @var string $port The destination port for the database on the host. */ + private $port; + /** @var string $dbname The name of the database of which to connect. */ + private $dbname; + /** @var string $login The username for accessing the database. + * + * It is recommended to change the login to a more restrictive account once + * the tables have been created (e.g. an account that can only insert, + * select, and update on that specific database). + */ + private $login; + /** @var string $password The password to the account for access. */ + private $password; + + /** + * Sets the values of the credentials. + * + * @return Cred + */ + public function __construct () + { + $host = 'localhost'; + $port = '5432'; + $dbname = 'ao'; + $login = 'r_access'; + $password = 'secret'; + } + + /** + * Destructor ensures clean wipe of credentials from existing. + * + * @return void + */ + public function __destruct () + { + $host = ''; + $port = ''; + $dbname = ''; + $login = ''; + $password = ''; + } + + /** + * Returns the value of the host of the database. + * + * @return string $host + */ + public function getHost () + { + return $host; + } + /** + * Returns the value of the port of the host of which to connect. + * + * @return string $port + */ + public function getPort () + { + return $port; + } + /** + * Returns the value of the name of the database of which to connect. + * + * @return string $dbname + */ + public function getDBName () + { + return $dbname; + } + /** + * Returns the value of the username of the account of the database. + * + * @return string $login + */ + public function getLogin () + { + return $login; + } + /** + * Returns the value of the password of the account of the database. + * + * @return string password + */ + public function getPassword () + { + return $password; + } +} + +// destroy as quickly as possible +$cred = new Cred(); + diff --git a/private/view/Result.php b/private/view/Result.php index 0a2dd95..43e69fc 100644 --- a/private/view/Result.php +++ b/private/view/Result.php @@ -1,5 +1,7 @@ <?php +declare(strict_types = On); + namespace AugustOffensive\view; use AugustOffensive\controller; @@ -28,7 +30,7 @@ class Result * * @return string $result resulting sendback object generated from query. */ - public function collect () + public function collect (): string { // return json_encode(array("Result-Type" => "", "Content" => array())); |
