1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MobileNotif package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace LinkValue\MobileNotif\Client; |
11
|
|
|
|
12
|
|
|
use LinkValue\MobileNotif\Logger\ClientLoggableTrait; |
13
|
|
|
use LinkValue\MobileNotif\Logger\NullLogger; |
14
|
|
|
use LinkValue\MobileNotif\Exception\PushException; |
15
|
|
|
use LinkValue\MobileNotif\Model\Message; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Apple Push Notification Service client implementation. |
19
|
|
|
* |
20
|
|
|
* @author Jamal Youssefi <[email protected]> |
21
|
|
|
* @author Valentin Coulon <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ApnsClient implements ClientInterface |
24
|
|
|
{ |
25
|
|
|
use ClientLoggableTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Push server params. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $params; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
*/ |
37
|
9 |
|
public function __construct() |
38
|
|
|
{ |
39
|
9 |
|
$this->logger = new NullLogger(); |
40
|
9 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set up parameters. |
44
|
|
|
* |
45
|
|
|
* @param array $params |
46
|
|
|
* |
47
|
|
|
* @throws \RuntimeException |
48
|
|
|
*/ |
49
|
8 |
|
public function setUp(array $params) |
50
|
|
|
{ |
51
|
8 |
|
if (empty($params['endpoint'])) { |
52
|
1 |
|
throw new \RuntimeException('Parameter "endpoint" cannot be empty.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
7 |
|
if (empty($params['ssl_pem_path'])) { |
56
|
1 |
|
throw new \RuntimeException('Parameter "ssl_pem_path" cannot be empty.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
if (!is_readable($params['ssl_pem_path'])) { |
60
|
1 |
|
throw new \RuntimeException('"ssl_pem_path" file does not exist or is not readable.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
$this->params = $params; |
64
|
5 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Send $message to a mobile client. |
68
|
|
|
* |
69
|
|
|
* @param Message $message |
70
|
|
|
* |
71
|
|
|
* @throws \RuntimeException if setUp method was not called. |
72
|
|
|
* @throws PushException if connection to APNS server failed. |
73
|
|
|
*/ |
74
|
5 |
|
public function push(Message $message) |
75
|
|
|
{ |
76
|
5 |
|
if (empty($this->params)) { |
77
|
1 |
|
throw new \RuntimeException('Please set up this client using setUp() method before pushing messages.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
if (empty($message->getTokens())) { |
81
|
1 |
|
throw new \RuntimeException('No device token set. Please add at least 1 token using Message::addToken() before trying to push the message.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
$this->logger->info('Connecting to APNS server'); |
85
|
3 |
|
$stream = $this->getStreamSocketClient(); |
86
|
|
|
|
87
|
2 |
|
$payload = $message->getPayloadAsJson(); |
88
|
|
|
|
89
|
2 |
|
foreach ($message->getTokens() as $token) { |
90
|
2 |
|
$this->logger->info('Sending message to APNS server.', array( |
91
|
2 |
|
'deviceToken' => $token, |
92
|
2 |
|
'payload' => $payload, |
93
|
2 |
|
)); |
94
|
2 |
|
$this->writePayloadOnStreamForGivenToken($payload, $stream, $token); |
95
|
2 |
|
} |
96
|
|
|
|
97
|
2 |
|
fclose($stream); |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the Stream socket client. |
102
|
|
|
* |
103
|
|
|
* Connect to the APNS server and open a client socket to it. |
104
|
|
|
* |
105
|
|
|
* @return resource Socket to the APNS server. |
106
|
|
|
* |
107
|
|
|
* @throws PushException if connection to APNS server failed. |
108
|
|
|
*/ |
109
|
3 |
|
private function getStreamSocketClient() |
110
|
|
|
{ |
111
|
3 |
|
$stream_socket_client = stream_socket_client( |
112
|
3 |
|
$this->params['endpoint'], |
113
|
3 |
|
$errno, |
114
|
3 |
|
$errstr, |
115
|
3 |
|
30, |
116
|
3 |
|
STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, |
117
|
3 |
|
$this->getStreamContext() |
118
|
3 |
|
); |
119
|
|
|
|
120
|
3 |
|
if ($stream_socket_client === false) { |
121
|
1 |
|
throw new PushException('An error occurred while trying to contact APNS server.'); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return $stream_socket_client; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get secured stream context from SSL certificate. |
129
|
|
|
* |
130
|
|
|
* @return resource |
131
|
|
|
*/ |
132
|
3 |
|
private function getStreamContext() |
133
|
|
|
{ |
134
|
|
|
$context = array( |
135
|
|
|
'ssl' => array( |
136
|
3 |
|
'local_cert' => $this->params['ssl_pem_path'], |
137
|
3 |
|
), |
138
|
3 |
|
); |
139
|
|
|
|
140
|
|
|
// Handle Certificate Bundle passphrase |
141
|
3 |
|
if (!empty($this->params['ssl_passphrase'])) { |
142
|
1 |
|
$context['ssl']['passphrase'] = $this->params['ssl_passphrase']; |
143
|
1 |
|
} |
144
|
|
|
|
145
|
3 |
|
return stream_context_create($context); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Write $payload on $stream, it will be sent to given device $token. |
150
|
|
|
* |
151
|
|
|
* @param $payload |
152
|
|
|
* @param $stream |
153
|
|
|
* @param $token |
154
|
|
|
*/ |
155
|
2 |
|
private function writePayloadOnStreamForGivenToken($payload, $stream, $token) |
156
|
|
|
{ |
157
|
2 |
|
$binaryMessage = sprintf('%s%s%s%s%s', |
158
|
2 |
|
chr(0), |
159
|
2 |
|
pack('n', 32), |
160
|
2 |
|
pack('H*', str_replace(' ', '', $token)), |
161
|
2 |
|
pack('n', strlen($payload)), |
162
|
|
|
$payload |
163
|
2 |
|
); |
164
|
|
|
|
165
|
2 |
|
fwrite($stream, $binaryMessage, strlen($binaryMessage)); |
166
|
2 |
|
} |
167
|
|
|
} |
168
|
|
|
|