From 4d80e4b1132ee06eb3f9c92a78fec1db82aa883d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Oli=C5=A1ar?= Date: Fri, 27 Feb 2015 14:11:56 +0100 Subject: [PATCH] Added configurable permission for creating folders --- readme.md | 9 +++++++++ src/Extension.php | 2 ++ src/Generator.php | 28 +++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index aecc664..7b7b3be 100644 --- a/readme.md +++ b/readme.md @@ -55,6 +55,15 @@ webimages: > By default all these routes will be prepended before your other routes - assuming you use `Nette\Application\Routers\RouteList` as your root router. You can disable this by setting `prependRoutesToRouter: false`. Then it's your responsibility to plug webimages router (service `webimages.router`) to your routing implementation. +If you need custom permission for creating folders, you can set the permission in config: + +``` +webimages: + umask: 0777 +``` + +> Custom permission implementation uses umask + Addon gives you new macro `n:src`. Now you're ready to use it. ```html diff --git a/src/Extension.php b/src/Extension.php index 49aaba5..18cc9b3 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -14,6 +14,7 @@ class Extension extends DI\CompilerExtension /** @var array */ private $defaults = [ + 'umask' => FALSE, 'routes' => [], 'prependRoutesToRouter' => TRUE, 'rules' => [], @@ -35,6 +36,7 @@ public function loadConfiguration() $generator = $container->addDefinition($this->prefix('generator')) ->setClass('DotBlue\WebImages\Generator', [ $config['wwwDir'], + $config['umask'], ]); foreach ($config['rules'] as $rule) { diff --git a/src/Generator.php b/src/Generator.php index 63c894e..88491af 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -33,12 +33,16 @@ class Generator extends Nette\Object /** @var IProvider[] */ private $providers = []; + + /** @var integer */ + private $umask; - public function __construct($wwwDir, Http\IRequest $httpRequest, Http\IResponse $httpResponse, Validator $validator) + public function __construct($wwwDir, $umask, Http\IRequest $httpRequest, Http\IResponse $httpResponse, Validator $validator) { $this->wwwDir = $wwwDir; + $this->umask = $umask; $this->httpRequest = $httpRequest; $this->httpResponse = $httpResponse; $this->validator = $validator; @@ -91,10 +95,7 @@ public function generateImage(ImageRequest $request) $dirname = dirname($destination); if (!is_dir($dirname)) { - $success = @mkdir($dirname, 0777, TRUE); - if (!$success) { - throw new Application\BadRequestException; - } + $this->createFolder($dirname); } $success = $image->save($destination, 90, $format); @@ -105,5 +106,22 @@ public function generateImage(ImageRequest $request) $image->send(); exit; } + + + + private function createFolder($dirname) + { + if ($this->umask) { + $oldmask = umask(0); + $success = @mkdir($dirname, octdec($this->umask), TRUE); + umask($oldmask); + } else { + $success = @mkdir($dirname, 0777, TRUE); + } + + if (!$success) { + throw new Application\BadRequestException; + } + } }