Aleksander Machniak
2016-04-02 6f2c007d1be866e47bf6a9f8e6900fe6ec2a6901
commit | author | age
48e9c1 1 <?php
T 2
3 /**
a63f14 4  * Emoticons
48e9c1 5  *
a63f14 6  * Plugin to replace emoticons in plain text message body with real icons.
AM 7  * Also it enables emoticons in HTML compose editor. Both features are optional.
48e9c1 8  *
T 9  * @version @package_version@
10  * @license GNU GPLv3+
11  * @author Thomas Bruederli
12  * @author Aleksander Machniak
13  * @website http://roundcube.net
14  */
15 class emoticons extends rcube_plugin
16 {
a63f14 17     public $task = 'mail|settings|utils';
48e9c1 18
a63f14 19
AM 20     /**
21      * Plugin initilization.
22      */
48e9c1 23     function init()
T 24     {
a63f14 25         $rcube = rcube::get_instance();
AM 26
27         $this->add_hook('message_part_after', array($this, 'message_part_after'));
28         $this->add_hook('message_outgoing_body', array($this, 'message_outgoing_body'));
29         $this->add_hook('html2text', array($this, 'html2text'));
30         $this->add_hook('html_editor', array($this, 'html_editor'));
31
32         if ($rcube->task == 'settings') {
33             $this->add_hook('preferences_list', array($this, 'preferences_list'));
34             $this->add_hook('preferences_save', array($this, 'preferences_save'));
35         }
48e9c1 36     }
T 37
a63f14 38     /**
AM 39      * 'message_part_after' hook handler to replace common plain text emoticons
40      * with emoticon images (<img>)
41      */
42     function message_part_after($args)
48e9c1 43     {
T 44         if ($args['type'] == 'plain') {
a63f14 45             $this->load_config();
AM 46
47             $rcube = rcube::get_instance();
48             if (!$rcube->config->get('emoticons_display', false)) {
49                 return $args;
50             }
51
52             require_once __DIR__ . '/emoticons_engine.php';
53
54             $args['body'] = emoticons_engine::text2icons($args['body']);
48e9c1 55         }
T 56
57         return $args;
58     }
59
a63f14 60     /**
AM 61      * 'message_outgoing_body' hook handler to replace image emoticons from TinyMCE
62      * editor with image attachments.
63      */
64     function message_outgoing_body($args)
6fa5b4 65     {
a63f14 66         if ($args['type'] == 'html') {
AM 67             $this->load_config();
68
69             $rcube = rcube::get_instance();
70             if (!$rcube->config->get('emoticons_compose', true)) {
71                 return $args;
72             }
73
74             require_once __DIR__ . '/emoticons_engine.php';
75
76             // look for "emoticon" images from TinyMCE and change their src paths to
77             // be file paths on the server instead of URL paths.
78             $images = emoticons_engine::replace($args['body']);
79
80             // add these images as attachments to the MIME message
81             foreach ($images as $img_name => $img_file) {
82                 $args['message']->addHTMLImage($img_file, 'image/gif', '', true, $img_name);
83             }
84         }
85
86         return $args;
87     }
88
89     /**
90      * 'html2text' hook handler to replace image emoticons from TinyMCE
91      * editor with plain text emoticons.
92      *
93      * This is executed on html2text action, i.e. when switching from HTML to text
94      * in compose window (or similiar place). Also when generating alternative
95      * text/plain part.
96      */
97     function html2text($args)
98     {
99         $rcube = rcube::get_instance();
100
101         if ($rcube->action == 'html2text' || $rcube->action == 'send') {
102             $this->load_config();
103
104             if (!$rcube->config->get('emoticons_compose', true)) {
105                 return $args;
106             }
107
108             require_once __DIR__ . '/emoticons_engine.php';
109
110             $args['body'] = emoticons_engine::icons2text($args['body']);
111         }
112
113         return $args;
114     }
115
116     /**
117      * 'html_editor' hook handler, where we enable emoticons in TinyMCE
118      */
119     function html_editor($args)
120     {
121         $rcube = rcube::get_instance();
122
123         $this->load_config();
124
2aa9ee 125         if ($rcube->config->get('emoticons_compose', true)) {
AM 126             $args['extra_plugins'][] = 'emoticons';
127             $args['extra_buttons'][] = 'emoticons';
a63f14 128         }
AM 129
130         return $args;
131     }
132
133     /**
134      * 'preferences_list' hook handler
135      */
136     function preferences_list($args)
137     {
138         $rcube         = rcube::get_instance();
139         $dont_override = $rcube->config->get('dont_override', array());
140
141         if ($args['section'] == 'mailview' && !in_array('emoticons_display', $dont_override)) {
142             $this->load_config();
143             $this->add_texts('localization');
144
145             $field_id = 'emoticons_display';
146             $checkbox = new html_checkbox(array('name' => '_' . $field_id, 'id' => $field_id, 'value' => 1));
147
148             $args['blocks']['main']['options']['emoticons_display'] = array(
149                     'title'   => $this->gettext('emoticonsdisplay'),
150                     'content' => $checkbox->show(intval($rcube->config->get('emoticons_display', false)))
151             );
152         }
153         else if ($args['section'] == 'compose' && !in_array('emoticons_compose', $dont_override)) {
154             $this->load_config();
155             $this->add_texts('localization');
156
157             $field_id = 'emoticons_compose';
158             $checkbox = new html_checkbox(array('name' => '_' . $field_id, 'id' => $field_id, 'value' => 1));
159
160             $args['blocks']['main']['options']['emoticons_compose'] = array(
161                     'title'   => $this->gettext('emoticonscompose'),
162                     'content' => $checkbox->show(intval($rcube->config->get('emoticons_compose', true)))
163             );
164         }
165
166         return $args;
167     }
168
169     /**
170      * 'preferences_save' hook handler
171      */
172     function preferences_save($args)
173     {
174         $rcube         = rcube::get_instance();
175         $dont_override = $rcube->config->get('dont_override', array());
176
177         if ($args['section'] == 'mailview' && !in_array('emoticons_display', $dont_override)) {
6f2c00 178             $args['prefs']['emoticons_display'] = !empty(rcube_utils::get_input_value('_emoticons_display', rcube_utils::INPUT_POST));
a63f14 179         }
AM 180         else if ($args['section'] == 'compose' && !in_array('emoticons_compose', $dont_override)) {
6f2c00 181             $args['prefs']['emoticons_compose'] = !empty(rcube_utils::get_input_value('_emoticons_compose', rcube_utils::INPUT_POST));
a63f14 182         }
AM 183
184         return $args;
48e9c1 185     }
T 186 }