Cloudflare - Version 1.2.6.Beta

Version Description

Download this release

Release Info

Developer furkan811
Plugin Icon 128x128 Cloudflare
Version 1.2.6.Beta
Comparing to
See all releases

Version 1.2.6.Beta

Files changed (5) hide show
  1. cloudflare.php +517 -0
  2. ip_in_range.php +214 -0
  3. ipv4_list.txt +0 -0
  4. ipv6_list.txt +0 -0
  5. readme.txt +122 -0
cloudflare.php ADDED
@@ -0,0 +1,517 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2.6.Beta
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.2.6.Beta');
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_pull_ip_list($ip_version=4)
39
+ {
40
+ $cf_ip_ranges = array();
41
+
42
+ $plugin_dir = plugin_dir_path(__FILE__);
43
+
44
+ if ($ip_version == 6) {
45
+ $ip_text_file = "ipv6_list.txt";
46
+ $ip_url = "https://www.cloudflare.com/ips-v6";
47
+ $ips_fallback_array = array("2400:cb00::/32", "2606:4700::/32", "2803:f800::/32");
48
+ }
49
+ else { // Default to ipv4
50
+ $ip_text_file = "ipv4_list.txt";
51
+ $ip_url = "https://www.cloudflare.com/ips-v4";
52
+ $ips_fallback_array = 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");
53
+ }
54
+
55
+ // Let's get the IPs from the official cloudflare IPv<$ip_version> list URL, which has the latest up to date list.
56
+ $request_file_refresh = FALSE;
57
+ $ip_file_data = @file_get_contents($plugin_dir . $ip_text_file);
58
+ $last_modified = @filemtime($plugin_dir . $ip_text_file);
59
+ if ($last_modified === FALSE) {
60
+ $request_file_refresh = TRUE;
61
+ } else {
62
+ $time_since_last_update = time() - $last_modified;
63
+ if ($time_since_last_update > 86400) {
64
+ $request_file_refresh = TRUE;
65
+ }
66
+ }
67
+
68
+ if ($ip_file_data === FALSE || trim($ip_file_data) == "" || $request_file_refresh === TRUE) {
69
+ $live_ips_list = @file_get_contents($ip_url);
70
+
71
+ if ($live_ips_list !== FALSE && trim($live_ips_list) != "") {
72
+ @file_put_contents($plugin_dir . $ip_text_file, $live_ips_list);
73
+ $cf_ip_ranges = explode("\n", $live_ips_list);
74
+ } else {
75
+ // Use the default static list for now, until we are able to access the live updated list again.
76
+ $cf_ip_ranges = $ips_fallback_array;
77
+ }
78
+ } else {
79
+ $cf_ip_ranges = explode("\n", $ip_file_data);
80
+ }
81
+
82
+ foreach ($cf_ip_ranges as $key=>$val) {
83
+ $cf_ip_ranges[$key] = trim($val);
84
+ }
85
+
86
+ return $cf_ip_ranges;
87
+ }
88
+
89
+ function cloudflare_init() {
90
+ global $cf_api_host, $cf_api_port, $is_cf;
91
+
92
+ $cf_api_host = "ssl://www.cloudflare.com";
93
+ $cf_api_port = 443;
94
+
95
+ $is_cf = ($_SERVER["HTTP_CF_CONNECTING_IP"])? TRUE: FALSE;
96
+
97
+ if (strpos($_SERVER["REMOTE_ADDR"], ":") === FALSE) {
98
+ $cf_ip_ranges = cloudflare_pull_ip_list(4);
99
+
100
+ // IPV4: Update the REMOTE_ADDR value if the current REMOTE_ADDR value is in the specified range.
101
+ foreach ($cf_ip_ranges as $range) {
102
+ if (ipv4_in_range($_SERVER["REMOTE_ADDR"], $range)) {
103
+ if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
104
+ $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
105
+ }
106
+ break;
107
+ }
108
+ }
109
+ }
110
+ else {
111
+ $cf_ip_ranges = cloudflare_pull_ip_list(6);
112
+
113
+ // IPV6: Update the REMOTE_ADDR value if the current REMOTE_ADDR value is in the specified range.
114
+ $ipv6 = get_ipv6_full($_SERVER["REMOTE_ADDR"]);
115
+ foreach ($cf_ip_ranges as $range) {
116
+ if (ipv6_in_range($ipv6, $range)) {
117
+ if ($_SERVER["HTTP_CF_CONNECTING_IP"]) {
118
+ $_SERVER["REMOTE_ADDR"] = $_SERVER["HTTP_CF_CONNECTING_IP"];
119
+ }
120
+ break;
121
+ }
122
+ }
123
+ }
124
+
125
+ // Let people know that the CF WP plugin is turned on.
126
+ if (!headers_sent()) {
127
+ header("X-CF-Powered-By: WP " . CLOUDFLARE_VERSION);
128
+ }
129
+ add_action('admin_menu', 'cloudflare_config_page');
130
+ cloudflare_admin_warnings();
131
+ }
132
+ add_action('init', 'cloudflare_init',1);
133
+
134
+ function cloudflare_admin_init() {
135
+
136
+ }
137
+
138
+ add_action('admin_init', 'cloudflare_admin_init');
139
+
140
+ function cloudflare_config_page() {
141
+ if ( function_exists('add_submenu_page') ) {
142
+ add_submenu_page('plugins.php', __('CloudFlare Configuration'), __('CloudFlare'), 'manage_options', 'cloudflare', 'cloudflare_conf');
143
+ }
144
+ }
145
+
146
+ function load_cloudflare_keys () {
147
+ global $cloudflare_api_key, $cloudflare_api_email;
148
+ if (!$cloudflare_api_key) {
149
+ $cloudflare_api_key = get_option('cloudflare_api_key');
150
+ }
151
+ if (!$cloudflare_api_email) {
152
+ $cloudflare_api_email = get_option('cloudflare_api_email');
153
+ }
154
+ }
155
+
156
+ function cloudflare_conf() {
157
+ if ( function_exists('current_user_can') && !current_user_can('manage_options') )
158
+ die(__('Cheatin&#8217; uh?'));
159
+ global $cloudflare_api_key, $cloudflare_api_email, $is_cf;
160
+ global $wpdb;
161
+
162
+ // get raw domain - may include www.
163
+ $urlparts = parse_url(site_url());
164
+ $raw_domain = $urlparts["host"];
165
+
166
+ // Attempt to get the matching host from CF
167
+ $domain = get_domain($cloudflare_api_key, $cloudflare_api_email, $raw_domain);
168
+
169
+ // If not found, default to pulling the domain via client side.
170
+ if (!$domain) {
171
+ $domain = $raw_domain;
172
+ }
173
+
174
+ define ("THIS_DOMAIN", $domain);
175
+
176
+ $db_results = array();
177
+
178
+ if ( isset($_POST['submit'])
179
+ && !($_POST['optimize'])
180
+ && check_admin_referer('cloudflare-db-api','cloudflare-db-api-nonce') ) {
181
+
182
+ if ( function_exists('current_user_can') && !current_user_can('manage_options') ) {
183
+ die(__('Cheatin&#8217; uh?'));
184
+ }
185
+
186
+ $key = $_POST['key'];
187
+ $email = $_POST['email'];
188
+ $dev_mode = esc_sql($_POST["dev_mode"]);
189
+
190
+ if ( empty($key) ) {
191
+ $key_status = 'empty';
192
+ $ms[] = 'new_key_empty';
193
+ delete_option('cloudflare_api_key');
194
+ } else {
195
+ $ms[] = 'new_key_valid';
196
+ update_option('cloudflare_api_key', esc_sql($key));
197
+ update_option('cloudflare_api_key_set_once', "TRUE");
198
+ }
199
+
200
+ if ( empty($email) || !is_email($email) ) {
201
+ $email_status = 'empty';
202
+ $ms[] = 'new_email_empty';
203
+ delete_option('cloudflare_api_email');
204
+ } else {
205
+ $ms[] = 'new_email_valid';
206
+ update_option('cloudflare_api_email', esc_sql($email));
207
+ update_option('cloudflare_api_email_set_once', "TRUE");
208
+ }
209
+
210
+ if ($key != "" && $email != "") {
211
+ set_dev_mode(esc_sql($key), esc_sql($email), THIS_DOMAIN, $dev_mode);
212
+ if ($dev_mode) {
213
+ $ms[] = 'dev_mode_on';
214
+ }
215
+ else {
216
+ $ms[] = 'dev_mode_off';
217
+ }
218
+ }
219
+
220
+ $messages = array(
221
+ 'new_key_empty' => array('color' => 'aa0', 'text' => __('Your key has been cleared.')),
222
+ 'new_key_valid' => array('color' => '2d2', 'text' => __('Your key has been verified. Happy blogging!')),
223
+ 'new_email_empty' => array('color' => 'aa0', 'text' => __('Your email has been cleared.')),
224
+ 'new_email_valid' => array('color' => '2d2', 'text' => __('Your email has been verified. Happy blogging!')),
225
+ 'dev_mode_on' => array('color' => '2d2', 'text' => __('Development mode is On. Happy blogging!')),
226
+ 'dev_mode_off' => array('color' => 'aa0', 'text' => __('Development mode is Off. Happy blogging!'))
227
+ );
228
+ } else if ( isset($_POST['submit'])
229
+ && isset($_POST['optimize'])
230
+ && check_admin_referer('cloudflare-db-opt','cloudflare-db-opt-nonce')) {
231
+
232
+ update_option('cloudflare_api_db_last_run', time());
233
+ if(current_user_can('administrator')) {
234
+ remove_action('admin_notices', 'cloudflare_warning');
235
+ $tables = $wpdb->get_col("SHOW TABLES");
236
+ foreach($tables as $table_name) {
237
+ $optimize = $wpdb->query("OPTIMIZE TABLE `$table_name`");
238
+ $analyze = $wpdb->query("ANALYZE TABLE `$table_name`");
239
+ if (!$optimize || !$analyze) {
240
+ $db_results[] = "Error optimizing $table_name";
241
+ }
242
+ }
243
+ if (count($db_results) == 0) {
244
+ $db_results[] = "All tables optimized without error.";
245
+ }
246
+ } else {
247
+ $db_results[] = "The current user does not have the permission \"manage_database\". Please run the command again with an appropriate user.";
248
+ }
249
+ }
250
+
251
+ ?>
252
+ <?php if ( !empty($_POST['submit'] ) && !($_POST['optimize']) ) { ?>
253
+ <div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>
254
+ <?php } else if ( isset($_POST['submit']) && isset($_POST['optimize']) ) {
255
+ foreach ($db_results as $res) {
256
+ ?><div id="message" class="updated fade"><p><strong><?php _e($res) ?></strong></p></div><?php
257
+ }
258
+ }
259
+ ?>
260
+ <div class="wrap">
261
+
262
+ <?php if ($is_cf) { ?>
263
+ <h3>You are currently using CloudFlare!</h3>
264
+ <?php } ?>
265
+
266
+ <h4><?php _e('CLOUDFLARE WORDPRESS PLUGIN:'); ?></h4>
267
+ <?php // <div class="narrow"> ?>
268
+
269
+ <u>**IMPORTANT NOTE: THIS IS A BETA VERSION OF THE CLOUDFLARE PLUGIN**</u><br/><br/>
270
+
271
+ <u>If you experience any major issues related to this plugin version, please revert back to the previous stable version (1.2.4).</u><br/><br/>
272
+
273
+ CloudFlare has developed a plugin for WordPress. By using the CloudFlare WordPress Plugin, you receive:
274
+ <ol>
275
+ <li>Correct IP Address information for comments posted to your site</li>
276
+ <li>Better protection as spammers from your WordPress blog get reported to CloudFlare</li>
277
+ <li>Optimization of your server database (optional)</li>
278
+ </ol>
279
+
280
+ <h4>VERSION COMPATIBILITY:</h4>
281
+
282
+ The plugin is compatible with WordPress version 2.8.6 and later. The plugin will not install unless you have a compatible platform.
283
+
284
+ <h4>THINGS YOU NEED TO KNOW:</h4>
285
+
286
+ <ol>
287
+ <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>
288
+
289
+ <li>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. To run the Database Optimizer, click the icon below.</li>
290
+
291
+ <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>
292
+
293
+ <li>We recommend that any user on CloudFlare with WordPress use this plugin. </li>
294
+
295
+ <li>NOTE: This plugin is complementary to Akismet and W3 Total Cache. We recommend that you continue to use those services.</li>
296
+
297
+ </ol>
298
+
299
+ <h4>MORE INFORMATION ON CLOUDFLARE:</h4>
300
+
301
+ 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>.
302
+
303
+ <?php
304
+ // if ($is_cf) {
305
+ $dev_mode = get_dev_mode_status($cloudflare_api_key, $cloudflare_api_email, THIS_DOMAIN);
306
+ ?>
307
+
308
+ <hr />
309
+
310
+ <form action="" method="post" id="cloudflare-conf">
311
+ <?php wp_nonce_field('cloudflare-db-api','cloudflare-db-api-nonce'); ?>
312
+ <?php if (get_option('cloudflare_api_key') && get_option('cloudflare_api_email')) { ?>
313
+ <?php } else { ?>
314
+ <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>
315
+ <?php } ?>
316
+ <?php if ($ms) { foreach ( $ms as $m ) { ?>
317
+ <p style="padding: .5em; color: #<?php echo $messages[$m]['color']; ?>; font-weight: bold;"><?php echo $messages[$m]['text']; ?></p>
318
+ <?php } } ?>
319
+ <h3><label for="key"><?php _e('CloudFlare API Key'); ?></label></h3>
320
+ <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>
321
+ <h3><label for="email"><?php _e('CloudFlare API Email'); ?></label></h3>
322
+ <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>'); ?>)
323
+ <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>
324
+ <div style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;">
325
+ <input type="radio" name="dev_mode" value="0" <? if ($dev_mode == "off") echo "checked"; ?>> Off
326
+ <input type="radio" name="dev_mode" value="1" <? if ($dev_mode == "on") echo "checked"; ?>> On
327
+ </div>
328
+ </p>
329
+
330
+ <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
331
+ </form>
332
+
333
+ <?php
334
+ // }
335
+ ?>
336
+
337
+ <form action="" method="post" id="cloudflare-db">
338
+ <?php wp_nonce_field('cloudflare-db-opt','cloudflare-db-opt-nonce'); ?>
339
+ <input type="hidden" name="optimize" value="1" />
340
+
341
+ <h4><label for="optimize_db"><?php _e('DATABASE OPTIMIZER (optional): Make your site run even faster.'); ?></label>
342
+ <input type="submit" name="submit" value="<?php _e('Run the optimizer'); ?>" /> (<?php _e('<a href="http://www.cloudflare.com/wiki/WordPressDBOptimizer">What is this?</a>'); ?>)</h4>
343
+
344
+ </form>
345
+
346
+ <?php // </div> ?>
347
+ </div>
348
+ <?php
349
+ }
350
+
351
+ function cloudflare_admin_warnings() {
352
+
353
+ global $cloudflare_api_key, $cloudflare_api_email;
354
+ load_cloudflare_keys();
355
+
356
+ /**
357
+ if ( !get_option('cloudflare_api_key_set_once') && !$cloudflare_api_key && !isset($_POST['submit']) ) {
358
+ function cloudflare_warning() {
359
+ echo "
360
+ <div id='cloudflare-warning' class='updated fade'><p><strong>".__('CloudFlare is almost ready.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your CloudFlare API key</a> for it to work.'), "plugins.php?page=cloudflare")."</p></div>
361
+ ";
362
+ }
363
+ add_action('admin_notices', 'cloudflare_warning');
364
+ return;
365
+ } else if ( !get_option('cloudflare_api_key_set_once') && !$cloudflare_api_email && !isset($_POST['submit']) ) {
366
+ function cloudflare_warning() {
367
+ echo "
368
+ <div id='cloudflare-warning' class='updated fade'><p><strong>".__('CloudFlare is almost ready.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your CloudFlare API email</a> for it to work.'), "plugins.php?page=cloudflare")."</p></div>
369
+ ";
370
+ }
371
+ add_action('admin_notices', 'cloudflare_warning');
372
+ return;
373
+ }
374
+ */
375
+
376
+ // Check to see if they should optimized their DB
377
+ $last_run_time = (int)get_option('cloudflare_api_db_last_run');
378
+ if (!$last_run_time) {
379
+ $last_run_time = time();
380
+ }
381
+ if (time() - $last_run_time > 5259487) { // 2 Months (avg)
382
+ function cloudflare_warning() {
383
+ echo "
384
+ <div id='cloudflare-warning' class='updated fade'><p><strong>".__('Your Database is due to be optimized again.')."</strong> ".sprintf(__('We recommend that you <a href="%1$s">run the CloudFlare optimizer</a> every two months to keep your blog running quickly. It\'s time to run it again.'), "plugins.php?page=cloudflare")."</p></div>
385
+ ";
386
+ }
387
+ add_action('admin_notices', 'cloudflare_warning');
388
+ return;
389
+ }
390
+ }
391
+
392
+ // Now actually allow CF to see when a comment is approved/not-approved.
393
+ function cloudflare_set_comment_status($id, $status) {
394
+ global $cf_api_host, $cf_api_port, $cloudflare_api_key, $cloudflare_api_email;
395
+ if (!$cf_api_host || !$cf_api_port) {
396
+ return;
397
+ }
398
+ load_cloudflare_keys();
399
+ if (!$cloudflare_api_key || !$cloudflare_api_email) {
400
+ return;
401
+ }
402
+
403
+ // ajax/external-event.html?email=ian@cloudflare.com&t=94606855d7e42adf3b9e2fd004c7660b941b8e55aa42d&evnt_v={%22dd%22:%22d%22}&evnt_t=WP_SPAM
404
+ $comment = get_comment($id);
405
+ $value = array("a" => $comment->comment_author,
406
+ "am" => $comment->comment_author_email,
407
+ "ip" => $comment->comment_author_IP,
408
+ "con" => substr($comment->comment_content, 0, 100));
409
+ $url = "/ajax/external-event.html?evnt_v=" . urlencode(json_encode($value)) . "&u=$cloudflare_api_email&tkn=$cloudflare_api_key&evnt_t=";
410
+
411
+ // If spam, send this info over to CloudFlare.
412
+ if ($status == "spam") {
413
+ $url .= "WP_SPAM";
414
+ $fp = @fsockopen($cf_api_host, $cf_api_port, $errno, $errstr, 30);
415
+ if ($fp) {
416
+ $out = "GET $url HTTP/1.1\r\n";
417
+ $out .= "Host: www.cloudflare.com\r\n";
418
+ $out .= "Connection: Close\r\n\r\n";
419
+ fwrite($fp, $out);
420
+ $res = "";
421
+ while (!feof($fp)) {
422
+ $res .= fgets($fp, 128);
423
+ }
424
+ fclose($fp);
425
+ }
426
+ }
427
+ }
428
+
429
+ add_action('wp_set_comment_status', 'cloudflare_set_comment_status', 1, 2);
430
+
431
+ function get_dev_mode_status($token, $email, $zone) {
432
+ $url = 'https://www.cloudflare.com/api_json.html';
433
+ $fields = array(
434
+ 'a'=>"zone_load",
435
+ 'tkn'=>$token,
436
+ 'email'=>$email,
437
+ 'z'=>$zone
438
+ );
439
+
440
+ foreach($fields as $key=>$value) {
441
+ $fields_string .= $key.'='.$value.'&';
442
+ }
443
+ rtrim($fields_string,'&');
444
+ $ch = curl_init();
445
+ curl_setopt($ch,CURLOPT_URL,$url);
446
+ curl_setopt($ch,CURLOPT_POST,count($fields));
447
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
448
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
449
+ $result = curl_exec($ch);
450
+ $result = json_decode($result);
451
+ curl_close($ch);
452
+
453
+ if ($result->response->zone->obj->zone_status_class == "status-dev-mode") {
454
+ return "on";
455
+ }
456
+
457
+ return "off";
458
+ }
459
+
460
+ function set_dev_mode($token, $email, $zone, $value)
461
+ {
462
+ $url = 'https://www.cloudflare.com/api_json.html';
463
+ $fields = array(
464
+ 'a'=>"devmode",
465
+ 'tkn'=>$token,
466
+ 'email'=>$email,
467
+ 'z'=>$zone,
468
+ 'v'=>$value
469
+ );
470
+ foreach($fields as $key=>$value) {
471
+ $fields_string .= $key.'='.$value.'&';
472
+ }
473
+ rtrim($fields_string,'&');
474
+ $ch = curl_init();
475
+ curl_setopt($ch,CURLOPT_URL,$url);
476
+ curl_setopt($ch,CURLOPT_POST,count($fields));
477
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
478
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
479
+ $result = curl_exec($ch);
480
+ $result = json_decode($result);
481
+ curl_close($ch);
482
+ }
483
+
484
+ function get_domain($token, $email, $raw_domain) {
485
+ $url = 'https://www.cloudflare.com/api_json.html';
486
+ $fields = array(
487
+ 'a'=>"zone_load_multi",
488
+ 'tkn'=>$token,
489
+ 'email'=>$email
490
+ );
491
+
492
+ foreach($fields as $key=>$value) {
493
+ $fields_string .= $key.'='.$value.'&';
494
+ }
495
+ rtrim($fields_string,'&');
496
+ $ch = curl_init();
497
+ curl_setopt($ch,CURLOPT_URL,$url);
498
+ curl_setopt($ch,CURLOPT_POST,count($fields));
499
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
500
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
501
+ $result = curl_exec($ch);
502
+ $result = json_decode($result);
503
+ curl_close($ch);
504
+
505
+ $zone_count = $result->response->zones->count;
506
+ if ($zone_count > 0) {
507
+ for ($i = 0; $i < $zone_count; $i++) {
508
+ $zone_name = $result->response->zones->objs[$i]->zone_name;
509
+ if (strpos($raw_domain, $zone_name) !== FALSE){
510
+ return $zone_name;
511
+ }
512
+ }
513
+ }
514
+
515
+ return null;
516
+ }
517
+ ?>
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
+ ?>
ipv4_list.txt ADDED
File without changes
ipv6_list.txt ADDED
File without changes
readme.txt ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.2.4
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
+ Try out our brand new Beta version, 1.2.6.Beta!
14
+ Included in this version:
15
+ 1) IPv6 support.
16
+ 2) Added Development Mode option to CloudFlare WordPress plugin settings page.
17
+ <a href="http://downloads.wordpress.org/plugin/cloudflare.1.2.6.Beta.zip" title="Download CloudFlare 1.2.6.Beta WordPress Plugin">Download the 1.2.6.Beta version here!</a>
18
+
19
+ CloudFlare has developed a plugin for WordPress. By using the CloudFlare WordPress Plugin, you receive:
20
+
21
+ * Correct IP Address information for comments posted to your site
22
+
23
+ * Optimization of your server database
24
+
25
+ * Better protection as spammers from your WordPress blog get reported to CloudFlare
26
+
27
+ THINGS YOU NEED TO KNOW:
28
+
29
+ * 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.
30
+
31
+ * 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.
32
+
33
+ * 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.
34
+
35
+ * We recommend that any user on WordPress and CloudFlare should use this plugin.
36
+
37
+ MORE INFORMATION ON CLOUDFLARE:
38
+
39
+ 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).
40
+
41
+ == Installation ==
42
+
43
+ Upload the CloudFlare plugin to your blog, Activate it, and you're done!
44
+
45
+ You will also want to sign up your blog with CloudFlare.com
46
+
47
+ [Read more](http://blog.cloudflare.com/introducing-the-cloudflare-wordpress-plugin) on why we created this plugin.
48
+
49
+ == Changelog ==
50
+
51
+ = 1.2.5.Beta =
52
+ * BETA RELEASE: IPv6 support - Pull the IPv6 range from https://www.cloudflare.com/ips-v6. Added Development Mode option to wordpress plugin settings page.
53
+
54
+ = 1.2.4 =
55
+ * Pull the IP range from https://www.cloudflare.com/ips-v4. Modified to keep all files within cloudflare plugin directory.
56
+
57
+ = 1.2.3 =
58
+ * Updated with new IP range
59
+
60
+ = 1.2.2 =
61
+ * Restricted database optimization to administrators
62
+
63
+ = 1.2.1 =
64
+ * Increased load priority to avoid conflicts with other plugins
65
+
66
+ = 1.2.0 =
67
+
68
+ * WP 3.3 compatibility.
69
+
70
+ = 1.1.9 =
71
+
72
+ * Includes latest CloudFlare IP allocation -- 108.162.192.0/18.
73
+
74
+ = 1.1.8 =
75
+
76
+ * WP 3.2 compatibility.
77
+
78
+ = 1.1.7 =
79
+
80
+ * Implements several security updates.
81
+
82
+ = 1.1.6 =
83
+
84
+ * Includes latest CloudFlare IP allocation -- 141.101.64.0/18.
85
+
86
+ = 1.1.5 =
87
+
88
+ * Includes latest CloudFlare IP allocation -- 103.22.200.0/22.
89
+
90
+ = 1.1.4 =
91
+
92
+ * Updated messaging.
93
+
94
+ = 1.1.3 =
95
+
96
+ * Better permission checking for DB optimizer.
97
+ * Added CloudFlare's latest /20 to the list of CloudFlare IP ranges.
98
+
99
+ = 1.1.2 =
100
+
101
+ * Fixed several broken help links.
102
+ * Fixed confusing error message.
103
+
104
+ = 1.1.1 =
105
+
106
+ * Fix for Admin menus which are breaking when page variable contains '-'.
107
+
108
+ = 1.1.0 =
109
+
110
+ * Added a box to input CloudFlare API credentials.
111
+ * Added a call to CloudFlare's report spam API when a comment is marked as spam.
112
+
113
+ = 1.0.1 =
114
+
115
+ * Fix to check that it is OK to add a header before adding one.
116
+
117
+ = 1.0.0 =
118
+
119
+ * Initial feature set
120
+ * Set RemoteIP header correctly.
121
+ * On comment spam, send the offending IP to CloudFlare.
122
+ * Clean up DB on load.