commit | author | age
|
48e9c1
|
1 |
<?php |
T |
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, write to the Free Software |
|
24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
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 |
* @version CVS: $Id: UserId.php 295621 2010-03-01 04:18:54Z gauthierm $ |
|
32 |
* @link http://pear.php.net/package/Crypt_GPG |
|
33 |
*/ |
|
34 |
|
|
35 |
// {{{ class Crypt_GPG_UserId |
|
36 |
|
|
37 |
/** |
|
38 |
* A class for GPG user id information |
|
39 |
* |
|
40 |
* This class is used to store the results of the {@link Crypt_GPG::getKeys()} |
|
41 |
* method. User id objects are members of a {@link Crypt_GPG_Key} object. |
|
42 |
* |
|
43 |
* @category Encryption |
|
44 |
* @package Crypt_GPG |
|
45 |
* @author Michael Gauthier <mike@silverorange.com> |
|
46 |
* @copyright 2008-2010 silverorange |
|
47 |
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|
48 |
* @link http://pear.php.net/package/Crypt_GPG |
|
49 |
* @see Crypt_GPG::getKeys() |
|
50 |
* @see Crypt_GPG_Key::getUserIds() |
|
51 |
*/ |
|
52 |
class Crypt_GPG_UserId |
|
53 |
{ |
|
54 |
// {{{ class properties |
|
55 |
|
|
56 |
/** |
|
57 |
* The name field of this user id |
|
58 |
* |
|
59 |
* @var string |
|
60 |
*/ |
|
61 |
private $_name = ''; |
|
62 |
|
|
63 |
/** |
|
64 |
* The comment field of this user id |
|
65 |
* |
|
66 |
* @var string |
|
67 |
*/ |
|
68 |
private $_comment = ''; |
|
69 |
|
|
70 |
/** |
|
71 |
* The email field of this user id |
|
72 |
* |
|
73 |
* @var string |
|
74 |
*/ |
|
75 |
private $_email = ''; |
|
76 |
|
|
77 |
/** |
|
78 |
* Whether or not this user id is revoked |
|
79 |
* |
|
80 |
* @var boolean |
|
81 |
*/ |
|
82 |
private $_isRevoked = false; |
|
83 |
|
|
84 |
/** |
|
85 |
* Whether or not this user id is valid |
|
86 |
* |
|
87 |
* @var boolean |
|
88 |
*/ |
|
89 |
private $_isValid = true; |
|
90 |
|
|
91 |
// }}} |
|
92 |
// {{{ __construct() |
|
93 |
|
|
94 |
/** |
|
95 |
* Creates a new user id |
|
96 |
* |
|
97 |
* User ids can be initialized from an array of named values. Available |
|
98 |
* names are: |
|
99 |
* |
|
100 |
* - <kbd>string name</kbd> - the name field of the user id. |
|
101 |
* - <kbd>string comment</kbd> - the comment field of the user id. |
|
102 |
* - <kbd>string email</kbd> - the email field of the user id. |
|
103 |
* - <kbd>boolean valid</kbd> - whether or not the user id is valid. |
|
104 |
* - <kbd>boolean revoked</kbd> - whether or not the user id is revoked. |
|
105 |
* |
|
106 |
* @param Crypt_GPG_UserId|string|array $userId optional. Either an |
|
107 |
* existing user id object, which is copied; a user id string, which |
|
108 |
* is parsed; or an array of initial values. |
|
109 |
*/ |
|
110 |
public function __construct($userId = null) |
|
111 |
{ |
|
112 |
// parse from string |
|
113 |
if (is_string($userId)) { |
|
114 |
$userId = self::parse($userId); |
|
115 |
} |
|
116 |
|
|
117 |
// copy from object |
|
118 |
if ($userId instanceof Crypt_GPG_UserId) { |
|
119 |
$this->_name = $userId->_name; |
|
120 |
$this->_comment = $userId->_comment; |
|
121 |
$this->_email = $userId->_email; |
|
122 |
$this->_isRevoked = $userId->_isRevoked; |
|
123 |
$this->_isValid = $userId->_isValid; |
|
124 |
} |
|
125 |
|
|
126 |
// initialize from array |
|
127 |
if (is_array($userId)) { |
|
128 |
if (array_key_exists('name', $userId)) { |
|
129 |
$this->setName($userId['name']); |
|
130 |
} |
|
131 |
|
|
132 |
if (array_key_exists('comment', $userId)) { |
|
133 |
$this->setComment($userId['comment']); |
|
134 |
} |
|
135 |
|
|
136 |
if (array_key_exists('email', $userId)) { |
|
137 |
$this->setEmail($userId['email']); |
|
138 |
} |
|
139 |
|
|
140 |
if (array_key_exists('revoked', $userId)) { |
|
141 |
$this->setRevoked($userId['revoked']); |
|
142 |
} |
|
143 |
|
|
144 |
if (array_key_exists('valid', $userId)) { |
|
145 |
$this->setValid($userId['valid']); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
// }}} |
|
151 |
// {{{ getName() |
|
152 |
|
|
153 |
/** |
|
154 |
* Gets the name field of this user id |
|
155 |
* |
|
156 |
* @return string the name field of this user id. |
|
157 |
*/ |
|
158 |
public function getName() |
|
159 |
{ |
|
160 |
return $this->_name; |
|
161 |
} |
|
162 |
|
|
163 |
// }}} |
|
164 |
// {{{ getComment() |
|
165 |
|
|
166 |
/** |
|
167 |
* Gets the comments field of this user id |
|
168 |
* |
|
169 |
* @return string the comments field of this user id. |
|
170 |
*/ |
|
171 |
public function getComment() |
|
172 |
{ |
|
173 |
return $this->_comment; |
|
174 |
} |
|
175 |
|
|
176 |
// }}} |
|
177 |
// {{{ getEmail() |
|
178 |
|
|
179 |
/** |
|
180 |
* Gets the email field of this user id |
|
181 |
* |
|
182 |
* @return string the email field of this user id. |
|
183 |
*/ |
|
184 |
public function getEmail() |
|
185 |
{ |
|
186 |
return $this->_email; |
|
187 |
} |
|
188 |
|
|
189 |
// }}} |
|
190 |
// {{{ isRevoked() |
|
191 |
|
|
192 |
/** |
|
193 |
* Gets whether or not this user id is revoked |
|
194 |
* |
|
195 |
* @return boolean true if this user id is revoked and false if it is not. |
|
196 |
*/ |
|
197 |
public function isRevoked() |
|
198 |
{ |
|
199 |
return $this->_isRevoked; |
|
200 |
} |
|
201 |
|
|
202 |
// }}} |
|
203 |
// {{{ isValid() |
|
204 |
|
|
205 |
/** |
|
206 |
* Gets whether or not this user id is valid |
|
207 |
* |
|
208 |
* @return boolean true if this user id is valid and false if it is not. |
|
209 |
*/ |
|
210 |
public function isValid() |
|
211 |
{ |
|
212 |
return $this->_isValid; |
|
213 |
} |
|
214 |
|
|
215 |
// }}} |
|
216 |
// {{{ __toString() |
|
217 |
|
|
218 |
/** |
|
219 |
* Gets a string representation of this user id |
|
220 |
* |
|
221 |
* The string is formatted as: |
|
222 |
* <b><kbd>name (comment) <email-address></kbd></b>. |
|
223 |
* |
|
224 |
* @return string a string representation of this user id. |
|
225 |
*/ |
|
226 |
public function __toString() |
|
227 |
{ |
|
228 |
$components = array(); |
|
229 |
|
|
230 |
if (strlen($this->_name) > 0) { |
|
231 |
$components[] = $this->_name; |
|
232 |
} |
|
233 |
|
|
234 |
if (strlen($this->_comment) > 0) { |
|
235 |
$components[] = '(' . $this->_comment . ')'; |
|
236 |
} |
|
237 |
|
|
238 |
if (strlen($this->_email) > 0) { |
|
239 |
$components[] = '<' . $this->_email. '>'; |
|
240 |
} |
|
241 |
|
|
242 |
return implode(' ', $components); |
|
243 |
} |
|
244 |
|
|
245 |
// }}} |
|
246 |
// {{{ setName() |
|
247 |
|
|
248 |
/** |
|
249 |
* Sets the name field of this user id |
|
250 |
* |
|
251 |
* @param string $name the name field of this user id. |
|
252 |
* |
|
253 |
* @return Crypt_GPG_UserId the current object, for fluent interface. |
|
254 |
*/ |
|
255 |
public function setName($name) |
|
256 |
{ |
|
257 |
$this->_name = strval($name); |
|
258 |
return $this; |
|
259 |
} |
|
260 |
|
|
261 |
// }}} |
|
262 |
// {{{ setComment() |
|
263 |
|
|
264 |
/** |
|
265 |
* Sets the comment field of this user id |
|
266 |
* |
|
267 |
* @param string $comment the comment field of this user id. |
|
268 |
* |
|
269 |
* @return Crypt_GPG_UserId the current object, for fluent interface. |
|
270 |
*/ |
|
271 |
public function setComment($comment) |
|
272 |
{ |
|
273 |
$this->_comment = strval($comment); |
|
274 |
return $this; |
|
275 |
} |
|
276 |
|
|
277 |
// }}} |
|
278 |
// {{{ setEmail() |
|
279 |
|
|
280 |
/** |
|
281 |
* Sets the email field of this user id |
|
282 |
* |
|
283 |
* @param string $email the email field of this user id. |
|
284 |
* |
|
285 |
* @return Crypt_GPG_UserId the current object, for fluent interface. |
|
286 |
*/ |
|
287 |
public function setEmail($email) |
|
288 |
{ |
|
289 |
$this->_email = strval($email); |
|
290 |
return $this; |
|
291 |
} |
|
292 |
|
|
293 |
// }}} |
|
294 |
// {{{ setRevoked() |
|
295 |
|
|
296 |
/** |
|
297 |
* Sets whether or not this user id is revoked |
|
298 |
* |
|
299 |
* @param boolean $isRevoked whether or not this user id is revoked. |
|
300 |
* |
|
301 |
* @return Crypt_GPG_UserId the current object, for fluent interface. |
|
302 |
*/ |
|
303 |
public function setRevoked($isRevoked) |
|
304 |
{ |
|
305 |
$this->_isRevoked = ($isRevoked) ? true : false; |
|
306 |
return $this; |
|
307 |
} |
|
308 |
|
|
309 |
// }}} |
|
310 |
// {{{ setValid() |
|
311 |
|
|
312 |
/** |
|
313 |
* Sets whether or not this user id is valid |
|
314 |
* |
|
315 |
* @param boolean $isValid whether or not this user id is valid. |
|
316 |
* |
|
317 |
* @return Crypt_GPG_UserId the current object, for fluent interface. |
|
318 |
*/ |
|
319 |
public function setValid($isValid) |
|
320 |
{ |
|
321 |
$this->_isValid = ($isValid) ? true : false; |
|
322 |
return $this; |
|
323 |
} |
|
324 |
|
|
325 |
// }}} |
|
326 |
// {{{ parse() |
|
327 |
|
|
328 |
/** |
|
329 |
* Parses a user id object from a user id string |
|
330 |
* |
|
331 |
* A user id string is of the form: |
|
332 |
* <b><kbd>name (comment) <email-address></kbd></b> with the <i>comment</i> |
|
333 |
* and <i>email-address</i> fields being optional. |
|
334 |
* |
|
335 |
* @param string $string the user id string to parse. |
|
336 |
* |
|
337 |
* @return Crypt_GPG_UserId the user id object parsed from the string. |
|
338 |
*/ |
|
339 |
public static function parse($string) |
|
340 |
{ |
|
341 |
$userId = new Crypt_GPG_UserId(); |
|
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 = $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 |
$name = $string; |
|
360 |
|
|
361 |
$userId->setName($name); |
|
362 |
$userId->setComment($comment); |
|
363 |
$userId->setEmail($email); |
|
364 |
|
|
365 |
return $userId; |
|
366 |
} |
|
367 |
|
|
368 |
// }}} |
|
369 |
} |
|
370 |
|
|
371 |
// }}} |
|
372 |
|
|
373 |
?> |