]> git.mxchange.org Git - friendica.git/blob - src/Content/Smilies.php
Allow using punctuation chars as smiley delimiters
[friendica.git] / src / Content / Smilies.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Hook;
26 use Friendica\DI;
27 use Friendica\Util\Strings;
28
29 /**
30  * This class contains functions to handle smiles
31  */
32
33 class Smilies
34 {
35         /**
36          * Replaces/adds the emoticon list
37          *
38          * This function should be used whenever emoticons are added
39          *
40          * @param array  $b              Array of emoticons
41          * @param string $smiley         The text smilie
42          * @param string $representation The replacement
43          * @return void
44          */
45         public static function add(array &$b, string $smiley, string $representation)
46         {
47                 $found = array_search($smiley, $b['texts']);
48
49                 if (!is_int($found)) {
50                         $b['texts'][] = $smiley;
51                         $b['icons'][] = $representation;
52                 } else {
53                         $b['icons'][$found] = $representation;
54                 }
55         }
56
57         /**
58          * Function to list all smilies
59          *
60          * Get an array of all smilies, both internal and from addons.
61          *
62          * @return array
63          *    'texts' => smilie shortcut
64          *    'icons' => icon in html
65          *
66          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
67          * @hook  smilie ('texts' => smilies texts array, 'icons' => smilies html array)
68          */
69         public static function getList(): array
70         {
71                 $texts = [
72                         '&lt;3',
73                         '&lt;/3',
74                         '&lt;\\3',
75                         ':-)',
76                         ';-)',
77                         ':-(',
78                         ':-P',
79                         ':-p',
80                         ':-"',
81                         ':-&quot;',
82                         ':-x',
83                         ':-X',
84                         ':-D',
85                         '8-|',
86                         '8-O',
87                         ':-O',
88                         '\\o/',
89                         'o.O',
90                         'O.o',
91                         'o_O',
92                         'O_o',
93                         ":'(",
94                         ":-!",
95                         ":-/",
96                         ":-[",
97                         "8-)",
98                         ':beer',
99                         ':homebrew',
100                         ':coffee',
101                         ':facepalm',
102                         ':like',
103                         ':dislike',
104                         '~friendica',
105                         'red#',
106                         'red#matrix'
107
108                 ];
109
110                 $baseUrl = (string)DI::baseUrl();
111
112                 $icons = [
113                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
114                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
115                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
116                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
117                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
118                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
119                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
120                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
121                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
122                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
123                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
124                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
125                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-laughing.gif" alt=":-D" title=":-D"  />',
126                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
127                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
128                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
129                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
130                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
131                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
132                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
133                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
134                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
135                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
136                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
137                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-embarrassed.gif" alt=":-[" title=":-[" />',
138                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
139                 '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
140                 '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
141                 '<img class="smiley" src="' . $baseUrl . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
142                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
143                 '<img class="smiley" src="' . $baseUrl . '/images/like.gif" alt=":like" title=":like" />',
144                 '<img class="smiley" src="' . $baseUrl . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
145                 '<a href="https://friendi.ca">~friendica <img class="smiley" width="16" height="16" src="' . $baseUrl . '/images/friendica.svg" alt="~friendica" title="~friendica" /></a>',
146                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
147                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
148                 ];
149
150                 $params = ['texts' => $texts, 'icons' => $icons];
151                 Hook::callAll('smilie', $params);
152
153                 return $params;
154         }
155
156         /**
157          * Normalizes smiley shortcodes into texts with no special symbols.
158          *
159          * @return array
160          *    'texts' => smilie shortcut
161          *    'icons' => icon url or an empty string
162          *    'norms' => normalized shortcut
163          */
164         public static function getNormalizedList(): array
165         {
166                 $smilies = self::getList();
167                 $norms = [];
168                 $icons = $smilies['icons'];
169                 foreach ($smilies['texts'] as $i => $shortcode) {
170                         // Extract urls
171                         $icon = $icons[$i];
172                         if (preg_match('/src="(.+?)"/', $icon, $match)) {
173                                 $icon = $match[1];
174                         } else {
175                                 $icon = '';
176                         }
177                         $icons[$i] = $icon;
178
179                         // Normalize name
180                         $norm = preg_replace('/[\s\-:#~]/', '', $shortcode);
181                         if (ctype_alnum($norm)) {
182                                 $norms[] = $norm;
183                         } elseif (preg_match('#/smiley-(\w+)\.gif#', $icon, $match)) {
184                                 $norms[] = $match[1];
185                         } else {
186                                 $norms[] = 'smiley' . $i;
187                         }
188                 }
189                 $smilies['norms'] = $norms;
190                 return $smilies;
191         }
192
193         /**
194          * Finds all used smilies (denoted by quoting colons like :heart:) in the provided text and normalizes their usages.
195          *
196          * @param string $text that might contain smiley usages
197          * @return array with smilie codes (colon included) as the keys, their image urls as values;
198          *               the normalized string is put under the '' (empty string) key
199          */
200         public static function extractUsedSmilies(string $text, string &$normalized = null): array
201         {
202                 $emojis = [];
203
204                 $normalized = BBCode::performWithEscapedTags($text, ['code'], function ($text) use (&$emojis) {
205                         return BBCode::performWithEscapedTags($text, ['noparse', 'nobb', 'pre'], function ($text) use (&$emojis) {
206                                 if (strpos($text, '[nosmile]') !== false || self::noSmilies()) {
207                                         return $text;
208                                 }
209                                 $smilies = self::getNormalizedList();
210                                 $normalized = array_combine($smilies['texts'], $smilies['norms']);
211                                 return self::performForEachWordMatch(
212                                         array_combine($smilies['texts'], $smilies['icons']),
213                                         $text,
214                                         function (string $name, string $image) use($normalized, &$emojis) {
215                                                 $name = $normalized[$name];
216                                                 if (preg_match('/src="(.+?)"/', $image, $match)) {
217                                                         $image = $match[1];
218                                                         $emojis[$name] = $image;
219                                                 }
220                                                 return ':' . $name . ':';
221                                         },
222                                 );
223                         });
224                 });
225
226                 return $emojis;
227         }
228
229         /**
230          * Similar to strtr but matches only whole words and replaces texts with $callback.
231          *
232          * @param array $words
233          * @param string $subject
234          * @param callable $callback ($offset, $value)
235          * @return string
236          */
237         private static function performForEachWordMatch(array $words, string $subject, callable $callback): string
238         {
239                 $ord1_bitset = 0;
240                 $ord2_bitset = 0;
241                 $prefixes = [];
242                 foreach ($words as $word => $_) {
243                         if (strlen($word) < 2 || !ctype_graph($word)) {
244                                 continue;
245                         }
246                         $ord1 = ord($word);
247                         $ord2 = ord($word[1]);
248                         $ord1_bitset |= 1 << ($ord1 & 31);
249                         $ord2_bitset |= 1 << ($ord2 & 31);
250                         if (!array_key_exists($word[0], $prefixes)) {
251                                 $prefixes[$word[0]] = [];
252                         }
253                         $prefixes[$word[0]][] = $word;
254                 }
255
256                 $result = '';
257                 $processed = 0;
258                 $s_start = 0; // Segment start
259                 // No spaces are allowed in smilies, so they can serve as delimiters.
260                 // Splitting by some delimiters may not necessary though?
261                 while (true) {
262                         if ($s_start >= strlen($subject)) {
263                                 $result .= substr($subject, $processed);
264                                 break;
265                         }
266                         if (preg_match('/\s+?(?=\S|$)/', $subject, $match, PREG_OFFSET_CAPTURE, $s_start)) {
267                                 [$whitespaces, $s_end] = $match[0];
268                         } else {
269                                 $s_end = strlen($subject);
270                                 $whitespaces = '';
271                         }
272                         $s_length = $s_end - $s_start;
273                         if ($s_length > 1) {
274                                 $segment = substr($subject, $s_start, $s_length);
275                                 // Find possible starting points for smilies.
276                                 // For built-in smilies, the two bitsets should make attempts quite efficient.
277                                 // However, presuming custom smilies follow the format of ":shortcode" or ":shortcode:",
278                                 // if the user adds more smilies (with addons), the second bitset may eventually become useless.
279                                 for ($i = 0; $i < $s_length - 1; $i++) {
280                                         $c = $segment[$i];
281                                         $d = $segment[$i + 1];
282                                         if (($ord1_bitset & (1 << (ord($c) & 31))) && ($ord2_bitset & (1 << (ord($d) & 31))) && array_key_exists($c, $prefixes)) {
283                                                 foreach ($prefixes[$c] as $word) {
284                                                         $wlength = strlen($word);
285                                                         if ($wlength <= $s_length - $i && substr($segment, $i, $wlength) === $word) {
286                                                                 // Check for boundaries
287                                                                 if (($i === 0 || ctype_space($segment[$i - 1]) || ctype_punct($segment[$i - 1]))
288                                                                         && ($i + $wlength >= $s_length || ctype_space($segment[$i + $wlength]) || ctype_punct($segment[$i + $wlength]))) {
289                                                                         $result .= substr($subject, $processed, $s_start - $processed + $i);
290                                                                         $result .= call_user_func($callback, $word, $words[$word]);
291                                                                         $i += $wlength;
292                                                                         $processed = $s_start + $i;
293                                                                         $i--;
294                                                                         break;
295                                                                 }
296                                                         }
297                                                 }
298                                         }
299                                 }
300                         }
301                         $s_start = $s_end + strlen($whitespaces);
302                 }
303                 return $result;
304         }
305
306         /**
307          * Copied from http://php.net/manual/en/function.str-replace.php#88569
308          * Modified for camel caps: renamed stro_replace -> strOrigReplace
309          *
310          * When using str_replace(...), values that did not exist in the original string (but were put there by previous
311          * replacements) will be replaced continuously.  This string replacement function is designed to replace the values
312          * in $search with those in $replace while not factoring in prior replacements.  Note that this function will
313          * always look for the longest possible match first and then work its way down to individual characters.
314          *
315          * @param array $search list of strings or characters that need to be replaced
316          * @param array $replace list of strings or characters that will replace the corresponding values in $search
317          * @param string $subject the string on which this operation is being performed
318          *
319          * @return string $subject with all substrings in the $search array replaced by the values in the $replace array
320          */
321         private static function strOrigReplace(array $search, array $replace, string $subject): string
322         {
323                 return self::performForEachWordMatch(
324                         array_combine($search, $replace),
325                         $subject,
326                         function (string $_, string $value) {
327                                 return $value;
328                         }
329                 );
330         }
331
332         /**
333          * Replaces text emoticons with graphical images
334          *
335          * It is expected that this function will be called using HTML text.
336          * We will escape text between HTML pre and code blocks from being
337          * processed.
338          *
339          * At a higher level, the bbcode [nosmile] tag can be used to prevent this
340          * function from being executed by the prepare_text() routine when preparing
341          * bbcode source for HTML display
342          *
343          * @param string  $s         Text that should be replaced
344          * @param boolean $no_images Only replace emoticons without images
345          *
346          * @return string HTML Output of the Smilie
347          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
348          */
349         public static function replace(string $s, bool $no_images = false): string
350         {
351                 $smilies = self::getList();
352
353                 $s = self::replaceFromArray($s, $smilies, $no_images);
354
355                 return $s;
356         }
357
358         private static function noSmilies(): bool {
359                 return (intval(DI::config()->get('system', 'no_smilies')) ||
360                                 (DI::userSession()->getLocalUserId() &&
361                                  intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'no_smilies'))));
362         }
363
364         /**
365          * Replaces emoji shortcodes in a string from a structured array of searches and replaces.
366          *
367          * Depends on system.no_smilies config value, skips <pre> and <code> tags.
368          *
369          * @param string $text      An HTML string
370          * @param array  $smilies   An string replacement array with the following structure: ['texts' => [], 'icons' => []]
371          * @param bool   $no_images Only replace shortcodes without image replacement (e.g. Unicode characters)
372          * @return string
373          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
374          */
375         public static function replaceFromArray(string $text, array $smilies, bool $no_images = false): string
376         {
377                 if (self::noSmilies()) {
378                         return $text;
379                 }
380
381                 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'encode'], $text);
382                 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', [self::class, 'encode'], $text);
383
384                 if ($no_images) {
385                         $cleaned = ['texts' => [], 'icons' => []];
386                         $icons = $smilies['icons'];
387                         foreach ($icons as $key => $icon) {
388                                 if (!strstr($icon, '<img ')) {
389                                         $cleaned['texts'][] = $smilies['texts'][$key];
390                                         $cleaned['icons'][] = $smilies['icons'][$key];
391                                 }
392                         }
393                         $smilies = $cleaned;
394                 }
395
396                 $text = preg_replace_callback('/\B&lt;3+?\b/', [self::class, 'heartReplaceCallback'], $text);
397                 $text = self::strOrigReplace($smilies['texts'], $smilies['icons'], $text);
398
399                 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', [self::class, 'decode'], $text);
400                 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'decode'], $text);
401
402                 return $text;
403         }
404
405         /**
406          * Encodes smiley match array to BASE64 string
407          *
408          * @param array $m Match array
409          * @return string base64 encoded string
410          */
411         private static function encode(array $m): string
412         {
413                 return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
414         }
415
416         /**
417          * Decodes a previously BASE64-encoded match array to a string
418          *
419          * @param array $m Matches array
420          * @return string base64 decoded string
421          * @throws \Exception
422          */
423         private static function decode(array $m): string
424         {
425                 return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
426         }
427
428
429         /**
430          * expand <3333 to the correct number of hearts
431          *
432          * @param array $matches
433          * @return string HTML Output
434          */
435         private static function heartReplaceCallback(array $matches): string
436         {
437                 return str_repeat('❤', strlen($matches[0]) - 4);
438         }
439
440         /**
441          * Checks if the body doesn't contain any alphanumeric characters
442          *
443          * @param string $body Possibly-HTML post body
444          * @return boolean
445          */
446         public static function isEmojiPost(string $body): bool
447         {
448                 // Strips all whitespace
449                 $conv = preg_replace('#\s#u', '', html_entity_decode($body));
450                 if (empty($conv)) {
451                         return false;
452                 }
453
454                 if (!class_exists('IntlChar')) {
455                         // Most Emojis are 4 byte Unicode characters, so this is a good workaround, when IntlChar does not exist on the system
456                         return strlen($conv) / mb_strlen($conv) == 4;
457                 }
458
459                 for ($i = 0; $i < mb_strlen($conv); $i++) {
460                         $character = mb_substr($conv, $i, 1);
461
462                         if (\IntlChar::isalnum($character) || \IntlChar::ispunct($character) || \IntlChar::isgraph($character) && (strlen($character) <= 2)) {
463                                 return false;
464                         }
465                 }
466                 return true;
467         }
468 }