* Addressbook: allow to import or paste S/Mime or PGP public keys via pubkey field into Vfs

This commit is contained in:
Ralf Becker 2018-05-09 10:05:34 +02:00
parent 668a4fede8
commit 95ea8445b5
2 changed files with 51 additions and 3 deletions

View File

@ -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 boolean $pgp true: PGP, false: S/Mime
* @param array $contact =null contact array to pass to get_backend() * @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); 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;
}
} }

View File

@ -25,7 +25,7 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv {
protected static $conditions = array( 'exists', 'equal' ); protected static $conditions = array( 'exists', 'equal' );
/** /**
* @var bocontacts * @var addressbook_bo
*/ */
private $bocontacts; private $bocontacts;
@ -51,7 +51,7 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv {
public function init(importexport_definition &$_definition ) { public function init(importexport_definition &$_definition ) {
// fetch the addressbook bo // fetch the addressbook bo
$this->bocontacts = new Api\Contacts(); $this->bocontacts = new addressbook_bo();
// Get the tracker for changes // Get the tracker for changes
$this->tracking = new Api\Contacts\Tracking($this->bocontacts); $this->tracking = new Api\Contacts\Tracking($this->bocontacts);