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 630c3ee
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 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 @@ -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,33 @@ 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 {
// 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');
$this->setter('textContent', [$content]);
}

public function getTextContent(): string {

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

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnType

lib/Db/Bookmark.php:132: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 134 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnStatement

lib/Db/Bookmark.php:134: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 {
// 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');
$this->setter('htmlContent', [$content]);
}

public function getHtmlContent(): string {

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

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnType

lib/Db/Bookmark.php:143: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 145 in lib/Db/Bookmark.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidReturnStatement

lib/Db/Bookmark.php:145: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 630c3ee

Please sign in to comment.