Skip to content

Instantly share code, notes, and snippets.

@tsing
Created May 29, 2009 02:16
Show Gist options
  • Save tsing/119731 to your computer and use it in GitHub Desktop.
Save tsing/119731 to your computer and use it in GitHub Desktop.
<?php
if ($_SERVER["GEOWHY_CDN_ENABLED"] != "1")
{
return;
}
require_once dirname(__FILE__)."/global.php";
require_once dirname(__FILE__)."/plugin.php";
$geowhycdn = new Geowhy_CDN();
class Geowhy_CDN
{
private $gscripts = null;
private $wp_upload_url_path = null;
private $uploads_use_yearmonth_folders = null;
private $base_url = null;
private $domain = null;
private $dbfile = null;
private $dbhandle = null;
private $redirect_domains = null;
private $cdn_domains = null;
function __construct() {
$this->gscripts = array(
'dojo' => 'dojo.xd',
'jquery' => 'jquery.min',
'mootools' => 'mootools-yui-compressed',
'prototype' => 'prototype'
);
global $g_redirect_domains, $g_cdn_domains;
$this->redirect_domains = $g_redirect_domains;
$this->cdn_domains = $g_cdn_domains;
$this->domain = $_SERVER["SERVER_NAME"];
$wp_upload_dir = wp_upload_dir();
$this->wp_upload_url_path = $wp_upload_dir['baseurl'];
if (!$guessurl = site_url())
$guessurl = wp_guess_url();
$this->base_url = $guessurl;
$this->dbfile = $_SERVER["STATIC_MAP_FILENAME"];
$this->dbhandle = $this->open_db($this->dbfile);
if (is_resource($this->dbhandle))
{
$this->bind_actions();
$this->bind_filters();
}
}
private function bind_actions() {
$this->add_action("wp_default_scripts", "rewrite_wp_default_scripts", 900);
}
private function bind_filters() {
$this->add_filter("the_content", "media_url_rewrite", 1000);
$this->add_filter("stylesheet_uri", "filter_url", 100);
}
function add_hook($type, $name, $callable, $priority)
{
global $g_plugin_exceptions;
if (isset($g_plugin_exceptions[$type][$this->domain][$name]))
{
return;
}
else
{
$method = "add_".$type;
$method($name, array($this, $callable), $priority);
}
}
function add_filter($name, $callable, $priority)
{
$this->add_hook("filter", $name, $callable, $priority);
}
function add_action($name, $callable, $priority)
{
$this->add_hook("action", $name, $callable, $priority);
}
function filter_url($url)
{
return $this->replace_url($url);
}
function filter_js($url)
{
return $this->replace_url($url);
}
function filter_css($url)
{
return $this->replace_url($url);
}
function filter_theme($url)
{
if (preg_match("/^http[s]?:\/\/[^\/]+(\/.*)$/", $url, $matches))
{
$url = $matches[1];
}
return "http://static.geowhy.org/".$this->domain.$url;
}
function media_url_rewrite($data) {
$file_upload_url = $this->wp_upload_url_path.'/';
$file_upload_url = str_replace('/','\/',str_replace('//','//(www.)?(',$file_upload_url));
preg_match_all('/'.$file_upload_url.'[^"\']+)/',$data,$matches);
$patterns = array();
$replacements = array();
foreach($matches[0] as $urls) {
$replacements[] = $this->replace_url($urls);
$patterns[] = '/'.str_replace('/','\/',$urls).'/';
}
$data = preg_replace($patterns,$replacements,$data);
return $data;
}
function rewrite_wp_default_styles(&$wp_styles) {
foreach ($wp_styles->registered as $styleobj) {
$styleobj->src = $this->replace_url($this->base_url.$styleobj->src);
}
}
function rewrite_wp_default_scripts(&$wp_scripts) {
foreach($wp_scripts->registered as $scriptobj) {
/*if (array_key_exists($scriptobj->handle, $this->gscripts))
{
$libname = $scriptobj->handle;
$jsname = $this->gscripts[$libname];
$ver = $scriptobj->ver;
$transport = (is_ssl()) ? "https:" : "http"; //make it ssl if the site is ssl.
$scriptobj->src = "$transport://ajax.googleapis.com/ajax/libs/$libname/$ver/$jsname.js";
}
else
{*/
$scriptobj->src = $this->replace_url($this->base_url.$scriptobj->src);
/*}*/
}
}
function open_db($dbfile)
{
return dba_open($dbfile, "r", "db4");
}
function get_redirect_url($path)
{
$domain = $this->redirect_domains[str2int($path) % count($this->redirect_domains)];
return "http://".$domain."/".$path;
}
function get_cdn_url($path)
{
$domain = $this->cdn_domains[str2int($path) % count($this->cdn_domains)];
return "http://".$domain."/".$path;
}
function replace_url($url)
{
$pathinfo = pathinfo($url);
if (in_array($pathinfo["extension"], array("mp3", "wma", "mp4", "wav")))
{
return $url;
}
if (preg_match("/^(http[s]?:\/\/)([^\/]+\/.*)$/", $url, $matches))
{
$url = $matches[2];
}
else
{
$url = $this->domain."/".trim($url, "/");
}
$urlinfo = parse_url($url);
$url = $urlinfo["path"];
$url = trim($url);
$url = trim($url, "/");
$value = $this->is_url_loaded($url);
if (! empty($value))
{
return $this->get_cdn_url($value);
}
else
{
return $this->get_redirect_url($url);
}
}
function is_url_loaded($url)
{
$key = str_replace("/", "_", $url);
return dba_fetch($key, $this->dbhandle);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment