/var/www/html_us/wp-content/plugins/cloudflare-flexible-ssl/plugin.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
<?php
/*
 * Plugin Name: Flexible SSL for CloudFlare
 * Plugin URI: https://icwp.io/cloudflaresslpluginauthor
 * Description: Fix For CloudFlare Flexible SSL Redirect Loop For WordPress
 * Version: 1.3.1
 * Text Domain: cloudflare-flexible-ssl
 * Author: One Dollar Plugin
 * Author URI: https://icwp.io/cloudflaresslpluginauthor
 */

/**
 * Copyright (c) 2020 One Dollar Plugin <[email protected]>
 * All rights reserved.
 * "CloudFlare Flexible SSL" plugin is distributed under the GNU General Public License, Version 2,
 * June 1991. Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA 02110, USA
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
class ICWP_Cloudflare_Flexible_SSL {

    public function 
__construct() {
    }

    public function 
run() {
        if ( !
$this->isSsl() && $this->isSslToNonSslProxy() ) {
            
$_SERVER'HTTPS' ] = 'on';
            
add_action'shutdown', array( $this'maintainPluginLoadPosition' ) );
        }
        if ( 
version_comparePHP_VERSION'5.4.0''>=' ) ) {
            include_once( 
dirname__FILE__ ).'/shieldprom.php' );
        }
    }

    
/**
     * @return bool
     */
    
private function isSsl() {
        return 
function_exists'is_ssl' ) && is_ssl();
    }

    
/**
     * @return bool
     */
    
private function isSslToNonSslProxy() {
        
$bIsProxy false;

        
$aServerKeys = array( 'HTTP_CF_VISITOR''HTTP_X_FORWARDED_PROTO' );
        foreach ( 
$aServerKeys as $sKey ) {
            if ( isset( 
$_SERVER$sKey ] ) && ( strpos$_SERVER$sKey ], 'https' ) !== false ) ) {
                
$bIsProxy true;
                break;
            }
        }

        return 
$bIsProxy;
    }

    
/**
     * Sets this plugin to be the first loaded of all the plugins.
     */
    
public function maintainPluginLoadPosition() {
        
$sBaseFile plugin_basename__FILE__ );
        
$nLoadPosition $this->getActivePluginLoadPosition$sBaseFile );
        if ( 
$nLoadPosition ) {
            
$this->setActivePluginLoadPosition$sBaseFile);
        }
    }

    
/**
     * @param string $sPluginFile
     * @return int
     */
    
private function getActivePluginLoadPosition$sPluginFile ) {
        
$sOptionKey is_multisite() ? 'active_sitewide_plugins' 'active_plugins';
        
$aActive get_option$sOptionKey );
        
$nPosition = -1;
        if ( 
is_array$aActive ) ) {
            
$nPosition array_search$sPluginFile$aActive );
            if ( 
$nPosition === false ) {
                
$nPosition = -1;
            }
        }
        return 
$nPosition;
    }

    
/**
     * @param string $sPluginFile
     * @param int    $nDesiredPosition
     */
    
private function setActivePluginLoadPosition$sPluginFile$nDesiredPosition ) {

        
$aActive $this->setArrayValueToPositionget_option'active_plugins' ), $sPluginFile$nDesiredPosition );
        
update_option'active_plugins'$aActive );

        if ( 
is_multisite() ) {
            
$aActive $this->setArrayValueToPositionget_option'active_sitewide_plugins' ), $sPluginFile$nDesiredPosition );
            
update_option'active_sitewide_plugins'$aActive );
        }
    }

    
/**
     * @param array $aSubjectArray
     * @param mixed $mValue
     * @param int   $nDesiredPosition
     * @return array
     */
    
private function setArrayValueToPosition$aSubjectArray$mValue$nDesiredPosition ) {

        if ( 
$nDesiredPosition || !is_array$aSubjectArray ) ) {
            return 
$aSubjectArray;
        }

        
$nMaxPossiblePosition count$aSubjectArray ) - 1;
        if ( 
$nDesiredPosition $nMaxPossiblePosition ) {
            
$nDesiredPosition $nMaxPossiblePosition;
        }

        
$nPosition array_search$mValue$aSubjectArray );
        if ( 
$nPosition !== false && $nPosition != $nDesiredPosition ) {

            
// remove existing and reset index
            
unset( $aSubjectArray$nPosition ] );
            
$aSubjectArray array_values$aSubjectArray );

            
// insert and update
            // http://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php
            
array_splice$aSubjectArray$nDesiredPosition0$mValue );
        }

        return 
$aSubjectArray;
    }
}

$oIcwpCfFlexibleSslCheck = new ICWP_Cloudflare_Flexible_SSL();
$oIcwpCfFlexibleSslCheck->run();