You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmojiService.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Service;
  24. use OCP\IDBConnection;
  25. /**
  26. * Class EmojiService
  27. *
  28. * @package OCA\UserStatus\Service
  29. */
  30. class EmojiService {
  31. /** @var IDBConnection */
  32. private $db;
  33. /**
  34. * EmojiService constructor.
  35. *
  36. * @param IDBConnection $db
  37. */
  38. public function __construct(IDBConnection $db) {
  39. $this->db = $db;
  40. }
  41. /**
  42. * @return bool
  43. */
  44. public function doesPlatformSupportEmoji(): bool {
  45. return $this->db->supports4ByteText() &&
  46. \class_exists(\IntlBreakIterator::class);
  47. }
  48. /**
  49. * @param string $emoji
  50. * @return bool
  51. */
  52. public function isValidEmoji(string $emoji): bool {
  53. $intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
  54. $intlBreakIterator->setText($emoji);
  55. $characterCount = 0;
  56. while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
  57. $characterCount++;
  58. }
  59. if ($characterCount !== 1) {
  60. return false;
  61. }
  62. $codePointIterator = \IntlBreakIterator::createCodePointInstance();
  63. $codePointIterator->setText($emoji);
  64. foreach ($codePointIterator->getPartsIterator() as $codePoint) {
  65. $codePointType = \IntlChar::charType($codePoint);
  66. // If the current code-point is an emoji or a modifier (like a skin-tone)
  67. // just continue and check the next character
  68. if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
  69. $codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
  70. $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL) {
  71. continue;
  72. }
  73. // If it's neither a modifier nor an emoji, we only allow
  74. // a zero-width-joiner or a variation selector 16
  75. $codePointValue = \IntlChar::ord($codePoint);
  76. if ($codePointValue === 8205 || $codePointValue === 65039) {
  77. continue;
  78. }
  79. return false;
  80. }
  81. return true;
  82. }
  83. }