Fixed a couple of bugs in the new /provisioning/create API

This commit is contained in:
Albert Santoni 2015-02-18 12:21:15 -05:00
parent d2fae5adae
commit dcac7ab652
3 changed files with 53 additions and 44 deletions

View File

@ -35,7 +35,7 @@ require_once (APPLICATION_PATH."/logging/Logging.php");
Logging::setLogPath('/var/log/airtime/zendphp.log'); Logging::setLogPath('/var/log/airtime/zendphp.log');
// We need to manually route because we can't load Zend without the database being initialized first. // We need to manually route because we can't load Zend without the database being initialized first.
if (strpos("/provisioning/create", $_SERVER["REDIRECT_URL"]) !== false) { if (strpos($_SERVER["REQUEST_URI"], "/provisioning/create") !== false) {
$provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]); $provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]);
$provisioningHelper->createAction(); $provisioningHelper->createAction();
die(); die();

View File

@ -1,12 +1,8 @@
<?php <?php
/**
* Created by PhpStorm.
* User: sourcefabric
* Date: 12/02/15
* Time: 2:10 PM
*/
class ProvisioningHelper { /** This class provides the business logic for station provisioning. */
class ProvisioningHelper
{
/* @var $dbh PDO */ /* @var $dbh PDO */
static $dbh; static $dbh;
@ -14,18 +10,18 @@ class ProvisioningHelper {
// Parameter values // Parameter values
private $dbuser, $dbpass, $dbname, $dbhost, $dbowner, $apikey; private $dbuser, $dbpass, $dbname, $dbhost, $dbowner, $apikey;
public function __construct($apikey) { public function __construct($apikey)
{
$this->apikey = $apikey; $this->apikey = $apikey;
} }
/** /**
* Endpoint for setting up and installing the Airtime database. This all has to be done without Zend * Endpoint for setting up and installing the Airtime database. This all has to be done without Zend
* which is why the code looks so old school (eg. http_response_code). * which is why the code looks so old school (eg. http_response_code). (We can't currently bootstrap our
* Zend app without the database unfortunately.)
*/ */
public function createAction() { public function createAction()
{
$this->getParams();
$apikey = $_SERVER['PHP_AUTH_USER']; $apikey = $_SERVER['PHP_AUTH_USER'];
if (!isset($apikey) || $apikey != $this->apikey) { if (!isset($apikey) || $apikey != $this->apikey) {
Logging::info("Invalid API Key: $apikey"); Logging::info("Invalid API Key: $apikey");
@ -35,15 +31,21 @@ class ProvisioningHelper {
} }
try { try {
// $this->setNewDatabaseConnection();
$this->parsePostParams();
//For security, the Airtime Pro provisioning system creates the database for the user.
// $this->setNewDatabaseConnection();
//if ($this->checkDatabaseExists()) { //if ($this->checkDatabaseExists()) {
// throw new Exception("ERROR: Airtime database already exists"); // throw new Exception("ERROR: Airtime database already exists");
//} //}
//$this->createDatabase(); //$this->createDatabase();
//All we need to do is create the database tables.
$this->createDatabaseTables(); $this->createDatabaseTables();
} catch(Exception $e) { } catch (Exception $e) {
http_response_code(400); http_response_code(400);
Logging::info($e->getMessage()); Logging::error($e->getMessage());
echo $e->getMessage(); echo $e->getMessage();
return; return;
} }
@ -55,30 +57,33 @@ class ProvisioningHelper {
* Check if the database settings and credentials given are valid * Check if the database settings and credentials given are valid
* @return boolean true if the database given exists and the user is valid and can access it * @return boolean true if the database given exists and the user is valid and can access it
*/ */
private function checkDatabaseExists() { private function checkDatabaseExists()
{
$statement = self::$dbh->prepare("SELECT datname FROM pg_database WHERE datname = :dbname"); $statement = self::$dbh->prepare("SELECT datname FROM pg_database WHERE datname = :dbname");
$statement->execute(array(":dbname" => $this->dbname)); $statement->execute(array(":dbname" => $this->dbname));
$result = $statement->fetch(); $result = $statement->fetch();
return isset($result[0]); return isset($result[0]);
} }
private function getParams() { private function parsePostParams()
$params = []; {
parse_str($_SERVER["QUERY_STRING"], $params); $this->dbuser = $_POST['dbuser'];
foreach ($params as $k => $v) { $this->dbpass = $_POST['dbpass'];
$this->$k = $v; $this->dbname = $_POST['dbname'];
} $this->dbhost = $_POST['dbhost'];
$this->dbowner = $_POST['dbowner'];
} }
/** /**
* Set up a new database connection based on the parameters in the request * Set up a new database connection based on the parameters in the request
* @throws PDOException upon failure to connect * @throws PDOException upon failure to connect
*/ */
private function setNewDatabaseConnection() { private function setNewDatabaseConnection()
{
self::$dbh = new PDO("pgsql:host=" . $this->dbhost self::$dbh = new PDO("pgsql:host=" . $this->dbhost
. ";dbname=postgres" . ";dbname=postgres"
. ";port=5432" . ";user=" . $this->dbuser . ";port=5432" . ";user=" . $this->dbuser
. ";password=" . $this->dbpass); . ";password=" . $this->dbpass);
$err = self::$dbh->errorInfo(); $err = self::$dbh->errorInfo();
if ($err[1] != null) { if ($err[1] != null) {
throw new PDOException("ERROR: Could not connect to database"); throw new PDOException("ERROR: Could not connect to database");
@ -89,11 +94,12 @@ class ProvisioningHelper {
* Creates the Airtime database using the given credentials * Creates the Airtime database using the given credentials
* @throws Exception * @throws Exception
*/ */
private function createDatabase() { private function createDatabase()
{
Logging::info("Creating database..."); Logging::info("Creating database...");
$statement = self::$dbh->prepare("CREATE DATABASE " . pg_escape_string($this->dbname) $statement = self::$dbh->prepare("CREATE DATABASE " . pg_escape_string($this->dbname)
. " WITH ENCODING 'UTF8' TEMPLATE template0" . " WITH ENCODING 'UTF8' TEMPLATE template0"
. " OWNER " . pg_escape_string($this->dbowner)); . " OWNER " . pg_escape_string($this->dbowner));
if (!$statement->execute()) { if (!$statement->execute()) {
throw new Exception("ERROR: Failed to create Airtime database"); throw new Exception("ERROR: Failed to create Airtime database");
} }
@ -103,24 +109,24 @@ class ProvisioningHelper {
* Install the Airtime database * Install the Airtime database
* @throws Exception * @throws Exception
*/ */
private function createDatabaseTables() { private function createDatabaseTables()
{
Logging::info("Creating database tables...");
$sqlDir = dirname(APPLICATION_PATH) . "/build/sql/"; $sqlDir = dirname(APPLICATION_PATH) . "/build/sql/";
$files = array("schema.sql", "sequences.sql", "views.sql", "triggers.sql", "defaultdata.sql"); $files = array("schema.sql", "sequences.sql", "views.sql", "triggers.sql", "defaultdata.sql");
foreach ($files as $f) { foreach ($files as $f) {
try { /*
/* * Unfortunately, we need to use exec here due to PDO's lack of support for importing
* Unfortunately, we need to use exec here due to PDO's lack of support for importing * multi-line .sql files. PDO->exec() almost works, but any SQL errors stop the import,
* multi-line .sql files. PDO->exec() almost works, but any SQL errors stop the import, * so the necessary DROPs on non-existent tables make it unusable. Prepared statements
* so the necessary DROPs on non-existent tables make it unusable. Prepared statements * have multiple issues; they similarly die on any SQL errors, fail to read in multi-line
* have multiple issues; they similarly die on any SQL errors, fail to read in multi-line * commands, and fail on any unescaped ? or $ characters.
* commands, and fail on any unescaped ? or $ characters. */
*/ exec("PGPASSWORD=$this->dbpass psql -U $this->dbuser --dbname $this->dbname -h $this->dbhost -f $sqlDir$f", $out, $status);
exec("export PGPASSWORD=" . $this->dbpass . " && psql -U " . $this->dbuser . " --dbname " if ($status != 0) {
. $this->dbname . " -h " . $this->dbhost . " -f $sqlDir$f 2>/dev/null", $out, $status);
} catch (Exception $e) {
throw new Exception("ERROR: Failed to create database tables"); throw new Exception("ERROR: Failed to create database tables");
} }
} }
} }
} }

View File

@ -1,3 +1,6 @@
-- Schema version
INSERT INTO cc_pref("keystr", "valstr") VALUES('system_version', '2.5.9');
INSERT INTO cc_subjs ("login", "type", "pass") VALUES ('admin', 'A', md5('admin')); INSERT INTO cc_subjs ("login", "type", "pass") VALUES ('admin', 'A', md5('admin'));
-- added in 2.3 -- added in 2.3
INSERT INTO cc_stream_setting ("keyname", "value", "type") VALUES ('off_air_meta', 'Airtime - offline', 'string'); INSERT INTO cc_stream_setting ("keyname", "value", "type") VALUES ('off_air_meta', 'Airtime - offline', 'string');