Changeset 1910390 for https-force/trunk/https-force.php
- Timestamp:
- 07/17/2018 01:21:35 PM (8 years ago)
- File:
-
- 1 edited
-
https-force/trunk/https-force.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
https-force/trunk/https-force.php
r1804057 r1910390 4 4 * Plugin URI: https://en.wordpress.org/plugins/https-force 5 5 * Description: This Plugin forces HTTPS protocol on internal links and references and will redirect to a secure version of the site. 6 * Version: 1.0. 16 * Version: 1.0.2 7 7 * Author: Thomas Vang (sitzz) 8 8 * Author URI: http://sitzz.dk/ … … 10 10 */ 11 11 12 if (!defined('ABSPATH')) exit; 12 if (!defined('ABSPATH')) { 13 exit; 14 } 13 15 14 15 class HTTPS_FORCE 16 class SitzzForceHttps 16 17 { 17 /*18 /* 18 19 * __construct: Constructor function 19 20 * Checks if site uses SSL. … … 21 22 * If it does NOT; Redirect to HTTPS site 22 23 */ 23 public function __construct() 24 { 25 if (is_ssl()) { 26 add_action('wp_loaded', array( 27 $this, 28 'theForceLoad' 29 ) , 99, 1); 24 public function __construct() 25 { 26 if (is_ssl()) { 27 add_action( 28 'wp_loaded', 29 array( 30 $this, 31 'theForceLoad' 32 ), 33 99, 34 1 35 ); 36 } else { 37 add_action( 38 'template_redirect', 39 array( 40 $this, 41 'theForceRedirect' 42 ), 43 1 44 ); 45 } 30 46 } 31 else32 {33 add_action('template_redirect', array(34 $this,35 'theForceRedirect'36 ) , 1);37 }38 }39 47 40 48 /* … … 42 50 * Registers redirect function for non-secure sites 43 51 */ 44 public function theForceRedirect()45 {46 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);47 exit();48 }52 public function theForceRedirect() 53 { 54 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301); 55 exit(); 56 } 49 57 50 /*58 /* 51 59 * theForceLoad: Loader function 52 60 * Registers the 'theForceMagic' function 53 61 */ 54 public function theForceLoad()55 {56 global $pagenow;57 ob_start(array(58 $this,59 'theForceMagic'60 ));61 }62 public function theForceLoad() 63 { 64 global $pagenow; 65 ob_start(array( 66 $this, 67 'theForceMagic' 68 )); 69 } 62 70 63 71 /* … … 65 73 * Replaces HTTP in links and references to internal resources with HTTPS 66 74 */ 67 public function theForceMagic($buffer) 68 { 69 $content_type = NULL; 70 foreach(headers_list() as $header) { 71 if (strpos(strtolower($header) , 'content-type:') === 0) { 72 $pieces = explode(':', strtolower($header)); 73 $content_type = trim($pieces[1]); 74 break; 75 } 76 } 77 if (is_null($content_type) || substr($content_type, 0, 9) === 'text/html') { 78 // Take care of 'href' links 79 $buffer = str_replace('href=\'http://' . $_SERVER['HTTP_HOST'], 'href=\'https://' . $_SERVER['HTTP_HOST'], $buffer); 80 $buffer = str_replace('href="http://' . $_SERVER['HTTP_HOST'], 'href="https://' . $_SERVER['HTTP_HOST'], $buffer); 81 // Take care of 'src' references 82 $buffer = str_replace('src=\'http://' . $_SERVER['HTTP_HOST'], 'src=\'https://' . $_SERVER['HTTP_HOST'], $buffer); 83 $buffer = str_replace('src="http://' . $_SERVER['HTTP_HOST'], 'src="https://' . $_SERVER['HTTP_HOST'], $buffer); 84 // Take care or 'content' references 85 $buffer = str_replace('content=\'http://' . $_SERVER['HTTP_HOST'], 'content=\'https://' . $_SERVER['HTTP_HOST'], $buffer); 86 $buffer = str_replace('content="http://' . $_SERVER['HTTP_HOST'], 'content="https://' . $_SERVER['HTTP_HOST'], $buffer); 87 // Take care of 'url' links 88 $buffer = str_replace('url(\'http://' . $_SERVER['HTTP_HOST'], 'url(\'https://' . $_SERVER['HTTP_HOST'], $buffer); 89 $buffer = str_replace('url("http://' . $_SERVER['HTTP_HOST'], 'url("https://' . $_SERVER['HTTP_HOST'], $buffer); 90 // Take care of 'loaderUrl' references 91 $buffer = str_replace('http:\/\/' . $_SERVER['HTTP_HOST'], 'https:\/\/' . $_SERVER['HTTP_HOST'], $buffer); 92 // Take care of URLs in text 93 $buffer = str_replace('http://' . $_SERVER['HTTP_HOST'], 'https://' . $_SERVER['HTTP_HOST'], $buffer); 94 // Take care of Google URLs 95 $buffer = str_replace('http://fonts.googleapis.com', 'https://fonts.googleapis.com', $buffer); 96 $buffer = str_replace('http://maps.googleapis.com', 'https://maps.googleapis.com', $buffer); 97 $buffer = str_replace('http://ajax.googleapis.com', 'https://ajax.googleapis.com', $buffer); 98 $buffer = str_replace('http://storage.googleapis.com', 'https://storage.googleapis.com', $buffer); 99 // Fix for visible links 100 $buffer = str_replace('>http://' . $_SERVER['HTTP_HOST'], '>https://' . $_SERVER['HTTP_HOST'], $buffer); 101 } 75 public function theForceMagic($buffer) 76 { 77 $content_type = null; 78 foreach (headers_list() as $header) { 79 if (strpos(strtolower($header), 'content-type:') === 0) { 80 $pieces = explode(':', strtolower($header)); 81 $content_type = trim($pieces[1]); 82 break; 83 } 84 } 85 if (is_null($content_type) || substr($content_type, 0, 9) === 'text/html') { 86 // Take care of 'href' links 87 $buffer = str_replace('href=\'http://' . $_SERVER['HTTP_HOST'], 'href=\'https://' . $_SERVER['HTTP_HOST'], $buffer); 88 $buffer = str_replace('href="http://' . $_SERVER['HTTP_HOST'], 'href="https://' . $_SERVER['HTTP_HOST'], $buffer); 89 90 // Take care of 'src' references 91 $buffer = str_replace('src=\'http://' . $_SERVER['HTTP_HOST'], 'src=\'https://' . $_SERVER['HTTP_HOST'], $buffer); 92 $buffer = str_replace('src="http://' . $_SERVER['HTTP_HOST'], 'src="https://' . $_SERVER['HTTP_HOST'], $buffer); 93 94 // Take care or 'content' references 95 $buffer = str_replace('content=\'http://' . $_SERVER['HTTP_HOST'], 'content=\'https://' . $_SERVER['HTTP_HOST'], $buffer); 96 $buffer = str_replace('content="http://' . $_SERVER['HTTP_HOST'], 'content="https://' . $_SERVER['HTTP_HOST'], $buffer); 97 98 // Take care of 'url' links 99 $buffer = str_replace('url(\'http://' . $_SERVER['HTTP_HOST'], 'url(\'https://' . $_SERVER['HTTP_HOST'], $buffer); 100 $buffer = str_replace('url("http://' . $_SERVER['HTTP_HOST'], 'url("https://' . $_SERVER['HTTP_HOST'], $buffer); 101 102 // Take care of 'loaderUrl' references 103 $buffer = str_replace('http:\/\/' . $_SERVER['HTTP_HOST'], 'https:\/\/' . $_SERVER['HTTP_HOST'], $buffer); 104 105 // Take care of URLs in text 106 $buffer = str_replace('http://' . $_SERVER['HTTP_HOST'], 'https://' . $_SERVER['HTTP_HOST'], $buffer); 107 108 // Take care of Google URLs 109 $buffer = str_replace('http://fonts.googleapis.com', 'https://fonts.googleapis.com', $buffer); 110 $buffer = str_replace('http://maps.googleapis.com', 'https://maps.googleapis.com', $buffer); 111 $buffer = str_replace('http://ajax.googleapis.com', 'https://ajax.googleapis.com', $buffer); 112 $buffer = str_replace('http://storage.googleapis.com', 'https://storage.googleapis.com', $buffer); 113 114 // Fix for visible links 115 $buffer = str_replace('>http://' . $_SERVER['HTTP_HOST'], '>https://' . $_SERVER['HTTP_HOST'], $buffer); 116 } 102 117 103 // Return the new contents...104 return $buffer;105 }118 // Return the new contents... 119 return $buffer; 120 } 106 121 } 107 122 … … 109 124 * Load class... 110 125 */ 111 new HTTPS_FORCE();126 new SitzzForceHttps();
Note: See TracChangeset
for help on using the changeset viewer.