Skip to content

Commit

Permalink
Merge pull request #2204 from nextcloud/fix/utf-8-charset-fixes
Browse files Browse the repository at this point in the history
fix(Db\Bookmark): Fix UTF-8 encoding of user-facing content
  • Loading branch information
marcelklehr authored Sep 7, 2024
2 parents 508d9d6 + fa12da2 commit 63a92f0
Show file tree
Hide file tree
Showing 30 changed files with 108 additions and 82 deletions.
10 changes: 5 additions & 5 deletions lib/Activity/ActivityPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function publishShare(ChangeEvent $event): void {
$activity->setTimestamp(time());

/**
* @var $sharedFolder SharedFolder
* @var SharedFolder $sharedFolder
*/
try {
$sharedFolder = $this->sharedFolderMapper->find($event->getId());
Expand Down Expand Up @@ -141,7 +141,7 @@ public function publishFolder(ChangeEvent $event): void {
$activity->setTimestamp(time());

/**
* @var $folder Folder
* @var Folder $folder
*/
try {
$folder = $this->folderMapper->find($event->getId());
Expand All @@ -163,7 +163,7 @@ public function publishFolder(ChangeEvent $event): void {
}

/**
* @var $shares SharedFolder[]
* @var SharedFolder[] $shares
*/
$shares = $this->sharedFolderMapper->findByOwner($this->authorizer->getUserId());
$shares = array_merge($shares, $this->sharedFolderMapper->findByUser($this->authorizer->getUserId()));
Expand Down Expand Up @@ -193,7 +193,7 @@ public function publishBookmark(ChangeEvent $event): void {
$activity->setTimestamp(time());

/**
* @var $bookmark Bookmark
* @var Bookmark $bookmark
*/
try {
$bookmark = $this->bookmarkMapper->find($event->getId());
Expand All @@ -213,7 +213,7 @@ public function publishBookmark(ChangeEvent $event): void {
}

/**
* @var $shares SharedFolder[]
* @var SharedFolder[] $shares
*/
$shares = $this->sharedFolderMapper->findByOwner($this->authorizer->getUserId());
$shares = array_merge($shares, $this->sharedFolderMapper->findByUser($this->authorizer->getUserId()));
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/BackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
}

protected function run($argument) {
$userIds = $this->config->getUsersForUserValue('bookmarks', 'backup.enabled', (string) true); // if users have set this in older versions
$userIds = $this->config->getUsersForUserValue('bookmarks', 'backup.enabled', (string)true); // if users have set this in older versions
$userIds = array_merge($userIds, $this->config->getUsersForUserValue('bookmarks', 'backup.enabled', 'true'));
$userIds = array_unique($userIds);
if (empty($userIds)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/ClearPreviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function configure() {
/**
* Execute the command
*
* @param InputInterface $input
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
*/
public function setSetting(string $setting, float|bool|int|string $value): JSONResponse {
try {
$this->settingsService->setSetting($setting, (string) $value);
$this->settingsService->setSetting($setting, (string)$value);
return new JSONResponse([], Http::STATUS_OK);
} catch (\Exception $e) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
Expand Down
16 changes: 8 additions & 8 deletions lib/Controller/BookmarkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ private function _getRootFolderId(): ?int {
if ($this->authorizer->getToken() !== null) {
try {
/**
* @var $publicFolder PublicFolder
* @var PublicFolder $publicFolder
*/
$publicFolder = $this->publicFolderMapper->find($this->authorizer->getToken());
$this->rootFolderId = $publicFolder->getFolderId();
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
$this->logger->error($e->getMessage()."\n".$e->getMessage());
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public function getSingleBookmark($id): JSONResponse {
}
try {
/**
* @var $bm Bookmark
* @var Bookmark $bm
*/
$bm = $this->bookmarkMapper->find($id);
} catch (DoesNotExistException $e) {
Expand Down Expand Up @@ -379,7 +379,7 @@ public function getBookmarks(
// If we have this user's permission to see the contents of their folder, simply set the userID
// to theirs
$userId = $folderEntity->getUserId();
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return new DataResponse(['status' => 'error', 'data' => ['Not found']], Http::STATUS_BAD_REQUEST);
}
$params->setFolder($this->toInternalFolderId($folder));
Expand All @@ -395,7 +395,7 @@ public function getBookmarks(
} else {
try {
$result = $this->bookmarkMapper->findAllInPublicFolder($this->authorizer->getToken(), $params);
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return new DataResponse(['status' => 'error', 'data' => ['Not found']], Http::STATUS_BAD_REQUEST);
}
}
Expand Down Expand Up @@ -527,7 +527,7 @@ public function deleteBookmark($id): JSONResponse {

try {
$this->bookmarkMapper->find($id);
} catch (DoesNotExistException | MultipleObjectsReturnedException) {
} catch (DoesNotExistException|MultipleObjectsReturnedException) {
return new JSONResponse(['status' => 'success']);
}

Expand Down Expand Up @@ -606,7 +606,7 @@ public function getBookmarkImage($id) {
return new NotFoundResponse();
}
return $this->doImageResponse($image);
} catch (DoesNotExistException | MultipleObjectsReturnedException | Exception $e) {
} catch (DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
return new NotFoundResponse();
}
}
Expand All @@ -633,7 +633,7 @@ public function getBookmarkFavicon($id) {
return new NotFoundResponse();
}
return $this->doImageResponse($image);
} catch (DoesNotExistException | MultipleObjectsReturnedException | Exception $e) {
} catch (DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
return new NotFoundResponse();
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/FoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ private function _getRootFolderId(): ?int {
if ($this->authorizer->getToken() !== null) {
try {
/**
* @var $publicFolder PublicFolder
* @var PublicFolder $publicFolder
*/
$publicFolder = $this->publicFolderMapper->find($this->authorizer->getToken());
$this->rootFolderId = $publicFolder->getFolderId();
} catch (DoesNotExistException | MultipleObjectsReturnedException $e) {
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
$this->logger->error($e->getMessage()."\n".$e->getMessage());
}
}
Expand Down Expand Up @@ -869,7 +869,7 @@ public function deleteShare(int $shareId): DataResponse {

try {
$this->folders->deleteShare($shareId);
} catch (UnsupportedOperation | DoesNotExistException | MultipleObjectsReturnedException | Exception $e) {
} catch (UnsupportedOperation|DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
return new Http\DataResponse(['status' => 'error', 'data' => ['Internal error']], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new Http\DataResponse(['status' => 'success']);
Expand Down
12 changes: 6 additions & 6 deletions lib/Controller/InternalBookmarkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function __construct(
public function getBookmarks(
$page = 0,
$tags = [],
$conjunction = "or",
$sortby = "",
$conjunction = 'or',
$sortby = '',
$search = [],
$limit = 10,
$untagged = false,
Expand Down Expand Up @@ -103,7 +103,7 @@ public function getSingleBookmark($id): JSONResponse {
*
* @NoAdminRequired
*/
public function newBookmark($url = "", $title = null, $description = null, $tags = null, $folders = [], $target = null): JSONResponse {
public function newBookmark($url = '', $title = null, $description = null, $tags = null, $folders = [], $target = null): JSONResponse {
return $this->publicController->newBookmark($url, $title, $description, $tags, $folders, $target);
}

Expand All @@ -119,7 +119,7 @@ public function newBookmark($url = "", $title = null, $description = null, $tags
*
* @NoAdminRequired
*/
public function editBookmark($id = null, $url = null, $title = null, $description = "", $tags = [], $folders = null, $target = null): JSONResponse {
public function editBookmark($id = null, $url = null, $title = null, $description = '', $tags = [], $folders = null, $target = null): JSONResponse {
return $this->publicController->editBookmark($id, $url, $title, $description, $tags, $folders, $target);
}

Expand All @@ -141,7 +141,7 @@ public function deleteBookmark($id = -1): JSONResponse {
public function deleteAllBookmarks(): DataResponse {
try {
$this->bookmarks->deleteAll($this->userId);
} catch (UnsupportedOperation | DoesNotExistException | MultipleObjectsReturnedException $e) {
} catch (UnsupportedOperation|DoesNotExistException|MultipleObjectsReturnedException $e) {
return new DataResponse(['status' => 'error', 'data' => ['Internal server error']], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new DataResponse(['status' => 'success']);
Expand All @@ -153,7 +153,7 @@ public function deleteAllBookmarks(): DataResponse {
* @return JSONResponse
* @NoAdminRequired
*/
public function clickBookmark($url = ""): JSONResponse {
public function clickBookmark($url = ''): JSONResponse {
return $this->publicController->clickBookmark($url);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/InternalTagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($appName, $request, TagsController $publicController
*
* @NoAdminRequired
*/
public function deleteTag($old_name = ""): JSONResponse {
public function deleteTag($old_name = ''): JSONResponse {
return $this->publicController->deleteTag($old_name);
}

Expand All @@ -36,7 +36,7 @@ public function deleteTag($old_name = ""): JSONResponse {
*
* @NoAdminRequired
*/
public function renameTag($old_name = "", $new_name = "", $name = ''): JSONResponse {
public function renameTag($old_name = '', $new_name = '', $name = ''): JSONResponse {
return $this->publicController->renameTag($old_name, $new_name, $name);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/Controller/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __construct($appName, $request, $userId, DB\TagMapper $tagMapper
* @NoCSRFRequired
*
*/
public function deleteTag($old_name = ""): JSONResponse {
if ($old_name === "") {
public function deleteTag($old_name = ''): JSONResponse {
if ($old_name === '') {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}

Expand All @@ -53,12 +53,12 @@ public function deleteTag($old_name = ""): JSONResponse {
* @NoCSRFRequired
*
*/
public function renameTag($old_name = "", $new_name = "", $name = ''): JSONResponse {
public function renameTag($old_name = '', $new_name = '', $name = ''): JSONResponse {
if ($new_name === '') {
$new_name = $name;
}

if ($old_name === "" || $new_name === "") {
if ($old_name === '' || $new_name === '') {
return new JSONResponse(['status' => 'error', 'data' => ['Must provide old_name and a new name']], Http::STATUS_BAD_REQUEST);
}

Expand All @@ -74,8 +74,8 @@ public function renameTag($old_name = "", $new_name = "", $name = ''): JSONRespo
*
*/
public function fullTags($count = false): JSONResponse {
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');

if ($count === true) {
$tags = $this->tagMapper->findAllWithCount($this->userId);
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/WebViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public function link(string $token) {
$userName = 'Unknown';
try {
/**
* @var $publicFolder PublicFolder
* @var PublicFolder $publicFolder
*/
$publicFolder = $this->publicFolderMapper->find($token);
/**
* @var $folder Folder
* @var Folder $folder
*/
$folder = $this->folderMapper->find($publicFolder->getFolderId());
$title = $folder->getTitle();
Expand Down
38 changes: 32 additions & 6 deletions lib/Db/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
* @method setAvailable(boolean $available)
* @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 +88,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 +106,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,10 +116,38 @@ 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');
}
$this->setter('textContent', [$content]);
}

public function getTextContent(): string {
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
return (string)mb_convert_encoding($this->textContent, 'UTF-8', 'UTF-8');
}

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');
}
$this->setter('htmlContent', [$content]);
}

public function getHtmlContent(): string {
// Remove non-utf-8 characters from string: https://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string
return (string)mb_convert_encoding($this->htmlContent, 'UTF-8', 'UTF-8');
}

public function isWebLink() {
return (bool) preg_match('/^https?:/i', $this->getUrl());
return (bool)preg_match('/^https?:/i', $this->getUrl());
}
}
2 changes: 1 addition & 1 deletion lib/Db/BookmarkMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private function _filterDuplicated(IQueryBuilder $qb, QueryParameters $params) {
if ($params->getDuplicated()) {
$subQuery = $this->db->getQueryBuilder();
$subQuery->select('trdup.parent_folder')
->from('*PREFIX*bookmarks_tree', 'trdup')
->from('*PREFIX*bookmarks_tree', 'trdup')
->where($subQuery->expr()->eq('b.id', 'trdup.id'))
->andWhere($subQuery->expr()->neq('trdup.parent_folder', 'tree.parent_folder'))
->andWhere($subQuery->expr()->eq('trdup.type', $qb->createPositionalParameter(TreeMapper::TYPE_BOOKMARK)));
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/BookmarkWithTagsAndParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function toArray(): array {
$array[$field] = [];
} else {
$array[$field] = array_values(array_unique(array_map(static function ($id) {
return (int) $id;
return (int)$id;
}, explode(',', $this->{$field}))));
}
continue;
Expand Down
Loading

0 comments on commit 63a92f0

Please sign in to comment.