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.

EmojiHelper.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC;
  26. use OCP\IDBConnection;
  27. use OCP\IEmojiHelper;
  28. class EmojiHelper implements IEmojiHelper {
  29. private IDBConnection $db;
  30. public function __construct(IDBConnection $db) {
  31. $this->db = $db;
  32. }
  33. public function doesPlatformSupportEmoji(): bool {
  34. return $this->db->supports4ByteText() &&
  35. \class_exists(\IntlBreakIterator::class);
  36. }
  37. public function isValidSingleEmoji(string $emoji): bool {
  38. $intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
  39. $intlBreakIterator->setText($emoji);
  40. $characterCount = 0;
  41. while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
  42. $characterCount++;
  43. }
  44. if ($characterCount !== 1) {
  45. return false;
  46. }
  47. $codePointIterator = \IntlBreakIterator::createCodePointInstance();
  48. $codePointIterator->setText($emoji);
  49. foreach ($codePointIterator->getPartsIterator() as $codePoint) {
  50. $codePointType = \IntlChar::charType($codePoint);
  51. // Unicode chars need 2 or more chars
  52. // The characterCount before this loop already validate if is a single emoji
  53. // This condition is to don't continue if non emoji chars
  54. if (strlen($emoji) >= 2) {
  55. // If the current code-point is an emoji or a modifier (like a skin-tone)
  56. // just continue and check the next character
  57. if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
  58. $codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
  59. $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL ||
  60. $codePointType === \IntlChar::CHAR_CATEGORY_FORMAT_CHAR || // i.e. 🏴󠁧󠁢󠁥󠁮󠁧󠁿 🏴󠁧󠁢󠁳󠁣󠁴󠁿
  61. $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_PUNCTUATION || // i.e. ‼️ ⁉️ #⃣
  62. $codePointType === \IntlChar::CHAR_CATEGORY_LOWERCASE_LETTER || // i.e. ℹ️
  63. $codePointType === \IntlChar::CHAR_CATEGORY_MATH_SYMBOL || // i.e. ↔️ ◻️ ⤴️ ⤵️
  64. $codePointType === \IntlChar::CHAR_CATEGORY_ENCLOSING_MARK || // i.e. 0⃣..9⃣
  65. $codePointType === \IntlChar::CHAR_CATEGORY_DECIMAL_DIGIT_NUMBER || // i.e. 0⃣..9⃣
  66. $codePointType === \IntlChar::CHAR_CATEGORY_DASH_PUNCTUATION || // i.e. 〰️
  67. $codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES
  68. ) {
  69. continue;
  70. }
  71. }
  72. // If it's neither a modifier nor an emoji, we only allow
  73. // a zero-width-joiner or a variation selector 16
  74. $codePointValue = \IntlChar::ord($codePoint);
  75. if ($codePointValue === 8205 || $codePointValue === 65039) {
  76. continue;
  77. }
  78. return false;
  79. }
  80. return true;
  81. }
  82. }