]> source.dussan.org Git - nextcloud-server.git/commitdiff
added a serverProtocol function that correctly returns the used protocol even if...
authorFrank Karlitschek <frank@owncloud.org>
Fri, 1 Jun 2012 08:38:44 +0000 (10:38 +0200)
committerFrank Karlitschek <frank@owncloud.org>
Fri, 1 Jun 2012 08:38:44 +0000 (10:38 +0200)
lib/base.php
lib/helper.php
lib/public/util.php

index fdb682bf5032a60c6d61d7a8779bad0ce83914a1..cb98a6c9e0b9d836028b1dbcb317234671c348e4 100644 (file)
@@ -214,8 +214,8 @@ class OC{
                // redirect to https site if configured
                if( OC_Config::getValue( "forcessl", false )){
                        ini_set("session.cookie_secure", "on");
-                       if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') {
-                               $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
+                       if(OC_Helper::serverProtocol()<>'https') {
+                               $url = "https://". OC_Helper::serverHost() . $_SERVER['REQUEST_URI'];
                                header("Location: $url");
                                exit();
                        }
@@ -376,11 +376,11 @@ class OC{
 
                // CSRF protection
                if(isset($_SERVER['HTTP_REFERER'])) $referer=$_SERVER['HTTP_REFERER']; else $referer='';
-               if(isset($_SERVER['HTTPS']) and $_SERVER['HTTPS']<>'') $protocol='https://'; else $protocol='http://';
+               $protocol=OC_Helper::serverProtocol().'://'; 
                if(!self::$CLI){
-                       $server=$protocol.$_SERVER['SERVER_NAME'];
+                       $server=$protocol.OC_Helper::serverHost();
                        if(($_SERVER['REQUEST_METHOD']=='POST') and (substr($referer,0,strlen($server))<>$server)) {
-                               $url = $protocol.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
+                               $url = $protocol.OC_Helper::serverProtocol().OC::$WEBROOT.'/index.php';
                                header("Location: $url");
                                exit();
                        }
index 537465b15ac67c1826318101f8e7e9b9b14f8d9e..f8f84b91ae3aee08b3299057af84c78bbe27831c 100644 (file)
@@ -89,6 +89,27 @@ class OC_Helper {
                return $host;
        }
 
+
+        /**
+         * @brief Returns the server protocol
+         * @returns the server protocol
+         *
+         * Returns the server protocol. It respects reverse proxy servers and load balancers
+         */
+       public static function serverProtocol() {
+               if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
+                       $proto = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
+               }else{
+                       if(isset($_SERVER['HTTPS']) and !empty($_SERVER['HTTPS']) and ($_SERVER['HTTPS']!='off')) {
+                               $proto = 'https';
+                       }else{
+                               $proto = 'http';
+                       }
+               }
+               return($proto);
+       }
+
+
        /**
         * @brief Creates an absolute url
         * @param $app app
@@ -99,9 +120,7 @@ class OC_Helper {
         */
        public static function linkToAbsolute( $app, $file ) {
                $urlLinkTo = self::linkTo( $app, $file );
-               // Checking if the request was made through HTTPS. The last in line is for IIS
-               $protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!='off');
-               $urlLinkTo = ($protocol?'https':'http') . '://'  . self::serverHost() . $urlLinkTo;
+               $urlLinkTo = OC_Helper::serverProtocol(). '://'  . self::serverHost() . $urlLinkTo;
                return $urlLinkTo;
        }
 
index 39a185d02a76aa2c4d2a546916a46b005412a41f..9b499574da1ca6b30f3f070b359ecb05147c29b9 100644 (file)
@@ -168,6 +168,16 @@ class Util {
                return(\OC_Helper::serverHost());
        }
 
+       /**
+        * @brief Returns the server protocol
+        * @returns the server protocol
+        *
+        * Returns the server protocol. It respects reverse proxy servers and load balancers
+        */
+       public static function getServerProtocol() {
+               return(\OC_Helper::serverProtocol());
+       }
+
        /**
         * @brief Creates path to an image
         * @param $app app