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