IMP_Smime::getPublicKey PHP Method

getPublicKey() public method

The key will be retrieved from a user's address book(s).
public getPublicKey ( string $address ) : string
$address string The e-mail address to search for.
return string The S/MIME public key requested.
    public function getPublicKey($address)
    {
        global $injector, $registry;
        try {
            $key = $injector->getInstance('Horde_Core_Hooks')->callHook('smime_key', 'imp', array($address));
            if ($key) {
                return $key;
            }
        } catch (Horde_Exception_HookNotSet $e) {
        }
        $contacts = $injector->getInstance('IMP_Contacts');
        try {
            $key = $registry->call('contacts/getField', array($address, self::PUBKEY_FIELD, $contacts->sources, true, true));
        } catch (Horde_Exception $e) {
            /* See if the address points to the user's public key. */
            $personal_pubkey = $this->getPersonalPublicKey();
            if (!empty($personal_pubkey) && $injector->getInstance('IMP_Identity')->hasAddress($address)) {
                return $personal_pubkey;
            }
            throw $e;
        }
        /* If more than one public key is returned, just return the first in
         * the array. There is no way of knowing which is the "preferred" key,
         * if the keys are different. */
        return is_array($key) ? reset($key) : $key;
    }

Usage Example

Exemplo n.º 1
0
 /**
  */
 protected function _init()
 {
     global $injector, $notification;
     $this->_smime = $injector->getInstance('IMP_Smime');
     /* Run through the action handlers */
     switch ($this->vars->actionID) {
         case 'import_public_key':
             $this->_importKeyDialog('process_import_public_key');
             break;
         case 'process_import_public_key':
             try {
                 $publicKey = $this->_getImportKey($this->vars->import_key);
                 /* Add the public key to the storage system. */
                 $this->_smime->addPublicKey($publicKey);
                 $notification->push(_("S/MIME public key successfully added."), 'horde.success');
                 $this->_reloadWindow();
             } catch (Horde_Browser_Exception $e) {
                 $notification->push(_("No S/MIME public key imported."), 'horde.error');
             } catch (Horde_Exception $e) {
                 $notification->push($e);
             }
             $this->vars->actionID = 'import_public_key';
             $this->_importKeyDialog('process_import_public_key');
             break;
         case 'view_public_key':
         case 'info_public_key':
             try {
                 $key = $this->_smime->getPublicKey($this->vars->email);
             } catch (Horde_Exception $e) {
                 $key = $e->getMessage();
             }
             if ($this->vars->actionID == 'view_public_key') {
                 $this->_textWindowOutput('S/MIME Public Key', $key);
             }
             $this->_printCertInfo($key);
             break;
         case 'view_personal_public_key':
             $this->_textWindowOutput('S/MIME Personal Public Key', $this->_smime->getPersonalPublicKey());
             break;
         case 'info_personal_public_key':
             $this->_printCertInfo($this->_smime->getPersonalPublicKey());
             break;
         case 'view_personal_private_key':
             $this->_textWindowOutput('S/MIME Personal Private Key', $this->_smime->getPersonalPrivateKey());
             break;
         case 'import_personal_certs':
             $this->_importKeyDialog('process_import_personal_certs');
             break;
         case 'process_import_personal_certs':
             try {
                 $pkcs12 = $this->_getImportKey($this->vars->import_key);
                 $this->_smime->addFromPKCS12($pkcs12, $this->vars->upload_key_pass, $this->vars->upload_key_pk_pass);
                 $notification->push(_("S/MIME Public/Private Keypair successfully added."), 'horde.success');
                 $this->_reloadWindow();
             } catch (Horde_Browser_Exception $e) {
                 $notification->push(_("Personal S/MIME certificates NOT imported."), 'horde.error');
             } catch (Horde_Exception $e) {
                 $notification->push(_("Personal S/MIME certificates NOT imported: ") . $e->getMessage(), 'horde.error');
             }
             $this->vars->actionID = 'import_personal_certs';
             $this->_importKeyDialog('process_import_personal_certs');
             break;
     }
 }
All Usage Examples Of IMP_Smime::getPublicKey