From 52813045a62d851ba21159deb38024678438ebbe Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@gmail.com>
Date: Tue, 3 Mar 2015 15:10:10 -0500
Subject: [PATCH 1/5] Additional parameters in provisioning when creating
 stations from dashboard

---
 .../application/common/ProvisioningHelper.php | 24 ++++++++++++++-----
 airtime_mvc/application/models/Preference.php |  5 ++++
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/airtime_mvc/application/common/ProvisioningHelper.php b/airtime_mvc/application/common/ProvisioningHelper.php
index 48bbddc8c..d8183a57c 100644
--- a/airtime_mvc/application/common/ProvisioningHelper.php
+++ b/airtime_mvc/application/common/ProvisioningHelper.php
@@ -10,6 +10,7 @@ class ProvisioningHelper
     // Parameter values
     private $dbuser, $dbpass, $dbname, $dbhost, $dbowner, $apikey;
     private $instanceId;
+    private $station_name, $description;
 
     public function __construct($apikey)
     {
@@ -34,7 +35,7 @@ class ProvisioningHelper
         try {
 
             $this->parsePostParams();
-            
+
             //For security, the Airtime Pro provisioning system creates the database for the user.
             $this->setNewDatabaseConnection();
 
@@ -58,6 +59,7 @@ class ProvisioningHelper
 
             $this->createDatabaseTables();
             $this->initializeMusicDirsTable($this->instanceId);
+            $this->initializePrefs();
         } catch (Exception $e) {
             http_response_code(400);
             Logging::error($e->getMessage()
@@ -102,6 +104,9 @@ class ProvisioningHelper
         $this->dbhost = $_POST['dbhost'];
         $this->dbowner = $_POST['dbowner'];
         $this->instanceId = $_POST['instanceid'];
+
+        $this->station_name = $_POST['station_name'];
+        $this->description = $_POST['description'];
     }
 
     /**
@@ -111,9 +116,9 @@ class ProvisioningHelper
     private function setNewDatabaseConnection()
     {
         self::$dbh = new PDO("pgsql:host=" . $this->dbhost
-            . ";dbname=" . $this->dbname
-            . ";port=5432" . ";user=" . $this->dbuser
-            . ";password=" . $this->dbpass);
+                             . ";dbname=" . $this->dbname
+                             . ";port=5432" . ";user=" . $this->dbuser
+                             . ";password=" . $this->dbpass);
         //Turn on PDO exceptions because they're off by default.
         //self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $err = self::$dbh->errorInfo();
@@ -130,8 +135,8 @@ class ProvisioningHelper
     {
         Logging::info("Creating database...");
         $statement = self::$dbh->prepare("CREATE DATABASE " . pg_escape_string($this->dbname)
-            . " WITH ENCODING 'UTF8' TEMPLATE template0"
-            . " OWNER " . pg_escape_string($this->dbowner));
+                                         . " WITH ENCODING 'UTF8' TEMPLATE template0"
+                                         . " OWNER " . pg_escape_string($this->dbowner));
         if (!$statement->execute()) {
             throw new Exception("ERROR: Failed to create Airtime database");
         }
@@ -182,5 +187,12 @@ class ProvisioningHelper
         $musicDir->save();
     }
 
+    /**
+     * Initialize preference values passed from the dashboard (if any exist)
+     */
+    private function initializePrefs() {
+        Application_Model_Preference::SetStationName($this->station_name);
+        Application_Model_Preference::SetStationDescription($this->description);
+    }
 
 }
diff --git a/airtime_mvc/application/models/Preference.php b/airtime_mvc/application/models/Preference.php
index aef7f0bb5..a25679826 100644
--- a/airtime_mvc/application/models/Preference.php
+++ b/airtime_mvc/application/models/Preference.php
@@ -326,6 +326,11 @@ class Application_Model_Preference
         return self::getValue("station_name");
     }
 
