Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

vcard_attachments.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Detect VCard attachments and show a button to add them to address book
  4. *
  5. * @version @package_version@
  6. * @license GNU GPLv3+
  7. * @author Thomas Bruederli, Aleksander Machniak
  8. */
  9. class vcard_attachments extends rcube_plugin
  10. {
  11. public $task = 'mail';
  12. private $message;
  13. private $vcard_parts = array();
  14. private $vcard_bodies = array();
  15. function init()
  16. {
  17. $rcmail = rcmail::get_instance();
  18. if ($rcmail->action == 'show' || $rcmail->action == 'preview') {
  19. $this->add_hook('message_load', array($this, 'message_load'));
  20. $this->add_hook('template_object_messagebody', array($this, 'html_output'));
  21. }
  22. else if (!$rcmail->output->framed && (!$rcmail->action || $rcmail->action == 'list')) {
  23. $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard.png';
  24. $rcmail->output->set_env('vcard_icon', $icon);
  25. $this->include_script('vcardattach.js');
  26. }
  27. $this->register_action('plugin.savevcard', array($this, 'save_vcard'));
  28. }
  29. /**
  30. * Check message bodies and attachments for vcards
  31. */
  32. function message_load($p)
  33. {
  34. $this->message = $p['object'];
  35. // handle attachments vcard attachments
  36. foreach ((array)$this->message->attachments as $attachment) {
  37. if ($this->is_vcard($attachment)) {
  38. $this->vcard_parts[] = $attachment->mime_id;
  39. }
  40. }
  41. // the same with message bodies
  42. foreach ((array)$this->message->parts as $part) {
  43. if ($this->is_vcard($part)) {
  44. $this->vcard_parts[] = $part->mime_id;
  45. $this->vcard_bodies[] = $part->mime_id;
  46. }
  47. }
  48. if ($this->vcard_parts)
  49. $this->add_texts('localization');
  50. }
  51. /**
  52. * This callback function adds a box below the message content
  53. * if there is a vcard attachment available
  54. */
  55. function html_output($p)
  56. {
  57. $attach_script = false;
  58. foreach ($this->vcard_parts as $part) {
  59. $vcards = rcube_vcard::import($this->message->get_part_content($part, null, true));
  60. // successfully parsed vcards?
  61. if (empty($vcards)) {
  62. continue;
  63. }
  64. // remove part's body
  65. if (in_array($part, $this->vcard_bodies)) {
  66. $p['content'] = '';
  67. }
  68. foreach ($vcards as $idx => $vcard) {
  69. // skip invalid vCards
  70. if (empty($vcard->email) || empty($vcard->email[0])) {
  71. continue;
  72. }
  73. $display = $vcard->displayname . ' <'.$vcard->email[0].'>';
  74. // add box below message body
  75. $p['content'] .= html::p(array('class' => 'vcardattachment'),
  76. html::a(array(
  77. 'href' => "#",
  78. 'onclick' => "return plugin_vcard_save_contact('" . rcube::JQ($part.':'.$idx) . "')",
  79. 'title' => $this->gettext('addvcardmsg'),
  80. ),
  81. html::span(null, rcube::Q($display)))
  82. );
  83. }
  84. $attach_script = true;
  85. }
  86. if ($attach_script) {
  87. $this->include_script('vcardattach.js');
  88. $this->include_stylesheet($this->local_skin_path() . '/style.css');
  89. }
  90. return $p;
  91. }
  92. /**
  93. * Handler for request action
  94. */
  95. function save_vcard()
  96. {
  97. $this->add_texts('localization', true);
  98. $uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST);
  99. $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
  100. $mime_id = rcube_utils::get_input_value('_part', rcube_utils::INPUT_POST);
  101. $rcmail = rcmail::get_instance();
  102. $message = new rcube_message($uid, $mbox);
  103. if ($uid && $mime_id) {
  104. list($mime_id, $index) = explode(':', $mime_id);
  105. $part = $message->get_part_content($mime_id, null, true);
  106. }
  107. $error_msg = $this->gettext('vcardsavefailed');
  108. if ($part && ($vcards = rcube_vcard::import($part))
  109. && ($vcard = $vcards[$index]) && $vcard->displayname && $vcard->email
  110. ) {
  111. $CONTACTS = $this->get_address_book();
  112. $email = $vcard->email[0];
  113. $contact = $vcard->get_assoc();
  114. $valid = true;
  115. // skip entries without an e-mail address or invalid
  116. if (empty($email) || !$CONTACTS->validate($contact, true)) {
  117. $valid = false;
  118. }
  119. else {
  120. // We're using UTF8 internally
  121. $email = rcube_utils::idn_to_utf8($email);
  122. // compare e-mail address
  123. $existing = $CONTACTS->search('email', $email, 1, false);
  124. // compare display name
  125. if (!$existing->count && $vcard->displayname) {
  126. $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
  127. }
  128. if ($existing->count) {
  129. $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
  130. $valid = false;
  131. }
  132. }
  133. if ($valid) {
  134. $plugin = $rcmail->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
  135. $contact = $plugin['record'];
  136. if (!$plugin['abort'] && $CONTACTS->insert($contact))
  137. $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
  138. else
  139. $rcmail->output->command('display_message', $error_msg, 'error');
  140. }
  141. }
  142. else {
  143. $rcmail->output->command('display_message', $error_msg, 'error');
  144. }
  145. $rcmail->output->send();
  146. }
  147. /**
  148. * Checks if specified message part is a vcard data
  149. *
  150. * @param rcube_message_part Part object
  151. *
  152. * @return boolean True if part is of type vcard
  153. */
  154. function is_vcard($part)
  155. {
  156. return (
  157. // Content-Type: text/vcard;
  158. $part->mimetype == 'text/vcard' ||
  159. // Content-Type: text/x-vcard;
  160. $part->mimetype == 'text/x-vcard' ||
  161. // Content-Type: text/directory; profile=vCard;
  162. ($part->mimetype == 'text/directory' && (
  163. ($part->ctype_parameters['profile'] &&
  164. strtolower($part->ctype_parameters['profile']) == 'vcard')
  165. // Content-Type: text/directory; (with filename=*.vcf)
  166. || ($part->filename && preg_match('/\.vcf$/i', $part->filename))
  167. )
  168. )
  169. );
  170. }
  171. /**
  172. * Getter for default (writable) addressbook
  173. */
  174. private function get_address_book()
  175. {
  176. if ($this->abook) {
  177. return $this->abook;
  178. }
  179. $rcmail = rcmail::get_instance();
  180. $abook = $rcmail->config->get('default_addressbook');
  181. // Get configured addressbook
  182. $CONTACTS = $rcmail->get_address_book($abook, true);
  183. // Get first writeable addressbook if the configured doesn't exist
  184. // This can happen when user deleted the addressbook (e.g. Kolab folder)
  185. if ($abook === null || $abook === '' || !is_object($CONTACTS)) {
  186. $source = reset($rcmail->get_address_sources(true));
  187. $CONTACTS = $rcmail->get_address_book($source['id'], true);
  188. }
  189. return $this->abook = $CONTACTS;
  190. }
  191. }