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.

vcard_attachments.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Detects VCard attachments and show a button to add them to address book
  4. * Adds possibility to attach a contact vcard to mail messages
  5. *
  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->action == 'upload') {
  23. $this->add_hook('attachment_from_uri', array($this, 'attach_vcard'));
  24. }
  25. else if ($rcmail->action == 'compose' && !$rcmail->output->framed) {
  26. $skin_path = $this->local_skin_path();
  27. $btn_class = strpos($skin_path, 'classic') ? 'button' : 'listbutton';
  28. $this->add_texts('localization', true);
  29. $this->include_stylesheet($skin_path . '/style.css');
  30. $this->include_script('vcardattach.js');
  31. $this->add_button(
  32. array(
  33. 'type' => 'link',
  34. 'label' => 'vcard_attachments.vcard',
  35. 'command' => 'attach-vcard',
  36. 'class' => $btn_class . ' vcard disabled',
  37. 'classact' => $btn_class . ' vcard',
  38. 'title' => 'vcard_attachments.attachvcard',
  39. 'innerclass' => 'inner',
  40. ),
  41. 'compose-contacts-toolbar');
  42. }
  43. else if (!$rcmail->output->framed && (!$rcmail->action || $rcmail->action == 'list')) {
  44. $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard.png';
  45. $rcmail->output->set_env('vcard_icon', $icon);
  46. $this->include_script('vcardattach.js');
  47. }
  48. $this->register_action('plugin.savevcard', array($this, 'save_vcard'));
  49. }
  50. /**
  51. * Check message bodies and attachments for vcards
  52. */
  53. function message_load($p)
  54. {
  55. $this->message = $p['object'];
  56. // handle attachments vcard attachments
  57. foreach ((array)$this->message->attachments as $attachment) {
  58. if ($this->is_vcard($attachment)) {
  59. $this->vcard_parts[] = $attachment->mime_id;
  60. }
  61. }
  62. // the same with message bodies
  63. foreach ((array)$this->message->parts as $part) {
  64. if ($this->is_vcard($part)) {
  65. $this->vcard_parts[] = $part->mime_id;
  66. $this->vcard_bodies[] = $part->mime_id;
  67. }
  68. }
  69. if ($this->vcard_parts) {
  70. $this->add_texts('localization');
  71. }
  72. }
  73. /**
  74. * This callback function adds a box below the message content
  75. * if there is a vcard attachment available
  76. */
  77. function html_output($p)
  78. {
  79. $attach_script = false;
  80. foreach ($this->vcard_parts as $part) {
  81. $vcards = rcube_vcard::import($this->message->get_part_content($part, null, true));
  82. // successfully parsed vcards?
  83. if (empty($vcards)) {
  84. continue;
  85. }
  86. // remove part's body
  87. if (in_array($part, $this->vcard_bodies)) {
  88. $p['content'] = '';
  89. }
  90. foreach ($vcards as $idx => $vcard) {
  91. // skip invalid vCards
  92. if (empty($vcard->email) || empty($vcard->email[0])) {
  93. continue;
  94. }
  95. $display = $vcard->displayname . ' <'.$vcard->email[0].'>';
  96. // add box below message body
  97. $p['content'] .= html::p(array('class' => 'vcardattachment'),
  98. html::a(array(
  99. 'href' => "#",
  100. 'onclick' => "return plugin_vcard_save_contact('" . rcube::JQ($part.':'.$idx) . "')",
  101. 'title' => $this->gettext('addvcardmsg'),
  102. ),
  103. html::span(null, rcube::Q($display)))
  104. );
  105. }
  106. $attach_script = true;
  107. }
  108. if ($attach_script) {
  109. $this->include_script('vcardattach.js');
  110. $this->include_stylesheet($this->local_skin_path() . '/style.css');
  111. }
  112. return $p;
  113. }
  114. /**
  115. * Handler for request action
  116. */
  117. function save_vcard()
  118. {
  119. $this->add_texts('localization', true);
  120. $uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST);
  121. $mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
  122. $mime_id = rcube_utils::get_input_value('_part', rcube_utils::INPUT_POST);
  123. $rcmail = rcmail::get_instance();
  124. $message = new rcube_message($uid, $mbox);
  125. if ($uid && $mime_id) {
  126. list($mime_id, $index) = explode(':', $mime_id);
  127. $part = $message->get_part_content($mime_id, null, true);
  128. }
  129. $error_msg = $this->gettext('vcardsavefailed');
  130. if ($part && ($vcards = rcube_vcard::import($part))
  131. && ($vcard = $vcards[$index]) && $vcard->displayname && $vcard->email
  132. ) {
  133. $CONTACTS = $this->get_address_book();
  134. $email = $vcard->email[0];
  135. $contact = $vcard->get_assoc();
  136. $valid = true;
  137. // skip entries without an e-mail address or invalid
  138. if (empty($email) || !$CONTACTS->validate($contact, true)) {
  139. $valid = false;
  140. }
  141. else {
  142. // We're using UTF8 internally
  143. $email = rcube_utils::idn_to_utf8($email);
  144. // compare e-mail address
  145. $existing = $CONTACTS->search('email', $email, 1, false);
  146. // compare display name
  147. if (!$existing->count && $vcard->displayname) {
  148. $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
  149. }
  150. if ($existing->count) {
  151. $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
  152. $valid = false;
  153. }
  154. }
  155. if ($valid) {
  156. $plugin = $rcmail->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
  157. $contact = $plugin['record'];
  158. if (!$plugin['abort'] && $CONTACTS->insert($contact))
  159. $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
  160. else
  161. $rcmail->output->command('display_message', $error_msg, 'error');
  162. }
  163. }
  164. else {
  165. $rcmail->output->command('display_message', $error_msg, 'error');
  166. }
  167. $rcmail->output->send();
  168. }
  169. /**
  170. * Checks if specified message part is a vcard data
  171. *
  172. * @param rcube_message_part Part object
  173. *
  174. * @return boolean True if part is of type vcard
  175. */
  176. function is_vcard($part)
  177. {
  178. return (
  179. // Content-Type: text/vcard;
  180. $part->mimetype == 'text/vcard' ||
  181. // Content-Type: text/x-vcard;
  182. $part->mimetype == 'text/x-vcard' ||
  183. // Content-Type: text/directory; profile=vCard;
  184. ($part->mimetype == 'text/directory' && (
  185. ($part->ctype_parameters['profile'] &&
  186. strtolower($part->ctype_parameters['profile']) == 'vcard')
  187. // Content-Type: text/directory; (with filename=*.vcf)
  188. || ($part->filename && preg_match('/\.vcf$/i', $part->filename))
  189. )
  190. )
  191. );
  192. }
  193. /**
  194. * Getter for default (writable) addressbook
  195. */
  196. private function get_address_book()
  197. {
  198. if ($this->abook) {
  199. return $this->abook;
  200. }
  201. $rcmail = rcmail::get_instance();
  202. $abook = $rcmail->config->get('default_addressbook');
  203. // Get configured addressbook
  204. $CONTACTS = $rcmail->get_address_book($abook, true);
  205. // Get first writeable addressbook if the configured doesn't exist
  206. // This can happen when user deleted the addressbook (e.g. Kolab folder)
  207. if ($abook === null || $abook === '' || !is_object($CONTACTS)) {
  208. $source = reset($rcmail->get_address_sources(true));
  209. $CONTACTS = $rcmail->get_address_book($source['id'], true);
  210. }
  211. return $this->abook = $CONTACTS;
  212. }
  213. /**
  214. * Attaches a contact vcard to composed mail
  215. */
  216. public function attach_vcard($args)
  217. {
  218. if (preg_match('|^vcard://(.+)$|', $args['uri'], $m)) {
  219. list($cid, $source) = explode('-', $m[1]);
  220. $vcard = $this->get_contact_vcard($source, $cid, $filename);
  221. $params = array(
  222. 'filename' => $filename,
  223. 'mimetype' => 'text/vcard',
  224. );
  225. if ($vcard) {
  226. $args['attachment'] = rcmail_save_attachment($vcard, null, $args['compose_id'], $params);
  227. }
  228. }
  229. return $args;
  230. }
  231. /**
  232. * Get vcard data for specified contact
  233. */
  234. private function get_contact_vcard($source, $cid, &$filename = null)
  235. {
  236. $rcmail = rcmail::get_instance();
  237. $source = $rcmail->get_address_book($source);
  238. $contact = $source->get_record($cid, true);
  239. if ($contact) {
  240. $fieldmap = $source ? $source->vcard_map : null;
  241. if (empty($contact['vcard'])) {
  242. $vcard = new rcube_vcard('', RCUBE_CHARSET, false, $fieldmap);
  243. $vcard->reset();
  244. foreach ($contact as $key => $values) {
  245. list($field, $section) = explode(':', $key);
  246. // avoid unwanted casting of DateTime objects to an array
  247. // (same as in rcube_contacts::convert_save_data())
  248. if (is_object($values) && is_a($values, 'DateTime')) {
  249. $values = array($values);
  250. }
  251. foreach ((array) $values as $value) {
  252. if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) {
  253. $vcard->set($field, $value, strtoupper($section));
  254. }
  255. }
  256. }
  257. $contact['vcard'] = $vcard->export();
  258. }
  259. $name = rcube_addressbook::compose_list_name($contact);
  260. $filename = (self::parse_filename($name) ?: 'contact') . '.vcf';
  261. // fix folding and end-of-line chars
  262. $vcard = preg_replace('/\r|\n\s+/', '', $contact['vcard']);
  263. $vcard = preg_replace('/\n/', rcube_vcard::$eol, $vcard);
  264. return rcube_vcard::rfc2425_fold($vcard) . rcube_vcard::$eol;
  265. }
  266. }
  267. /**
  268. * Helper function to convert contact name into filename
  269. */
  270. static private function parse_filename($str)
  271. {
  272. $str = preg_replace('/[\t\n\r\0\x0B:\/]+\s*/', ' ', $str);
  273. return trim($str, " ./_");
  274. }
  275. }