-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
128 lines (109 loc) · 4.1 KB
/
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
<?php
/*
* Plugin Name: CloudFlare Flexible SSL
* Plugin URI: http://icwp.io/2f
* Description: Fix For CloudFlare Flexible SSL Redirect Loop For WordPress
* Version: 1.2.2
* Text Domain: cloudflare-flexible-ssl
* Author: iControlWP
* Author URI: http://icwp.io/2e
*/
/**
* Copyright (c) 2016 iControlWP <support@icontrolwp.com>
* 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() {
$aIcwpHttpsServerOpts = array( 'HTTP_CF_VISITOR', 'HTTP_X_FORWARDED_PROTO' );
foreach( $aIcwpHttpsServerOpts as $sOption ) {
if ( isset( $_SERVER[ $sOption ] ) && ( strpos( $_SERVER[ $sOption ], 'https' ) !== false ) ) {
$_SERVER[ 'HTTPS' ] = 'on';
break;
}
}
if ( is_admin() ) {
add_action( 'admin_init', array( $this, 'maintainPluginLoadPosition') );
}
}
/**
* 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 > 1 ) {
$this->setActivePluginLoadPosition( $sBaseFile, 0 );
}
}
/**
* @param string $sPluginFile
* @return int
*/
public 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
*/
public function setActivePluginLoadPosition( $sPluginFile, $nDesiredPosition = 0 ) {
$aActive = $this->setArrayValueToPosition( get_option( 'active_plugins' ), $sPluginFile, $nDesiredPosition );
update_option( 'active_plugins', $aActive );
if ( is_multisite() ) {
$aActive = $this->setArrayValueToPosition( get_option( 'active_sitewide_plugins' ), $sPluginFile, $nDesiredPosition );
update_option( 'active_sitewide_plugins', $aActive );
}
}
/**
* @param array $aSubjectArray
* @param mixed $mValue
* @param int $nDesiredPosition
* @return array
*/
public function setArrayValueToPosition( $aSubjectArray, $mValue, $nDesiredPosition ) {
if ( $nDesiredPosition < 0 || !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, $nDesiredPosition, 0, $mValue );
}
return $aSubjectArray;
}
}
$oIcwpCfFlexibleSslCheck = new ICWP_Cloudflare_Flexible_SSL();
$oIcwpCfFlexibleSslCheck->run();