Hostmask.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Phergie
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://phergie.org/license
  13. *
  14. * @category Phergie
  15. * @package Phergie
  16. * @author Phergie Development Team <team@phergie.org>
  17. * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
  18. * @license http://phergie.org/license New BSD License
  19. * @link http://pear.phergie.org/package/Phergie
  20. */
  21. /**
  22. * Data structure for a hostmask.
  23. *
  24. * @category Phergie
  25. * @package Phergie
  26. * @author Phergie Development Team <team@phergie.org>
  27. * @license http://phergie.org/license New BSD License
  28. * @link http://pear.phergie.org/package/Phergie
  29. */
  30. class Phergie_Hostmask
  31. {
  32. /**
  33. * Host
  34. *
  35. * @var string
  36. */
  37. protected $host;
  38. /**
  39. * Nick
  40. *
  41. * @var string
  42. */
  43. protected $nick;
  44. /**
  45. * Username
  46. *
  47. * @var string
  48. */
  49. protected $username;
  50. /**
  51. * Regular expression used to parse a hostmask
  52. *
  53. * @var string
  54. */
  55. protected static $regex = '/^([^!@]+)!(?:[ni]=)?([^@]+)@([^ ]+)/';
  56. /**
  57. * Constructor to initialize components of the hostmask.
  58. *
  59. * @param string $nick Nick component
  60. * @param string $username Username component
  61. * @param string $host Host component
  62. *
  63. * @return void
  64. */
  65. public function __construct($nick, $username, $host)
  66. {
  67. $this->nick = $nick;
  68. $this->username = $username;
  69. $this->host = $host;
  70. }
  71. /**
  72. * Returns whether a given string appears to be a valid hostmask.
  73. *
  74. * @param string $string Alleged hostmask string
  75. *
  76. * @return bool TRUE if the string appears to be a valid hostmask, FALSE
  77. * otherwise
  78. */
  79. public static function isValid($string)
  80. {
  81. return (preg_match(self::$regex, $string) > 0);
  82. }
  83. /**
  84. * Parses a string containing the entire hostmask into a new instance of
  85. * this class.
  86. *
  87. * @param string $hostmask Entire hostmask including the nick, username,
  88. * and host components
  89. *
  90. * @return Phergie_Hostmask New instance populated with data parsed from
  91. * the provided hostmask string
  92. * @throws Phergie_Hostmask_Exception
  93. */
  94. public static function fromString($hostmask)
  95. {
  96. if (preg_match(self::$regex, $hostmask, $match)) {
  97. list(, $nick, $username, $host) = $match;
  98. return new self($nick, $username, $host);
  99. }
  100. throw new Phergie_Hostmask_Exception(
  101. 'Invalid hostmask specified: "' . $hostmask . '"',
  102. Phergie_Hostmask_Exception::ERR_INVALID_HOSTMASK
  103. );
  104. }
  105. /**
  106. * Sets the hostname.
  107. *
  108. * @param string $host Hostname
  109. *
  110. * @return Phergie_Hostmask Provides a fluent interface
  111. */
  112. public function setHost($host)
  113. {
  114. $this->host = $host;
  115. return $this;
  116. }
  117. /**
  118. * Returns the hostname.
  119. *
  120. * @return string
  121. */
  122. public function getHost()
  123. {
  124. return $this->host;
  125. }
  126. /**
  127. * Sets the username of the user.
  128. *
  129. * @param string $username Username
  130. *
  131. * @return Phergie_Hostmask Provides a fluent interface
  132. */
  133. public function setUsername($username)
  134. {
  135. $this->username = $username;
  136. return $this;
  137. }
  138. /**
  139. * Returns the username of the user.
  140. *
  141. * @return string
  142. */
  143. public function getUsername()
  144. {
  145. return $this->username;
  146. }
  147. /**
  148. * Sets the nick of the user.
  149. *
  150. * @param string $nick User nick
  151. *
  152. * @return Phergie_Hostmask Provides a fluent interface
  153. */
  154. public function setNick($nick)
  155. {
  156. $this->nick = $nick;
  157. return $this;
  158. }
  159. /**
  160. * Returns the nick of the user.
  161. *
  162. * @return string
  163. */
  164. public function getNick()
  165. {
  166. return $this->nick;
  167. }
  168. /**
  169. * Returns the hostmask for the originating server or user.
  170. *
  171. * @return string
  172. */
  173. public function __toString()
  174. {
  175. return $this->nick . '!' . $this->username . '@' . $this->host;
  176. }
  177. /**
  178. * Returns whether a given hostmask matches a given pattern.
  179. *
  180. * @param string $pattern Pattern using conventions of a ban mask where
  181. * represents a wildcard
  182. * @param string $hostmask Optional hostmask to match against, if not
  183. * the current hostmask instance
  184. *
  185. * @return bool TRUE if the hostmask matches the pattern, FALSE otherwise
  186. * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_3 Examples
  187. */
  188. public function matches($pattern, $hostmask = null)
  189. {
  190. if (!$hostmask) {
  191. $hostmask = (string) $this;
  192. }
  193. $pattern = str_replace('*', '.*', $pattern);
  194. return (preg_match('#^' . $pattern . '$#', $hostmask) > 0);
  195. }
  196. }