https-redirection
Last commit date
css
12 years ago
images
12 years ago
js
12 years ago
languages
12 years ago
https-redirection-settings.php
9 years ago
https-redirection.php
9 years ago
https-rules-helper.php
9 years ago
https-utillity-functions.php
11 years ago
readme.txt
9 years ago
screenshot-1.png
12 years ago
https-redirection.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | Plugin Name: Easy HTTPS (SSL) Redirection |
| 5 | Plugin URI: https://www.tipsandtricks-hq.com/development-center |
| 6 | Description: The plugin HTTPS Redirection allows an automatic redirection to the "HTTPS" version/URL of the site. |
| 7 | Author: Tips and Tricks HQ |
| 8 | Version: 1.6 |
| 9 | Author URI: https://www.tipsandtricks-hq.com/ |
| 10 | License: GPLv2 or later |
| 11 | */ |
| 12 | |
| 13 | if (!defined('ABSPATH')) { |
| 14 | exit; //Exit if accessed directly |
| 15 | } |
| 16 | |
| 17 | include_once('https-rules-helper.php'); |
| 18 | include_once('https-redirection-settings.php'); |
| 19 | |
| 20 | function httpsrdrctn_plugin_init() { |
| 21 | global $httpsrdrctn_options; |
| 22 | if (empty($httpsrdrctn_options)) { |
| 23 | $httpsrdrctn_options = get_option('httpsrdrctn_options'); |
| 24 | } |
| 25 | |
| 26 | //Do force resource embedded using HTTPS |
| 27 | if (isset($httpsrdrctn_options['force_resources']) && $httpsrdrctn_options['force_resources'] == '1') { |
| 28 | //Handle the appropriate content filters to force the static resources to use HTTPS URL |
| 29 | if (is_admin()) { |
| 30 | add_action("admin_init", "httpsrdrctn_start_buffer"); |
| 31 | } else { |
| 32 | add_action("init", "httpsrdrctn_start_buffer"); |
| 33 | add_action("init", "httpsrdrctn_init_time_tasks"); |
| 34 | } |
| 35 | add_action("shutdown", "httpsrdrctn_end_buffer"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | function httpsrdrctn_start_buffer() { |
| 40 | ob_start("httpsrdrctn_the_content"); |
| 41 | } |
| 42 | |
| 43 | function httpsrdrctn_end_buffer() { |
| 44 | if (ob_get_length()) |
| 45 | ob_end_flush(); |
| 46 | } |
| 47 | |
| 48 | function httpsrdrctn_init_time_tasks() { |
| 49 | httpsrdrctn_load_language(); |
| 50 | } |
| 51 | |
| 52 | function httpsrdrctn_load_language() { |
| 53 | /* Internationalization */ |
| 54 | load_plugin_textdomain('https_redirection', false, dirname(plugin_basename(__FILE__)) . '/languages/'); |
| 55 | } |
| 56 | |
| 57 | function httpsrdrctn_plugin_admin_init() { |
| 58 | global $httpsrdrctn_plugin_info; |
| 59 | |
| 60 | $httpsrdrctn_plugin_info = get_plugin_data(__FILE__, false); |
| 61 | |
| 62 | /* Call register settings function */ |
| 63 | if (isset($_GET['page']) && "https-redirection" == $_GET['page']) { |
| 64 | register_httpsrdrctn_settings(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* Register settings function */ |
| 69 | |
| 70 | function register_httpsrdrctn_settings() { |
| 71 | global $wpmu, $httpsrdrctn_options, $httpsrdrctn_plugin_info; |
| 72 | |
| 73 | $httpsrdrctn_option_defaults = array( |
| 74 | 'https' => 0, |
| 75 | 'https_domain' => 0, |
| 76 | 'https_pages_array' => array(), |
| 77 | 'force_resources' => 0, |
| 78 | 'plugin_option_version' => $httpsrdrctn_plugin_info["Version"] |
| 79 | ); |
| 80 | |
| 81 | /* Install the option defaults */ |
| 82 | if (1 == $wpmu) { |
| 83 | if (!get_site_option('httpsrdrctn_options')) |
| 84 | add_site_option('httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes'); |
| 85 | } else { |
| 86 | if (!get_option('httpsrdrctn_options')) |
| 87 | add_option('httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes'); |
| 88 | } |
| 89 | |
| 90 | /* Get options from the database */ |
| 91 | if (1 == $wpmu) |
| 92 | $httpsrdrctn_options = get_site_option('httpsrdrctn_options'); |
| 93 | else |
| 94 | $httpsrdrctn_options = get_option('httpsrdrctn_options'); |
| 95 | |
| 96 | /* Array merge incase this version has added new options */ |
| 97 | if (!isset($httpsrdrctn_options['plugin_option_version']) || $httpsrdrctn_options['plugin_option_version'] != $httpsrdrctn_plugin_info["Version"]) { |
| 98 | $httpsrdrctn_options = array_merge($httpsrdrctn_option_defaults, $httpsrdrctn_options); |
| 99 | $httpsrdrctn_options['plugin_option_version'] = $httpsrdrctn_plugin_info["Version"]; |
| 100 | update_option('httpsrdrctn_options', $httpsrdrctn_options); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function httpsrdrctn_plugin_action_links($links, $file) { |
| 105 | /* Static so we don't call plugin_basename on every plugin row. */ |
| 106 | static $this_plugin; |
| 107 | if (!$this_plugin) |
| 108 | $this_plugin = plugin_basename(__FILE__); |
| 109 | |
| 110 | if ($file == $this_plugin) { |
| 111 | $settings_link = '<a href="admin.php?page=https-redirection">' . __('Settings', 'https_redirection') . '</a>'; |
| 112 | array_unshift($links, $settings_link); |
| 113 | } |
| 114 | return $links; |
| 115 | } |
| 116 | |
| 117 | function httpsrdrctn_register_plugin_links($links, $file) { |
| 118 | $base = plugin_basename(__FILE__); |
| 119 | if ($file == $base) { |
| 120 | $links[] = '<a href="admin.php?page=https-redirection">' . __('Settings', 'https_redirection') . '</a>'; |
| 121 | } |
| 122 | return $links; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Function that changes "http" embeds to "https" |
| 127 | * TODO - Need to make it better so it only does it for static resources like JS, CSS and Images |
| 128 | */ |
| 129 | |
| 130 | function httpsrdrctn_filter_content($content) { |
| 131 | //filter buffer |
| 132 | $home_no_www = str_replace("://www.", "://", get_option('home')); |
| 133 | $home_yes_www = str_replace("://", "://www.", $home_no_www); |
| 134 | $http_urls = array( |
| 135 | str_replace("https://", "http://", $home_yes_www), |
| 136 | str_replace("https://", "http://", $home_no_www), |
| 137 | "src='http://", |
| 138 | 'src="http://', |
| 139 | ); |
| 140 | $ssl_array = str_replace("http://", "https://", $http_urls); |
| 141 | //now replace these links |
| 142 | $str = str_replace($http_urls, $ssl_array, $content); |
| 143 | |
| 144 | //replace all http links except hyperlinks |
| 145 | //all tags with src attr are already fixed by str_replace |
| 146 | |
| 147 | $pattern = array( |
| 148 | '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i', |
| 149 | '/<link .*?href=[\'"]\K(http:\/\/)(?=[^\'"]+)/i', |
| 150 | '/<meta property="og:image" .*?content=[\'"]\K(http:\/\/)(?=[^\'"]+)/i', |
| 151 | '/<form [^>]*?action=[\'"]\K(http:\/\/)(?=[^\'"]+)/i', |
| 152 | ); |
| 153 | $str = preg_replace($pattern, 'https://', $str); |
| 154 | return $str; |
| 155 | } |
| 156 | |
| 157 | function httpsrdrctn_the_content($content) { |
| 158 | global $httpsrdrctn_options; |
| 159 | if (empty($httpsrdrctn_options)) { |
| 160 | $httpsrdrctn_options = get_option('httpsrdrctn_options'); |
| 161 | } |
| 162 | |
| 163 | $current_page = sanitize_post($GLOBALS['wp_the_query']->get_queried_object()); |
| 164 | // Get the page slug |
| 165 | $slug = str_replace(home_url() . '/', '', get_permalink($current_page)); |
| 166 | $slug = rtrim($slug, "/"); //remove trailing slash if it's there |
| 167 | |
| 168 | if ($httpsrdrctn_options['force_resources'] == '1' && $httpsrdrctn_options['https'] == 1) { |
| 169 | if ($httpsrdrctn_options['https_domain'] == 1) { |
| 170 | $content = httpsrdrctn_filter_content($content); |
| 171 | } else if (!empty($httpsrdrctn_options['https_pages_array'])) { |
| 172 | $pages_str = ''; |
| 173 | $on_https_page = false; |
| 174 | foreach ($httpsrdrctn_options['https_pages_array'] as $https_page) { |
| 175 | $pages_str.=preg_quote($https_page, '/') . '[\/|][\'"]|'; //let's add page to the preg expression string in case we'd need it later |
| 176 | if ($https_page == $slug) { //if we are on the page that is in the array, let's set the var to true |
| 177 | $on_https_page = true; |
| 178 | } else { //if not - let's replace all links to that page only to https |
| 179 | $http_domain = home_url(); |
| 180 | $https_domain = str_replace('http://', 'https://', home_url()); |
| 181 | $content = str_replace($http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content); |
| 182 | } |
| 183 | } |
| 184 | if ($on_https_page) { //we are on one of the https pages |
| 185 | $pages_str = substr($pages_str, 0, strlen($pages_str) - 1); //remove last '|' |
| 186 | $content = httpsrdrctn_filter_content($content); //let's change everything to https first |
| 187 | $http_domain = str_replace('https://', 'http://', home_url()); |
| 188 | $https_domain = str_replace('http://', 'https://', home_url()); |
| 189 | //now let's change all inner links to http, excluding those that user sets to be https in plugin settings |
| 190 | $content = preg_replace('/<a .*?href=[\'"]\K' . preg_quote($https_domain, '/') . '\/((?!' . $pages_str . ').)(?=[^\'"]+)/i', $http_domain . '/$1', $content); |
| 191 | $content = preg_replace('/' . preg_quote($https_domain, '/') . '([\'"])/i', $http_domain . '$1', $content); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | return $content; |
| 196 | } |
| 197 | |
| 198 | if (!function_exists('httpsrdrctn_admin_head')) { |
| 199 | |
| 200 | function httpsrdrctn_admin_head() { |
| 201 | if (isset($_REQUEST['page']) && 'https-redirection' == $_REQUEST['page']) { |
| 202 | wp_enqueue_style('httpsrdrctn_stylesheet', plugins_url('css/style.css', __FILE__)); |
| 203 | wp_enqueue_script('httpsrdrctn_script', plugins_url('js/script.js', __FILE__)); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | /* Function for delete delete options */ |
| 210 | if (!function_exists('httpsrdrctn_delete_options')) { |
| 211 | |
| 212 | function httpsrdrctn_delete_options() { |
| 213 | delete_option('httpsrdrctn_options'); |
| 214 | delete_site_option('httpsrdrctn_options'); |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
| 219 | function add_httpsrdrctn_admin_menu() { |
| 220 | add_submenu_page('options-general.php', 'HTTPS Redirection', 'HTTPS Redirection', 'manage_options', 'https-redirection', 'httpsrdrctn_settings_page', plugins_url("images/px.png", __FILE__), 1001); |
| 221 | } |
| 222 | |
| 223 | add_action('admin_menu', 'add_httpsrdrctn_admin_menu'); |
| 224 | add_action('admin_init', 'httpsrdrctn_plugin_admin_init'); |
| 225 | add_action('admin_enqueue_scripts', 'httpsrdrctn_admin_head'); |
| 226 | |
| 227 | /* Adds "Settings" link to the plugin action page */ |
| 228 | add_filter('plugin_action_links', 'httpsrdrctn_plugin_action_links', 10, 2); |
| 229 | /* Additional links on the plugin page */ |
| 230 | add_filter('plugin_row_meta', 'httpsrdrctn_register_plugin_links', 10, 2); |
| 231 | //add_filter('mod_rewrite_rules', 'httpsrdrctn_mod_rewrite_rules');//TODO 5 |
| 232 | |
| 233 | register_uninstall_hook(__FILE__, 'httpsrdrctn_delete_options'); |
| 234 | |
| 235 | httpsrdrctn_plugin_init(); |
| 236 |