From 081e0b3dd97f2ab888d3dc76a8c2c60c6c61bfbf Mon Sep 17 00:00:00 2001 From: Tomasz Gemza Date: Wed, 9 Mar 2022 08:35:29 +0100 Subject: [PATCH] Added possibility to define access permissions to created attachment directories + small change in server encoding configuration definition --- DependencyInjection/Configuration.php | 8 ++++++++ README.md | 9 +++++++++ Service/Imap.php | 25 ++++++++++++++++++------- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 35e67f2..4e81533 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -45,9 +45,17 @@ public function getConfigTreeBuilder() ->end() ->scalarNode('attachments_dir') ->cannotBeEmpty() + ->defaultValue(null) + ->end() + ->booleanNode('create_attachments_dir_if_not_exists') + ->defaultTrue() + ->end() + ->integerNode('created_attachments_dir_permissions') + ->defaultValue(770) ->end() ->scalarNode('server_encoding') ->cannotBeEmpty() + ->defaultValue('UTF-8') ->end() ->end() ->end() diff --git a/README.md b/README.md index 9684cba..41ef25f 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,15 @@ imap: password: "password" attachments_dir: "%kernel.project_dir%/var/imap/attachments" server_encoding: "UTF-8" + + full_config_connection: + mailbox: "{localhost:143}INBOX" + username: "username" + password: "password" + attachments_dir: "%kernel.project_dir%/var/imap/attachments" + create_attachments_dir_if_not_exists: true # default true + created_attachments_dir_permissions: 777 # default 770 + server_encoding: "UTF-8" ``` If you're using Symfony to connect to a Microsoft 365 business environment, there's a good chance you'll want to connect to a shared mailbox. In that case you need to specify the parameters ```authuser``` and ```user```. Where *shared_account* is the username without domain, like: diff --git a/Service/Imap.php b/Service/Imap.php index 599b9fb..91e4d6c 100644 --- a/Service/Imap.php +++ b/Service/Imap.php @@ -91,15 +91,19 @@ protected function getMailbox($name) $config = $this->connections[$name]; if (isset($config['attachments_dir'])) { - $this->checkAttachmentsDir($config['attachments_dir']); + $this->checkAttachmentsDir( + $config['attachments_dir'], + $config['create_attachments_dir_if_not_exists'], + $config['created_attachments_dir_permissions'] + ); } return new Mailbox( $config['mailbox'], $config['username'], $config['password'], - isset($config['attachments_dir']) ? $config['attachments_dir'] : null, - isset($config['server_encoding']) ? $config['server_encoding'] : 'UTF-8' + $config['attachments_dir'], + $config['server_encoding'] ); } @@ -108,10 +112,11 @@ protected function getMailbox($name) * * @param null|string $directoryPath * @param bool $createIfNotExists + * @param int $directoryPermissions In decimal format! 775 instead of 0775 * * @throws \Exception */ - protected function checkAttachmentsDir($directoryPath, $createIfNotExists = true) + protected function checkAttachmentsDir($directoryPath, $createIfNotExists, $directoryPermissions) { if (!$directoryPath) { return; @@ -123,10 +128,16 @@ protected function checkAttachmentsDir($directoryPath, $createIfNotExists = true } if (!is_readable($directoryPath) || !is_writable($directoryPath)) { - throw new \Exception(sprintf('Directory "%s" does not have expected access permissions', $directoryPath)); + throw new \Exception(sprintf('Directory "%s" does not have enough access permissions', $directoryPath)); + } + } elseif($createIfNotExists) { + $umask = umask(0); + $created = mkdir($directoryPath, decoct($directoryPermissions), true); + umask($umask); + + if (!$created) { + throw new \Exception(sprintf('Cannot create the attachments directory "%s"', $directoryPath)); } - } elseif($createIfNotExists && !mkdir($directoryPath, 0770, true)) { - throw new \Exception(sprintf('Cannot create the attachments directory "%s"', $directoryPath)); } } }