Smutty
MVC Framework
View Code: Smutty_GPG
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this class provides some functions for dealing with the
5
* gpg keyring (if there is one).
6
*
7
* unless otherwise specified, it will create it's own keyring
8
* directory in the application folder.
9
*
10
*/
11
12
class Smutty_GPG extends Smutty_Object {
13
14
/** path to gpg binary */
15
private $bin;
16
17
/** command to execute gpg */
18
private $gpg;
19
20
/** gpg keyring dir */
21
private $homedir;
22
23
/** singleton */
24
private static $instance;
25
26
/**
27
* constructor. makes sure everything is set up ok. creates
28
* the keyring directory if it doesn't exist.
29
*
30
* @return nothing
31
*
32
*/
33
35
36
$this->homedir = $homedir ? $homedir : 'application/data/gpg';
37
$this->bin = '/usr/bin/gpg';
38
$this->gpg = " $this->bin --homedir " . escapeshellarg($this->homedir) . " 2>&1 ";
39
40
// check required keyring dir exists
41
if ( !file_exists($this->homedir) )
42
if ( !mkdir($this->homedir) )
43
Smutty_Error::fatal( 'cannot create directory data/gpg', 'ClassSmutty_PGP' );
44
45
// check we can write to keyring dir
46
if ( !is_writable($this->homedir) )
47
Smutty_Error::fatal( 'data/gpg is not writable', 'ClassSmutty_GPG' );
48
49
}
50
51
/**
52
* returns the singleton instance of this class
53
*
54
* @return Smutty_GPG the singleton
55
*
56
*/
57
58
public static function &getInstance() {
59
if ( self::$instance == null )
60
self::$instance = new Smutty_GPG();
61
return self::$instance;
62
}
63
64
/**
65
* verifies a signature with the keyring. if the sig is valid
66
* then it'll return a Smutty_GPG_ValidSignature object with all
67
* the info about the sig. otherwise it'll return false.
68
*
69
* @param String $sig the signature to verify
70
* @return Smutty_GPG_ValidSignature a valid sig object
71
*
72
*/
73
75
$sig = escapeshellarg( $sig );
76
exec( "echo $sig | $this->gpg --verify ", $result );
77
// look for success
78
foreach ( $result as $line )
79
if ( preg_match('/Good signature from "(.*)"/',$line,$matches) )
80
return new Smutty_GPG_ValidSignature( $result );
81
return false;
82
}
83
84
/**
85
* imports a public key into the keyring. returns a
86
* boolean indicating if it went ok or not.
87
*
88
* @param String $key the public key to import
89
* @return Smutty_GPG_PublicKey the imported key
90
*
91
*/
92
94
$key = escapeshellarg( $key );
95
exec( "echo $key | $this->gpg --import ", $result );
96
// check for success
97
foreach ( $result as $line )
98
if ( preg_match('/key .* [imported|not changed]/',$line) )
99
return new Smutty_GPG_PublicKey( $result );
100
return false;
101
}
102
103
/**
104
* lists the keys in the keyring. returns an array
105
* of Smutty_GPG_Key objects.
106
*
107
* @return array Smutty_GPG_Key's
108
*
109
*/
110
112
113
$keys = array();
114
exec( " $this->gpg --list-keys ", $result );
115
116
// remove info lines
117
while ( $line = array_shift($result) )
118
if ( preg_match('/^-----/',$line) )
119
break;
120
121
while ( $result ) {
122
$pub = substr( array_shift($result), 6 );
123
$uid = substr( array_shift($result), 6 );
124
$sub = substr( array_shift($result), 6 );
125
array_shift( $result ); // blank line
126
array_push( $keys, new Smutty_GPG_Key(
127
$pub, $uid, $sub
128
));
129
}
130
131
return $keys;
132
133
}
134
135
}
136
137
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.