source: github/plugins/emoticons/emoticons.php @ 884add1

Last change on this file since 884add1 was 884add1, checked in by thomascube <thomas@…>, 15 months ago

Tagging plugins for 0.8-beta

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