Plugin Directory


Ignore:
Timestamp:
07/17/2018 01:21:35 PM (8 years ago)
Author:
sitzz
Message:

Tested and confirmed compatible with WP 4.9.7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • https-force/trunk/https-force.php

    r1804057 r1910390  
    44 * Plugin URI: https://en.wordpress.org/plugins/https-force
    55 * 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.1
     6 * Version: 1.0.2
    77 * Author: Thomas Vang (sitzz)
    88 * Author URI: http://sitzz.dk/
     
    1010 */
    1111
    12 if (!defined('ABSPATH')) exit;
     12if (!defined('ABSPATH')) {
     13    exit;
     14}
    1315
    14 
    15 class HTTPS_FORCE
     16class SitzzForceHttps
    1617{
    17     /*
     18    /*
    1819   * __construct: Constructor function
    1920   * Checks if site uses SSL.
     
    2122   * If it does NOT; Redirect to HTTPS site
    2223   */
    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        }
    3046    }
    31     else
    32     {
    33       add_action('template_redirect', array(
    34         $this,
    35         'theForceRedirect'
    36       ) , 1);
    37     }
    38     }
    3947 
    4048  /*
     
    4250   * Registers redirect function for non-secure sites
    4351   */
    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    }
    4957 
    50     /*
     58    /*
    5159   * theForceLoad: Loader function
    5260   * Registers the 'theForceMagic' function
    5361   */
    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    }
    6270 
    6371  /*
     
    6573   * Replaces HTTP in links and references to internal resources with HTTPS
    6674   */
    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        }
    102117   
    103     // Return the new contents...
    104         return $buffer;
    105     }
     118        // Return the new contents...
     119        return $buffer;
     120    }
    106121}
    107122
     
    109124 * Load class...
    110125 */
    111 new HTTPS_FORCE();
     126new SitzzForceHttps();
Note: See TracChangeset for help on using the changeset viewer.