Skip to content

Commit

Permalink
UI centric code should be done in application, not lib
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Apr 9, 2015
1 parent 476337e commit f7fa1ad
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion imp/lib/Basic/Pgp.php
Expand Up @@ -194,7 +194,7 @@ protected function _reloadWindow()
protected function _printKeyInfo($key = '')
{
try {
$key_info = $this->_pgp->pgpPrettyKey($key);
$key_info = $this->_pgp->prettyKey($key);
} catch (Horde_Crypt_Exception $e) {
Horde::log($e, 'INFO');
$key_info = $e->getMessage();
Expand Down
2 changes: 1 addition & 1 deletion imp/lib/Mime/Viewer/Pgp.php
Expand Up @@ -82,7 +82,7 @@ protected function _render()
// Throws exception on error.
return array(
$this->_mimepart->getMimeId() => array(
'data' => '<html><body><tt>' . nl2br(str_replace(' ', '&nbsp;', $GLOBALS['injector']->getInstance('IMP_Pgp')->pgpPrettyKey($this->_mimepart->getContents()))) . '</tt></body></html>',
'data' => '<html><body><tt>' . nl2br(str_replace(' ', '&nbsp;', $GLOBALS['injector']->getInstance('IMP_Pgp')->prettyKey($this->_mimepart->getContents()))) . '</tt></body></html>',
'type' => 'text/html; charset=' . $this->getConfigParam('charset')
)
);
Expand Down
85 changes: 85 additions & 0 deletions imp/lib/Pgp.php
Expand Up @@ -730,6 +730,91 @@ public function getKeys($data)
return $out;
}

/**
* Returns human readable information on a PGP key.
*
* @param string $pgpdata The PGP data block.
*
* @return string Tabular information on the PGP key.
* @throws Horde_Pgp_Exception
*/
public function prettyKey($pgpdata)
{
$msg = '';
$info = $this->_pgp->pgpPacketInformation($pgpdata);

if (empty($info['signature'])) {
return $msg;
}

$fingerprints = $this->_pgp->getFingerprintsFromKey($pgpdata);

$getKeyIdString = function($keyid) {
/* Get the 8 character key ID string. */
if (strpos($keyid, '0x') === 0) {
$keyid = substr($keyid, 2);
}
if (strlen($keyid) > 8) {
$keyid = substr($keyid, -8);
}
return '0x' . $keyid;
};

/* Making the property names the same width for all localizations .*/
$leftrow = array(
_("Name"),
_("Key Type"),
_("Key Creation"),
_("Expiration Date"),
_("Key Length"),
_("Comment"),
_("E-Mail"),
_("Hash-Algorithm"),
_("Key ID"),
_("Key Fingerprint")
);

array_walk(
$leftrow,
function (&$s, $k, $m) {
$s .= ':' . str_repeat(' ', $m - Horde_String::length($s));
},
max(array_map('strlen', $leftrow)) + 2
);

foreach ($info['signature'] as $uid_idx => $val) {
if ($uid_idx == '_SIGNATURE') {
continue;
}

$key = $this->_pgp->pgpPacketSignatureByUidIndex(
$pgpdata,
$uid_idx
);

$keyid = empty($key['keyid'])
? null
: $getKeyIdString($key['keyid']);
$fingerprint = isset($fingerprints[$keyid])
? $fingerprints[$keyid]
: null;
$sig_key = 'sig_' . $key['keyid'];

$msg .= $leftrow[0] . (isset($key['name']) ? stripcslashes($key['name']) : '') . "\n"
. $leftrow[1] . (($key['key_type'] == 'public_key') ? _("Public Key") : _("Private Key")) . "\n"
. $leftrow[2] . strftime("%D", $val[$sig_key]['created']) . "\n"
. $leftrow[3] . (empty($val[$sig_key]['expires']) ? '[' . _("Never") . ']' : strftime("%D", $val[$sig_key]['expires'])) . "\n"
. $leftrow[4] . $key['key_size'] . " Bytes\n"
. $leftrow[5] . (empty($key['comment']) ? '[' . _("None") . ']' : $key['comment']) . "\n"
. $leftrow[6] . (empty($key['email']) ? '[' . _("None") . ']' : $key['email']) . "\n"
. $leftrow[7] . (empty($key['micalg']) ? '[' . _("Unknown") . ']' : $key['micalg']) . "\n"
. $leftrow[8] . (empty($keyid) ? '[' . _("Unknown") . ']' : $keyid) . "\n"
. $leftrow[9] . (empty($fingerprint) ? '[' . _("Unknown") . ']' : $fingerprint) . "\n\n";
}

return $msg;
}

/**
* Return list of keyserver objects.
*
Expand Down

0 comments on commit f7fa1ad

Please sign in to comment.