]> git.mxchange.org Git - friendica.git/blob - src/Content/Smilies.php
ce785d66f44f48687160a8ed334a6fe01d3550ed
[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          * @return void
38          */
39         public static function add(&$b, $smiley, $representation)
40         {
41                 $found = array_search($smiley, $b['texts']);
42
43                 if (!is_int($found)) {
44                         $b['texts'][] = $smiley;
45                         $b['icons'][] = $representation;
46                 } else {
47                         $b['icons'][$found] = $representation;
48                 }
49         }
50
51         /**
52          * @brief Function to list all smilies
53          *
54          * Get an array of all smilies, both internal and from addons.
55          *
56          * @return array
57          *      'texts' => smilie shortcut
58          *      'icons' => icon in html
59          *
60          * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
61          */
62         public static function getList()
63         {
64                 $texts =  array(
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                 $icons = array(
104                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
105                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.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-smile.gif" alt=":-)" title=":-)" />',
108                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
109                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
110                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
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-kiss.gif" alt=":-\" title=":-\" />',
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=":-x" title=":-x" />',
115                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
116                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-laughing.gif" alt=":-D" title=":-D"  />',
117                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
118                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
119                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
120                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\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-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-cry.gif" alt=":\'(" title=":\'("/>',
126                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
127                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
128                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
129                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
130                 '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
131                 '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
132                 '<img class="smiley" src="' . System::baseUrl() . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
133                 '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
134                 '<img class="smiley" src="' . System::baseUrl() . '/images/like.gif" alt=":like" title=":like" />',
135                 '<img class="smiley" src="' . System::baseUrl() . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
136                 '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . System::baseUrl() . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
137                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
138                 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
139                 );
140
141                 $params = array('texts' => $texts, 'icons' => $icons);
142                 call_hooks('smilie', $params);
143
144                 return $params;
145         }
146
147         /**
148          * @brief Replaces text emoticons with graphical images
149          *
150          * It is expected that this function will be called using HTML text.
151          * We will escape text between HTML pre and code blocks from being
152          * processed.
153          *
154          * At a higher level, the bbcode [nosmile] tag can be used to prevent this
155          * function from being executed by the prepare_text() routine when preparing
156          * bbcode source for HTML display
157          *
158          * @param string  $s         Text that should be replaced
159          * @param boolean $sample    optional, default false
160          * @param boolean $no_images Only replace emoticons without images
161          *
162          * @return string HML Output of the Smilie
163          */
164         public static function replace($s, $sample = false, $no_images = false)
165         {
166                 if (intval(Config::get('system', 'no_smilies'))
167                         || (local_user() && intval(PConfig::get(local_user(), 'system', 'no_smilies')))
168                 ) {
169                         return $s;
170                 }
171
172                 $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism', 'self::encode', $s);
173                 $s = preg_replace_callback('/<code>(.*?)<\/code>/ism', 'self::encode', $s);
174
175                 $params = self::getList();
176
177                 if ($no_images) {
178                         $cleaned = array('texts' => array(), 'icons' => array());
179                         $icons = $params['icons'];
180                         foreach ($icons as $key => $icon) {
181                                 if (!strstr($icon, '<img ')) {
182                                         $cleaned['texts'][] = $params['texts'][$key];
183                                         $cleaned['icons'][] = $params['icons'][$key];
184                                 }
185                         }
186                         $params = $cleaned;
187                 }
188
189                 $params['string'] = $s;
190
191                 if ($sample) {
192                         $s = '<div class="smiley-sample">';
193                         for ($x = 0; $x < count($params['texts']); $x ++) {
194                                 $s .= '<dl><dt>' . $params['texts'][$x] . '</dt><dd>' . $params['icons'][$x] . '</dd></dl>';
195                         }
196                 } else {
197                         $params['string'] = preg_replace_callback('/&lt;(3+)/', 'self::pregHeart', $params['string']);
198                         $s = str_replace($params['texts'], $params['icons'], $params['string']);
199                 }
200
201                 $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism', 'self::decode', $s);
202                 $s = preg_replace_callback('/<code>(.*?)<\/code>/ism', 'self::decode', $s);
203
204                 return $s;
205         }
206
207         /**
208          * @param string $m string
209          *
210          * @return string base64 encoded string
211          */
212         private static function encode($m)
213         {
214                 return(str_replace($m[1], base64url_encode($m[1]), $m[0]));
215         }
216
217         /**
218          * @param string $m string
219          *
220          * @return string base64 decoded string
221          */
222         private static function decode($m)
223         {
224                 return(str_replace($m[1], base64url_decode($m[1]), $m[0]));
225         }
226
227
228         /**
229          * @brief expand <3333 to the correct number of hearts
230          *
231          * @param string $x string
232          *
233          * @return string HTML Output
234          *
235          * @todo: Rework because it doesn't work correctly
236          */
237         private static function pregHeart($x)
238         {
239                 if (strlen($x[1]) == 1) {
240                         return $x[0];
241                 }
242                 $t = '';
243                 for ($cnt = 0; $cnt < strlen($x[1]); $cnt ++) {
244                         $t .= '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" />';
245                 }
246                 $r =  str_replace($x[0], $t, $x[0]);
247                 return $r;
248         }
249 }