79 public function __construct($nEnvironment, $sProviderCertificateFile)
81 if ($nEnvironment != self::ENVIRONMENT_PRODUCTION && $nEnvironment != self::ENVIRONMENT_SANDBOX) {
83 "Invalid environment '{$nEnvironment}'"
86 $this->_nEnvironment = $nEnvironment;
88 if (!is_readable($sProviderCertificateFile)) {
90 "Unable to read certificate file '{$sProviderCertificateFile}'"
93 $this->_sProviderCertificateFile = $sProviderCertificateFile;
95 $this->_nConnectTimeout = ini_get(
"default_socket_timeout");
96 $this->_nWriteInterval = self::WRITE_INTERVAL;
97 $this->_nConnectRetryInterval = self::CONNECT_RETRY_INTERVAL;
98 $this->_nSocketSelectTimeout = self::SOCKET_SELECT_TIMEOUT;
120 if (!is_object($logger)) {
122 "The logger should be an instance of 'ApnsPHP_Log_Interface'"
127 "Unable to use an instance of '" . get_class($logger) .
"' as logger: " .
128 "a logger must implements ApnsPHP_Log_Interface."
131 $this->_logger = $logger;
152 $this->_sProviderCertificatePassphrase = $sProviderCertificatePassphrase;
172 if (!is_readable($sRootCertificationAuthorityFile)) {
174 "Unable to read Certificate Authority file '{$sRootCertificationAuthorityFile}'"
177 $this->_sRootCertificationAuthorityFile = $sRootCertificationAuthorityFile;
201 $this->_nWriteInterval = (int)$nWriteInterval;
224 $this->_nConnectTimeout = (int)$nTimeout;
247 $this->_nConnectRetryTimes = (int)$nRetryTimes;
272 $this->_nConnectRetryInterval = (int)$nRetryInterval;
304 $this->_nSocketSelectTimeout = (int)$nSelectTimeout;
332 while (!$bConnected) {
336 $this->
_log(
'ERROR: ' . $e->getMessage());
337 if ($nRetry >= $this->_nConnectRetryTimes) {
341 "INFO: Retry to connect (" . ($nRetry+1) .
342 "/{$this->_nConnectRetryTimes})..."
344 usleep($this->_nConnectRetryInterval);
358 if (is_resource($this->_hSocket)) {
359 $this->
_log(
'INFO: Disconnected.');
360 return fclose($this->_hSocket);
376 $this->
_log(
"INFO: Trying {$sURL}...");
381 $streamContext = stream_context_create(array(
'ssl' => array(
382 'verify_peer' => isset($this->_sRootCertificationAuthorityFile),
383 'cafile' => $this->_sRootCertificationAuthorityFile,
384 'local_cert' => $this->_sProviderCertificateFile
387 if (!empty($this->_sProviderCertificatePassphrase)) {
388 stream_context_set_option($streamContext,
'ssl',
389 'passphrase', $this->_sProviderCertificatePassphrase);
392 $this->_hSocket = @stream_socket_client($sURL, $nError, $sError,
393 $this->_nConnectTimeout, STREAM_CLIENT_CONNECT, $streamContext);
395 if (!$this->_hSocket) {
397 "Unable to connect to '{$sURL}': {$sError} ({$nError})"
401 stream_set_blocking($this->_hSocket, 0);
402 stream_set_write_buffer($this->_hSocket, 0);
404 $this->
_log(
"INFO: Connected to {$sURL}.");
414 protected function _log($sMessage)
416 if (!isset($this->_logger)) {
419 $this->_logger->log($sMessage);
integer $_nConnectRetryTimes
Connect retry times.
void setLogger(ApnsPHP_Log_Interface $logger)
Set the Logger instance to use for logging purpose.
void setSocketSelectTimeout(integer $nSelectTimeout)
Set the TCP socket select timeout.
string $_sProviderCertificateFile
Provider certificate file with key (Bundled PEM).
void setWriteInterval(integer $nWriteInterval)
Set the write interval.
boolean _connect()
Connects to Apple Push Notification service server.
void setRootCertificationAuthority(string $sRootCertificationAuthorityFile)
Set the Root Certification Authority file.
integer $_nConnectRetryInterval
Connect retry interval in micro seconds.
void setProviderCertificatePassphrase(string $sProviderCertificatePassphrase)
Set the Provider Certificate passphrase.
const ENVIRONMENT_SANDBOX
integer Sandbox environment.
void setConnectRetryInterval(integer $nRetryInterval)
Set the connect retry interval.
void connect()
Connects to Apple Push Notification service server.
ApnsPHP_Log_Interface getLogger()
Get the Logger instance.
const WRITE_INTERVAL
integer Default write interval in micro seconds.
resource $_hSocket
SSL Socket.
Abstract class: this is the superclass for all Apple Push Notification Service classes.
integer $_nSocketSelectTimeout
Socket select timeout in micro seconds.
array $_aServiceURLs
Container for service URLs environments.
string $_sRootCertificationAuthorityFile
Root certification authority file.
integer getConnectTimeout()
Get the connection timeout.
boolean disconnect()
Disconnects from Apple Push Notifications service server.
string $_sProviderCertificatePassphrase
Provider certificate passphrase.
integer $_nWriteInterval
Write interval in micro seconds.
void _log(string $sMessage)
Logs a message through the Logger.
integer getWriteInterval()
Get the write interval.
ApnsPHP_Log_Interface $_logger
Logger.
void __construct(integer $nEnvironment, string $sProviderCertificateFile)
Constructor.
integer getConnectRetryTimes()
Get the connect retry time value.
const SOCKET_SELECT_TIMEOUT
integer Default socket select timeout in micro seconds.
void setConnectRetryTimes(integer $nRetryTimes)
Set the connect retry times value.
string getCertificateAuthority()
Get the Root Certification Authority file path.
const DEVICE_BINARY_SIZE
integer Device token length.
const CONNECT_RETRY_INTERVAL
integer Default connect retry interval in micro seconds.
const ENVIRONMENT_PRODUCTION
integer Production environment.
integer getConnectRetryInterval()
Get the connect retry interval.
integer $_nEnvironment
Active environment.
void setConnectTimeout(integer $nTimeout)
Set the connection timeout.
integer getSocketSelectTimeout()
Get the TCP socket select timeout.
integer $_nConnectTimeout
Connect timeout in seconds.