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