+    public static function SetStationName($station_name)
+    {
+        self::setValue("station_name", $station_name);
+    }
+
     public static function SetAutoUploadRecordedShowToSoundcloud($upload)
     {
         self::setValue("soundcloud_auto_upload_recorded_show", $upload);

From 95db8533b534d22fd99f68eacef6015c8c079862 Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@gmail.com>
Date: Tue, 3 Mar 2015 15:29:34 -0500
Subject: [PATCH 2/5] Made provisioning helper slightly more robust

---
 airtime_mvc/application/Bootstrap.php         |  2 +-
 .../application/common/ProvisioningHelper.php | 29 ++++++++++---------
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/airtime_mvc/application/Bootstrap.php b/airtime_mvc/application/Bootstrap.php
index 153ce672d..7c9cf8c20 100644
--- a/airtime_mvc/application/Bootstrap.php
+++ b/airtime_mvc/application/Bootstrap.php
@@ -36,7 +36,7 @@ require_once (APPLICATION_PATH."/logging/Logging.php");
 Logging::setLogPath('/var/log/airtime/zendphp.log');
 
 // We need to manually route because we can't load Zend without the database being initialized first.
-if (array_key_exists("REQUEST_URI", $_SERVER) && (strpos($_SERVER["REQUEST_URI"], "/provisioning/create") !== false)) {
+if (array_key_exists("REQUEST_URI", $_SERVER) && (stripos($_SERVER["REQUEST_URI"], "/provisioning/create") !== false)) {
     $provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]);
     $provisioningHelper->createAction();
     die();
diff --git a/airtime_mvc/application/common/ProvisioningHelper.php b/airtime_mvc/application/common/ProvisioningHelper.php
index d8183a57c..a436e6974 100644
--- a/airtime_mvc/application/common/ProvisioningHelper.php
+++ b/airtime_mvc/application/common/ProvisioningHelper.php
@@ -37,28 +37,31 @@ class ProvisioningHelper
             $this->parsePostParams();
 
             //For security, the Airtime Pro provisioning system creates the database for the user.
-            $this->setNewDatabaseConnection();
+            if ($this->dbhost && !empty($this->dbhost)) {
+                $this->setNewDatabaseConnection();
 
-            //if ($this->checkDatabaseExists()) {
-            //    throw new Exception("ERROR: Airtime database already exists");
-            //}
+                //if ($this->checkDatabaseExists()) {
+                //    throw new Exception("ERROR: Airtime database already exists");
+                //}
 
-            if (!$this->checkDatabaseExists()) {
-                throw new Exception("ERROR: $this->dbname database does not exist.");
-            }
+                if (!$this->checkDatabaseExists()) {
+                    throw new Exception("ERROR: $this->dbname database does not exist.");
+                }
 
-            //We really want to do this check because all the Propel-generated SQL starts with "DROP TABLE IF EXISTS".
-            //If we don't check, then a second call to this API endpoint would wipe all the tables!
-            if ($this->checkTablesExist()) {
-                throw new Exception("ERROR: airtime tables already exists");
+                //We really want to do this check because all the Propel-generated SQL starts with "DROP TABLE IF EXISTS".
+                //If we don't check, then a second call to this API endpoint would wipe all the tables!
+                if ($this->checkTablesExist()) {
+                    throw new Exception("ERROR: airtime tables already exists");
+                }
+
+                $this->createDatabaseTables();
+                $this->initializeMusicDirsTable($this->instanceId);
             }
 
             //$this->createDatabase();
 
             //All we need to do is create the database tables.
 
-            $this->createDatabaseTables();
-            $this->initializeMusicDirsTable($this->instanceId);
             $this->initializePrefs();
         } catch (Exception $e) {
             http_response_code(400);

From e2054c13c8e45b21dd71f7d6e2861e476a37e645 Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@gmail.com>
Date: Tue, 3 Mar 2015 16:04:45 -0500
Subject: [PATCH 3/5] Added provisioning code for change requests

---
 airtime_mvc/application/Bootstrap.php         |  5 ++++
 .../application/common/ProvisioningHelper.php | 29 +++++++++++++++++--
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/airtime_mvc/application/Bootstrap.php b/airtime_mvc/application/Bootstrap.php
index 7c9cf8c20..2db23c8f7 100644
--- a/airtime_mvc/application/Bootstrap.php
+++ b/airtime_mvc/application/Bootstrap.php
@@ -36,10 +36,15 @@ require_once (APPLICATION_PATH."/logging/Logging.php");
 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 should probably look for a better way to do this rather tan overloading this if statement
 if (array_key_exists("REQUEST_URI", $_SERVER) && (stripos($_SERVER["REQUEST_URI"], "/provisioning/create") !== false)) {
     $provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]);
     $provisioningHelper->createAction();
     die();
+} else if (array_key_exists("REQUEST_URI", $_SERVER) && (stripos($_SERVER["REQUEST_URI"], "/provisioning/change") !== false)) {
+    $provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]);
+    $provisioningHelper->changeAction();
+    die();
 }
 
 Config::setAirtimeVersion();
