Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Media/CDN server w/ Symfony1
# You can find more information about this file on the symfony website:
# http://www.symfony-project.org/reference/1_4/en/12-Filters
rendering: ~
remember_me:
class: sfGuardRememberMeFilter
security: ~
# insert your own filters here
mediaServer:
class: MediaServerFilter
cache: ~
execution: ~
<?php
class MediaServerFilter extends sfFilter
{
public function execute($filterChain)
{
$filterChain->execute();
// Execute this filter only once
if ($this->isFirstCall() && sfConfig::get('app_media_filter_disable', false) == false) {
// Filters don't have direct access to the request and user objects.
// You will need to use the context object to get them
$site = dcmsContext::getSite();
$response = $this->getContext()->getResponse();
$cont = $response->getContent();
$host = $site->hostname;
if (preg_match('/^[a-z0-9]+\.yourdomain\.com/', $host)) {
$host = preg_replace('/^[0-9a-z]+\./', 'media.', $host);
}
$cont = str_replace('"/css/', '"http://' . $host . '/css/', $cont);
$cont = str_replace('"/js/', '"http://' . $host . '/js/', $cont);
$cont = str_replace('"/images/', '"http://' . $host . '/images/', $cont);
$cont = str_replace('url(/media_cache/', 'url(http://' . $host . '/media_cache/', $cont);
$cont = str_replace('"/media_cache/', '"http://' . $host . '/media_cache/', $cont);
$cont = str_replace('"/cache/', '"http://' . $host . '/cache/', $cont);
$cont = str_replace('\'/media_cache/', '\'http://' . $host . '/media_cache/', $cont);
$cont = str_replace('\'/media/', '\'http://' . $host . '/media/', $cont);
$cont = str_replace('\'/flash/', '\'http://' . $host . '/flash/', $cont);
// conflicts with far-future expires.
//$cont = preg_replace_callback('/'.preg_quote($host).'/', function($matches) { return str_replace('media', 'media'.rand(1, 5), $matches[0]); }, $cont);
$response->setContent($cont);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment