1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright (c) 2020, Georg Ehrke |
7
|
|
|
* |
8
|
|
|
* @author Georg Ehrke <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @license AGPL-3.0 |
11
|
|
|
* |
12
|
|
|
* This code is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
14
|
|
|
* as published by the Free Software Foundation. |
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, version 3, |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OCA\UserStatus\Service; |
27
|
|
|
|
28
|
|
|
use OCP\IDBConnection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class EmojiService |
32
|
|
|
* |
33
|
|
|
* @package OCA\UserStatus\Service |
34
|
|
|
*/ |
35
|
|
|
class EmojiService { |
36
|
|
|
|
37
|
|
|
/** @var IDBConnection */ |
38
|
|
|
private $db; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* EmojiService constructor. |
42
|
|
|
* |
43
|
|
|
* @param IDBConnection $db |
44
|
|
|
*/ |
45
|
|
|
public function __construct(IDBConnection $db) { |
46
|
|
|
$this->db = $db; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function doesPlatformSupportEmoji(): bool { |
53
|
|
|
return $this->db->supports4ByteText() && |
54
|
|
|
\class_exists(\IntlBreakIterator::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $emoji |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isValidEmoji(string $emoji): bool { |
62
|
|
|
$intlBreakIterator = \IntlBreakIterator::createCharacterInstance(); |
|
|
|
|
63
|
|
|
$intlBreakIterator->setText($emoji); |
64
|
|
|
|
65
|
|
|
$characterCount = 0; |
66
|
|
|
while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) { |
67
|
|
|
$characterCount++; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($characterCount !== 1) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$codePointIterator = \IntlBreakIterator::createCodePointInstance(); |
75
|
|
|
$codePointIterator->setText($emoji); |
76
|
|
|
|
77
|
|
|
foreach ($codePointIterator->getPartsIterator() as $codePoint) { |
78
|
|
|
$codePointType = \IntlChar::charType($codePoint); |
79
|
|
|
|
80
|
|
|
// If the current code-point is an emoji or a modifier (like a skin-tone) |
81
|
|
|
// just continue and check the next character |
82
|
|
|
if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL || |
83
|
|
|
$codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER || |
84
|
|
|
$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If it's neither a modifier nor an emoji, we only allow |
89
|
|
|
// a zero-width-joiner or a variation selector 16 |
90
|
|
|
$codePointValue = \IntlChar::ord($codePoint); |
91
|
|
|
if ($codePointValue === 8205 || $codePointValue === 65039) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.