Connection.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 connection metadata.
  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_Connection
  31. {
  32. /**
  33. * Host to which the client will connect
  34. *
  35. * @var string
  36. */
  37. protected $host;
  38. /**
  39. * Port on which the client will connect, defaults to the standard IRC
  40. * port
  41. *
  42. * @var int
  43. */
  44. protected $port;
  45. /**
  46. * Transport for the connection, defaults to tcp but can be set to ssl
  47. * or variations thereof to connect over SSL
  48. *
  49. * @var string
  50. */
  51. protected $transport;
  52. /**
  53. * Encoding method for the connection, defaults to ISO-8859-1 but can
  54. * be set to UTF8 if necessary
  55. *
  56. * @var strng
  57. */
  58. protected $encoding;
  59. /**
  60. * Nick that the client will use
  61. *
  62. * @var string
  63. */
  64. protected $nick;
  65. /**
  66. * Username that the client will use
  67. *
  68. * @var string
  69. */
  70. protected $username;
  71. /**
  72. * Realname that the client will use
  73. *
  74. * @var string
  75. */
  76. protected $realname;
  77. /**
  78. * Password that the client will use
  79. *
  80. * @var string
  81. */
  82. protected $password;
  83. /**
  84. * Hostmask for the connection
  85. *
  86. * @var Phergie_Hostmask
  87. */
  88. protected $hostmask;
  89. /**
  90. * Constructor to initialize instance properties.
  91. *
  92. * @param array $options Optional associative array of property values
  93. * to initialize
  94. *
  95. * @return void
  96. */
  97. public function __construct(array $options = array())
  98. {
  99. $this->transport = 'tcp';
  100. $this->encoding = 'ISO-8859-1';
  101. // @note this may need changed to something different, for broader support.
  102. // @note also may need to make use of http://us.php.net/manual/en/function.stream-encoding.php
  103. $this->setOptions($options);
  104. }
  105. /**
  106. * Emits an error related to a required connection setting does not have
  107. * value set for it.
  108. *
  109. * @param string $setting Name of the setting
  110. *
  111. * @return void
  112. */
  113. protected function checkSetting($setting)
  114. {
  115. if (empty($this->$setting)) {
  116. throw new Phergie_Connection_Exception(
  117. 'Required connection setting "' . $setting . '" missing',
  118. Phergie_Connection_Exception::ERR_REQUIRED_SETTING_MISSING
  119. );
  120. }
  121. }
  122. /**
  123. * Returns a hostmask that uniquely identifies the connection.
  124. *
  125. * @return string
  126. */
  127. public function getHostmask()
  128. {
  129. if (empty($this->hostmask)) {
  130. $this->hostmask = new Phergie_Hostmask(
  131. $this->getNick(),
  132. $this->getUsername(),
  133. $this->getHost()
  134. );
  135. }
  136. return $this->hostmask;
  137. }
  138. /**
  139. * Sets the host to which the client will connect.
  140. *
  141. * @param string $host Hostname
  142. *
  143. * @return Phergie_Connection Provides a fluent interface
  144. */
  145. public function setHost($host)
  146. {
  147. if (empty($this->host)) {
  148. $this->host = (string) $host;
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Returns the host to which the client will connect if it is set or
  154. * emits an error if it is not set.
  155. *
  156. * @return string
  157. */
  158. public function getHost()
  159. {
  160. $this->checkSetting('host');
  161. return $this->host;
  162. }
  163. /**
  164. * Sets the port on which the client will connect.
  165. *
  166. * @param int $port Port
  167. *
  168. * @return Phergie_Connection Provides a fluent interface
  169. */
  170. public function setPort($port)
  171. {
  172. if (empty($this->port)) {
  173. $this->port = (int) $port;
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Returns the port on which the client will connect.
  179. *
  180. * @return int
  181. */
  182. public function getPort()
  183. {
  184. if (empty($this->port)) {
  185. $this->port = 6667;
  186. }
  187. return $this->port;
  188. }
  189. /**
  190. * Sets the transport for the connection to use.
  191. *
  192. * @param string $transport Transport (ex: tcp, ssl, etc.)
  193. *
  194. * @return Phergie_Connection Provides a fluent interface
  195. */
  196. public function setTransport($transport)
  197. {
  198. $this->transport = (string) $transport;
  199. if (!in_array($this->transport, stream_get_transports())) {
  200. throw new Phergie_Connection_Exception(
  201. 'Transport ' . $this->transport . ' is not supported',
  202. Phergie_Connection_Exception::ERR_TRANSPORT_NOT_SUPPORTED
  203. );
  204. }
  205. return $this;
  206. }
  207. /**
  208. * Returns the transport in use by the connection.
  209. *
  210. * @return string Transport (ex: tcp, ssl, etc.)
  211. */
  212. public function getTransport()
  213. {
  214. return $this->transport;
  215. }
  216. /**
  217. * Sets the encoding for the connection to use.
  218. *
  219. * @param string $encoding Encoding to use (ex: ASCII, ISO-8859-1, UTF8, etc.)
  220. *
  221. * @return Phergie_Connection Provides a fluent interface
  222. */
  223. public function setEncoding($encoding)
  224. {
  225. $this->encoding = (string) $encoding;
  226. if (!in_array($this->encoding, mb_list_encodings())) {
  227. throw new Phergie_Connection_Exception(
  228. 'Encoding ' . $this->encoding . ' is not supported',
  229. Phergie_Connection_Exception::ERR_ENCODING_NOT_SUPPORTED
  230. );
  231. }
  232. return $this;
  233. }
  234. /**
  235. * Returns the encoding in use by the connection.
  236. *
  237. * @return string Encoding (ex: ASCII, ISO-8859-1, UTF8, etc.)
  238. */
  239. public function getEncoding()
  240. {
  241. return $this->encoding;
  242. }
  243. /**
  244. * Sets the nick that the client will use.
  245. *
  246. * @param string $nick Nickname
  247. *
  248. * @return Phergie_Connection Provides a fluent interface
  249. */
  250. public function setNick($nick)
  251. {
  252. if (empty($this->nick)) {
  253. $this->nick = (string) $nick;
  254. }
  255. return $this;
  256. }
  257. /**
  258. * Returns the nick that the client will use.
  259. *
  260. * @return string
  261. */
  262. public function getNick()
  263. {
  264. $this->checkSetting('nick');
  265. return $this->nick;
  266. }
  267. /**
  268. * Sets the username that the client will use.
  269. *
  270. * @param string $username Username
  271. *
  272. * @return Phergie_Connection Provides a fluent interface
  273. */
  274. public function setUsername($username)
  275. {
  276. if (empty($this->username)) {
  277. $this->username = (string) $username;
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Returns the username that the client will use.
  283. *
  284. * @return string
  285. */
  286. public function getUsername()
  287. {
  288. $this->checkSetting('username');
  289. return $this->username;
  290. }
  291. /**
  292. * Sets the realname that the client will use.
  293. *
  294. * @param string $realname Real name
  295. *
  296. * @return Phergie_Connection Provides a fluent interface
  297. */
  298. public function setRealname($realname)
  299. {
  300. if (empty($this->realname)) {
  301. $this->realname = (string) $realname;
  302. }
  303. return $this;
  304. }
  305. /**
  306. * Returns the realname that the client will use.
  307. *
  308. * @return string
  309. */
  310. public function getRealname()
  311. {
  312. $this->checkSetting('realname');
  313. return $this->realname;
  314. }
  315. /**
  316. * Sets the password that the client will use.
  317. *
  318. * @param string $password Password
  319. *
  320. * @return Phergie_Connection Provides a fluent interface
  321. */
  322. public function setPassword($password)
  323. {
  324. if (empty($this->password)) {
  325. $this->password = (string) $password;
  326. }
  327. return $this;
  328. }
  329. /**
  330. * Returns the password that the client will use.
  331. *
  332. * @return string
  333. */
  334. public function getPassword()
  335. {
  336. return $this->password;
  337. }
  338. /**
  339. * Sets multiple connection settings using an array.
  340. *
  341. * @param array $options Associative array of setting names mapped to
  342. * corresponding values
  343. *
  344. * @return Phergie_Connection Provides a fluent interface
  345. */
  346. public function setOptions(array $options)
  347. {
  348. foreach ($options as $option => $value) {
  349. $method = 'set' . ucfirst($option);
  350. if (method_exists($this, $method)) {
  351. $this->$method($value);
  352. }
  353. }
  354. }
  355. }