PluginProbe ʕ •ᴥ•ʔ
Easy HTTPS Redirection (SSL) / trunk
Easy HTTPS Redirection (SSL) vtrunk
trunk 1.5 1.6 1.8 1.9.1 1.9.2 2.0.0 2.0.1
https-redirection / classes / ehssl-init-time-tasks.php
https-redirection / classes Last commit date
utilities 3 days ago ehssl-config.php 1 year ago ehssl-cronjob.php 1 year ago ehssl-custom-post-types.php 1 year ago ehssl-debug-logger.php 1 year ago ehssl-email-handler.php 1 year ago ehssl-init-time-tasks.php 3 days ago ehssl-installation.php 3 days ago ehssl-non-https-resources-scan-result-table.php 3 days ago ehssl-non-https-resources-scan-update.php 3 days ago ehssl-rules-helper.php 3 days ago ehssl-ssl-certificate.php 1 year ago index.php 1 year ago
ehssl-init-time-tasks.php
124 lines
1 <?php
2
3 class EHSSL_Init_Time_Tasks {
4
5 // Constructor
6 public function __construct() {
7 // Add the init action hook
8 add_action('init', array($this, 'early_priority_init_handler'), 0);// Early priority init.
9 add_action('init', array($this, 'run_init_time_tasks'));//Normal priority init.
10
11 include_once EASY_HTTPS_SSL_PATH . '/classes/ehssl-non-https-resources-scan-update.php';
12 }
13
14 // Method to run at 'init' action hook.
15 public function run_init_time_tasks() {
16 // Register custom post types.
17 EHSSL_Custom_Post_Types::get_instance()->register_custom_post_types();
18 }
19
20 public function early_priority_init_handler() {
21 //This is the early priority init handler (needed for some features of the plugin).
22 global $httpsrdrctn_options;
23 if (empty($httpsrdrctn_options)) {
24 $httpsrdrctn_options = get_option('httpsrdrctn_options');
25 }
26
27 //Mixed content fix feature. Do force resource embedded using HTTPS.
28 if (isset($httpsrdrctn_options['force_resources']) && $httpsrdrctn_options['force_resources'] == '1') {
29 // Handle the appropriate content filters to force the static resources to use HTTPS URL.
30 if (is_admin()) {
31 if (!isset($_GET['page']) || $_GET['page'] != 'ehssl_settings') {
32 add_action("admin_init", array($this, "ehssl_start_buffer"));
33 add_action("shutdown", array($this, "ehssl_end_buffer"));
34 }
35 } else {
36 add_action("init", array($this, "ehssl_start_buffer"));
37 add_action("shutdown", array($this, "ehssl_end_buffer"));
38 }
39 }
40
41 }
42
43 public function ehssl_start_buffer(){
44 ob_start(array($this, "ehssl_the_content"));
45 }
46
47 public function ehssl_end_buffer() {
48 if (ob_get_length()) {
49 ob_end_flush();
50 }
51 }
52
53 public function ehssl_the_content($content) {
54 global $httpsrdrctn_options;
55 if (empty($httpsrdrctn_options)) {
56 $httpsrdrctn_options = get_option('httpsrdrctn_options');
57 }
58
59 $current_page = sanitize_post($GLOBALS['wp_the_query']->get_queried_object());
60 // Get the page slug
61 $slug = str_replace(home_url() . '/', '', get_permalink($current_page));
62 $slug = rtrim($slug, "/"); //remove trailing slash if it's there
63
64 if ($httpsrdrctn_options['force_resources'] == '1' && $httpsrdrctn_options['https'] == 1) {
65 if ($httpsrdrctn_options['https_domain'] == 1) {
66 $content = $this->ehssl_filter_content($content);
67 } else if (!empty($httpsrdrctn_options['https_pages_array'])) {
68 $pages_str = '';
69 $on_https_page = false;
70 foreach ($httpsrdrctn_options['https_pages_array'] as $https_page) {
71 $pages_str .= preg_quote($https_page, '/') . '[\/|][\'"]|'; //let's add page to the preg expression string in case we'd need it later
72 if ($https_page == $slug) { //if we are on the page that is in the array, let's set the var to true
73 $on_https_page = true;
74 } else { //if not - let's replace all links to that page only to https
75 $http_domain = home_url();
76 $https_domain = str_replace('http://', 'https://', home_url());
77 $content = str_replace($http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content);
78 }
79 }
80 if ($on_https_page) { //we are on one of the https pages
81 $pages_str = substr($pages_str, 0, strlen($pages_str) - 1); //remove last '|'
82 $content = $this->ehssl_filter_content($content); //let's change everything to https first
83 $http_domain = str_replace('https://', 'http://', home_url());
84 $https_domain = str_replace('http://', 'https://', home_url());
85 //now let's change all inner links to http, excluding those that user sets to be https in plugin settings
86 $content = preg_replace('/<a .*?href=[\'"]\K' . preg_quote($https_domain, '/') . '\/((?!' . $pages_str . ').)(?=[^\'"]+)/i', $http_domain . '/$1', $content);
87 $content = preg_replace('/' . preg_quote($https_domain, '/') . '([\'"])/i', $http_domain . '$1', $content);
88 }
89 }
90 }
91 return $content;
92 }
93
94 /**
95 * Function that changes "http" embeds to "https"
96 */
97 public function ehssl_filter_content($content) {
98 //filter buffer
99 $home_no_www = str_replace("://www.", "://", get_option('home'));
100 $home_yes_www = str_replace("://", "://www.", $home_no_www);
101 $http_urls = array(
102 str_replace("https://", "http://", $home_yes_www),
103 str_replace("https://", "http://", $home_no_www),
104 "src='http://",
105 'src="http://',
106 );
107 $ssl_array = str_replace("http://", "https://", $http_urls);
108 //now replace these links
109 $str = str_replace($http_urls, $ssl_array, $content);
110
111 //replace all http links except hyperlinks
112 //all tags with src attr are already fixed by str_replace
113
114 $pattern = array(
115 '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i',
116 '/<link .*?href=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
117 '/<meta property="og:image" .*?content=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
118 '/<form [^>]*?action=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
119 );
120 $str = preg_replace($pattern, 'https://', $str);
121 return $str;
122 }
123
124 }