Skip to content

Commit

Permalink
Added possibility to define access permissions to created attachment …
Browse files Browse the repository at this point in the history
…directories + small change in server encoding configuration definition
  • Loading branch information
secit-pl committed Mar 9, 2022
1 parent fc162b1 commit 081e0b3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
8 changes: 8 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
25 changes: 18 additions & 7 deletions Service/Imap.php
Expand Up @@ -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']
);
}

Expand All @@ -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;
Expand All @@ -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));
}
}
}

0 comments on commit 081e0b3

Please sign in to comment.