2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Assets\Flysystem;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
use League\Flysystem\Adapter\Local;
|
2016-08-19 00:51:35 +02:00
|
|
|
use League\Flysystem\Config as FlysystemConfig;
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
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
|
|
|
*/
|
|
|
|
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
|
2016-08-19 00:51:35 +02:00
|
|
|
$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
|
2016-08-19 00:51:35 +02:00
|
|
|
* @throws \Exception
|
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
|
2016-08-19 00:51:35 +02:00
|
|
|
$rules = Config::inst()->get(get_class($this), 'server_configuration', Config::FIRST_SET);
|
2015-12-09 22:19:23 +01:00
|
|
|
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
|
2016-08-19 00:51:35 +02:00
|
|
|
$config = new FlysystemConfig();
|
2015-12-09 22:19:23 +01:00
|
|
|
$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-08-19 00:51:35 +02:00
|
|
|
* @return string 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();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach(File::config()->allowed_extensions as $extension) {
|
2015-12-09 22:19:23 +01:00
|
|
|
if($extension) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$allowedExtensions->push(new ArrayData(array(
|
2015-12-09 22:19:23 +01:00
|
|
|
'Extension' => preg_quote($extension)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$viewer = new SSViewer(array($template));
|
|
|
|
return (string)$viewer->process(new ArrayData(array(
|
2015-12-09 22:19:23 +01:00
|
|
|
'AllowedExtensions' => $allowedExtensions
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|