From 4527b63c7b30e94c816551b7eb377f982c18141a Mon Sep 17 00:00:00 2001
From: Lucas Bickel <hairmare@rabe.ch>
Date: Sat, 11 Feb 2017 13:52:15 +0100
Subject: [PATCH] General fixes for TLS

This was tested in airtime web and with integration of media-monitor and silan up and running. It might work for pypo but that needs testing.
---
 airtime_mvc/application/models/StoredFile.php     |  4 ++++
 python_apps/api_clients/api_clients/api_client.py | 10 ++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php
index f1588fb16..819df76b4 100644
--- a/airtime_mvc/application/models/StoredFile.php
+++ b/airtime_mvc/application/models/StoredFile.php
@@ -510,6 +510,10 @@ SQL;
     	$serverPort = $_SERVER['SERVER_PORT'];
     	$subDir = $CC_CONFIG['baseDir'];
 
+        if ($protocol === 'https' && $serverPort == 80) {
+            $serverPort = 443;
+        }
+
     	if ($subDir[0] === "/") {
     		$subDir = substr($subDir, 1, strlen($subDir) - 1);
     	}
diff --git a/python_apps/api_clients/api_clients/api_client.py b/python_apps/api_clients/api_clients/api_client.py
index 4eeea61d9..3174e273a 100644
--- a/python_apps/api_clients/api_clients/api_client.py
+++ b/python_apps/api_clients/api_clients/api_client.py
@@ -186,8 +186,9 @@ class RequestProvider(object):
         self.requests = {}
         if self.config["general"]["base_dir"].startswith("/"):
             self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
-        self.url = ApcUrl("http://%s:%s/%s%s/%s" \
-            % (self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
+        self.url = ApcUrl("%s://%s:%s/%s%s/%s" \
+            % (str(("https", "http")[self.config["general"]["base_port"] == 443]),
+               self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
                self.config["general"]["base_dir"], self.config["api_base"],
                '%%action%%'))
         # Now we must discover the possible actions
@@ -323,8 +324,9 @@ class AirtimeApiClient(object):
         # TODO : Make other methods in this class use this this method.
         if self.config["general"]["base_dir"].startswith("/"):
             self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
-        url = "http://%s:%s/%s%s/%s" %  \
-            (self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
+        url = "%s://%s:%s/%s%s/%s" %  \
+            (str(("https", "http")[self.config["general"]["base_port"] == 443]),
+             self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
              self.config["general"]["base_dir"], self.config["api_base"],
              self.config[config_action_key])
         url = url.replace("%%api_key%%", self.config["general"]["api_key"])