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