Pony.fm/public/asset.php

71 lines
2.3 KiB
PHP
Raw Normal View History

2013-07-25 23:33:04 +02:00
<?php
if ((isset($_GET['file']) && strstr($_GET['file'], '..'))) {
exit();
}
require __DIR__.'/../bootstrap/autoload.php';
require __DIR__.'/../bootstrap/start.php';
use Assetic\Asset\AssetCache;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;
use Assetic\Cache\FilesystemCache;
use Assetic\Filter\CoffeeScriptFilter;
use Assetic\Filter\LessFilter;
use Assetic\Filter\UglifyCssFilter;
use Assetic\Filter\UglifyJs2Filter;
$bundle = null;
$cacheDirectory = storage_path() . '/cache';
if ($_GET['type'] == 'coffee') {
header('Content-Type: text/javascript');
if (!isset($_GET['file']) || !Config::get('app.debug')) {
$bundle = Assets::scriptAssetCollection($_GET['area']);
2013-09-01 10:52:05 +02:00
$bundle->ensureFilter(new UglifyJs2Filter(Config::get('app.uglify-js'), Config::get('app.node')));
2013-07-25 23:33:04 +02:00
$bundle->setTargetPath('scripts');
} else {
$filePath = trim($_GET['file'], '/');
2013-09-01 09:27:04 +02:00
$bundle = new AssetCollection([new FileAsset($filePath)], [new CoffeeScriptFilter(Config::get('app.coffee'), Config::get('app.node'))]);
2013-07-25 23:33:04 +02:00
$bundle->setTargetPath($filePath);
}
$bundle = new AssetCache($bundle, new FilesystemCache("$cacheDirectory/scripts"));
} else if ($_GET['type'] == 'less') {
header('Content-Type: text/css');
if (!isset($_GET['file']) || !Config::get('app.debug')) {
$bundle = Assets::styleAssetCollection($_GET['area']);
2013-09-01 10:52:05 +02:00
$bundle->ensureFilter(new UglifyCssFilter(Config::get('app.uglify-css'), Config::get('app.node')));
2013-07-25 23:33:04 +02:00
$bundle->setTargetPath('styles');
} else {
2013-09-01 11:17:53 +02:00
$node = Config::get('app.node');
if ($node == null)
$node = 'node';
2013-07-25 23:33:04 +02:00
$filePath = trim($_GET['file'], '/');
$lastModifiedCollection = new AssetCollection([new GlobAsset("styles/*.less")]);
2013-08-01 20:09:23 +02:00
$bundle = new AssetCollection([new FileAsset($filePath), new CacheBusterAsset($lastModifiedCollection->getLastModified())],
2013-09-01 11:17:53 +02:00
[new LessFilter($node, Config::get('app.node_paths'))]);
2013-07-25 23:33:04 +02:00
$bundle->setTargetPath($filePath);
}
$bundle = new AssetCache($bundle, new FilesystemCache("$cacheDirectory/styles"));
} else {
exit();
}
$time = gmdate($bundle->getLastModified());
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $time == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
header('HTTP/1.0 304 Not Modified');
exit();
}
header('Last-Modified: ' . $time);
header('Cache-Control: max-age=' . (60 * 60 * 24 * 7));
echo $bundle->dump();