emoticons.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Emoticons.
*
* Plugin to replace emoticons in plain text message body with real emoji.
* Also it enables emoticons in HTML compose editor. Both features are optional.
*
* @license GNU GPLv3+
* @author Thomas Bruederli
* @author Aleksander Machniak
* @website https://roundcube.net
*/
class emoticons extends rcube_plugin
{
public $task = 'mail|settings|utils';
/**
* Plugin initialization.
*/
function init()
{
$rcube = rcube::get_instance();
$this->add_hook('message_part_after', [$this, 'message_part_after']);
$this->add_hook('html_editor', [$this, 'html_editor']);
if ($rcube->task == 'settings') {
$this->add_hook('preferences_list', [$this, 'preferences_list']);
$this->add_hook('preferences_save', [$this, 'preferences_save']);
}
}
/**
* 'message_part_after' hook handler to replace common
* plain text emoticons with emoji
*/
function message_part_after($args)
{
if ($args['type'] == 'plain') {
$this->load_config();
$rcube = rcube::get_instance();
if (!$rcube->config->get('emoticons_display', false)) {
return $args;
}
$args['body'] = self::text2icons($args['body']);
}
return $args;
}
/**
* 'html_editor' hook handler, where we enable emoticons in TinyMCE
*/
function html_editor($args)
{
$rcube = rcube::get_instance();
$this->load_config();
if ($rcube->config->get('emoticons_compose', true)) {
$args['extra_plugins'][] = 'emoticons';
$args['extra_buttons'][] = 'emoticons';
}
return $args;
}
/**
* 'preferences_list' hook handler
*/
function preferences_list($args)
{
$rcube = rcube::get_instance();
$dont_override = $rcube->config->get('dont_override', []);
if ($args['section'] == 'mailview' && !in_array('emoticons_display', $dont_override)) {
$this->load_config();
$this->add_texts('localization');
$field_id = 'emoticons_display';
$checkbox = new html_checkbox(['name' => '_' . $field_id, 'id' => $field_id, 'value' => 1]);
$args['blocks']['main']['options']['emoticons_display'] = [
'title' => html::label($field_id, $this->gettext('emoticonsdisplay')),
'content' => $checkbox->show(intval($rcube->config->get('emoticons_display', false)))
];
}
else if ($args['section'] == 'compose' && !in_array('emoticons_compose', $dont_override)) {
$this->load_config();
$this->add_texts('localization');
$field_id = 'emoticons_compose';
$checkbox = new html_checkbox(['name' => '_' . $field_id, 'id' => $field_id, 'value' => 1]);
$args['blocks']['main']['options']['emoticons_compose'] = [
'title' => html::label($field_id, $this->gettext('emoticonscompose')),
'content' => $checkbox->show(intval($rcube->config->get('emoticons_compose', true)))
];
}
return $args;
}
/**
* 'preferences_save' hook handler
*/
function preferences_save($args)
{
if ($args['section'] == 'mailview') {
$args['prefs']['emoticons_display'] = (bool) rcube_utils::get_input_value('_emoticons_display', rcube_utils::INPUT_POST);
}
else if ($args['section'] == 'compose') {
$args['prefs']['emoticons_compose'] = (bool) rcube_utils::get_input_value('_emoticons_compose', rcube_utils::INPUT_POST);
}
return $args;
}
/**
* Replace common plain text emoticons with emoji
*
* @param string $text Text
*
* @return string Converted text
*/
protected static function text2icons($text)
{
// This is a lookbehind assertion which will exclude html entities
// E.g. situation when ";)" in "")" shouldn't be replaced by the icon
// It's so long because of assertion format restrictions
$entity = '(?<!&'
. '[a-zA-Z0-9]{2}' . '|' . '#[0-9]{2}' . '|'
. '[a-zA-Z0-9]{3}' . '|' . '#[0-9]{3}' . '|'
. '[a-zA-Z0-9]{4}' . '|' . '#[0-9]{4}' . '|'
. '[a-zA-Z0-9]{5}' . '|'
. '[a-zA-Z0-9]{6}' . '|'
. '[a-zA-Z0-9]{7}'
. ')';
// map of emoticon replacements
$map = [
'/(?<!mailto):-?D/' => self::ico_tag('1f603', ':D' ), // laugh
'/:-?\(/' => self::ico_tag('1f626', ':(' ), // frown
'/'.$entity.';-?\)/' => self::ico_tag('1f609', ';)' ), // wink
'/8-?\)/' => self::ico_tag('1f60e', '8)' ), // cool
'/(?<!mailto):-?O/i' => self::ico_tag('1f62e', ':O' ), // surprised
'/(?<!mailto):-?P/i' => self::ico_tag('1f61b', ':P' ), // tongue out
'/(?<!mailto):-?@/i' => self::ico_tag('1f631', ':-@' ), // yell
'/O:-?\)/i' => self::ico_tag('1f607', 'O:-)' ), // innocent
'/(?<!O):-?\)/' => self::ico_tag('1f60a', ':-)' ), // smile
'/(?<!mailto):-?\$/' => self::ico_tag('1f633', ':-$' ), // embarrassed
'/(?<!mailto):-?\*/i' => self::ico_tag('1f48b', ':-*' ), // kiss
'/(?<!mailto):-?S/i' => self::ico_tag('1f615', ':-S' ), // undecided
];
return preg_replace(array_keys($map), array_values($map), $text);
}
protected static function ico_tag($ico, $title)
{
return html::span(['title' => $title], "&#x{$ico};");
}
}