]> git.mxchange.org Git - friendica.git/blob - src/Content/Smilies.php
68093e843d3790caccfd4ad13a9a769edceb5582
[friendica.git] / src / Content / Smilies.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\Hook;
25 use Friendica\Core\Session;
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 = 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-embarassed.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         /**
158          * Copied from http://php.net/manual/en/function.str-replace.php#88569
159          * Modified for camel caps: renamed stro_replace -> strOrigReplace
160          *
161          * When using str_replace(...), values that did not exist in the original string (but were put there by previous
162          * replacements) will be replaced continuously.  This string replacement function is designed to replace the values
163          * in $search with those in $replace while not factoring in prior replacements.  Note that this function will
164          * always look for the longest possible match first and then work its way down to individual characters.
165          *
166          * @param array $search list of strings or characters that need to be replaced
167          * @param array $replace list of strings or characters that will replace the corresponding values in $search
168          * @param string $subject the string on which this operation is being performed
169          *
170          * @return string $subject with all substrings in the $search array replaced by the values in the $replace array
171          */
172         private static function strOrigReplace(array $search, array $replace, string $subject): string
173         {
174                 return strtr($subject, array_combine($search, $replace));
175         }
176
177         /**
178          * Replaces text emoticons with graphical images
179          *
180          * It is expected that this function will be called using HTML text.
181          * We will escape text between HTML pre and code blocks from being
182          * processed.
183          *
184          * At a higher level, the bbcode [nosmile] tag can be used to prevent this
185          * function from being executed by the prepare_text() routine when preparing
186          * bbcode source for HTML display
187          *
188          * @param string  $s         Text that should be replaced
189          * @param boolean $no_images Only replace emoticons without images
190          *
191          * @return string HTML Output of the Smilie
192          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
193          */
194         public static function replace(string $s, bool $no_images = false): string
195         {
196                 $smilies = self::getList();
197
198                 $s = self::replaceFromArray($s, $smilies, $no_images);
199
200                 return $s;
201         }
202
203         /**
204          * Replaces emoji shortcodes in a string from a structured array of searches and replaces.
205          *
206          * Depends on system.no_smilies config value, skips <pre> and <code> tags.
207          *
208          * @param string $text      An HTML string
209          * @param array  $smilies   An string replacement array with the following structure: ['texts' => [], 'icons' => []]
210          * @param bool   $no_images Only replace shortcodes without image replacement (e.g. Unicode characters)
211          * @return string
212          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
213          */
214         public static function replaceFromArray(string $text, array $smilies, bool $no_images = false): string
215         {
216                 if (intval(DI::config()->get('system', 'no_smilies'))
217                         || (Session::getLocalUser() && intval(DI::pConfig()->get(Session::getLocalUser(), 'system', 'no_smilies')))
218                 ) {
219                         return $text;
220                 }
221
222                 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::encode', $text);
223                 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::encode', $text);
224
225                 if ($no_images) {
226                         $cleaned = ['texts' => [], 'icons' => []];
227                         $icons = $smilies['icons'];
228                         foreach ($icons as $key => $icon) {
229                                 if (!strstr($icon, '<img ')) {
230                                         $cleaned['texts'][] = $smilies['texts'][$key];
231                                         $cleaned['icons'][] = $smilies['icons'][$key];
232                                 }
233                         }
234                         $smilies = $cleaned;
235                 }
236
237                 $text = preg_replace_callback('/&lt;(3+)/', 'self::heartReplaceCallback', $text);
238                 $text = self::strOrigReplace($smilies['texts'], $smilies['icons'], $text);
239
240                 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::decode', $text);
241                 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::decode', $text);
242
243                 return $text;
244         }
245
246         /**
247          * Encodes smiley match array to BASE64 string
248          *
249          * @param array $m Match array
250          * @return string base64 encoded string
251          */
252         private static function encode(array $m): string
253         {
254                 return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
255         }
256
257         /**
258          * Decodes a previously BASE64-encoded match array to a string
259          *
260          * @param array $m Matches array
261          * @return string base64 decoded string
262          * @throws \Exception
263          */
264         private static function decode(array $m): string
265         {
266                 return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
267         }
268
269
270         /**
271          * expand <3333 to the correct number of hearts
272          *
273          * @param array $matches
274          * @return string HTML Output
275          */
276         private static function heartReplaceCallback(array $matches): string
277         {
278                 if (strlen($matches[1]) == 1) {
279                         return $matches[0];
280                 }
281
282                 $t = '';
283                 for ($cnt = 0; $cnt < strlen($matches[1]); $cnt ++) {
284                         $t .= '❤';
285                 }
286
287                 return str_replace($matches[0], $t, $matches[0]);
288         }
289 }