PHP Classes

update for GNUPG class

Recommend this page to a friend!

      gnuPG class  >  All threads  >  update for GNUPG class  >  (Un) Subscribe thread alerts  
Subject:update for GNUPG class
Summary:gnupg_class needs an update to work with gnuPG 2.0.13
Messages:1
Author:robert hart
Date:2010-05-09 07:44:43
 

  1. update for GNUPG class   Reply   Report abuse  
Picture of robert hart robert hart - 2010-05-09 07:44:43
Our web host recently updated the version of gnupg on our server to 2.0.13.
without telling us of course!! This broke GNuPG class at the list keys function. and caused big problems for us.

gnupg 2.0.13 outputs a different format to older versions.

we had to hurriedly write a funtion which converts the output from the new gnups into the old format for listkeys() to read

This worked and the code is below. I am sure it can be tidied up we wrote it in a big hurry.
Two functions are shown the new gpg_format() and listkeys() with a slight code mod to read from gpg_format()

//convert between old and new gpgp output may 2010
function gpg_format($input){
$n1 = $n2 = 0;
foreach($input as $tak => $tav){
$fds = explode(':', $tav);
if (count($fds) <= 3) continue;

if($fds[0] == 'pub' || $fds[0] == 'sec' ) {

$ta1[$n1] = "$fds[0]:$fds[1]:$fds[2]:$fds[3]:$fds[4]:$fds[5]:$fds[6]:$fds[7]:$fds[8]:";//$fds[9]:";
$ta2[$n1] = $tav;
$n1++;
}elseif($fds[0] == 'uid'){
$ta3[$n2] = "$fds[9]:$fds[7]:";
$n2++;
}else{
}
//}

}
unset($fds);
if($n2){//if uid rows present
foreach($ta1 as $tk2 => $tv2){
$contents[$tk2] = $tv2.$ta3[$tk2];
}
}else{
$contents = $ta2;
}
return $contents;
}

function ListKeys($KeyKind = 'public')
{
// validate the KeyKind
$KeyKind = strtolower(substr($KeyKind, 0, 3));
if (($KeyKind != 'pub') && ($KeyKind != 'sec')) {
$this->error = 'The Key kind must be public or secret';
return false;
}

// initialize the output
$contents = '';

// execute the GPG command
if ( $this->_fork_process($this->program_path . ' --homedir ' . $this->home_directory .
' --with-colons ' . (($KeyKind == 'pub') ? '--list-public-keys': '--list-secret-keys'),
false, $contents) ) {

// initialize the array data
$returned_keys = array();

// the keys are \n separated
$contents = explode("\n", $contents);
//print_r($contents);
$contents2 = $this-> gpg_format($contents);//change format for updated gpg on unix 2.0.0.13 quadrahosting
// find each key
foreach ($contents2 as $data) {
// read the fields to get the : separated, the sub record is dismiss
$fields = explode(':', $data);
if (count($fields) <= 3) continue;

// verify the that the record is valid
if (($fields[0] == 'pub') || ($fields[0] == 'sec' || $fields[0] == 'uid')) {
//echo count($fields);
//print_r($fields);
if ( $fields[0] == 'pub' || $fields[0] == 'sec'){
array_push($returned_keys, array(
'RecordType' => $fields[0],
'CalculatedTrust' => $fields[1],
'KeyLength' => $fields[2],
'Algorithm' => $fields[3],
'KeyID' => $fields[4],
'CreationDate' => $fields[5],
'ExpirationDate' => $fields[6],
'LocalID' => $fields[7],
'Ownertrust' => $fields[8],
'UserID' => $fields[9],
'FingerPrint' =>$fields[10]
)
);
}
}
}
return $returned_keys;
} else
return false;
}