0
|
1 <?php
|
|
2
|
|
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
4
|
|
5 /**
|
|
6 * Contains a data class representing a GPG user id
|
|
7 *
|
|
8 * PHP version 5
|
|
9 *
|
|
10 * LICENSE:
|
|
11 *
|
|
12 * This library is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU Lesser General Public License as
|
|
14 * published by the Free Software Foundation; either version 2.1 of the
|
|
15 * License, or (at your option) any later version.
|
|
16 *
|
|
17 * This library is distributed in the hope that it will be useful,
|
|
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 * Lesser General Public License for more details.
|
|
21 *
|
|
22 * You should have received a copy of the GNU Lesser General Public
|
|
23 * License along with this library; if not, see
|
|
24 * <http://www.gnu.org/licenses/>
|
|
25 *
|
|
26 * @category Encryption
|
|
27 * @package Crypt_GPG
|
|
28 * @author Michael Gauthier <mike@silverorange.com>
|
|
29 * @copyright 2008-2010 silverorange
|
|
30 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
31 * @link http://pear.php.net/package/Crypt_GPG
|
|
32 */
|
|
33
|
|
34 // {{{ class Crypt_GPG_UserId
|
|
35
|
|
36 /**
|
|
37 * A class for GPG user id information
|
|
38 *
|
|
39 * This class is used to store the results of the {@link Crypt_GPG::getKeys()}
|
|
40 * method. User id objects are members of a {@link Crypt_GPG_Key} object.
|
|
41 *
|
|
42 * @category Encryption
|
|
43 * @package Crypt_GPG
|
|
44 * @author Michael Gauthier <mike@silverorange.com>
|
|
45 * @copyright 2008-2010 silverorange
|
|
46 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
|
47 * @link http://pear.php.net/package/Crypt_GPG
|
|
48 * @see Crypt_GPG::getKeys()
|
|
49 * @see Crypt_GPG_Key::getUserIds()
|
|
50 */
|
|
51 class Crypt_GPG_UserId
|
|
52 {
|
|
53 // {{{ class properties
|
|
54
|
|
55 /**
|
|
56 * The name field of this user id
|
|
57 *
|
|
58 * @var string
|
|
59 */
|
|
60 private $_name = '';
|
|
61
|
|
62 /**
|
|
63 * The comment field of this user id
|
|
64 *
|
|
65 * @var string
|
|
66 */
|
|
67 private $_comment = '';
|
|
68
|
|
69 /**
|
|
70 * The email field of this user id
|
|
71 *
|
|
72 * @var string
|
|
73 */
|
|
74 private $_email = '';
|
|
75
|
|
76 /**
|
|
77 * Whether or not this user id is revoked
|
|
78 *
|
|
79 * @var boolean
|
|
80 */
|
|
81 private $_isRevoked = false;
|
|
82
|
|
83 /**
|
|
84 * Whether or not this user id is valid
|
|
85 *
|
|
86 * @var boolean
|
|
87 */
|
|
88 private $_isValid = true;
|
|
89
|
|
90 // }}}
|
|
91 // {{{ __construct()
|
|
92
|
|
93 /**
|
|
94 * Creates a new user id
|
|
95 *
|
|
96 * User ids can be initialized from an array of named values. Available
|
|
97 * names are:
|
|
98 *
|
|
99 * - <kbd>string name</kbd> - the name field of the user id.
|
|
100 * - <kbd>string comment</kbd> - the comment field of the user id.
|
|
101 * - <kbd>string email</kbd> - the email field of the user id.
|
|
102 * - <kbd>boolean valid</kbd> - whether or not the user id is valid.
|
|
103 * - <kbd>boolean revoked</kbd> - whether or not the user id is revoked.
|
|
104 *
|
|
105 * @param Crypt_GPG_UserId|string|array $userId optional. Either an
|
|
106 * existing user id object, which is copied; a user id string, which
|
|
107 * is parsed; or an array of initial values.
|
|
108 */
|
|
109 public function __construct($userId = null)
|
|
110 {
|
|
111 // parse from string
|
|
112 if (is_string($userId)) {
|
|
113 $userId = self::parse($userId);
|
|
114 }
|
|
115
|
|
116 // copy from object
|
|
117 if ($userId instanceof Crypt_GPG_UserId) {
|
|
118 $this->_name = $userId->_name;
|
|
119 $this->_comment = $userId->_comment;
|
|
120 $this->_email = $userId->_email;
|
|
121 $this->_isRevoked = $userId->_isRevoked;
|
|
122 $this->_isValid = $userId->_isValid;
|
|
123 }
|
|
124
|
|
125 // initialize from array
|
|
126 if (is_array($userId)) {
|
|
127 if (array_key_exists('name', $userId)) {
|
|
128 $this->setName($userId['name']);
|
|
129 }
|
|
130
|
|
131 if (array_key_exists('comment', $userId)) {
|
|
132 $this->setComment($userId['comment']);
|
|
133 }
|
|
134
|
|
135 if (array_key_exists('email', $userId)) {
|
|
136 $this->setEmail($userId['email']);
|
|
137 }
|
|
138
|
|
139 if (array_key_exists('revoked', $userId)) {
|
|
140 $this->setRevoked($userId['revoked']);
|
|
141 }
|
|
142
|
|
143 if (array_key_exists('valid', $userId)) {
|
|
144 $this->setValid($userId['valid']);
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148
|
|
149 // }}}
|
|
150 // {{{ getName()
|
|
151
|
|
152 /**
|
|
153 * Gets the name field of this user id
|
|
154 *
|
|
155 * @return string the name field of this user id.
|
|
156 */
|
|
157 public function getName()
|
|
158 {
|
|
159 return $this->_name;
|
|
160 }
|
|
161
|
|
162 // }}}
|
|
163 // {{{ getComment()
|
|
164
|
|
165 /**
|
|
166 * Gets the comments field of this user id
|
|
167 *
|
|
168 * @return string the comments field of this user id.
|
|
169 */
|
|
170 public function getComment()
|
|
171 {
|
|
172 return $this->_comment;
|
|
173 }
|
|
174
|
|
175 // }}}
|
|
176 // {{{ getEmail()
|
|
177
|
|
178 /**
|
|
179 * Gets the email field of this user id
|
|
180 *
|
|
181 * @return string the email field of this user id.
|
|
182 */
|
|
183 public function getEmail()
|
|
184 {
|
|
185 return $this->_email;
|
|
186 }
|
|
187
|
|
188 // }}}
|
|
189 // {{{ isRevoked()
|
|
190
|
|
191 /**
|
|
192 * Gets whether or not this user id is revoked
|
|
193 *
|
|
194 * @return boolean true if this user id is revoked and false if it is not.
|
|
195 */
|
|
196 public function isRevoked()
|
|
197 {
|
|
198 return $this->_isRevoked;
|
|
199 }
|
|
200
|
|
201 // }}}
|
|
202 // {{{ isValid()
|
|
203
|
|
204 /**
|
|
205 * Gets whether or not this user id is valid
|
|
206 *
|
|
207 * @return boolean true if this user id is valid and false if it is not.
|
|
208 */
|
|
209 public function isValid()
|
|
210 {
|
|
211 return $this->_isValid;
|
|
212 }
|
|
213
|
|
214 // }}}
|
|
215 // {{{ __toString()
|
|
216
|
|
217 /**
|
|
218 * Gets a string representation of this user id
|
|
219 *
|
|
220 * The string is formatted as:
|
|
221 * <b><kbd>name (comment) <email-address></kbd></b>.
|
|
222 *
|
|
223 * @return string a string representation of this user id.
|
|
224 */
|
|
225 public function __toString()
|
|
226 {
|
|
227 $components = array();
|
|
228
|
|
229 if (mb_strlen($this->_name, '8bit') > 0) {
|
|
230 $components[] = $this->_name;
|
|
231 }
|
|
232
|
|
233 if (mb_strlen($this->_comment, '8bit') > 0) {
|
|
234 $components[] = '(' . $this->_comment . ')';
|
|
235 }
|
|
236
|
|
237 if (mb_strlen($this->_email, '8bit') > 0) {
|
|
238 $components[] = '<' . $this->_email. '>';
|
|
239 }
|
|
240
|
|
241 return implode(' ', $components);
|
|
242 }
|
|
243
|
|
244 // }}}
|
|
245 // {{{ setName()
|
|
246
|
|
247 /**
|
|
248 * Sets the name field of this user id
|
|
249 *
|
|
250 * @param string $name the name field of this user id.
|
|
251 *
|
|
252 * @return Crypt_GPG_UserId the current object, for fluent interface.
|
|
253 */
|
|
254 public function setName($name)
|
|
255 {
|
|
256 $this->_name = strval($name);
|
|
257 return $this;
|
|
258 }
|
|
259
|
|
260 // }}}
|
|
261 // {{{ setComment()
|
|
262
|
|
263 /**
|
|
264 * Sets the comment field of this user id
|
|
265 *
|
|
266 * @param string $comment the comment field of this user id.
|
|
267 *
|
|
268 * @return Crypt_GPG_UserId the current object, for fluent interface.
|
|
269 */
|
|
270 public function setComment($comment)
|
|
271 {
|
|
272 $this->_comment = strval($comment);
|
|
273 return $this;
|
|
274 }
|
|
275
|
|
276 // }}}
|
|
277 // {{{ setEmail()
|
|
278
|
|
279 /**
|
|
280 * Sets the email field of this user id
|
|
281 *
|
|
282 * @param string $email the email field of this user id.
|
|
283 *
|
|
284 * @return Crypt_GPG_UserId the current object, for fluent interface.
|
|
285 */
|
|
286 public function setEmail($email)
|
|
287 {
|
|
288 $this->_email = strval($email);
|
|
289 return $this;
|
|
290 }
|
|
291
|
|
292 // }}}
|
|
293 // {{{ setRevoked()
|
|
294
|
|
295 /**
|
|
296 * Sets whether or not this user id is revoked
|
|
297 *
|
|
298 * @param boolean $isRevoked whether or not this user id is revoked.
|
|
299 *
|
|
300 * @return Crypt_GPG_UserId the current object, for fluent interface.
|
|
301 */
|
|
302 public function setRevoked($isRevoked)
|
|
303 {
|
|
304 $this->_isRevoked = ($isRevoked) ? true : false;
|
|
305 return $this;
|
|
306 }
|
|
307
|
|
308 // }}}
|
|
309 // {{{ setValid()
|
|
310
|
|
311 /**
|
|
312 * Sets whether or not this user id is valid
|
|
313 *
|
|
314 * @param boolean $isValid whether or not this user id is valid.
|
|
315 *
|
|
316 * @return Crypt_GPG_UserId the current object, for fluent interface.
|
|
317 */
|
|
318 public function setValid($isValid)
|
|
319 {
|
|
320 $this->_isValid = ($isValid) ? true : false;
|
|
321 return $this;
|
|
322 }
|
|
323
|
|
324 // }}}
|
|
325 // {{{ parse()
|
|
326
|
|
327 /**
|
|
328 * Parses a user id object from a user id string
|
|
329 *
|
|
330 * A user id string is of the form:
|
|
331 * <b><kbd>name (comment) <email-address></kbd></b> with the <i>comment</i>
|
|
332 * and <i>email-address</i> fields being optional.
|
|
333 *
|
|
334 * @param string $string the user id string to parse.
|
|
335 *
|
|
336 * @return Crypt_GPG_UserId the user id object parsed from the string.
|
|
337 */
|
|
338 public static function parse($string)
|
|
339 {
|
|
340 $userId = new Crypt_GPG_UserId();
|
|
341 $name = '';
|
|
342 $email = '';
|
|
343 $comment = '';
|
|
344
|
|
345 // get email address from end of string if it exists
|
|
346 $matches = array();
|
|
347 if (preg_match('/^(.*?)<([^>]+)>$/', $string, $matches) === 1) {
|
|
348 $string = trim($matches[1]);
|
|
349 $email = $matches[2];
|
|
350 }
|
|
351
|
|
352 // get comment from end of string if it exists
|
|
353 $matches = array();
|
|
354 if (preg_match('/^(.+?) \(([^\)]+)\)$/', $string, $matches) === 1) {
|
|
355 $string = $matches[1];
|
|
356 $comment = $matches[2];
|
|
357 }
|
|
358
|
|
359 // there can be an email without a name
|
|
360 if (!$email && preg_match('/^[\S]+@[\S]+$/', $string, $matches) === 1) {
|
|
361 $email = $string;
|
|
362 } else {
|
|
363 $name = $string;
|
|
364 }
|
|
365
|
|
366 $userId->setName($name);
|
|
367 $userId->setComment($comment);
|
|
368 $userId->setEmail($email);
|
|
369
|
|
370 return $userId;
|
|
371 }
|
|
372
|
|
373 // }}}
|
|
374 }
|
|
375
|
|
376 // }}}
|
|
377
|
|
378 ?>
|