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