Cloudflare - Version 1.3.6

Version Description

  • Remove Database Optimizer.
Download this release

Release Info

Developer furkan811
Plugin Icon 128x128 Cloudflare
Version 1.3.6
Comparing to
See all releases

Version 1.3.6

Files changed (3) hide show
  1. cloudflare.php +397 -0
  2. ip_in_range.php +214 -0
  3. readme.txt +127 -0
cloudflare.php ADDED
@@ -0,0 +1,397 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: CloudFlare
4
+ Plugin URI: http://www.cloudflare.com/wiki/CloudFlareWordPressPlugin
5
+ Description: CloudFlare integrates your blog with the CloudFlare platform.
6
+ Version: 1.3.6
7
+ Author: Ian Pye, Jerome Chen, James Greene (CloudFlare Team)
8
+ License: GPLv2
9
+ */
10
+
11
+ /*
12
+ This program is free software; you can redistribute it and/or modify
13
+ it under the terms of the GNU General Public License as published by
14
+ the Free Software Foundation; version 2 of the License.
15
+
16
+ This program is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ GNU General Public License for more details.
20
+
21
+ You should have received a copy of the GNU General Public License
22
+ along with this program; if not, write to the Free Software
23
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+
25
+ Plugin adapted from the Akismet WP plugin.
26
+
27
+ */
28
+
29
+ define('CLOUDFLARE_VERSION', '1.3.6');
30
+ require_once("ip_in_range.php");
31
+
32
+ // Make sure we don't expose any info if called directly
33
+ if ( !function_exists( 'add_action' ) ) {
34
+ echo "Hi there! I'm just a plugin, not much I can do when called directly.";
35
+ exit;
36
+ }
37
+
38
+ function cloudflare_init() {
39
+ global $cf_api_host, $cf_api_port, $is_cf;
40
+
41
+ $cf_api_host = "ssl://www.cloudflare.com";
42
+ $cf_api_port = 443;
43
+
44
+ $is_cf = ($_SERVER["HTTP_CF_CONNECTING_IP"])? TRUE: FALSE;
45
+
46
+ if (strpos($_SERVER["REMOTE_ADDR"], ":") === FALSE) {
47
+ $cf_ip_ranges = array("204.93.240.0/24", "204.93.177.0/24", "199.27.128.0/21", "173.245.48.0/20", "103.22.200.0/22", "141.101.64.0/18", "108.162.192.0/18","190.93.240.1/20");
48
+ // IPV4: Update the REMOTE_ADDR value if the current REMOTE_ADDR value is in the specified range.
49
+ foreach ($cf_ip_ranges as $range) {
50
+ if (ipv4_in_range($_SERVER["REMOTE_ADDR"], $range)) {
51
+ if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
52
+ $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
53
+ }
54
+ break;
55
+ }
56
+ }
57
+ }
58
+ else {
59
+ $cf_ip_ranges = array("2400:cb00::/32", "2606:4700::/32", "2803:f800::/32");
60
+ $ipv6 = get_ipv6_full($_SERVER["REMOTE_ADDR"]);
61
+ foreach ($cf_ip_ranges as $range) {
62
+ if (ipv6_in_range($ipv6, $range)) {
63
+ if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
64
+ $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
65
+ }
66
+ break;
67
+ }
68
+ }
69
+ }
70
+
71
+ // Let people know that the CF WP plugin is turned on.
72
+ if (!headers_sent()) {
73
+ header("X-CF-Powered-By: WP " . CLOUDFLARE_VERSION);
74
+ }
75
+ add_action('admin_menu', 'cloudflare_config_page');
76
+ cloudflare_admin_warnings();
77
+ }
78
+ add_action('init', 'cloudflare_init',1);
79
+
80
+ function cloudflare_admin_init() {
81
+
82
+ }
83
+
84
+ add_action('admin_init', 'cloudflare_admin_init');
85
+
86
+ function cloudflare_config_page() {
87
+ if ( function_exists('add_submenu_page') ) {
88
+ add_submenu_page('plugins.php', __('CloudFlare Configuration'), __('CloudFlare'), 'manage_options', 'cloudflare', 'cloudflare_conf');
89
+ }
90
+ }
91
+
92
+ function load_cloudflare_keys () {
93
+ global $cloudflare_api_key, $cloudflare_api_email;
94
+ if (!$cloudflare_api_key) {
95
+ $cloudflare_api_key = get_option('cloudflare_api_key');
96
+ }
97
+ if (!$cloudflare_api_email) {
98
+ $cloudflare_api_email = get_option('cloudflare_api_email');
99
+ }
100
+ }
101
+
102
+ function cloudflare_conf() {
103
+ if ( function_exists('current_user_can') && !current_user_can('manage_options') )
104
+ die(__('Cheatin&#8217; uh?'));
105
+ global $cloudflare_api_key, $cloudflare_api_email, $is_cf;
106
+ global $wpdb;
107
+
108
+ // get raw domain - may include www.
109
+ $urlparts = parse_url(site_url());
110
+ $raw_domain = $urlparts["host"];
111
+
112
+ $curl_installed = function_exists('curl_init');
113
+
114
+ if ($curl_installed) {
115
+ // Attempt to get the matching host from CF
116
+ $domain = get_domain($cloudflare_api_key, $cloudflare_api_email, $raw_domain);
117
+ // If not found, default to pulling the domain via client side.
118
+ if (!$domain) {
119
+ $domain = $raw_domain;
120
+ }
121
+ }
122
+ else {
123
+ $domain = $raw_domain;
124
+ }
125
+
126
+ define ("THIS_DOMAIN", $domain);
127
+
128
+ $db_results = array();
129
+
130
+ if ( isset($_POST['submit'])
131
+ && check_admin_referer('cloudflare-db-api','cloudflare-db-api-nonce') ) {
132
+
133
+ if ( function_exists('current_user_can') && !current_user_can('manage_options') ) {
134
+ die(__('Cheatin&#8217; uh?'));
135
+ }
136
+
137
+ $key = $_POST['key'];
138
+ $email = $_POST['email'];
139
+ $dev_mode = esc_sql($_POST["dev_mode"]);
140
+
141
+ if ( empty($key) ) {
142
+ $key_status = 'empty';
143
+ $ms[] = 'new_key_empty';
144
+ delete_option('cloudflare_api_key');
145
+ } else {
146
+ $ms[] = 'new_key_valid';
147
+ update_option('cloudflare_api_key', esc_sql($key));
148
+ update_option('cloudflare_api_key_set_once', "TRUE");
149
+ }
150
+
151
+ if ( empty($email) || !is_email($email) ) {
152
+ $email_status = 'empty';
153
+ $ms[] = 'new_email_empty';
154
+ delete_option('cloudflare_api_email');
155
+ } else {
156
+ $ms[] = 'new_email_valid';
157
+ update_option('cloudflare_api_email', esc_sql($email));
158
+ update_option('cloudflare_api_email_set_once', "TRUE");
159
+ }
160
+
161
+
162
+ $messages = array(
163
+ 'new_key_empty' => array('color' => 'aa0', 'text' => __('Your key has been cleared.')),
164
+ 'new_key_valid' => array('color' => '2d2', 'text' => __('Your key has been verified. Happy blogging!')),
165
+ 'new_email_empty' => array('color' => 'aa0', 'text' => __('Your email has been cleared.')),
166
+ 'new_email_valid' => array('color' => '2d2', 'text' => __('Your email has been verified. Happy blogging!'))
167
+ );
168
+
169
+ if ($curl_installed) {
170
+ if ($key != "" && $email != "") {
171
+ set_dev_mode(esc_sql($key), esc_sql($email), THIS_DOMAIN, $dev_mode);
172
+ if ($dev_mode) {
173
+ $ms[] = 'dev_mode_on';
174
+ }
175
+ else {
176
+ $ms[] = 'dev_mode_off';
177
+ }
178
+ }
179
+
180
+ $messages['dev_mode_on'] = array('color' => '2d2', 'text' => __('Development mode is On. Happy blogging!'));
181
+ $messages['dev_mode_off'] = array('color' => 'aa0', 'text' => __('Development mode is Off. Happy blogging!'));
182
+ }
183
+ }
184
+ ?>
185
+ <?php if ( !empty($_POST['submit'] )) { ?>
186
+ <div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>
187
+ <?php } ?>
188
+ <div class="wrap">
189
+
190
+ <?php if ($is_cf) { ?>
191
+ <h3>You are currently using CloudFlare!</h3>
192
+ <?php } ?>
193
+
194
+ <h4><?php _e('CLOUDFLARE WORDPRESS PLUGIN:'); ?></h4>
195
+ <?php // <div class="narrow"> ?>
196
+
197
+ CloudFlare has developed a plugin for WordPress. By using the CloudFlare WordPress Plugin, you receive:
198
+ <ol>
199
+ <li>Correct IP Address information for comments posted to your site</li>
200
+ <li>Better protection as spammers from your WordPress blog get reported to CloudFlare</li>
201
+ <li>Optimization of your server database (optional)</li>
202
+ </ol>
203
+
204
+ <h4>VERSION COMPATIBILITY:</h4>
205
+
206
+ The plugin is compatible with WordPress version 2.8.6 and later. The plugin will not install unless you have a compatible platform.
207
+
208
+ <h4>THINGS YOU NEED TO KNOW:</h4>
209
+
210
+ <ol>
211
+ <li>The main purpose of this plugin is to ensure you have no change to your originating IPs when using CloudFlare. Since CloudFlare acts a reverse proxy, connecting IPs now come from CloudFlare's range. This plugin will ensure you can continue to see the originating IP. Once you install the plugin, the IP benefit will be activated.</li>
212
+
213
+ <li>Every time you click the 'spam' button on your blog, this threat information is sent to CloudFlare to ensure you are constantly getting the best site protection.</li>
214
+
215
+ <li>We recommend that any user on CloudFlare with WordPress use this plugin. </li>
216
+
217
+ <li>NOTE: This plugin is complementary to Akismet and W3 Total Cache. We recommend that you continue to use those services.</li>
218
+
219
+ </ol>
220
+
221
+ <h4>MORE INFORMATION ON CLOUDFLARE:</h4>
222
+
223
+ CloudFlare is a service that makes websites load faster and protects sites from online spammers and hackers. Any website with a root domain (ie www.mydomain.com) can use CloudFlare. On average, it takes less than 5 minutes to sign up. You can learn more here: <a href="http://www.cloudflare.com/">CloudFlare.com</a>.
224
+
225
+ <?php
226
+ if ($curl_installed) {
227
+ $dev_mode = get_dev_mode_status($cloudflare_api_key, $cloudflare_api_email, THIS_DOMAIN);
228
+ }
229
+ ?>
230
+
231
+ <hr />
232
+
233
+ <form action="" method="post" id="cloudflare-conf">
234
+ <?php wp_nonce_field('cloudflare-db-api','cloudflare-db-api-nonce'); ?>
235
+ <?php if (get_option('cloudflare_api_key') && get_option('cloudflare_api_email')) { ?>
236
+ <?php } else { ?>
237
+ <p><?php printf(__('Input your API key from your CloudFlare Accounts Settings page here. To find your API key, log in to <a href="%1$s">CloudFlare</a> and go to \'Account\'.'), 'https://www.cloudflare.com/my-account.html'); ?></p>
238
+ <?php } ?>
239
+ <?php if ($ms) { foreach ( $ms as $m ) { ?>
240
+ <p style="padding: .5em; color: #<?php echo $messages[$m]['color']; ?>; font-weight: bold;"><?php echo $messages[$m]['text']; ?></p>
241
+ <?php } } ?>
242
+ <h3><label for="key"><?php _e('CloudFlare API Key'); ?></label></h3>
243
+ <p><input id="key" name="key" type="text" size="50" maxlength="48" value="<?php echo get_option('cloudflare_api_key'); ?>" style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;" /> (<?php _e('<a href="https://www.cloudflare.com/my-account.html">Get this?</a>'); ?>)</p>
244
+ <h3><label for="email"><?php _e('CloudFlare API Email'); ?></label></h3>
245
+ <p><input id="email" name="email" type="text" size="50" maxlength="48" value="<?php echo get_option('cloudflare_api_email'); ?>" style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;" /> (<?php _e('<a href="https://www.cloudflare.com/my-account.html">Get this?</a>'); ?>)
246
+ <h3><label for="dev_mode"><?php _e('Development Mode'); ?></label> <span style="font-size:9pt;">(<a href="http://support.cloudflare.com/kb/what-do-the-various-cloudflare-settings-do/what-does-cloudflare-development-mode-mean" " target="_blank">What is this?</a>)</span></h3>
247
+
248
+ <? if ($curl_installed) { ?>
249
+ <div style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;">
250
+ <input type="radio" name="dev_mode" value="0" <? if ($dev_mode == "off") echo "checked"; ?>> Off
251
+ <input type="radio" name="dev_mode" value="1" <? if ($dev_mode == "on") echo "checked"; ?>> On
252
+ </div>
253
+ <? } else { ?>
254
+ You cannot toggle development mode because cURL is not installed for your domain. Please contact a server administrator for assistance with installing cURL.
255
+ <? } ?>
256
+
257
+ </p>
258
+ <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
259
+ </form>
260
+
261
+ <?php // </div> ?>
262
+ </div>
263
+ <?php
264
+ }
265
+
266
+ function cloudflare_admin_warnings() {
267
+
268
+ global $cloudflare_api_key, $cloudflare_api_email;
269
+ load_cloudflare_keys();
270
+ }
271
+
272
+ // Now actually allow CF to see when a comment is approved/not-approved.
273
+ function cloudflare_set_comment_status($id, $status) {
274
+ global $cf_api_host, $cf_api_port, $cloudflare_api_key, $cloudflare_api_email;
275
+ if (!$cf_api_host || !$cf_api_port) {
276
+ return;
277
+ }
278
+ load_cloudflare_keys();
279
+ if (!$cloudflare_api_key || !$cloudflare_api_email) {
280
+ return;
281
+ }
282
+
283
+ // ajax/external-event.html?email=ian@cloudflare.com&t=94606855d7e42adf3b9e2fd004c7660b941b8e55aa42d&evnt_v={%22dd%22:%22d%22}&evnt_t=WP_SPAM
284
+ $comment = get_comment($id);
285
+ $value = array("a" => $comment->comment_author,
286
+ "am" => $comment->comment_author_email,
287
+ "ip" => $comment->comment_author_IP,
288
+ "con" => substr($comment->comment_content, 0, 100));
289
+ $url = "/ajax/external-event.html?evnt_v=" . urlencode(json_encode($value)) . "&u=$cloudflare_api_email&tkn=$cloudflare_api_key&evnt_t=";
290
+
291
+ // If spam, send this info over to CloudFlare.
292
+ if ($status == "spam") {
293
+ $url .= "WP_SPAM";
294
+ $fp = @fsockopen($cf_api_host, $cf_api_port, $errno, $errstr, 30);
295
+ if ($fp) {
296
+ $out = "GET $url HTTP/1.1\r\n";
297
+ $out .= "Host: www.cloudflare.com\r\n";
298
+ $out .= "Connection: Close\r\n\r\n";
299
+ fwrite($fp, $out);
300
+ $res = "";
301
+ while (!feof($fp)) {
302
+ $res .= fgets($fp, 128);
303
+ }
304
+ fclose($fp);
305
+ }
306
+ }
307
+ }
308
+
309
+ add_action('wp_set_comment_status', 'cloudflare_set_comment_status', 1, 2);
310
+
311
+ function get_dev_mode_status($token, $email, $zone) {
312
+ $url = 'https://www.cloudflare.com/api_json.html';
313
+ $fields = array(
314
+ 'a'=>"zone_load",
315
+ 'tkn'=>$token,
316
+ 'email'=>$email,
317
+ 'z'=>$zone
318
+ );
319
+
320
+ foreach($fields as $key=>$value) {
321
+ $fields_string .= $key.'='.$value.'&';
322
+ }
323
+ rtrim($fields_string,'&');
324
+ $ch = curl_init();
325
+ curl_setopt($ch,CURLOPT_URL,$url);
326
+ curl_setopt($ch,CURLOPT_POST,count($fields));
327
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
328
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
329
+ $result = curl_exec($ch);
330
+ $result = json_decode($result);
331
+ curl_close($ch);
332
+
333
+ if ($result->response->zone->obj->zone_status_class == "status-dev-mode") {
334
+ return "on";
335
+ }
336
+
337
+ return "off";
338
+ }
339
+
340
+ function set_dev_mode($token, $email, $zone, $value)
341
+ {
342
+ $url = 'https://www.cloudflare.com/api_json.html';
343
+ $fields = array(
344
+ 'a'=>"devmode",
345
+ 'tkn'=>$token,
346
+ 'email'=>$email,
347
+ 'z'=>$zone,
348
+ 'v'=>$value
349
+ );
350
+ foreach($fields as $key=>$value) {
351
+ $fields_string .= $key.'='.$value.'&';
352
+ }
353
+ rtrim($fields_string,'&');
354
+ $ch = curl_init();
355
+ curl_setopt($ch,CURLOPT_URL,$url);
356
+ curl_setopt($ch,CURLOPT_POST,count($fields));
357
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
358
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
359
+ $result = curl_exec($ch);
360
+ $result = json_decode($result);
361
+ curl_close($ch);
362
+ }
363
+
364
+ function get_domain($token, $email, $raw_domain) {
365
+ $url = 'https://www.cloudflare.com/api_json.html';
366
+ $fields = array(
367
+ 'a'=>"zone_load_multi",
368
+ 'tkn'=>$token,
369
+ 'email'=>$email
370
+ );
371
+
372
+ foreach($fields as $key=>$value) {
373
+ $fields_string .= $key.'='.$value.'&';
374
+ }
375
+ rtrim($fields_string,'&');
376
+ $ch = curl_init();
377
+ curl_setopt($ch,CURLOPT_URL,$url);
378
+ curl_setopt($ch,CURLOPT_POST,count($fields));
379
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
380
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
381
+ $result = curl_exec($ch);
382
+ $result = json_decode($result);
383
+ curl_close($ch);
384
+
385
+ $zone_count = $result->response->zones->count;
386
+ if ($zone_count > 0) {
387
+ for ($i = 0; $i < $zone_count; $i++) {
388
+ $zone_name = $result->response->zones->objs[$i]->zone_name;
389
+ if (strpos($raw_domain, $zone_name) !== FALSE){
390
+ return $zone_name;
391
+ }
392
+ }
393
+ }
394
+
395
+ return null;
396
+ }
397
+ ?>
ip_in_range.php ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ * ip_in_range.php - Function to determine if an IP is located in a
5
+ * specific range as specified via several alternative
6
+ * formats.
7
+ *
8
+ * Network ranges can be specified as:
9
+ * 1. Wildcard format: 1.2.3.*
10
+ * 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
11
+ * 3. Start-End IP format: 1.2.3.0-1.2.3.255
12
+ *
13
+ * Return value BOOLEAN : ip_in_range($ip, $range);
14
+ *
15
+ * Copyright 2008: Paul Gregg <pgregg@pgregg.com>
16
+ * 10 January 2008
17
+ * Version: 1.2
18
+ *
19
+ * Source website: http://www.pgregg.com/projects/php/ip_in_range/
20
+ * Version 1.2
21
+ *
22
+ * This software is Donationware - if you feel you have benefited from
23
+ * the use of this tool then please consider a donation. The value of
24
+ * which is entirely left up to your discretion.
25
+ * http://www.pgregg.com/donate/
26
+ *
27
+ * Please do not remove this header, or source attibution from this file.
28
+ */
29
+
30
+ /*
31
+ * Modified by James Greene <james@cloudflare.com> to include IPV6 support
32
+ * (original version only supported IPV4).
33
+ * 21 May 2012
34
+ */
35
+
36
+
37
+ // decbin32
38
+ // In order to simplify working with IP addresses (in binary) and their
39
+ // netmasks, it is easier to ensure that the binary strings are padded
40
+ // with zeros out to 32 characters - IP addresses are 32 bit numbers
41
+ function decbin32 ($dec) {
42
+ return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
43
+ }
44
+
45
+ // ipv4_in_range
46
+ // This function takes 2 arguments, an IP address and a "range" in several
47
+ // different formats.
48
+ // Network ranges can be specified as:
49
+ // 1. Wildcard format: 1.2.3.*
50
+ // 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
51
+ // 3. Start-End IP format: 1.2.3.0-1.2.3.255
52
+ // The function will return true if the supplied IP is within the range.
53
+ // Note little validation is done on the range inputs - it expects you to
54
+ // use one of the above 3 formats.
55
+ function ipv4_in_range($ip, $range) {
56
+ if (strpos($range, '/') !== false) {
57
+ // $range is in IP/NETMASK format
58
+ list($range, $netmask) = explode('/', $range, 2);
59
+ if (strpos($netmask, '.') !== false) {
60
+ // $netmask is a 255.255.0.0 format
61
+ $netmask = str_replace('*', '0', $netmask);
62
+ $netmask_dec = ip2long($netmask);
63
+ return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
64
+ } else {
65
+ // $netmask is a CIDR size block
66
+ // fix the range argument
67
+ $x = explode('.', $range);
68
+ while(count($x)<4) $x[] = '0';
69
+ list($a,$b,$c,$d) = $x;
70
+ $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
71
+ $range_dec = ip2long($range);
72
+ $ip_dec = ip2long($ip);
73
+
74
+ # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
75
+ #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
76
+
77
+ # Strategy 2 - Use math to create it
78
+ $wildcard_dec = pow(2, (32-$netmask)) - 1;
79
+ $netmask_dec = ~ $wildcard_dec;
80
+
81
+ return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
82
+ }
83
+ } else {
84
+ // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
85
+ if (strpos($range, '*') !==false) { // a.b.*.* format
86
+ // Just convert to A-B format by setting * to 0 for A and 255 for B
87
+ $lower = str_replace('*', '0', $range);
88
+ $upper = str_replace('*', '255', $range);
89
+ $range = "$lower-$upper";
90
+ }
91
+
92
+ if (strpos($range, '-')!==false) { // A-B format
93
+ list($lower, $upper) = explode('-', $range, 2);
94
+ $lower_dec = (float)sprintf("%u",ip2long($lower));
95
+ $upper_dec = (float)sprintf("%u",ip2long($upper));
96
+ $ip_dec = (float)sprintf("%u",ip2long($ip));
97
+ return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
98
+ }
99
+ return false;
100
+ }
101
+ }
102
+
103
+ function ip2long6($ip) {
104
+ if (substr_count($ip, '::')) {
105
+ $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
106
+ }
107
+
108
+ $ip = explode(':', $ip);
109
+ $r_ip = '';
110
+ foreach ($ip as $v) {
111
+ $r_ip .= str_pad(base_convert($v, 16, 2), 16, 0, STR_PAD_LEFT);
112
+ }
113
+
114
+ return base_convert($r_ip, 2, 10);
115
+ }
116
+
117
+ // Get the ipv6 full format and return it as a decimal value.
118
+ function get_ipv6_full($ip)
119
+ {
120
+ $pieces = explode ("/", $ip, 2);
121
+ $left_piece = $pieces[0];
122
+ $right_piece = $pieces[1];
123
+
124
+ // Extract out the main IP pieces
125
+ $ip_pieces = explode("::", $left_piece, 2);
126
+ $main_ip_piece = $ip_pieces[0];
127
+ $last_ip_piece = $ip_pieces[1];
128
+
129
+ // Pad out the shorthand entries.
130
+ $main_ip_pieces = explode(":", $main_ip_piece);
131
+ foreach($main_ip_pieces as $key=>$val) {
132
+ $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
133
+ }
134
+
135
+ // Check to see if the last IP block (part after ::) is set
136
+ $last_piece = "";
137
+ $size = count($main_ip_pieces);
138
+ if (trim($last_ip_piece) != "") {
139
+ $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
140
+
141
+ // Build the full form of the IPV6 address considering the last IP block set
142
+ for ($i = $size; $i < 7; $i++) {
143
+ $main_ip_pieces[$i] = "0000";
144
+ }
145
+ $main_ip_pieces[7] = $last_piece;
146
+ }
147
+ else {
148
+ // Build the full form of the IPV6 address
149
+ for ($i = $size; $i < 8; $i++) {
150
+ $main_ip_pieces[$i] = "0000";
151
+ }
152
+ }
153
+
154
+ // Rebuild the final long form IPV6 address
155
+ $final_ip = implode(":", $main_ip_pieces);
156
+
157
+ return ip2long6($final_ip);
158
+ }
159
+
160
+
161
+ // Determine whether the IPV6 address is within range.
162
+ // $ip is the IPV6 address in decimal format to check if its within the IP range created by the cloudflare IPV6 address, $range_ip.
163
+ // $ip and $range_ip are converted to full IPV6 format.
164
+ // Returns true if the IPV6 address, $ip, is within the range from $range_ip. False otherwise.
165
+ function ipv6_in_range($ip, $range_ip)
166
+ {
167
+ $pieces = explode ("/", $range_ip, 2);
168
+ $left_piece = $pieces[0];
169
+ $right_piece = $pieces[1];
170
+
171
+ // Extract out the main IP pieces
172
+ $ip_pieces = explode("::", $left_piece, 2);
173
+ $main_ip_piece = $ip_pieces[0];
174
+ $last_ip_piece = $ip_pieces[1];
175
+
176
+ // Pad out the shorthand entries.
177
+ $main_ip_pieces = explode(":", $main_ip_piece);
178
+ foreach($main_ip_pieces as $key=>$val) {
179
+ $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
180
+ }
181
+
182
+ // Create the first and last pieces that will denote the IPV6 range.
183
+ $first = $main_ip_pieces;
184
+ $last = $main_ip_pieces;
185
+
186
+ // Check to see if the last IP block (part after ::) is set
187
+ $last_piece = "";
188
+ $size = count($main_ip_pieces);
189
+ if (trim($last_ip_piece) != "") {
190
+ $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
191
+
192
+ // Build the full form of the IPV6 address considering the last IP block set
193
+ for ($i = $size; $i < 7; $i++) {
194
+ $first[$i] = "0000";
195
+ $last[$i] = "ffff";
196
+ }
197
+ $main_ip_pieces[7] = $last_piece;
198
+ }
199
+ else {
200
+ // Build the full form of the IPV6 address
201
+ for ($i = $size; $i < 8; $i++) {
202
+ $first[$i] = "0000";
203
+ $last[$i] = "ffff";
204
+ }
205
+ }
206
+
207
+ // Rebuild the final long form IPV6 address
208
+ $first = ip2long6(implode(":", $first));
209
+ $last = ip2long6(implode(":", $last));
210
+ $in_range = ($ip >= $first && $ip <= $last);
211
+
212
+ return $in_range;
213
+ }
214
+ ?>
readme.txt ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === CloudFlare ===
2
+ Contributors: i3149, jchen329, jamescf
3
+ Tags: cloudflare, comments, spam, cdn, free, website, performance, speed
4
+ Requires at least: 2.8
5
+ Tested up to: 3.3
6
+ Stable tag: 1.3.6
7
+ License: GPLv2
8
+
9
+ The CloudFlare WordPress Plugin ensures your WordPress blog is running optimally on the CloudFlare platform.
10
+
11
+ == Description ==
12
+
13
+ CloudFlare has developed a plugin for WordPress. By using the CloudFlare WordPress Plugin, you receive:
14
+
15
+ * Correct IP Address information for comments posted to your site
16
+
17
+ * Optimization of your server database
18
+
19
+ * Better protection as spammers from your WordPress blog get reported to CloudFlare
20
+
21
+ THINGS YOU NEED TO KNOW:
22
+
23
+ * The main purpose of this plugin is to ensure you have no change to your originating IPs when using CloudFlare. Since CloudFlare acts a reverse proxy, connecting IPs now come from CloudFlare's range. This plugin will ensure you can continue to see the originating IP.
24
+
25
+ * This plugin can also help to ensure your server database is running optimally. If you are going to run the Database Optimizer associated with this plugin, then run it at a low traffic time. While the database optimizer is running, your site will go into Read Only mode, which means that you or your visitors will not be allowed to post. The optimizer should run quickly. Once the optimizer is done running, you will be able to post to your site again.
26
+
27
+ * Every time you click the 'spam' button on your blog, this threat information is sent to CloudFlare to ensure you are constantly getting the best site protection.
28
+
29
+ * We recommend that any user on WordPress and CloudFlare should use this plugin.
30
+
31
+ MORE INFORMATION ON CLOUDFLARE:
32
+
33
+ CloudFlare is a service that makes websites load faster and protects sites from online spammers and hackers. Any website with a root domain (ie www.mydomain.com) can use CloudFlare. On average, it takes less than 5 minutes to sign up. You can learn more here: [CloudFlare.com](https://www.cloudflare.com/overview.html).
34
+
35
+ == Installation ==
36
+
37
+ Upload the CloudFlare plugin to your blog, Activate it, and you're done!
38
+
39
+ You will also want to sign up your blog with CloudFlare.com
40
+
41
+ [Read more](http://blog.cloudflare.com/introducing-the-cloudflare-wordpress-plugin) on why we created this plugin.
42
+
43
+ == Changelog ==
44
+ = 1.3.6 =
45
+ * Remove Database Optimizer.
46
+
47
+ = 1.3.5 =
48
+ * Disable Development Mode option if cURL not installed. Will Use JSONP in future release to allow domains without cURL to use Development Mode.
49
+
50
+ = 1.3.4 =
51
+ * Add in IPV6 support and Development Mode option to wordpress plugin settings page. Remove cached IP range text file.
52
+
53
+ = 1.3.3 =
54
+ * Bump stable version number.
55
+
56
+ = 1.3.2.Beta =
57
+ * BETA RELEASE: IPv6 support - Pull the IPv6 range from https://www.cloudflare.com/ips-v6. Added Development Mode option to wordpress plugin settings page.
58
+
59
+ = 1.2.4 =
60
+ * Pull the IP range from https://www.cloudflare.com/ips-v4. Modified to keep all files within cloudflare plugin directory.
61
+
62
+ = 1.2.3 =
63
+ * Updated with new IP range
64
+
65
+ = 1.2.2 =
66
+ * Restricted database optimization to administrators
67
+
68
+ = 1.2.1 =
69
+ * Increased load priority to avoid conflicts with other plugins
70
+
71
+ = 1.2.0 =
72
+
73
+ * WP 3.3 compatibility.
74
+
75
+ = 1.1.9 =
76
+
77
+ * Includes latest CloudFlare IP allocation -- 108.162.192.0/18.
78
+
79
+ = 1.1.8 =
80
+
81
+ * WP 3.2 compatibility.
82
+
83
+ = 1.1.7 =
84
+
85
+ * Implements several security updates.
86
+
87
+ = 1.1.6 =
88
+
89
+ * Includes latest CloudFlare IP allocation -- 141.101.64.0/18.
90
+
91
+ = 1.1.5 =
92
+
93
+ * Includes latest CloudFlare IP allocation -- 103.22.200.0/22.
94
+
95
+ = 1.1.4 =
96
+
97
+ * Updated messaging.
98
+
99
+ = 1.1.3 =
100
+
101
+ * Better permission checking for DB optimizer.
102
+ * Added CloudFlare's latest /20 to the list of CloudFlare IP ranges.
103
+
104
+ = 1.1.2 =
105
+
106
+ * Fixed several broken help links.
107
+ * Fixed confusing error message.
108
+
109
+ = 1.1.1 =
110
+
111
+ * Fix for Admin menus which are breaking when page variable contains '-'.
112
+
113
+ = 1.1.0 =
114
+
115
+ * Added a box to input CloudFlare API credentials.
116
+ * Added a call to CloudFlare's report spam API when a comment is marked as spam.
117
+
118
+ = 1.0.1 =
119
+
120
+ * Fix to check that it is OK to add a header before adding one.
121
+
122
+ = 1.0.0 =
123
+
124
+ * Initial feature set
125
+ * Set RemoteIP header correctly.
126
+ * On comment spam, send the offending IP to CloudFlare.
127
+ * Clean up DB on load.