Skip to content

Commit

Permalink
fix(Db\Bookmark): Fix UTF-8 encoding ofuser-facing content
Browse files Browse the repository at this point in the history
see #2203

Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Aug 5, 2024
1 parent c6e572a commit b029d03
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lib/Db/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
* @method int getArchivedFile()
* @method setArchivedFile(int $fileId)
* @method string getTextContent()
* @method setTextContent(string $content)
* @method string getHtmlContent()
* @method setHtmlContent(string $content)
* @method string getUserId()
* @method setUserId(string $userId)
*/
Expand Down Expand Up @@ -92,7 +90,7 @@ public function toArray(): array {
$array['target'] = $this->url;
continue;
}
$array[$field] = $this->{$field};
$array[$field] = $this->{'get' . $field}();
}
return $array;
}
Expand All @@ -110,6 +108,8 @@ public function setTitle(string $title): void {
if (strlen($title) > 1024) {
$title = substr($title, 0, 1020) . '';
}
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
$title = mb_convert_encoding($title, 'UTF-8', 'UTF-8');
$this->setter('title', [$title]);
}

Expand All @@ -118,9 +118,37 @@ public function setDescription(string $desc): void {
if (strlen($desc) > 1024) {
$desc = substr($desc, 0, 1020) . '';
}
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
$desc = mb_convert_encoding($desc, 'UTF-8', 'UTF-8');
$this->setter('description', [$desc]);
}

public function setTextContent(?string $content): void {
if ($content === null) {
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
$content = mb_convert_encoding($content, 'UTF-8', 'UTF-8');

Check failure on line 129 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

NullArgument

lib/Db/Bookmark.php:129:35: NullArgument: Argument 1 of mb_convert_encoding cannot be null, null value provided to parameter with type array<array-key, mixed>|string (see https://psalm.dev/057)
}
$this->setter('textContent', [$content]);
}

public function getTextContent(): string {

Check failure on line 134 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnType

lib/Db/Bookmark.php:134:36: InvalidReturnType: The declared return type 'string' for OCA\Bookmarks\Db\Bookmark::getTextContent is incorrect, got 'array<array-key, mixed|string>|false|string' (see https://psalm.dev/011)
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
return mb_convert_encoding($this->textContent, 'UTF-8', 'UTF-8');

Check failure on line 136 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnStatement

lib/Db/Bookmark.php:136:10: InvalidReturnStatement: The inferred type 'array<array-key, mixed|string>|false|string' does not match the declared return type 'string' for OCA\Bookmarks\Db\Bookmark::getTextContent (see https://psalm.dev/128)
}

public function setHtmlContent(?string $content): void {
if ($content === null) {
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
$content = mb_convert_encoding($content, 'UTF-8', 'UTF-8');

Check failure on line 142 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

NullArgument

lib/Db/Bookmark.php:142:35: NullArgument: Argument 1 of mb_convert_encoding cannot be null, null value provided to parameter with type array<array-key, mixed>|string (see https://psalm.dev/057)
}
$this->setter('htmlContent', [$content]);
}

public function getHtmlContent(): string {

Check failure on line 147 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnType

lib/Db/Bookmark.php:147:36: InvalidReturnType: The declared return type 'string' for OCA\Bookmarks\Db\Bookmark::getHtmlContent is incorrect, got 'array<array-key, mixed|string>|false|string' (see https://psalm.dev/011)
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
return mb_convert_encoding($this->htmlContent, 'UTF-8', 'UTF-8');

Check failure on line 149 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnStatement

lib/Db/Bookmark.php:149:10: InvalidReturnStatement: The inferred type 'array<array-key, mixed|string>|false|string' does not match the declared return type 'string' for OCA\Bookmarks\Db\Bookmark::getHtmlContent (see https://psalm.dev/128)
}

public function isWebLink() {
return (bool) preg_match('/^https?:/i', $this->getUrl());
}
Expand Down

0 comments on commit b029d03

Please sign in to comment.