2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Filesystem\Flysystem;
|
|
|
|
|
|
|
|
use League\Flysystem\Adapter\Local;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-06-03 10:34:59 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-09 22:19:23 +01:00
|
|
|
* Adapter for local filesystem based on assets directory
|
2015-09-03 07:46:08 +02:00
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage filesystem
|
|
|
|
*/
|
|
|
|
class AssetAdapter extends Local {
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Server specific configuration necessary to block http traffic to a local folder
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array Mapping of server configurations to configuration files necessary
|
|
|
|
*/
|
|
|
|
private static $server_configuration = array();
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
/**
|
|
|
|
* Config compatible permissions configuration
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $file_permissions = array(
|
|
|
|
'file' => [
|
|
|
|
'public' => 0744,
|
|
|
|
'private' => 0700,
|
|
|
|
],
|
|
|
|
'dir' => [
|
|
|
|
'public' => 0755,
|
|
|
|
'private' => 0700,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS) {
|
2015-11-30 03:03:46 +01:00
|
|
|
// Get root path
|
2015-12-09 22:19:23 +01:00
|
|
|
$root = $this->findRoot($root);
|
2015-11-30 03:03:46 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Override permissions with config
|
|
|
|
$permissions = \Config::inst()->get(get_class($this), 'file_permissions');
|
2015-11-30 03:03:46 +01:00
|
|
|
parent::__construct($root, $writeFlags, $linkHandling, $permissions);
|
2015-12-09 22:19:23 +01:00
|
|
|
|
|
|
|
// Configure server
|
|
|
|
$this->configureServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the root folder absolute system path
|
|
|
|
*
|
|
|
|
* @param string $root
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function findRoot($root) {
|
|
|
|
// Empty root will set the path to assets
|
|
|
|
if (!$root) {
|
|
|
|
throw new \InvalidArgumentException("Missing argument for root path");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Substitute leading ./ with BASE_PATH
|
|
|
|
if(strpos($root, './') === 0) {
|
|
|
|
return BASE_PATH . substr($root, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Substitute leading ./ with parent of BASE_PATH, in case storage is outside of the webroot.
|
|
|
|
if(strpos($root, '../') === 0) {
|
|
|
|
return dirname(BASE_PATH) . substr($root, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $root;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-09 22:19:23 +01:00
|
|
|
* Force flush and regeneration of server files
|
|
|
|
*/
|
|
|
|
public function flush() {
|
|
|
|
$this->configureServer(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure server files for this store
|
2015-09-03 07:46:08 +02:00
|
|
|
*
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param bool $forceOverwrite Force regeneration even if files already exist
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
protected function configureServer($forceOverwrite = false) {
|
|
|
|
// Get server type
|
|
|
|
$type = isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '*';
|
|
|
|
list($type) = explode('/', strtolower($type));
|
2015-09-03 07:46:08 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
// Determine configurations to write
|
|
|
|
$rules = \Config::inst()->get(get_class($this), 'server_configuration', \Config::FIRST_SET);
|
|
|
|
if(empty($rules[$type])) {
|
|
|
|
return;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
$configurations = $rules[$type];
|
2015-09-03 07:46:08 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
// Apply each configuration
|
|
|
|
$config = new \League\Flysystem\Config();
|
|
|
|
$config->set('visibility', 'private');
|
|
|
|
foreach($configurations as $file => $template) {
|
|
|
|
if ($forceOverwrite || !$this->has($file)) {
|
|
|
|
// Evaluate file
|
|
|
|
$content = $this->renderTemplate($template);
|
|
|
|
$success = $this->write($file, $content, $config);
|
|
|
|
if(!$success) {
|
|
|
|
throw new \Exception("Error writing server configuration file \"{$file}\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render server configuration file from a template file
|
|
|
|
*
|
|
|
|
* @param string $template
|
2016-06-03 10:34:59 +02:00
|
|
|
* @return DBHTMLText Rendered results
|
2015-12-09 22:19:23 +01:00
|
|
|
*/
|
|
|
|
protected function renderTemplate($template) {
|
|
|
|
// Build allowed extensions
|
2016-06-15 06:03:16 +02:00
|
|
|
$allowedExtensions = new ArrayList();
|
2015-12-09 22:19:23 +01:00
|
|
|
foreach(\File::config()->allowed_extensions as $extension) {
|
|
|
|
if($extension) {
|
|
|
|
$allowedExtensions->push(new \ArrayData(array(
|
|
|
|
'Extension' => preg_quote($extension)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$viewer = new \SSViewer(array($template));
|
|
|
|
return (string)$viewer->process(new \ArrayData(array(
|
|
|
|
'AllowedExtensions' => $allowedExtensions
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|