3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Crypt_GPG is a package to use GPG from PHP
8 * This file contains an object that handles GPG's status output for the verify
15 * This library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as
17 * published by the Free Software Foundation; either version 2.1 of the
18 * License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * @category Encryption
31 * @author Michael Gauthier <mike@silverorange.com>
32 * @copyright 2008 silverorange
33 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
34 * @version CVS: $Id: VerifyStatusHandler.php 302908 2010-08-31 03:56:54Z gauthierm $
35 * @link http://pear.php.net/package/Crypt_GPG
36 * @link http://www.gnupg.org/
40 * Signature object class definition
42 require_once 'Crypt/GPG/Signature.php';
45 * Status line handler for the verify operation
47 * This class is used internally by Crypt_GPG and does not need be used
48 * directly. See the {@link Crypt_GPG} class for end-user API.
50 * This class is responsible for building signature objects that are returned
51 * by the {@link Crypt_GPG::verify()} method. See <b>doc/DETAILS</b> in the
52 * {@link http://www.gnupg.org/download/ GPG distribution} for detailed
53 * information on GPG's status output for the verify operation.
55 * @category Encryption
57 * @author Michael Gauthier <mike@silverorange.com>
58 * @copyright 2008 silverorange
59 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
60 * @link http://pear.php.net/package/Crypt_GPG
61 * @link http://www.gnupg.org/
63 class Crypt_GPG_VerifyStatusHandler
65 // {{{ protected properties
68 * The current signature id
70 * Ths signature id is emitted by GPG before the new signature line so we
71 * must remember it temporarily.
75 protected $signatureId = '';
78 * List of parsed {@link Crypt_GPG_Signature} objects
82 protected $signatures = array();
85 * Array index of the current signature
89 protected $index = -1;
95 * Handles a status line
97 * @param string $line the status line to handle.
101 public function handle($line)
103 $tokens = explode(' ', $line);
104 switch ($tokens[0]) {
110 $signature = new Crypt_GPG_Signature();
112 // if there was a signature id, set it on the new signature
113 if ($this->signatureId != '') {
114 $signature->setId($this->signatureId);
115 $this->signatureId = '';
118 // Detect whether fingerprint or key id was returned and set
119 // signature values appropriately. Key ids are strings of either
120 // 16 or 8 hexadecimal characters. Fingerprints are strings of 40
121 // hexadecimal characters. The key id is the last 16 characters of
122 // the key fingerprint.
123 if (strlen($tokens[1]) > 16) {
124 $signature->setKeyFingerprint($tokens[1]);
125 $signature->setKeyId(substr($tokens[1], -16));
127 $signature->setKeyId($tokens[1]);
130 // get user id string
131 $string = implode(' ', array_splice($tokens, 2));
132 $string = rawurldecode($string);
134 $signature->setUserId(Crypt_GPG_UserId::parse($string));
137 $this->signatures[$this->index] = $signature;
141 $signature = new Crypt_GPG_Signature();
143 // if there was a signature id, set it on the new signature
144 if ($this->signatureId != '') {
145 $signature->setId($this->signatureId);
146 $this->signatureId = '';
149 // Detect whether fingerprint or key id was returned and set
150 // signature values appropriately. Key ids are strings of either
151 // 16 or 8 hexadecimal characters. Fingerprints are strings of 40
152 // hexadecimal characters. The key id is the last 16 characters of
153 // the key fingerprint.
154 if (strlen($tokens[1]) > 16) {
155 $signature->setKeyFingerprint($tokens[1]);
156 $signature->setKeyId(substr($tokens[1], -16));
158 $signature->setKeyId($tokens[1]);
162 $this->signatures[$this->index] = $signature;
167 if (!array_key_exists($this->index, $this->signatures)) {
171 $signature = $this->signatures[$this->index];
173 $signature->setValid(true);
174 $signature->setKeyFingerprint($tokens[1]);
176 if (strpos($tokens[3], 'T') === false) {
177 $signature->setCreationDate($tokens[3]);
179 $signature->setCreationDate(strtotime($tokens[3]));
182 if (array_key_exists(4, $tokens)) {
183 if (strpos($tokens[4], 'T') === false) {
184 $signature->setExpirationDate($tokens[4]);
186 $signature->setExpirationDate(strtotime($tokens[4]));
193 // note: signature id comes before new signature line and may not
194 // exist for some signature types
195 $this->signatureId = $tokens[1];
201 // {{{ getSignatures()
204 * Gets the {@link Crypt_GPG_Signature} objects parsed by this handler
206 * @return array the signature objects parsed by this handler.
208 public function getSignatures()
210 return $this->signatures;