public
Last active

is_ssl() for most cases - mu-plugin

  • Download Gist
is_ssl.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: Real SSL
* Plugin URI: http://unserkaiser.com
* Description: A real check for SSL. Adds the template tags (public API fn) <code>get_maybe_ssl_path( $path = '' )</code> and <code>is_maybe_ssl()</code>
* Version: 0.2
* Author: Franz Josef Kaiser
* Author URI: http://unserkaiser.com
*/
 
 
/**
* @author Franz Josef Kaiser
* @link http://unserkaiser.com
* @license MIT
*/
class oxoCheckSSL
{
/**
* Path relative to the root
* Can be set from the parent constructor call in the extended class
* @since 0.1
* @var string
*/
private $path;
 
 
/**
* Secure port for SSL
* Can be overridden in the extended class
* @since 0.1
* @var integer
*/
protected $custom_port = 443;
 
 
/**
* Constructor
*
* @since 0.1
* @param string $path Relative path from wpurl
*
* @return string $url
*/
public function __construct( $path = '' )
{
$this->path = $path;
}
 
 
/**
* Returns a path relative from the WP root dir
* Adds https for secure connections
*
* @since 0.1
*
* @return string $url
*/
public function get_url()
{
$path = $this->path;
$scheme = $this->is_ssl() ? 'https' : 'http';
 
// Check if the url is accessible, or if is_ssl() told us 'secure' by mistake
if ( in_array( 'curl', get_loaded_extensions() ) )
! url_is_accessable_via_ssl( get_site_url( null, $path, $scheme ) ) AND $scheme = 'http';
 
return get_site_url( null, $path, $scheme );
}
 
 
/**
* Extended version of the core is_ssl() function
*
* HTTPS
* When using ISAPI with IIS, the value will be off,
* if the request was not made through the HTTPS protocol
*
* HTTP_X_FORWARDED_PROTO
* required if you are operating behind a load balancer that terminates the SSL connection
* and then forwards to the machines behind it on port 80
* (common set-up on the Amazon cloud)
*
* @since 0.1
*
* @return boolean true/false Wheter on a secure connection or not
*/
public function is_ssl()
{
$port = $this->get_port();
 
if (
! empty( $_SERVER['HTTPS'] )
AND $_SERVER['HTTPS']
)
{
// 1. Maybe won't work if apache is configured name based (more than one website per IP)
if ( 'on' === strtolower( $_SERVER['HTTPS'] ) )
return true;
 
if ( '1' == $_SERVER['HTTPS'] )
return true;
}
elseif (
isset( $_SERVER['SERVER_PORT'] )
AND (string) "{$port}" === $_SERVER['SERVER_PORT']
)
{
return true;
}
elseif (
isset( $_SERVER['HTTP_X_FORWARDED_PORT'] )
AND (string) "{$port}" === $_SERVER['HTTP_X_FORWARDED_PORT']
)
{
return true;
}
elseif (
isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] )
AND 'https' === strtolower( $_SERVER['HTTP_X_FORWARDED_PROTO'] )
)
{
return true;
}
 
return false;
}
 
 
/**
* Gets the correct port
* Adapted from @link http://stackoverflow.com/a/5004886/376483
*
* Testing with fsocketopen() is avoided.
* Assuming the user knows his custom port, if set.
*
* @since 0.1
*
* @return integer $port
*/
public function get_port()
{
// Check if the port is set. If not, try to get it from the environment
if (
! isset( $_SERVER["SERVER_PORT"] )
OR ! $_SERVER["SERVER_PORT"]
)
{
if ( ! isset( $_ENV["SERVER_PORT"] ) )
{
getenv("SERVER_PORT");
$_SERVER["SERVER_PORT"] = $_ENV["SERVER_PORT"];
}
}
 
// Fall back to custom port
if (
! isset( $_SERVER["SERVER_PORT"] )
OR ! $_SERVER["SERVER_PORT"]
)
$_SERVER["SERVER_PORT"] = $this->custom_port;
 
return $_SERVER["SERVER_PORT"];
}
} // END Class oxoCheckSSL
 
 
/**********************************************
* Public API
**********************************************/
 
/**
* Retrieves a https or http path
*
* @since 0.1
* @param string $path Relative from the wpurl
*
* @return string path The path with http or https prefix
*/
function get_maybe_ssl_path( $path = '' )
{
$class = new oxoCheckSSL( $path );
 
return $class->get_url();
}
 
/**
* Check if the url is ssl or not
*
* @since 0.1
*
* @return bool true/false wheter the url is ssl or not
*/
function is_maybe_ssl()
{
$class = new oxoCheckSSL( $path );
return $class->is_ssl();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.