Cloudflare - Version 1.3.15

Version Description

  • Plugin settings are now found under Settings -> CloudFlare
  • Plugin is now using the WordPress HTTP_API - this will give better support to those in hosting environments without cURL or an up to date CA cert bundle
  • Fixes to squash some PHP Warnings. Relocated error logging to only happen in WP_DEBUG mode
  • Added Protocol Rewriting option to support Flexible SSL
Download this release

Release Info

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

Version 1.3.15

Files changed (3) hide show
  1. cloudflare.php +589 -0
  2. ip_in_range.php +216 -0
  3. readme.txt +167 -0
cloudflare.php ADDED
@@ -0,0 +1,589 @@
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.15
7
+ Author: Ian Pye, Jerome Chen, James Greene, Simon Moore, David Fritsch (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.15');
30
+ define('CLOUDFLARE_API_URL', 'https://www.cloudflare.com/api_json.html');
31
+ define('CLOUDFLARE_SPAM_URL', 'https://www.cloudflare.com/ajax/external-event.html');
32
+
33
+ require_once("ip_in_range.php");
34
+
35
+ // Make sure we don't expose any info if called directly
36
+ if ( !function_exists( 'add_action' ) ) {
37
+ echo "Hi there! I'm just a plugin, not much I can do when called directly.";
38
+ exit;
39
+ }
40
+
41
+ function cloudflare_init() {
42
+ global $is_cf;
43
+
44
+ $is_cf = (isset($_SERVER["HTTP_CF_CONNECTING_IP"]))? TRUE: FALSE;
45
+
46
+ // only run this logic if the REMOTE_ADDR is populated, to avoid causing notices in CLI mode
47
+ if (isset($_SERVER["REMOTE_ADDR"])) {
48
+ if (strpos($_SERVER["REMOTE_ADDR"], ":") === FALSE) {
49
+
50
+ $cf_ip_ranges = array("199.27.128.0/21","173.245.48.0/20","103.21.244.0/22","103.22.200.0/22","103.31.4.0/22","141.101.64.0/18","108.162.192.0/18","190.93.240.0/20","188.114.96.0/20","197.234.240.0/22","198.41.128.0/17","162.158.0.0/15","104.16.0.0/12");
51
+ // IPV4: Update the REMOTE_ADDR value if the current REMOTE_ADDR value is in the specified range.
52
+ foreach ($cf_ip_ranges as $range) {
53
+ if (ipv4_in_range($_SERVER["REMOTE_ADDR"], $range)) {
54
+ if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
55
+ $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
56
+ }
57
+ break;
58
+ }
59
+ }
60
+ }
61
+ else {
62
+ $cf_ip_ranges = array("2400:cb00::/32","2606:4700::/32","2803:f800::/32","2405:b500::/32","2405:8100::/32");
63
+ $ipv6 = get_ipv6_full($_SERVER["REMOTE_ADDR"]);
64
+ foreach ($cf_ip_ranges as $range) {
65
+ if (ipv6_in_range($ipv6, $range)) {
66
+ if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
67
+ $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
68
+ }
69
+ break;
70
+ }
71
+ }
72
+ }
73
+ }
74
+
75
+ // Let people know that the CF WP plugin is turned on.
76
+ if (!headers_sent()) {
77
+ header("X-CF-Powered-By: WP " . CLOUDFLARE_VERSION);
78
+ }
79
+ add_action('admin_menu', 'cloudflare_config_page');
80
+ }
81
+ add_action('init', 'cloudflare_init',1);
82
+
83
+ function cloudflare_admin_init() {
84
+
85
+ }
86
+
87
+ add_action('admin_init', 'cloudflare_admin_init');
88
+
89
+ function cloudflare_plugin_action_links( $links ) {
90
+ $links[] = '<a href="'. get_admin_url(null, 'options-general.php?page=cloudflare') .'">Settings</a>';
91
+ return $links;
92
+ }
93
+
94
+ add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'cloudflare_plugin_action_links' );
95
+
96
+ function cloudflare_config_page() {
97
+ if (function_exists('add_options_page')) {
98
+ add_options_page(__('CloudFlare Configuration'), __('CloudFlare'), 'manage_options', 'cloudflare', 'cloudflare_conf');
99
+ }
100
+ }
101
+
102
+ function load_cloudflare_keys () {
103
+ global $cloudflare_api_key, $cloudflare_api_email, $cloudflare_zone_name, $cloudflare_protocol_rewrite;
104
+ $cloudflare_api_key = get_option('cloudflare_api_key');
105
+ $cloudflare_api_email = get_option('cloudflare_api_email');
106
+ $cloudflare_zone_name = get_option('cloudflare_zone_name');
107
+ $cloudflare_protocol_rewrite = load_protocol_rewrite();
108
+ }
109
+
110
+ function load_protocol_rewrite() {
111
+ return get_option('cloudflare_protocol_rewrite', 1);
112
+ }
113
+
114
+ function cloudflare_conf() {
115
+
116
+ if ( function_exists('current_user_can') && !current_user_can('manage_options') )
117
+ die(__('Cheatin&#8217; uh?'));
118
+ global $cloudflare_zone_name, $cloudflare_api_key, $cloudflare_api_email, $cloudflare_protocol_rewrite, $is_cf;
119
+ global $wpdb;
120
+
121
+ load_cloudflare_keys();
122
+
123
+ $messages = array(
124
+ 'ip_restore_on' => array('text' => __('Plugin Status: True visitor IP is being restored')),
125
+ 'comment_spam_on' => array('text' => __('Plugin Status: CloudFlare will be notified when you mark comments as spam')),
126
+ 'comment_spam_off' => array('text' => __('Plugin Status: CloudFlare will NOT be notified when you mark comments as spam, enter your API details below')),
127
+ 'curl_not_installed' => array('text' => __('Plugin Status: cURL is not installed/enabled on your server. You will not be able to toggle <a href="https://support.cloudflare.com/hc/en-us/articles/200168246-What-does-CloudFlare-Development-mode-mean-" target="_blank">development mode</a> via this plugin. If you wish to do this, please contact your server administrator or hosting provider for assistance with installing cURL.')),
128
+ 'dev_mode_on' => array('text' => __('Development mode is On. Happy blogging!')),
129
+ 'dev_mode_off' => array('text' => __('Development mode is Off. Happy blogging!')),
130
+ 'protocol_rewrite_on' => array('text' => __('Protocol rewriting is On. Happy blogging!')),
131
+ 'protocol_rewrite_off' => array('text' => __('Protocol rewriting is Off. Happy blogging!')),
132
+ 'manual_entry' => array('text' => __('Enter your CloudFlare domain name, e-mail address and API key below')),
133
+ 'options_saved' => array('text' => __('Options Saved')),
134
+ );
135
+
136
+ $notices = array();
137
+ $warnings = array();
138
+ $errors = array();
139
+
140
+ $notices[] = 'ip_restore_on';
141
+
142
+ // get raw domain - may include www.
143
+ $urlparts = parse_url(site_url());
144
+ $raw_domain = $urlparts["host"];
145
+
146
+ // if we don't have a domain name already populated
147
+ if (empty($cloudflare_zone_name)) {
148
+
149
+ if (!empty($cloudflare_api_key) && !empty($cloudflare_api_email)) {
150
+ // Attempt to get the matching host from CF
151
+ $getDomain = get_domain($cloudflare_api_key, $cloudflare_api_email, $raw_domain);
152
+
153
+ // If not found, default to pulling the domain via client side.
154
+ if (is_wp_error($getDomain)) {
155
+ $messages['get_domain_failed'] = array('text' => __('Unable to automatically get domain - ' . $getDomain->get_error_message() . ' - please tell us your domain in the form below'));
156
+ $warnings[] = 'get_domain_failed';
157
+ }
158
+ else {
159
+ update_option('cloudflare_zone_name', esc_sql($getDomain));
160
+ update_option('cloudflare_zone_name_set_once', "TRUE");
161
+ load_cloudflare_keys();
162
+ }
163
+ }
164
+ }
165
+
166
+ $db_results = array();
167
+
168
+ if ( isset($_POST['submit'])
169
+ && check_admin_referer('cloudflare-db-api','cloudflare-db-api-nonce') ) {
170
+
171
+ if ( function_exists('current_user_can') && !current_user_can('manage_options') ) {
172
+ die(__('Cheatin&#8217; uh?'));
173
+ }
174
+
175
+ $zone_name = $_POST['cloudflare_zone_name'];
176
+ $key = $_POST['key'];
177
+ $email = $_POST['email'];
178
+ $dev_mode = esc_sql($_POST["dev_mode"]);
179
+ $protocol_rewrite = esc_sql($_POST["protocol_rewrite"]);
180
+
181
+ if ( empty($zone_name) ) {
182
+ $zone_status = 'empty';
183
+ $zone_message = 'Your domain name has been cleared.';
184
+ delete_option('cloudflare_zone_name');
185
+ } else {
186
+ $zone_message = 'Your domain name has been saved.';
187
+ update_option('cloudflare_zone_name', esc_sql($zone_name));
188
+ update_option('cloudflare_zone_name_set_once', "TRUE");
189
+ }
190
+
191
+ if ( empty($key) ) {
192
+ $key_status = 'empty';
193
+ $key_message = 'Your key has been cleared.';
194
+ delete_option('cloudflare_api_key');
195
+ } else {
196
+ $key_message = 'Your key has been verified.';
197
+ update_option('cloudflare_api_key', esc_sql($key));
198
+ update_option('cloudflare_api_key_set_once', "TRUE");
199
+ }
200
+
201
+ if ( empty($email) || !is_email($email) ) {
202
+ $email_status = 'empty';
203
+ $email_message = 'Your email has been cleared.';
204
+ delete_option('cloudflare_api_email');
205
+ } else {
206
+ $email_message = 'Your email has been verified.';
207
+ update_option('cloudflare_api_email', esc_sql($email));
208
+ update_option('cloudflare_api_email_set_once', "TRUE");
209
+ }
210
+
211
+ if (in_array($protocol_rewrite, array("0","1")) === TRUE) {
212
+ update_option('cloudflare_protocol_rewrite', $protocol_rewrite);
213
+ }
214
+
215
+ // update the values
216
+ load_cloudflare_keys();
217
+
218
+ if ($cloudflare_api_key != "" && $cloudflare_api_email != "" && $cloudflare_zone_name != "" && $dev_mode != "") {
219
+
220
+ $result = set_dev_mode(esc_sql($cloudflare_api_key), esc_sql($cloudflare_api_email), $cloudflare_zone_name, $dev_mode);
221
+
222
+ if (is_wp_error($result)) {
223
+ trigger_error($result->get_error_message(), E_USER_WARNING);
224
+ $messages['set_dev_mode_failed'] = array('text' => __('Unable to set development mode - ' . $result->get_error_message() . ' - try logging into cloudflare.com to set development mode'));
225
+ $errors[] = 'set_dev_mode_failed';
226
+ }
227
+ else {
228
+ if ($dev_mode && $result->result == 'success') {
229
+ $notices[] = 'dev_mode_on';
230
+ }
231
+ else if (!$dev_mode && $result->result == 'success') {
232
+ $notices[] = 'dev_mode_off';
233
+ }
234
+ }
235
+ }
236
+
237
+ $notices[] = 'options_saved';
238
+ }
239
+
240
+ if (!empty($cloudflare_api_key) && !empty($cloudflare_api_email) && !empty($cloudflare_zone_name)) {
241
+ $dev_mode = get_dev_mode_status($cloudflare_api_key, $cloudflare_api_email, $cloudflare_zone_name);
242
+
243
+ if (is_wp_error($dev_mode)) {
244
+ $messages['get_dev_mode_failed'] = array('text' => __('Unable to get current development mode status - ' . $dev_mode->get_error_message()));
245
+ $errors[] = 'get_dev_mode_failed';
246
+ }
247
+ }
248
+ else {
249
+ $warnings[] = 'manual_entry';
250
+ }
251
+
252
+ if (!empty($cloudflare_api_key) && !empty($cloudflare_api_email)) $notices[] = 'comment_spam_on';
253
+ else $warnings[] = 'comment_spam_off';
254
+
255
+ ?>
256
+ <div class="wrap">
257
+
258
+ <?php if ($is_cf) { ?>
259
+ <h3>You are currently using CloudFlare!</h3>
260
+ <?php } ?>
261
+
262
+ <?php if ($notices) { foreach ( $notices as $m ) { ?>
263
+ <div class="updated" style="border-left-color: #7ad03a; padding: 10px;"><?php echo $messages[$m]['text']; ?></div>
264
+ <?php } } ?>
265
+
266
+ <?php if ($warnings) { foreach ( $warnings as $m ) { ?>
267
+ <div class="updated" style="border-left-color: #ffba00; padding: 10px;"><em><?php echo $messages[$m]['text']; ?></em></div>
268
+ <?php } } ?>
269
+
270
+ <?php if ($errors) { foreach ( $errors as $m ) { ?>
271
+ <div class="updated" style="border-left-color: #dd3d36; padding: 10px;"><b><?php echo $messages[$m]['text']; ?></b></div>
272
+ <?php } } ?>
273
+
274
+ <h4><?php _e('CLOUDFLARE WORDPRESS PLUGIN:'); ?></h4>
275
+
276
+ CloudFlare has developed a plugin for WordPress. By using the CloudFlare WordPress Plugin, you receive:
277
+ <ol>
278
+ <li>Correct IP Address information for comments posted to your site</li>
279
+ <li>Better protection as spammers from your WordPress blog get reported to CloudFlare</li>
280
+ <li>If cURL is installed, you can enter your CloudFlare API details so you can toggle <a href="https://support.cloudflare.com/hc/en-us/articles/200168246-What-does-CloudFlare-Development-mode-mean-" target="_blank">Development mode</a> on/off using the form below</li>
281
+ </ol>
282
+
283
+ <h4>VERSION COMPATIBILITY:</h4>
284
+
285
+ The plugin is compatible with WordPress version 2.8.6 and later. The plugin will not install unless you have a compatible platform.
286
+
287
+ <h4>THINGS YOU NEED TO KNOW:</h4>
288
+
289
+ <ol>
290
+ <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>
291
+
292
+ <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>
293
+
294
+ <li>We recommend that any user on CloudFlare with WordPress use this plugin. </li>
295
+
296
+ <li>NOTE: This plugin is complementary to Akismet and W3 Total Cache. We recommend that you continue to use those services.</li>
297
+
298
+ </ol>
299
+
300
+ <h4>MORE INFORMATION ON CLOUDFLARE:</h4>
301
+
302
+ 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/" target="_blank">CloudFlare.com</a>.
303
+
304
+ <hr />
305
+
306
+ <form action="" method="post" id="cloudflare-conf">
307
+ <?php wp_nonce_field('cloudflare-db-api','cloudflare-db-api-nonce'); ?>
308
+ <?php if (get_option('cloudflare_api_key') && get_option('cloudflare_api_email')) { ?>
309
+ <?php } else { ?>
310
+ <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>
311
+ <?php } ?>
312
+ <h3><label for="cloudflare_zone_name"><?php _e('CloudFlare Domain Name'); ?></label></h3>
313
+ <p>
314
+ <input id="cloudflare_zone_name" name="cloudflare_zone_name" type="text" size="50" maxlength="255" value="<?php echo $cloudflare_zone_name; ?>" style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;" /> (<?php _e('<a href="https://www.cloudflare.com/my-websites" target="_blank">Get this?</a>'); ?>)
315
+ </p>
316
+ <p>E.g. Enter domain.com not www.domain.com / blog.domain.com</p>
317
+ <?php if (isset($zone_message)) echo sprintf('<p>%s</p>', $zone_message); ?>
318
+ <h3><label for="key"><?php _e('CloudFlare API Key'); ?></label></h3>
319
+ <p>
320
+ <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" target="_blank">Get this?</a>'); ?>)
321
+ </p>
322
+ <?php if (isset($key_message)) echo sprintf('<p>%s</p>', $key_message); ?>
323
+
324
+ <h3><label for="email"><?php _e('CloudFlare API Email'); ?></label></h3>
325
+ <p>
326
+ <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" target="_blank">Get this?</a>'); ?>)
327
+ </p>
328
+ <?php if (isset($key_message)) echo sprintf('<p>%s</p>', $key_message); ?>
329
+
330
+ <h3><label for="dev_mode"><?php _e('Development Mode'); ?></label> <span style="font-size:9pt;">(<a href="https://support.cloudflare.com/hc/en-us/articles/200168246-What-does-CloudFlare-Development-mode-mean-" target="_blank">What is this?</a>)</span></h3>
331
+
332
+ <div style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;">
333
+ <input type="radio" name="dev_mode" value="0" <?php if ($dev_mode == "off") echo "checked"; ?>> Off
334
+ <input type="radio" name="dev_mode" value="1" <?php if ($dev_mode == "on") echo "checked"; ?>> On
335
+ </div>
336
+
337
+ <h3><label for="protocol_rewrite"><?php _e('HTTPS Protocol Rewriting'); ?></label> <span style="font-size:9pt;">(<a href="https://support.cloudflare.com/hc/en-us/articles/203652674" target="_blank">What is this?</a>)</span></h3>
338
+
339
+ <div style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;">
340
+ <input type="radio" name="protocol_rewrite" value="0" <?php if ($cloudflare_protocol_rewrite == 0) echo "checked"; ?>> Off
341
+ <input type="radio" name="protocol_rewrite" value="1" <?php if ($cloudflare_protocol_rewrite == 1) echo "checked"; ?>> On
342
+ </div>
343
+
344
+ <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
345
+ </form>
346
+
347
+ <?php // </div> ?>
348
+ </div>
349
+ <?php
350
+ }
351
+
352
+ // Now actually allow CF to see when a comment is approved/not-approved.
353
+ function cloudflare_set_comment_status($id, $status) {
354
+
355
+ if ($status == 'spam') {
356
+
357
+ global $cloudflare_api_key, $cloudflare_api_email;
358
+
359
+ load_cloudflare_keys();
360
+
361
+ if (!$cloudflare_api_key || !$cloudflare_api_email) {
362
+ return;
363
+ }
364
+
365
+ $comment = get_comment($id);
366
+
367
+ // make sure we have a comment
368
+ if (!is_null($comment)) {
369
+
370
+ $payload = array(
371
+ "a" => $comment->comment_author,
372
+ "am" => $comment->comment_author_email,
373
+ "ip" => $comment->comment_author_IP,
374
+ "con" => substr($comment->comment_content, 0, 100)
375
+ );
376
+
377
+ $payload = urlencode(json_encode($payload));
378
+
379
+ $args = array(
380
+ 'method' => 'GET',
381
+ 'timeout' => 20,
382
+ 'sslverify' => true,
383
+ 'user-agent' => 'CloudFlare/WordPress/'.CLOUDFLARE_VERSION,
384
+ );
385
+
386
+ $url = sprintf('%s?evnt_v=%s&u=%s&tkn=%s&evnt_t=%s', CLOUDFLARE_SPAM_URL, $payload, $cloudflare_api_email, $cloudflare_api_key, 'WP_SPAM');
387
+
388
+ // fire and forget here, for better or worse
389
+ wp_remote_get($url, $args);
390
+ }
391
+
392
+ // ajax/external-event.html?email=ian@cloudflare.com&t=94606855d7e42adf3b9e2fd004c7660b941b8e55aa42d&evnt_v={%22dd%22:%22d%22}&evnt_t=WP_SPAM
393
+ }
394
+ }
395
+
396
+ add_action('wp_set_comment_status', 'cloudflare_set_comment_status', 1, 2);
397
+
398
+ function get_dev_mode_status($token, $email, $zone) {
399
+
400
+ $fields = array(
401
+ 'a'=>"zone_load",
402
+ 'tkn'=>$token,
403
+ 'email'=>$email,
404
+ 'z'=>$zone
405
+ );
406
+
407
+ $result = cloudflare_curl(CLOUDFLARE_API_URL, $fields, true);
408
+
409
+ if (is_wp_error($result)) {
410
+ trigger_error($result->get_error_message(), E_USER_WARNING);
411
+ return $result;
412
+ }
413
+
414
+ if ($result->response->zone->obj->zone_status_class == "status-dev-mode") {
415
+ return "on";
416
+ }
417
+
418
+ return "off";
419
+ }
420
+
421
+ function set_dev_mode($token, $email, $zone, $value) {
422
+
423
+ $fields = array(
424
+ 'a'=>"devmode",
425
+ 'tkn'=>$token,
426
+ 'email'=>$email,
427
+ 'z'=>$zone,
428
+ 'v'=>$value
429
+ );
430
+
431
+ $result = cloudflare_curl(CLOUDFLARE_API_URL, $fields, true);
432
+
433
+ if (is_wp_error($result)) {
434
+ trigger_error($result->get_error_message(), E_USER_WARNING);
435
+ return $result;
436
+ }
437
+
438
+ return $result;
439
+ }
440
+
441
+ function get_domain($token, $email, $raw_domain) {
442
+
443
+ $fields = array(
444
+ 'a'=>"zone_load_multi",
445
+ 'tkn'=>$token,
446
+ 'email'=>$email
447
+ );
448
+
449
+ $result = cloudflare_curl(CLOUDFLARE_API_URL, $fields, true);
450
+
451
+ if (is_wp_error($result)) {
452
+ trigger_error($result->get_error_message(), E_USER_WARNING);
453
+ return $result;
454
+ }
455
+
456
+ $zone_count = $result->response->zones->count;
457
+ $zone_names = array();
458
+
459
+ if ($zone_count < 1) {
460
+ return new WP_Error('match_domain', 'API did not return any domains');
461
+ }
462
+ else {
463
+ for ($i = 0; $i < $zone_count; $i++) {
464
+ $zone_names[] = $result->response->zones->objs[$i]->zone_name;
465
+ }
466
+
467
+ $match = match_domain_to_zone($raw_domain, $zone_names);
468
+
469
+ if (is_null($match)) {
470
+ return new WP_Error('match_domain', 'Unable to automatically find your domain (no match)');
471
+ }
472
+ else {
473
+ return $match;
474
+ }
475
+ }
476
+ }
477
+
478
+ /**
479
+ * @param $domain string the domain portion of the WP URL
480
+ * @param $zone_names array an array of zone_names to compare against
481
+ *
482
+ * @returns null|string null in the case of a failure, string in the case of a match
483
+ */
484
+ function match_domain_to_zone($domain, $zones) {
485
+
486
+ $splitDomain = explode('.', $domain);
487
+ $totalParts = count($splitDomain);
488
+
489
+ // minimum parts for a complete zone match will be 2, e.g. blah.com
490
+ for ($i = 0; $i <= ($totalParts - 2); $i++) {
491
+ $copy = $splitDomain;
492
+ $currentDomain = implode('.', array_splice($copy, $i));
493
+ foreach ($zones as $zone_name) {
494
+ if (strtolower($currentDomain) == strtolower($zone_name)) {
495
+ return $zone_name;
496
+ }
497
+ }
498
+ }
499
+
500
+ return null;
501
+ }
502
+
503
+ /**
504
+ * @param $url string the URL to curl
505
+ * @param $fields array an associative array of arguments for POSTing
506
+ * @param $json boolean attempt to decode response as JSON
507
+ *
508
+ * @returns WP_ERROR|string|object in the case of an error, otherwise a $result string or JSON object
509
+ */
510
+ function cloudflare_curl($url, $fields = array(), $json = true) {
511
+
512
+ $args = array(
513
+ 'method' => 'GET',
514
+ 'timeout' => 20,
515
+ 'sslverify' => true,
516
+ 'user-agent' => 'CloudFlare/WordPress/'.CLOUDFLARE_VERSION,
517
+ );
518
+
519
+ if (!empty($fields)) {
520
+ $args['method'] = 'POST';
521
+ $args['body'] = $fields;
522
+ }
523
+
524
+ $response = wp_remote_request($url, $args);
525
+
526
+ // if we have an array, we have a HTTP Response
527
+ if (is_array($response)) {
528
+ // Always expect a HTTP 200 from the API
529
+
530
+ // HERE BE DRAGONS
531
+ // WP_HTTP does not return conistent types - cURL seems to return an int for the reponse code, streams returns a string.
532
+ if (intval($response['response']['code']) !== 200) {
533
+ // Invalid response code
534
+ return new WP_Error('cloudflare', sprintf('CloudFlare API returned a HTTP Error: %s - %s', $response['response']['code'], $response['response']['message']));
535
+ }
536
+ else {
537
+ if ($json == true) {
538
+ $result = json_decode($response['body']);
539
+ // not a perfect test, but better than nothing perhaps
540
+ if ($result == null) {
541
+ return new WP_Error('json_decode', sprintf('Unable to decode JSON response'), $result);
542
+ }
543
+
544
+ // check for the CloudFlare API failure response
545
+ if (property_exists($result, 'result') && $result->result !== 'success') {
546
+ $msg = 'Unknown Error';
547
+ if (property_exists($result, 'msg') && !empty($result->msg)) $msg = $result->msg;
548
+ return new WP_Error('cloudflare', $msg);
549
+ }
550
+
551
+ return $result;
552
+ }
553
+ else {
554
+ return $response['body'];
555
+ }
556
+ }
557
+ }
558
+ else if (is_wp_error($response)) {
559
+ return $response;
560
+ }
561
+
562
+ // Should never happen!
563
+ return new WP_Error('unknown_wp_http_error', sprintf('Unknown response from wp_remote_request - unable to contact CloudFlare API'));
564
+ }
565
+
566
+ function cloudflare_buffer_wrapup($buffer) {
567
+ // replace href or src attributes within script, link, base, and img tags with just "//" for protocol
568
+ $re = "/(<(script|link|base|img|form)([^>]*)(href|src|action)=[\"'])https?:\\/\\//i";
569
+ $subst = "$1//";
570
+ $return = preg_replace($re, $subst, $buffer);
571
+
572
+ // on regex error, skip overwriting buffer
573
+ if ($return) {
574
+ $buffer = $return;
575
+ }
576
+
577
+ return $buffer;
578
+ }
579
+
580
+ function cloudflare_buffer_init() {
581
+ // load just the single option, defaulting to on
582
+ $cloudflare_protocol_rewrite = load_protocol_rewrite();
583
+
584
+ if ($cloudflare_protocol_rewrite == 1) {
585
+ ob_start('cloudflare_buffer_wrapup');
586
+ }
587
+ }
588
+
589
+ add_action('plugins_loaded', 'cloudflare_buffer_init');
ip_in_range.php ADDED
@@ -0,0 +1,216 @@
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 = null;
123
+ if (count($pieces) > 1) $right_piece = $pieces[1];
124
+
125
+ // Extract out the main IP pieces
126
+ $ip_pieces = explode("::", $left_piece, 2);
127
+ $main_ip_piece = $ip_pieces[0];
128
+ $last_ip_piece = null;
129
+ if (count($ip_pieces) > 1) $last_ip_piece = $ip_pieces[1];
130
+
131
+ // Pad out the shorthand entries.
132
+ $main_ip_pieces = explode(":", $main_ip_piece);
133
+ foreach($main_ip_pieces as $key=>$val) {
134
+ $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
135
+ }
136
+
137
+ // Check to see if the last IP block (part after ::) is set
138
+ $last_piece = "";
139
+ $size = count($main_ip_pieces);
140
+ if (trim($last_ip_piece) != "") {
141
+ $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
142
+
143
+ // Build the full form of the IPV6 address considering the last IP block set
144
+ for ($i = $size; $i < 7; $i++) {
145
+ $main_ip_pieces[$i] = "0000";
146
+ }
147
+ $main_ip_pieces[7] = $last_piece;
148
+ }
149
+ else {
150
+ // Build the full form of the IPV6 address
151
+ for ($i = $size; $i < 8; $i++) {
152
+ $main_ip_pieces[$i] = "0000";
153
+ }
154
+ }
155
+
156
+ // Rebuild the final long form IPV6 address
157
+ $final_ip = implode(":", $main_ip_pieces);
158
+
159
+ return ip2long6($final_ip);
160
+ }
161
+
162
+
163
+ // Determine whether the IPV6 address is within range.
164
+ // $ip is the IPV6 address in decimal format to check if its within the IP range created by the cloudflare IPV6 address, $range_ip.
165
+ // $ip and $range_ip are converted to full IPV6 format.
166
+ // Returns true if the IPV6 address, $ip, is within the range from $range_ip. False otherwise.
167
+ function ipv6_in_range($ip, $range_ip)
168
+ {
169
+ $pieces = explode ("/", $range_ip, 2);
170
+ $left_piece = $pieces[0];
171
+ $right_piece = $pieces[1];
172
+
173
+ // Extract out the main IP pieces
174
+ $ip_pieces = explode("::", $left_piece, 2);
175
+ $main_ip_piece = $ip_pieces[0];
176
+ $last_ip_piece = $ip_pieces[1];
177
+
178
+ // Pad out the shorthand entries.
179
+ $main_ip_pieces = explode(":", $main_ip_piece);
180
+ foreach($main_ip_pieces as $key=>$val) {
181
+ $main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
182
+ }
183
+
184
+ // Create the first and last pieces that will denote the IPV6 range.
185
+ $first = $main_ip_pieces;
186
+ $last = $main_ip_pieces;
187
+
188
+ // Check to see if the last IP block (part after ::) is set
189
+ $last_piece = "";
190
+ $size = count($main_ip_pieces);
191
+ if (trim($last_ip_piece) != "") {
192
+ $last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
193
+
194
+ // Build the full form of the IPV6 address considering the last IP block set
195
+ for ($i = $size; $i < 7; $i++) {
196
+ $first[$i] = "0000";
197
+ $last[$i] = "ffff";
198
+ }
199
+ $main_ip_pieces[7] = $last_piece;
200
+ }
201
+ else {
202
+ // Build the full form of the IPV6 address
203
+ for ($i = $size; $i < 8; $i++) {
204
+ $first[$i] = "0000";
205
+ $last[$i] = "ffff";
206
+ }
207
+ }
208
+
209
+ // Rebuild the final long form IPV6 address
210
+ $first = ip2long6(implode(":", $first));
211
+ $last = ip2long6(implode(":", $last));
212
+ $in_range = ($ip >= $first && $ip <= $last);
213
+
214
+ return $in_range;
215
+ }
216
+ ?>
readme.txt ADDED
@@ -0,0 +1,167 @@
1
+ === CloudFlare ===
2
+ Contributors: i3149, jchen329, jamescf, simon-says, dfritsch
3
+ Tags: cloudflare, comments, spam, cdn, free, website, performance, speed
4
+ Requires at least: 2.8
5
+ Tested up to: 4.1
6
+ Stable tag: 1.3.15
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
+ * Better protection as spammers from your WordPress blog get reported to CloudFlare
18
+
19
+ THINGS YOU NEED TO KNOW:
20
+
21
+ * 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.
22
+
23
+ * 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.
24
+
25
+ * We recommend any WordPress and CloudFlare user use this plugin. For more best practices around using WordPress and CloudFlare, see: https://support.cloudflare.com/hc/en-us/articles/201717894-Using-CloudFlare-and-WordPress-Five-Easy-First-Steps
26
+
27
+ MORE INFORMATION ON CLOUDFLARE:
28
+
29
+ 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).
30
+
31
+ == Installation ==
32
+
33
+ Upload the CloudFlare plugin to your blog, Activate it, and you're done!
34
+
35
+ You will also want to sign up your blog with CloudFlare.com
36
+
37
+ [Read more](http://blog.cloudflare.com/introducing-the-cloudflare-wordpress-plugin) on why we created this plugin.
38
+
39
+ == Changelog ==
40
+
41
+ = 1.3.15 =
42
+
43
+ * Plugin settings are now found under Settings -> CloudFlare
44
+ * Plugin is now using the WordPress HTTP_API - this will give better support to those in hosting environments without cURL or an up to date CA cert bundle
45
+ * Fixes to squash some PHP Warnings. Relocated error logging to only happen in WP_DEBUG mode
46
+ * Added Protocol Rewriting option to support Flexible SSL
47
+
48
+ = 1.3.14 =
49
+
50
+ * Improved logic to detect the customer domain, with added option for a manual override
51
+ * Standardised error display
52
+ * Updated CloudFlare IP Ranges
53
+
54
+ = 1.3.13 =
55
+
56
+ * Clarified error messaging in the plugin further
57
+ * Added cURL error detection to explain issues with server installed cert bundles
58
+
59
+ = 1.3.12 =
60
+
61
+ * Removed use of php short-code in a couple of places
62
+ * Added some cURL / json_decode error handling to output to the screen any failures
63
+ * Reformatted error / notice display slightly
64
+
65
+ = 1.3.11 =
66
+
67
+ * Adjusted a line syntax to account for differing PHP configurations.
68
+
69
+ = 1.3.10 =
70
+
71
+ * Added IP ranges.
72
+
73
+ = 1.3.9 =
74
+ * Made adjustment to syntax surrounding cURL detection for PHP installations that do not have short_open_tag enabled.
75
+
76
+ = 1.3.8 =
77
+ * Fixed issue with invalid header.
78
+ * Updated IP ranges
79
+ * Fixed support link
80
+
81
+ = 1.3.7 =
82
+ * Remove Database Optimizer related text.
83
+
84
+ = 1.3.6 =
85
+ * Remove Database Optimizer.
86
+
87
+ = 1.3.5 =
88
+ * Disable Development Mode option if cURL not installed. Will Use JSONP in future release to allow domains without cURL to use Development Mode.
89
+
90
+ = 1.3.4 =
91
+ * Add in IPV6 support and Development Mode option to wordpress plugin settings page. Remove cached IP range text file.
92
+
93
+ = 1.3.3 =
94
+ * Bump stable version number.
95
+
96
+ = 1.3.2.Beta =
97
+ * BETA RELEASE: IPv6 support - Pull the IPv6 range from https://www.cloudflare.com/ips-v6. Added Development Mode option to wordpress plugin settings page.
98
+
99
+ = 1.2.4 =
100
+ * Pull the IP range from https://www.cloudflare.com/ips-v4. Modified to keep all files within cloudflare plugin directory.
101
+
102
+ = 1.2.3 =
103
+ * Updated with new IP range
104
+
105
+ = 1.2.2 =
106
+ * Restricted database optimization to administrators
107
+
108
+ = 1.2.1 =
109
+ * Increased load priority to avoid conflicts with other plugins
110
+
111
+ = 1.2.0 =
112
+
113
+ * WP 3.3 compatibility.
114
+
115
+ = 1.1.9 =
116
+
117
+ * Includes latest CloudFlare IP allocation -- 108.162.192.0/18.
118
+
119
+ = 1.1.8 =
120
+
121
+ * WP 3.2 compatibility.
122
+
123
+ = 1.1.7 =
124
+
125
+ * Implements several security updates.
126
+
127
+ = 1.1.6 =
128
+
129
+ * Includes latest CloudFlare IP allocation -- 141.101.64.0/18.
130
+
131
+ = 1.1.5 =
132
+
133
+ * Includes latest CloudFlare IP allocation -- 103.22.200.0/22.
134
+
135
+ = 1.1.4 =
136
+
137
+ * Updated messaging.
138
+
139
+ = 1.1.3 =
140
+
141
+ * Better permission checking for DB optimizer.
142
+ * Added CloudFlare's latest /20 to the list of CloudFlare IP ranges.
143
+
144
+ = 1.1.2 =
145
+
146
+ * Fixed several broken help links.
147
+ * Fixed confusing error message.
148
+
149
+ = 1.1.1 =
150
+
151
+ * Fix for Admin menus which are breaking when page variable contains '-'.
152
+
153
+ = 1.1.0 =
154
+
155
+ * Added a box to input CloudFlare API credentials.
156
+ * Added a call to CloudFlare's report spam API when a comment is marked as spam.
157
+
158
+ = 1.0.1 =
159
+
160
+ * Fix to check that it is OK to add a header before adding one.
161
+
162
+ = 1.0.0 =
163
+
164
+ * Initial feature set
165
+ * Set RemoteIP header correctly.
166
+ * On comment spam, send the offending IP to CloudFlare.
167
+ * Clean up DB on load.