You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

emoticons.php 5.9KB

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