]> git.mxchange.org Git - friendica.git/blob - src/Content/Smilies.php
464b5ee83ee28bad158025565c9bed59c610e832
[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\Config;
19 use Friendica\Core\PConfig;
20 use Friendica\Core\System;
21
22 /**
23  * This class contains functions to handle smiles
24  */
25
26 class Smilies
27 {
28         /**
29          * @brief Replaces/adds the emoticon list
30          *
31          * This function should be used whenever emoticons are added
32          *
33          * @param array  $b              Array of emoticons
34          * @param string $smiley         The text smilie
35          * @param string $representation The replacement
36          */
37         public static function add(&$b, $smiley, $representation)
38         {
39                 $found = array_search($smiley, $b['texts']);
40
41                 if (!is_int($found)) {
42                         $b['texts'][] = $smiley;
43                         $b['icons'][] = $representation;
44                 } else {
45                         $b['icons'][$found] = $representation;
46                 }
47         }
48
49         /**
50          * @brief Function to list all smilies
51          *
52          * Get an array of all smilies, both internal and from addons.
53          *
54          * @return array
55          *      'texts' => smilie shortcut
56          *      'icons' => icon in html
57          *
58          * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
59          */
60         public static function get_list()
61         {
62                 $texts =  array(
63                         '&lt;3',
64                         '&lt;/3',
65                         '&lt;\\3',
66                         ':-)',
67                         ';-)',
68                         ':-(',
69                         ':-P',
70                         ':-p',
71                         ':-"',
72                         ':-&quot;',
73                         ':-x',
74                         ':-X',
75                         ':-D',
76                         '8-|',
77                         '8-O',
78                         ':-O',
79                         '\\o/',
80                         'o.O',
81                         'O.o',
82                         'o_O',
83                         'O_o',
84                         ":'(",
85                         ":-!",
86                         ":-/",
87                         ":-[",
88                         "8-)",
89                         ':beer',
90                         ':homebrew',
91                         ':coffee',
92                         ':facepalm',
93                         ':like',
94                         ':dislike',
95                         '~friendica',
96                         'red#',
97                         'red#matrix'
98
99                 );
100
101                 $icons = array(
102                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
103                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
104                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
105                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
106                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
107                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
108                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
109                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
110                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
111                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
112                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
113                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
114                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-laughing.gif" alt=":-D" title=":-D"  />',
115                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
116                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
117                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
118                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
119                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
120                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
121                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o_O" title="o_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-cry.gif" alt=":\'(" title=":\'("/>',
124                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
125                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
126                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
127                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
128                 '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
129                 '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
130                 '<img class="smiley" src="' . System::baseUrl() . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
131                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
132                 '<img class="smiley" src="' . System::baseUrl() . '/images/like.gif" alt=":like" title=":like" />',
133                 '<img class="smiley" src="' . System::baseUrl() . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
134                 '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . System::baseUrl() . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
135                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
136                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
137                 );
138
139                 $params = array('texts' => $texts, 'icons' => $icons);
140                 call_hooks('smilie', $params);
141
142                 return $params;
143         }
144
145         /**
146          * @brief Replaces text emoticons with graphical images
147          *
148          * It is expected that this function will be called using HTML text.
149          * We will escape text between HTML pre and code blocks from being
150          * processed.
151          *
152          * At a higher level, the bbcode [nosmile] tag can be used to prevent this
153          * function from being executed by the prepare_text() routine when preparing
154          * bbcode source for HTML display
155          *
156          * @param string  $s         Text that should be replaced
157          * @param boolean $sample
158          * @param boolean $no_images Only replace emoticons without images
159          *
160          * @return string HML Output of the Smilie
161          */
162         public static function replace($s, $sample = false, $no_images = false)
163         {
164                 if (intval(Config::get('system', 'no_smilies'))
165                         || (local_user() && intval(PConfig::get(local_user(), 'system', 'no_smilies')))
166                 ) {
167                         return $s;
168                 }
169
170                 $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism', 'self::encode', $s);
171                 $s = preg_replace_callback('/<code>(.*?)<\/code>/ism', 'self::encode', $s);
172
173                 $params = self::get_list();
174
175                 if ($no_images) {
176                         $cleaned = array('texts' => array(), 'icons' => array());
177                         $icons = $params['icons'];
178                         foreach ($icons as $key => $icon) {
179                                 if (!strstr($icon, '<img ')) {
180                                         $cleaned['texts'][] = $params['texts'][$key];
181                                         $cleaned['icons'][] = $params['icons'][$key];
182                                 }
183                         }
184                         $params = $cleaned;
185                 }
186
187                 $params['string'] = $s;
188
189                 if ($sample) {
190                         $s = '<div class="smiley-sample">';
191                         for ($x = 0; $x < count($params['texts']); $x ++) {
192                                 $s .= '<dl><dt>' . $params['texts'][$x] . '</dt><dd>' . $params['icons'][$x] . '</dd></dl>';
193                         }
194                 } else {
195                         $params['string'] = preg_replace_callback('/&lt;(3+)/', 'self::preg_heart', $params['string']);
196                         $s = str_replace($params['texts'], $params['icons'], $params['string']);
197                 }
198
199                 $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism', 'self::decode', $s);
200                 $s = preg_replace_callback('/<code>(.*?)<\/code>/ism', 'self::decode', $s);
201
202                 return $s;
203         }
204
205         private static function encode($m)
206         {
207                 return(str_replace($m[1], base64url_encode($m[1]), $m[0]));
208         }
209
210         private static function decode($m)
211         {
212                 return(str_replace($m[1], base64url_decode($m[1]), $m[0]));
213         }
214
215
216         /**
217          * @brief expand <3333 to the correct number of hearts
218          *
219          * @param string $x
220          *
221          * @return string HTML Output
222          *
223          * @todo: Rework because it doesn't work correctly
224          */
225         private static function preg_heart($x)
226         {
227                 if (strlen($x[1]) == 1) {
228                         return $x[0];
229                 }
230                 $t = '';
231                 for ($cnt = 0; $cnt < strlen($x[1]); $cnt ++) {
232                         $t .= '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" />';
233                 }
234                 $r =  str_replace($x[0], $t, $x[0]);
235                 return $r;
236         }
237 }