2017-06-27 04:30:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
2017-06-28 06:59:41 +02:00
|
|
|
use InvalidArgumentException;
|
2017-09-21 07:54:06 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Manifest\ModuleResource;
|
2017-06-27 04:30:48 +02:00
|
|
|
use SilverStripe\Core\Manifest\ResourceURLGenerator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate URLs assuming that BASE_PATH is also the webroot
|
|
|
|
* Standard SilverStripe 3 operation
|
|
|
|
*/
|
|
|
|
class SimpleResourceURLGenerator implements ResourceURLGenerator
|
|
|
|
{
|
2017-09-21 07:54:06 +02:00
|
|
|
/**
|
|
|
|
* Rewrites applied after generating url.
|
|
|
|
* Note: requires either silverstripe/vendor-plugin-helper or silverstripe/vendor-plugin
|
|
|
|
* to ensure the file is available.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $url_rewrites = [
|
|
|
|
'#^vendor/#i' => 'resources/',
|
|
|
|
];
|
|
|
|
|
2017-06-27 04:30:48 +02:00
|
|
|
/*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $nonceStyle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the style of nonce-suffixes to use, or null if disabled
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getNonceStyle()
|
|
|
|
{
|
|
|
|
return $this->nonceStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the style of nonce-suffixes to use, or null to disable
|
|
|
|
* Currently only "mtime" is allowed
|
|
|
|
*
|
|
|
|
* @param string|null $nonceStyle The style of nonces to apply, or null to disable
|
2017-06-28 06:59:41 +02:00
|
|
|
* @return $this
|
2017-06-27 04:30:48 +02:00
|
|
|
*/
|
|
|
|
public function setNonceStyle($nonceStyle)
|
|
|
|
{
|
|
|
|
if ($nonceStyle && $nonceStyle !== 'mtime') {
|
|
|
|
throw new InvalidArgumentException('The only allowed NonceStyle is mtime');
|
|
|
|
}
|
|
|
|
$this->nonceStyle = $nonceStyle;
|
2017-06-28 06:59:41 +02:00
|
|
|
return $this;
|
2017-06-27 04:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the URL for a resource, prefixing with Director::baseURL() and suffixing with a nonce
|
|
|
|
*
|
2017-09-21 07:54:06 +02:00
|
|
|
* @param string|ModuleResource $relativePath File or directory path relative to BASE_PATH
|
2017-06-27 04:30:48 +02:00
|
|
|
* @return string Doman-relative URL
|
|
|
|
* @throws InvalidArgumentException If the resource doesn't exist
|
|
|
|
*/
|
|
|
|
public function urlForResource($relativePath)
|
|
|
|
{
|
2017-09-21 07:54:06 +02:00
|
|
|
if ($relativePath instanceof ModuleResource) {
|
|
|
|
// Load from module resource
|
|
|
|
$resource = $relativePath;
|
|
|
|
$relativePath = $resource->getRelativePath();
|
|
|
|
$exists = $resource->exists();
|
|
|
|
$absolutePath = $resource->getPath();
|
2018-01-23 05:31:43 +01:00
|
|
|
} else if (Director::is_absolute_url($relativePath)) {
|
|
|
|
// Path is not relative, and probably not of this site
|
|
|
|
return $relativePath;
|
2017-09-21 07:54:06 +02:00
|
|
|
} else {
|
|
|
|
// Use normal string
|
|
|
|
$absolutePath = preg_replace('/\?.*/', '', Director::baseFolder() . '/' . $relativePath);
|
|
|
|
$exists = file_exists($absolutePath);
|
|
|
|
}
|
|
|
|
if (!$exists) {
|
2017-06-27 04:30:48 +02:00
|
|
|
throw new InvalidArgumentException("File {$relativePath} does not exist");
|
|
|
|
}
|
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
// Apply url rewrites
|
|
|
|
$rules = Config::inst()->get(static::class, 'url_rewrites') ?: [];
|
|
|
|
foreach ($rules as $from => $to) {
|
|
|
|
$relativePath = preg_replace($from, $to, $relativePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply nonce
|
2017-06-27 04:30:48 +02:00
|
|
|
$nonce = '';
|
2017-06-28 06:59:41 +02:00
|
|
|
// Don't add nonce to directories
|
|
|
|
if ($this->nonceStyle && is_file($absolutePath)) {
|
2017-06-27 04:30:48 +02:00
|
|
|
$nonce = (strpos($relativePath, '?') === false) ? '?' : '&';
|
|
|
|
|
|
|
|
switch ($this->nonceStyle) {
|
|
|
|
case 'mtime':
|
|
|
|
$nonce .= "m=" . filemtime($absolutePath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Director::baseURL() . $relativePath . $nonce;
|
|
|
|
}
|
|
|
|
}
|