CDN Rewrites Plugin for WordPress
I had been using Apache mod_rewrite to do redirects to CoralCDN, but all of the extra requests seemed to have been destroying page load performance. I have opted for rewriting the static content references with the CDN Rewrites WordPress plugin. It almost got the job done. I didn’t want it rewriting anything for requests local to the server, so I had to make some changes…
I just added a new column to the table for regular expressions to run against REMOTE_ADDR and REMOTE_HOST. The regex input gets its backslashes escaped somewhere and I didn’t bother to fix it, so that’s currently a feature.
Also pretty important is that this is not compatible with the old schema. It won’t bother to automatically update it if you already use CDN Rewrites, either. You can either drop the table and reconfigure the profiles or add the column manually.
I included a minor optimization for the tag location process. It was searching for all of the static content whether or not you had the checkboxes set, so now it’s lazy about it.
diff -u -b -B -N -r --exclude CVS -ur cdn-rewrites.orig/cdnr.class.php cdn-rewrites/cdnr.class.php
--- cdn-rewrites.orig/cdnr.class.php 2009-12-06 09:52:28.000000000 -0600
+++ cdn-rewrites/cdnr.class.php 2009-12-08 22:14:50.000000000 -0600
@@ -136,16 +136,21 @@
require_once(dirname(__FILE__) . '/simple_html_dom.php');
$this->document = str_get_html($html);
- $css_tags = $this->document->find("link[rel='stylesheet']");
- $js_tags = $this->document->find('script[src]');
- $img_tags = $this->document->find('img');
- $embed_tags = array(
- 'param' => $this->document->find("param[name='filename']"),
- 'embed' => $this->document->find('embed[src]'),
- );
-
foreach ($active_profiles as $p)
{
+ $p->clientexcludes = unserialize($p->clientexcludes);
+ // CDN_Rewrites_Helper::log($p->clientexcludes);
+
+ if ($this->is_client_excluded($_SERVER['REMOTE_ADDR'], $p->clientexcludes)) {
+ // CDN_Rewrites_Helper::log("\n\nSkipping profile {$p->name}: client {$_SERVER['REMOTE_ADDR']} excluded");
+ continue;
+ }
+
+ if (isset($_SERVER['REMOTE_HOST']) && $this->is_client_excluded($_SERVER['REMOTE_HOST'], $p->clientexcludes)) {
+ // CDN_Rewrites_Helper::log("\n\nSkipping profile {$p->name}: client {$_SERVER['REMOTE_HOST']} excluded");
+ continue;
+ }
+
// CDN_Rewrites_Helper::log("\n\nApplying profile {$p->name}: {$p->origin} to {$p->dest}");
$p->apply = explode(',', $p->apply);
@@ -155,6 +160,10 @@
// external stylesheets
if (in_array('css', $p->apply))
{
+ if (!isset($css_tags)) {
+ $css_tags = $this->document->find("link[rel='stylesheet']");
+ }
+
foreach ($css_tags as $css)
{
if (!$this->is_excluded($css->href, $p->excludes))
@@ -169,6 +178,10 @@
if (in_array('js', $p->apply))
{
+ if (!isset($js_tags)) {
+ $js_tags = $this->document->find('script[src]');
+ }
+
foreach ($js_tags as $js)
{
if (!$this->is_excluded($js->src, $p->excludes))
@@ -181,6 +194,10 @@
// images
if (in_array('img', $p->apply))
{
+ if (!isset($img_tags)) {
+ $img_tags = $this->document->find('img');
+ }
+
foreach ($img_tags as $img)
{
if (!$this->is_excluded($img->src, $p->excludes))
@@ -193,6 +210,13 @@
// embeded
if (in_array('embed', $p->apply))
{
+ if (!isset($embed_tags)) {
+ $embed_tags = array(
+ 'param' => $this->document->find("param[name='filename']"),
+ 'embed' => $this->document->find('embed[src]'),
+ );
+ }
+
// 1. <object> tag
foreach ($embed_tags['param'] as $param)
{
@@ -286,6 +310,21 @@
/**
* @desc
*/
+ function is_client_excluded($clientaddr, $clientexcludes)
+ {
+ if (!is_array($clientexcludes)) return false;
+
+ foreach ($clientexcludes as $clientexclude)
+ {
+ if (preg_match('/'.$clientexclude.'/', $clientaddr)) {
+ // CDN_Rewrites_Helper::log("\n\nClient matched {$clientexclude}: client {$clientaddr} excluded");
+ return true;
+ }
+ }
+
+ return false;
+ }
+
function is_excluded($url, $excludes)
{
if (!is_array($excludes)) return false;
diff -u -b -B -N -r --exclude CVS -ur cdn-rewrites.orig/profile.class.php cdn-rewrites/profile.class.php
--- cdn-rewrites.orig/profile.class.php 2009-12-06 09:52:28.000000000 -0600
+++ cdn-rewrites/profile.class.php 2009-12-08 21:12:33.000000000 -0600
@@ -28,6 +28,7 @@
`dest` varchar(255) NOT NULL,
`apply` varchar(255) NOT NULL,
`excludes` text NOT NULL,
+ `clientexcludes` text NOT NULL,
`enabled` tinyint(4) NOT NULL default '1',
PRIMARY KEY (`id`)
);";
@@ -139,6 +140,10 @@
<label for="excludes">Exclude these URL\'s or those start with one of them (one per line)</label><br />
<textarea name="excludes" id="excludes" style="width: 600px; height: 100px"></textarea>
</li>
+ <li>
+ <label for="clientexcludes">Exclude these clients from rewrites (one regex per line)</label><br />
+ <textarea name="clientexcludes" id="clientexcludes" style="width: 600px; height: 100px"></textarea>
+ </li>
<li><input type="checkbox" name="enabled" id="enabled" checked="checked"><label for="enabled">This profile is Active</label></li>
</ul>
<input type="hidden" name="_nonce" value="%s" />
@@ -156,13 +161,13 @@
$msg = $this->validate();
if (!empty($msg)) CDN_Rewrites_Helper::ajax_error('<p>Profile NOT saved.</p><ul><li>' . implode('</li><li>', $msg) . '</li></ul>');
- list($name, $origin, $dest, $apply, $excludes, $enabled) = $this->parse_post_data();
+ list($name, $origin, $dest, $apply, $excludes, $clientexcludes, $enabled) = $this->parse_post_data();
global $wpdb;
- $sql = $wpdb->prepare("INSERT INTO `{$this->config['profiles_table_name']}`(`name`, `origin`, `dest`, `apply`, `excludes`, `enabled`)
- VALUES(%s, %s, %s, %s, %s, %d)",
- $name, $origin, $dest, $apply, $excludes, $enabled);
+ $sql = $wpdb->prepare("INSERT INTO `{$this->config['profiles_table_name']}`(`name`, `origin`, `dest`, `apply`, `excludes`, `clientexcludes`, `enabled`)
+ VALUES(%s, %s, %s, %s, %s, %s, %d)",
+ $name, $origin, $dest, $apply, $excludes, $clientexcludes, $enabled);
// CDN_Rewrites_Helper::log($sql);
@@ -203,6 +208,7 @@
$p->apply = explode(',', $p->apply);
$p->excludes = unserialize($p->excludes);
+ $p->clientexcludes = unserialize($p->clientexcludes);
printf(
'
@@ -226,6 +232,10 @@
<label for="excludesE">Exclude these URL\'s or those start with one of them (one per line)</label><br />
<textarea name="excludes" id="excludesE" style="width: 600px; height: 100px">%s</textarea>
</li>
+ <li>
+ <label for="clientexcludesE">Exclude these clients from rewrites (one regex per line)</label><br />
+ <textarea name="clientexcludes" id="clientexcludesE" style="width: 600px; height: 100px">%s</textarea>
+ </li>
<li><input %s type="checkbox" name="enabled" id="enabledE"><label for="enabledE">This profile is Active</label></li>
</ul>
<input type="hidden" name="id" value="%s" />
@@ -247,6 +257,7 @@
in_array('bgr', $p->apply) ? 'checked="checked"' : '',
in_array('embed', $p->apply) ? 'checked="checked"' : '',
is_array($p->excludes) ? implode("\n", $p->excludes) : '',
+ is_array($p->clientexcludes) ? implode("\n", $p->clientexcludes) : '',
$p->enabled ? 'checked="checked"' : '',
$p->id,
wp_create_nonce($this->config['plugin_name']));
@@ -257,14 +268,14 @@
$msg = $this->validate();
if (!empty($msg)) CDN_Rewrites_Helper::ajax_error('<p>Profile NOT saved.</p><ul><li>' . implode('</li><li>', $msg) . '</li></ul>');
- list($name, $origin, $dest, $apply, $excludes, $enabled) = $this->parse_post_data();
+ list($name, $origin, $dest, $apply, $excludes, $clientexcludes, $enabled) = $this->parse_post_data();
global $wpdb;
$sql = $wpdb->prepare("UPDATE `{$this->config['profiles_table_name']}`
- SET `name`=%s, `origin`=%s, `dest`=%s, `apply`=%s, `excludes`=%s, `enabled`=%d
+ SET `name`=%s, `origin`=%s, `dest`=%s, `apply`=%s, `excludes`=%s, `clientexcludes`=%s, `enabled`=%d
WHERE `id`=%d",
- $name, $origin, $dest, $apply, $excludes, $enabled,
+ $name, $origin, $dest, $apply, $excludes, $clientexcludes, $enabled,
$id);
// CDN_Rewrites_Helper::log($sql);
@@ -314,8 +325,9 @@
$apply = implode(',', $apply);
$excludes = serialize(explode("\n", trim($_POST['excludes'])));
+ $clientexcludes = serialize(explode("\n", trim($_POST['clientexcludes'])));
$enabled = isset($_POST['enabled']);
- return array($name, $origin, $dest, $apply, $excludes, $enabled);
+ return array($name, $origin, $dest, $apply, $excludes, $clientexcludes, $enabled);
}
}
\ No newline at end of file

CDN Rewrites Plugin for WordPress by cum grano salis — nobody likes a clever bastard, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.

Leave a Reply
You must be logged in to post a comment.