From 95ea8445b545f98bd424a8400d1d96726549f625 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Wed, 9 May 2018 10:05:34 +0200 Subject: [PATCH] * Addressbook: allow to import or paste S/Mime or PGP public keys via pubkey field into Vfs --- addressbook/inc/class.addressbook_bo.inc.php | 50 ++++++++++++++++++- ...ss.addressbook_import_contacts_csv.inc.php | 4 +- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/addressbook/inc/class.addressbook_bo.inc.php b/addressbook/inc/class.addressbook_bo.inc.php index 91418d2e4a..2eb0c8d849 100755 --- a/addressbook/inc/class.addressbook_bo.inc.php +++ b/addressbook/inc/class.addressbook_bo.inc.php @@ -155,7 +155,7 @@ class addressbook_bo extends Api\Contacts } /** - * Where to store public key delpending on type and storage backend + * Where to store public key depending on type and storage backend * * @param boolean $pgp true: PGP, false: S/Mime * @param array $contact =null contact array to pass to get_backend() @@ -380,4 +380,52 @@ class addressbook_bo extends Api\Contacts { return $this->set_keys($keys, false, $allow_user_updates); } + + /** + * Saves contact + * + * Reimplemented to strip pubkeys pasted into pubkey field or imported and store them as files in Vfs. + * We allways store PGP pubkeys to Vfs, but S/Mime ones only for SQL backend, not for LDAP or AD. + * + * @param array &$contact contact array from etemplate::exec + * @param boolean $ignore_acl =false should the acl be checked or not + * @param boolean $touch_modified =true should modified/r be updated + * @return int|string|boolean id on success, false on failure, the error-message is in $this->error + */ + function save(&$contact, $ignore_acl=false, $touch_modified=true) + { + if (($id = parent::save($contact, $ignore_acl, $touch_modified)) && !empty($contact['pubkey'])) + { + $files = 0; + foreach(array( + array(addressbook_bo::$pgp_key_regexp, Api\Contacts::FILES_PGP_PUBKEY, Api\Contacts::FILES_BIT_PGP_PUBKEY), + array(Api\Mail\Smime::$certificate_regexp, Api\Contacts::FILES_SMIME_PUBKEY, Api\Contacts::FILES_BIT_SMIME_PUBKEY), + ) as $data) + { + list($regexp, $file, $bit) = $data; + $matches = null; + if (!empty($contact['pubkey']) && preg_match($regexp, $contact['pubkey'], $matches) && + // check if we store that pubkey as file (PGP allways, but S/Mime only for SQL backend, not for LDAP or AD!) + $this->pubkey_use_file($bit === Api\Contacts::FILES_BIT_PGP_PUBKEY, $contact)) + { + // check_perms && save check ACL, in case of access only via own-account we have to use root to allow the update + $backup = Api\Vfs::$is_root; Api\Vfs::$is_root = true; + if (file_put_contents(Api\Link::vfs_path('addressbook', $id, $file), $matches[0])) + { + $files |= $bit; + $contact['pubkey'] = str_replace($matches[0], '', $contact['pubkey']); + } + Api\Vfs::$is_root = $backup; + } + } + // if we stripped a pubkey / stored it as file --> remove it from DB + if ($files) + { + if (!trim($contact['pubkey'])) $contact['pubkey'] = null; + $contact['files'] |= $files; + parent::save($contact, $ignore_acl, $touch_modified); + } + } + return $id; + } } diff --git a/addressbook/inc/class.addressbook_import_contacts_csv.inc.php b/addressbook/inc/class.addressbook_import_contacts_csv.inc.php index 4f3509c160..047991f26c 100644 --- a/addressbook/inc/class.addressbook_import_contacts_csv.inc.php +++ b/addressbook/inc/class.addressbook_import_contacts_csv.inc.php @@ -25,7 +25,7 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv { protected static $conditions = array( 'exists', 'equal' ); /** - * @var bocontacts + * @var addressbook_bo */ private $bocontacts; @@ -51,7 +51,7 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv { public function init(importexport_definition &$_definition ) { // fetch the addressbook bo - $this->bocontacts = new Api\Contacts(); + $this->bocontacts = new addressbook_bo(); // Get the tracker for changes $this->tracking = new Api\Contacts\Tracking($this->bocontacts);