]> git.mxchange.org Git - friendica.git/blob - src/Content/Smilies.php
Remove deprecated code
[friendica.git] / src / Content / Smilies.php
1 <?php
2
3 /**
4  * @file src/Content/Smilies.php
5  * This file contains the Smilies class which contains functions to handle smiles
6  *
7  * @todo Use the shortcodes from here:
8  * https://github.com/iamcal/emoji-data/blob/master/emoji_pretty.json?raw=true
9  * https://raw.githubusercontent.com/emojione/emojione/master/extras/alpha-codes/eac.json?raw=true
10  * https://github.com/johannhof/emoji-helper/blob/master/data/emoji.json?raw=true
11  *
12  * Have also a look here:
13  * https://www.webpagefx.com/tools/emoji-cheat-sheet/
14  */
15 namespace Friendica\Content;
16
17 use Friendica\Core\Hook;
18 use Friendica\DI;
19 use Friendica\Util\Strings;
20
21 /**
22  * This class contains functions to handle smiles
23  */
24
25 class Smilies
26 {
27         /**
28          * Replaces/adds the emoticon list
29          *
30          * This function should be used whenever emoticons are added
31          *
32          * @param array  $b              Array of emoticons
33          * @param string $smiley         The text smilie
34          * @param string $representation The replacement
35          *
36          * @return void
37          */
38         public static function add(&$b, $smiley, $representation)
39         {
40                 $found = array_search($smiley, $b['texts']);
41
42                 if (!is_int($found)) {
43                         $b['texts'][] = $smiley;
44                         $b['icons'][] = $representation;
45                 } else {
46                         $b['icons'][$found] = $representation;
47                 }
48         }
49
50         /**
51          * Function to list all smilies
52          *
53          * Get an array of all smilies, both internal and from addons.
54          *
55          * @return array
56          *    'texts' => smilie shortcut
57          *    'icons' => icon in html
58          *
59          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60          * @hook  smilie ('texts' => smilies texts array, 'icons' => smilies html array)
61          */
62         public static function getList()
63         {
64                 $texts =  [
65                         '&lt;3',
66                         '&lt;/3',
67                         '&lt;\\3',
68                         ':-)',
69                         ';-)',
70                         ':-(',
71                         ':-P',
72                         ':-p',
73                         ':-"',
74                         ':-&quot;',
75                         ':-x',
76                         ':-X',
77                         ':-D',
78                         '8-|',
79                         '8-O',
80                         ':-O',
81                         '\\o/',
82                         'o.O',
83                         'O.o',
84                         'o_O',
85                         'O_o',
86                         ":'(",
87                         ":-!",
88                         ":-/",
89                         ":-[",
90                         "8-)",
91                         ':beer',
92                         ':homebrew',
93                         ':coffee',
94                         ':facepalm',
95                         ':like',
96                         ':dislike',
97                         '~friendica',
98                         'red#',
99                         'red#matrix'
100
101                 ];
102
103                 $baseUrl = DI::baseUrl();
104
105                 $icons = [
106                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
107                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
108                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
109                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
110                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
111                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
112                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
113                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
114                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
115                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
116                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
117                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
118                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-laughing.gif" alt=":-D" title=":-D"  />',
119                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
120                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
121                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
122                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
123                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
124                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
125                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
126                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
127                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
128                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
129                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
130                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
131                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
132                 '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
133                 '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
134                 '<img class="smiley" src="' . $baseUrl . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
135                 '<img class="smiley" src="' . $baseUrl . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
136                 '<img class="smiley" src="' . $baseUrl . '/images/like.gif" alt=":like" title=":like" />',
137                 '<img class="smiley" src="' . $baseUrl . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
138                 '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . $baseUrl . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
139                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
140                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
141                 ];
142
143                 $params = ['texts' => $texts, 'icons' => $icons];
144                 Hook::callAll('smilie', $params);
145
146                 return $params;
147         }
148
149
150         /**
151          * Copied from http://php.net/manual/en/function.str-replace.php#88569
152          * Modified for camel caps: renamed stro_replace -> strOrigReplace
153          *
154          * When using str_replace(...), values that did not exist in the original string (but were put there by previous
155          * replacements) will be replaced continuously.  This string replacement function is designed to replace the values
156          * in $search with those in $replace while not factoring in prior replacements.  Note that this function will
157          * always look for the longest possible match first and then work its way down to individual characters.
158          *
159          * @param array $search list of strings or characters that need to be replaced
160          * @param array $replace list of strings or characters that will replace the corresponding values in $search
161          * @param string $subject the string on which this operation is being performed
162          *
163          * @return string $subject with all substrings in the $search array replaced by the values in the $replace array
164          */
165         private static function strOrigReplace($search, $replace, $subject)
166         {
167                 return strtr($subject, array_combine($search, $replace));
168         }
169
170         /**
171          * Replaces text emoticons with graphical images
172          *
173          * It is expected that this function will be called using HTML text.
174          * We will escape text between HTML pre and code blocks from being
175          * processed.
176          *
177          * At a higher level, the bbcode [nosmile] tag can be used to prevent this
178          * function from being executed by the prepare_text() routine when preparing
179          * bbcode source for HTML display
180          *
181          * @param string  $s         Text that should be replaced
182          * @param boolean $no_images Only replace emoticons without images
183          *
184          * @return string HTML Output of the Smilie
185          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
186          */
187         public static function replace($s, $no_images = false)
188         {
189                 $smilies = self::getList();
190
191                 $s = self::replaceFromArray($s, $smilies, $no_images);
192
193                 return $s;
194         }
195
196         /**
197          * Replaces emoji shortcodes in a string from a structured array of searches and replaces.
198          *
199          * Depends on system.no_smilies config value, skips <pre> and <code> tags.
200          *
201          * @param string $text      An HTML string
202          * @param array  $smilies   An string replacement array with the following structure: ['texts' => [], 'icons' => []]
203          * @param bool   $no_images Only replace shortcodes without image replacement (e.g. Unicode characters)
204          * @return string
205          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
206          */
207         public static function replaceFromArray($text, array $smilies, $no_images = false)
208         {
209                 if (intval(DI::config()->get('system', 'no_smilies'))
210                         || (local_user() && intval(DI::pConfig()->get(local_user(), 'system', 'no_smilies')))
211                 ) {
212                         return $text;
213                 }
214
215                 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::encode', $text);
216                 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::encode', $text);
217
218                 if ($no_images) {
219                         $cleaned = ['texts' => [], 'icons' => []];
220                         $icons = $smilies['icons'];
221                         foreach ($icons as $key => $icon) {
222                                 if (!strstr($icon, '<img ')) {
223                                         $cleaned['texts'][] = $smilies['texts'][$key];
224                                         $cleaned['icons'][] = $smilies['icons'][$key];
225                                 }
226                         }
227                         $smilies = $cleaned;
228                 }
229
230                 $text = preg_replace_callback('/&lt;(3+)/', 'self::pregHeart', $text);
231                 $text = self::strOrigReplace($smilies['texts'], $smilies['icons'], $text);
232
233                 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::decode', $text);
234                 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::decode', $text);
235
236                 return $text;
237         }
238
239         /**
240          * @param string $m string
241          *
242          * @return string base64 encoded string
243          */
244         private static function encode($m)
245         {
246                 return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
247         }
248
249         /**
250          * @param string $m string
251          *
252          * @return string base64 decoded string
253          * @throws \Exception
254          */
255         private static function decode($m)
256         {
257                 return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
258         }
259
260
261         /**
262          * expand <3333 to the correct number of hearts
263          *
264          * @param string $x string
265          *
266          * @return string HTML Output
267          *
268          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
269          */
270         private static function pregHeart($x)
271         {
272                 if (strlen($x[1]) == 1) {
273                         return $x[0];
274                 }
275
276                 $t = '';
277                 for ($cnt = 0; $cnt < strlen($x[1]); $cnt ++) {
278                         $t .= '❤';
279                 }
280
281                 $r =  str_replace($x[0], $t, $x[0]);
282                 return $r;
283         }
284 }