PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/ezc/Mail/src/transports/transport_connection.php

https://github.com/ksecor/civicrm
PHP | 257 lines | 128 code | 19 blank | 110 comment | 16 complexity | dd675b62bb23dbd469bfb8ac9964fbf2 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcMailTransportConnection class
  4. *
  5. * @package Mail
  6. * @version 1.6.3
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * ezcMailTransportConnection is an internal class used to connect to
  13. * a server and have line based communication with.
  14. *
  15. * @property ezcMailTransportOptions $options
  16. * Holds the options you can set to the transport connection.
  17. *
  18. * @package Mail
  19. * @version 1.6.3
  20. * @access private
  21. */
  22. class ezcMailTransportConnection
  23. {
  24. /**
  25. * The line-break characters to send to the server.
  26. */
  27. const CRLF = "\r\n";
  28. /**
  29. * The connection to the server or null if there is none.
  30. *
  31. * @var resource
  32. */
  33. private $connection = null;
  34. /**
  35. * Options for a transport connection.
  36. *
  37. * @var ezcMailTransportOptions
  38. */
  39. private $options;
  40. /**
  41. * Constructs a new connection to the $server using the port $port.
  42. *
  43. * {@link ezcMailTransportOptions} for options you can specify for a
  44. * transport connection.
  45. *
  46. * @todo The @ should be removed when PHP doesn't throw warnings for connect problems.
  47. *
  48. * @throws ezcMailTransportException
  49. * if a connection to the server could not be made
  50. * @throws ezcBaseExtensionNotFoundException
  51. * if trying to use SSL and the extension openssl is not installed
  52. * @throws ezcBasePropertyNotFoundException
  53. * if $options contains a property not defined
  54. * @throws ezcBaseValueException
  55. * if $options contains a property with a value not allowed
  56. * @param string $server
  57. * @param int $port
  58. * @param ezcMailTransportOptions $options
  59. */
  60. public function __construct( $server, $port, ezcMailTransportOptions $options = null )
  61. {
  62. $errno = null;
  63. $errstr = null;
  64. if ( $options === null )
  65. {
  66. $this->options = new ezcMailTransportOptions();
  67. }
  68. else
  69. {
  70. $this->options = $options;
  71. }
  72. if ( $this->options->ssl )
  73. {
  74. if ( ezcBaseFeatures::hasExtensionSupport( 'openssl' ) !== true )
  75. {
  76. throw new ezcBaseExtensionNotFoundException( 'openssl', null, "PHP not configured --with-openssl." );
  77. }
  78. $this->connection = @stream_socket_client( "ssl://{$server}:{$port}",
  79. $errno, $errstr, $this->options->timeout );
  80. }
  81. else
  82. {
  83. $this->connection = @stream_socket_client( "tcp://{$server}:{$port}",
  84. $errno, $errstr, $this->options->timeout );
  85. }
  86. if ( is_resource( $this->connection ) )
  87. {
  88. stream_set_timeout( $this->connection, $this->options->timeout );
  89. }
  90. else
  91. {
  92. throw new ezcMailTransportException( "Failed to connect to the server: {$server}:{$port}." );
  93. }
  94. }
  95. /**
  96. * Sets the property $name to $value.
  97. *
  98. * @throws ezcBasePropertyNotFoundException
  99. * if the property $name does not exist
  100. * @throws ezcBaseValueException
  101. * if $value is not accepted for the property $name
  102. * @param string $name
  103. * @param mixed $value
  104. * @ignore
  105. */
  106. public function __set( $name, $value )
  107. {
  108. switch ( $name )
  109. {
  110. case 'options':
  111. if ( !( $value instanceof ezcMailTransportOptions ) )
  112. {
  113. throw new ezcBaseValueException( 'options', $value, 'instanceof ezcMailTransportOptions' );
  114. }
  115. $this->options = $value;
  116. break;
  117. default:
  118. throw new ezcBasePropertyNotFoundException( $name );
  119. }
  120. }
  121. /**
  122. * Returns the value of the property $name.
  123. *
  124. * @throws ezcBasePropertyNotFoundException
  125. * if the property $name does not exist
  126. * @param string $name
  127. * @ignore
  128. */
  129. public function __get( $name )
  130. {
  131. switch ( $name )
  132. {
  133. case 'options':
  134. return $this->options;
  135. default:
  136. throw new ezcBasePropertyNotFoundException( $name );
  137. }
  138. }
  139. /**
  140. * Returns true if the property $name is set, otherwise false.
  141. *
  142. * @param string $name
  143. * @return bool
  144. * @ignore
  145. */
  146. public function __isset( $name )
  147. {
  148. switch ( $name )
  149. {
  150. case 'options':
  151. return true;
  152. default:
  153. return false;
  154. }
  155. }
  156. /**
  157. * Send $data to the server through the connection.
  158. *
  159. * This method appends one line-break at the end of $data.
  160. *
  161. * @throws ezcMailTransportException
  162. * if there is no valid connection.
  163. * @param string $data
  164. */
  165. public function sendData( $data )
  166. {
  167. if ( is_resource( $this->connection ) )
  168. {
  169. if ( fwrite( $this->connection, $data . self::CRLF,
  170. strlen( $data ) + strlen( self::CRLF ) ) === false )
  171. {
  172. throw new ezcMailTransportException( 'Could not write to the stream. It was probably terminated by the host.' );
  173. }
  174. }
  175. }
  176. /**
  177. * Returns one line of data from the stream.
  178. *
  179. * The returned line will have linebreaks removed if the $trim option is set.
  180. *
  181. * @throws ezcMailTransportConnection
  182. * if there is no valid connection
  183. * @param bool $trim
  184. * @return string
  185. */
  186. public function getLine( $trim = false )
  187. {
  188. $data = '';
  189. $line = '';
  190. if ( is_resource( $this->connection ) )
  191. {
  192. // in case there is a problem with the connection fgets() returns false
  193. while ( strpos( $data, self::CRLF ) === false )
  194. {
  195. $line = fgets( $this->connection, 512 );
  196. /* If the mail server aborts the connection, fgets() will
  197. * return false. We need to throw an exception here to prevent
  198. * the calling code from looping indefinitely. */
  199. if ( $line === false )
  200. {
  201. $this->connection = null;
  202. throw new ezcMailTransportException( 'Could not read from the stream. It was probably terminated by the host.' );
  203. }
  204. $data .= $line;
  205. }
  206. if ( $trim == false )
  207. {
  208. return $data;
  209. }
  210. else
  211. {
  212. return rtrim( $data, "\r\n" );
  213. }
  214. }
  215. throw new ezcMailTransportException( 'Could not read from the stream. It was probably terminated by the host.' );
  216. }
  217. /**
  218. * Returns if the connection is open.
  219. *
  220. * @return bool
  221. */
  222. public function isConnected()
  223. {
  224. return is_resource( $this->connection );
  225. }
  226. /**
  227. * Closes the connection to the server if it is open.
  228. */
  229. public function close()
  230. {
  231. if ( is_resource( $this->connection ) )
  232. {
  233. fclose( $this->connection );
  234. $this->connection = null;
  235. }
  236. }
  237. }
  238. ?>