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