diff --git a/airtime_mvc/application/common/ProvisioningHelper.php b/airtime_mvc/application/common/ProvisioningHelper.php
index a436e6974..61127463a 100644
--- a/airtime_mvc/application/common/ProvisioningHelper.php
+++ b/airtime_mvc/application/common/ProvisioningHelper.php
@@ -65,8 +65,33 @@ class ProvisioningHelper
             $this->initializePrefs();
         } catch (Exception $e) {
             http_response_code(400);
-            Logging::error($e->getMessage()
-            );
+            Logging::error($e->getMessage());
+            echo $e->getMessage() . PHP_EOL;
+            return;
+        }
+
+        http_response_code(201);
+    }
+
+    /**
+     * Endpoint to change Airtime preferences remotely.
+     * Mainly for use with the dashboard right now.
+     */
+    public function changeAction() {
+        $apikey = $_SERVER['PHP_AUTH_USER'];
+        if (!isset($apikey) || $apikey != $this->apikey) {
+            Logging::info("Invalid API Key: $apikey");
+            http_response_code(403);
+            echo "ERROR: Incorrect API key";
+            return;
+        }
+
+        try {
+            $this->parsePostParams();
+            $this->initializePrefs();
+        } catch (Exception $e) {
+            http_response_code(400);
+            Logging::error($e->getMessage());
             echo $e->getMessage() . PHP_EOL;
             return;
         }

From 918631d676fbf09113142affdbe536bf6482fdba Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@gmail.com>
Date: Thu, 5 Mar 2015 12:24:02 -0500
Subject: [PATCH 4/5] When calling /change, don't set prefs if empty/no
 parameters are given

---
 airtime_mvc/application/common/ProvisioningHelper.php | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/airtime_mvc/application/common/ProvisioningHelper.php b/airtime_mvc/application/common/ProvisioningHelper.php
index 61127463a..f8e90b11c 100644
--- a/airtime_mvc/application/common/ProvisioningHelper.php
+++ b/airtime_mvc/application/common/ProvisioningHelper.php
@@ -219,8 +219,12 @@ class ProvisioningHelper
      * Initialize preference values passed from the dashboard (if any exist)
      */
     private function initializePrefs() {
-        Application_Model_Preference::SetStationName($this->station_name);
-        Application_Model_Preference::SetStationDescription($this->description);
+        if ($this->statio_name) {
+            Application_Model_Preference::SetStationName($this->station_name);
+        }
+        if ($this->descption) {
+            Application_Model_Preference::SetStationDescription($this->description);
+        }
     }
 
 }

From 716244011a284909aca824bf86d7f02087409d41 Mon Sep 17 00:00:00 2001
From: Duncan Sommerville <duncan.sommerville@gmail.com>
Date: Tue, 10 Mar 2015 13:20:11 -0400
Subject: [PATCH 5/5] Removed unnecessary conditional around
 /provisioning/change in Bootstrap

---
 airtime_mvc/application/Bootstrap.php | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/airtime_mvc/application/Bootstrap.php b/airtime_mvc/application/Bootstrap.php
index 2db23c8f7..7c9cf8c20 100644
--- a/airtime_mvc/application/Bootstrap.php
+++ b/airtime_mvc/application/Bootstrap.php
@@ -36,15 +36,10 @@ require_once (APPLICATION_PATH."/logging/Logging.php");
 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 should probably look for a better way to do this rather tan overloading this if statement
 if (array_key_exists("REQUEST_URI", $_SERVER) && (stripos($_SERVER["REQUEST_URI"], "/provisioning/create") !== false)) {
     $provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]);
     $provisioningHelper->createAction();
     die();
-} else if (array_key_exists("REQUEST_URI", $_SERVER) && (stripos($_SERVER["REQUEST_URI"], "/provisioning/change") !== false)) {
-    $provisioningHelper = new ProvisioningHelper($CC_CONFIG["apiKey"][0]);
-    $provisioningHelper->changeAction();
-    die();
 }
 
 Config::setAirtimeVersion();