Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image support to the text editor #6935

Merged
merged 19 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/Db/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@
* @method string|null getSignature()
* @method void setProvisioningId(int $provisioningId)
* @method int|null getProvisioningId()
* @method int getSignatureMode()
* @method void setSignatureMode(int $signatureMode)
*/
class Alias extends Entity implements JsonSerializable {
public const SIGNATURE_MODE_PLAIN = MailAccount::SIGNATURE_MODE_PLAIN;
public const SIGNATURE_MODE_HTML = MailAccount::SIGNATURE_MODE_HTML;

/** @var int */
protected $accountId;
Expand All @@ -56,11 +60,15 @@ class Alias extends Entity implements JsonSerializable {
/** @var int|null */
protected $provisioningId;

/** @var integer */
protected $signatureMode;

public function __construct() {
$this->addType('accountId', 'int');
$this->addType('name', 'string');
$this->addType('alias', 'string');
$this->addType('provisioningId', 'int');
$this->addType('signatureMode', 'int');
}

public function isProvisioned(): bool {
Expand All @@ -75,6 +83,7 @@ public function jsonSerialize() {
'alias' => $this->getAlias(),
'signature' => $this->getSignature(),
'provisioned' => $this->isProvisioned(),
'signatureMode' => $this->getSignatureMode(),
];
}
}
9 changes: 9 additions & 0 deletions lib/Db/MailAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@
* @method void setSignatureAboveQuote(bool $signatureAboveQuote)
* @method string getAuthMethod()
* @method void setAuthMethod(string $method)
* @method int getSignatureMode()
* @method void setSignatureMode(int $signatureMode)
*/
class MailAccount extends Entity {
public const SIGNATURE_MODE_PLAIN = 0;
public const SIGNATURE_MODE_HTML = 1;

protected $userId;
protected $name;
protected $email;
Expand Down Expand Up @@ -143,6 +148,8 @@ class MailAccount extends Entity {
/** @var int|null */
protected $provisioningId;

/** @var int */
protected $signatureMode;

/**
* @param array $params
Expand Down Expand Up @@ -209,6 +216,7 @@ public function __construct(array $params = []) {
$this->addType('sieveEnabled', 'boolean');
$this->addType('sievePort', 'integer');
$this->addType('signatureAboveQuote', 'boolean');
$this->addType('signatureMode', 'int');
}

/**
Expand All @@ -235,6 +243,7 @@ public function toJson() {
'trashMailboxId' => $this->getTrashMailboxId(),
'sieveEnabled' => ($this->isSieveEnabled() === true),
'signatureAboveQuote' => ($this->isSignatureAboveQuote() === true),
'signatureMode' => $this->getSignatureMode(),
];

if (!is_null($this->getOutboundHost())) {
Expand Down
32 changes: 32 additions & 0 deletions lib/Exception/InvalidDataUriException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* @author 2022 Daniel Kesselberg <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Exception;

use Exception;

class InvalidDataUriException extends Exception {
public function __construct() {
parent::__construct('Invalid data uri');
}
}
126 changes: 126 additions & 0 deletions lib/Migration/Version1140Date20220628174152.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Daniel Kesselberg <[email protected]>
*
* @author Daniel Kesselberg <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Mail\Migration;

use Closure;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Types\Types;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version1140Date20220628174152 extends SimpleMigrationStep {
private IDBConnection $connection;

public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

/*
* Increase size limit for signature column.
*
* Initially the signature column was created with length = 1024.
* On mysql/mariadb the column is able to store 65535 bytes.
*
* To create a column with type longtext length must be null (or an integer bigger than 16777215):
* https://github.com/nextcloud/3rdparty/blob/2ae1a1d6f688ae8394d6559ee673fecbee975db4/doctrine/dbal/src/Platforms/MySQLPlatform.php#L237-L265
*
* Length option is only relevant for MySQL/MariaDB. Postgre, Oracle and Sqlite don't have a
* concept like tinytext, mediumtext, text and longtext.
*
* Postgre: https://www.postgresql.org/docs/9.1/datatype-character.html
* Oracle: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlqr/Data-Types.html#GUID-219C338B-FE60-422A-B196-2F0A01CAD9A4
* Sqlite: https://www.sqlite.org/datatype3.html / https://www.sqlite.org/limits.html
*
* To make it worse our doctrine version (3.1.6 for Nextcloud 24) is missing the logic to detect
* that the column length changed: https://github.com/doctrine/dbal/issues/2566
*/

if ($this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
$alterQuery = "ALTER TABLE `%s` MODIFY `%s` longtext null;";

$accountsTable = $schema->getTable('mail_accounts');
$accountsSignatureColumn = $accountsTable->getColumn('signature');

$this->connection->executeStatement(
sprintf($alterQuery, $accountsTable->getName(), $accountsSignatureColumn->getName())
);

$aliasesTable = $schema->getTable('mail_aliases');
$aliasesSignatureColumn = $accountsTable->getColumn('signature');

$this->connection->executeStatement(
sprintf($alterQuery, $aliasesTable->getName(), $aliasesSignatureColumn->getName())
);

unset(
$accountsTable,
$accountsSignatureColumn,
$aliasesTable,
$aliasesSignatureColumn
);
}
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$accountsTable = $schema->getTable('mail_accounts');
$aliasesTables = $schema->getTable('mail_aliases');

if (!$accountsTable->hasColumn('signature_mode')) {
$accountsTable->addColumn('signature_mode', Types::SMALLINT, [
'default' => 0,
]);
}

if (!$aliasesTables->hasColumn('signature_mode')) {
$aliasesTables->addColumn('signature_mode', Types::SMALLINT, [
'default' => 0,
]);
}

return $schema;
}
}
54 changes: 54 additions & 0 deletions lib/Service/DataUri/DataUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* @author 2022 Daniel Kesselberg <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Service\DataUri;

class DataUri {
private string $mediaType;
private array $parameters;
private bool $base64;
private string $data;

public function __construct(string $mediaType, array $parameters, bool $base64, $data) {
$this->mediaType = $mediaType;
$this->parameters = $parameters;
$this->base64 = $base64;
$this->data = $data;
}

public function getMediaType(): string {
return $this->mediaType;
}

public function getParameters(): array {
return $this->parameters;
}

public function isBase64(): bool {
return $this->base64;
}

public function getData(): string {
return $this->data;
}
}
73 changes: 73 additions & 0 deletions lib/Service/DataUri/DataUriParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/**
* @author 2022 Daniel Kesselberg <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Service\DataUri;

use OCA\Mail\Exception\InvalidDataUriException;

class DataUriParser {
private const PATTERN = '#^data:(?<media_type>[^,.]*),(?<data>.*)$#';

/**
* @throws InvalidDataUriException
*/
public function parse(string $dataUri): DataUri {
$matches = [];

if (preg_match(self::PATTERN, $dataUri, $matches) !== 1) {
throw new InvalidDataUriException();
}

if ($matches['media_type'] === '') {
$items = [];
} else {
$items = explode(';', $matches['media_type']);
}

$mediaType = 'text/plain';
$parameters = ['charset' => 'US-ASCII'];
$base64 = false;

if (count($items) > 0) {
$mediaType = array_shift($items);
foreach ($items as $item) {
if ($item === 'base64') {
$base64 = true;
continue;
}

if (str_contains($item, '=')) {
[$key, $value] = explode('=', $item);
$parameters[$key] = $value;
}
}
}

return new DataUri(
$mediaType,
$parameters,
$base64,
$matches['data']
);
}
}
9 changes: 8 additions & 1 deletion lib/Service/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ public function sanitizeHtmlMailBody(string $mailBody, array $messageParameters,
$uri = $config->getDefinition('URI');
$uri->addFilter(new TransformURLScheme($messageParameters, $mapCidToAttachmentId, $this->urlGenerator, $this->request), $config);

HTMLPurifier_URISchemeRegistry::instance()->register('cid', new CidURIScheme());
$uriSchemeRegistry = HTMLPurifier_URISchemeRegistry::instance();
$uriSchemeRegistry->register('cid', new CidURIScheme());

$uriSchemaData = new \HTMLPurifier_URIScheme_data();
$uriSchemaData->allowed_types['image/bmp'] = true;
$uriSchemaData->allowed_types['image/tiff'] = true;
$uriSchemaData->allowed_types['image/webp'] = true;
$uriSchemeRegistry->register('data', $uriSchemaData);

$purifier = new HTMLPurifier($config);

Expand Down
Loading