thomascube
2010-12-17 db1a87cd6c506f2afbd1a37c64cb56ae11120b49
commit | author | age
5fec6d 1 <?php
T 2
3 /**
4  * Display Emoticons
5  *
6  * Sample plugin to replace emoticons in plain text message body with real icons
7  *
db1a87 8  * @version 1.3
5fec6d 9  * @author Thomas Bruederli
T 10  * @author Aleksander Machniak
11  * @website http://roundcube.net
12  */
13 class emoticons extends rcube_plugin
14 {
15     public $task = 'mail';
16
17     function init()
18     {
19         $this->add_hook('message_part_after', array($this, 'replace'));
20     }
21
22     function replace($args)
23     {
24         // This is a lookbehind assertion which will exclude html entities
25         // E.g. situation when ";)" in "&quot;)" shouldn't be replaced by the icon
26         // It's so long because of assertion format restrictions
27         $entity = '(?<!&'
28             . '[a-zA-Z0-9]{2}' . '|' . '#[0-9]{2}' . '|'
29             . '[a-zA-Z0-9]{3}' . '|' . '#[0-9]{3}' . '|'
30             . '[a-zA-Z0-9]{4}' . '|' . '#[0-9]{4}' . '|'
31             . '[a-zA-Z0-9]{5}' . '|'
32             . '[a-zA-Z0-9]{6}' . '|'
33             . '[a-zA-Z0-9]{7}'
34             . ')';
35
36         // map of emoticon replacements
37         $map = array(
db1a87 38             '/:\)/'             => $this->img_tag('smiley-smile.gif',       ':)'    ),
T 39             '/:-\)/'            => $this->img_tag('smiley-smile.gif',       ':-)'   ),
40             '/(?<!mailto):D/'   => $this->img_tag('smiley-laughing.gif',    ':D'    ),
41             '/:-D/'             => $this->img_tag('smiley-laughing.gif',    ':-D'   ),
42             '/:\(/'             => $this->img_tag('smiley-frown.gif',       ':('    ),
43             '/:-\(/'            => $this->img_tag('smiley-frown.gif',       ':-('   ),
44             '/'.$entity.';\)/'  => $this->img_tag('smiley-wink.gif',        ';)'    ),
45             '/'.$entity.';-\)/' => $this->img_tag('smiley-wink.gif',        ';-)'   ),
46             '/8\)/'             => $this->img_tag('smiley-cool.gif',        '8)'    ),
47             '/8-\)/'            => $this->img_tag('smiley-cool.gif',        '8-)'   ),
48             '/(?<!mailto):O/i'  => $this->img_tag('smiley-surprised.gif',   ':O'    ),
49             '/(?<!mailto):-O/i' => $this->img_tag('smiley-surprised.gif',   ':-O'   ),
50             '/(?<!mailto):P/i'  => $this->img_tag('smiley-tongue-out.gif',  ':P'    ),
51             '/(?<!mailto):-P/i' => $this->img_tag('smiley-tongue-out.gif',  ':-P'   ),
52             '/(?<!mailto):@/i'  => $this->img_tag('smiley-yell.gif',        ':@'    ),
53             '/(?<!mailto):-@/i' => $this->img_tag('smiley-yell.gif',        ':-@'   ),
54             '/O:\)/i'           => $this->img_tag('smiley-innocent.gif',    'O:)'   ),
55             '/O:-\)/i'          => $this->img_tag('smiley-innocent.gif',    'O:-)'  ),
56             '/(?<!mailto):$/'   => $this->img_tag('smiley-embarassed.gif',  ':$'    ),
57             '/(?<!mailto):-$/'  => $this->img_tag('smiley-embarassed.gif',  ':-$'   ),
58             '/(?<!mailto):\*/i'  => $this->img_tag('smiley-kiss.gif',       ':*'    ),
59             '/(?<!mailto):-\*/i' => $this->img_tag('smiley-kiss.gif',       ':-*'   ),
60             '/(?<!mailto):S/i'  => $this->img_tag('smiley-undecided.gif',   ':S'    ),
61             '/(?<!mailto):-S/i' => $this->img_tag('smiley-undecided.gif',   ':-S'   ),
5fec6d 62         );
T 63
64         if ($args['type'] == 'plain') {
65             $args['body'] = preg_replace(
66                 array_keys($map), array_values($map), $args['body']);
67         }
68
69         return $args;
70     }
71
db1a87 72     private function img_tag($ico, $title)
T 73     { 
74         $path = './program/js/tiny_mce/plugins/emotions/img/';
75         return html::img(array('src' => $path.$ico, 'title' => $title));
76     }
5fec6d 77 }