You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserId.php 9.3KB

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