Commit cbc172ac authored by Richard Weinhold's avatar Richard Weinhold 🔨
Browse files

some codecleanup in APNSHandler

parent e5dab13e
......@@ -30,7 +30,8 @@ class APNSHandler extends PushHandler {
* @return bool
*/
public function send($message, array $devices, array $data = null) {
$result = true;
$result = true;
$arbitrary = [];
// init payload
$payload = ['aps' => [
......@@ -39,24 +40,14 @@ class APNSHandler extends PushHandler {
'sound' => 'default',
]];
// default arbitrary settings
$arbitrary = [
'expire' => 0,
'messageID' => 0,
'priority' => 10,
'command' => 1,
];
// handle arbitrary settings
foreach (['expire', 'messageID', 'priority', 'command'] as $key) {
if (isset($data[$key])) {
$arbitrary[$key] = (int) abs($data[$key]);
unset($data[$key]);
} elseif (isset($this->_server[$key])) {
$arbitrary[$key] = (int) abs($this->_server[$key]);
}
}
// apply additional data to payload
......@@ -70,11 +61,11 @@ class APNSHandler extends PushHandler {
// check and set cert-path
$certpath = realpath($this->_server['token']);
if (empty($certpath) || $certpath === DIRECTORY_SEPARATOR || !is_file($certpath)) {
throw new \Exception('invalid cert-file: ' . $certpath, 500);
throw new \Exception('Invalid cert-file: ' . $certpath, 500);
}
stream_context_set_option($ctx, 'ssl', 'local_cert', $certpath);
// set cert passphrase if required
// set cert passphrase if given
if ($this->_server['passphrase'] !== null) {
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->_server['passphrase']);
}
......@@ -83,7 +74,7 @@ class APNSHandler extends PushHandler {
$stream = @stream_socket_client($this->_server['url'], $errno, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$stream) {
throw new \Exception('error processing APNS [' . $errno . ']:' . $errstr, 500);
throw new \Exception('Error connecting to APNS-Server [' . $errno . ']: ' . $errstr, 500);
}
$payload = json_encode($payload);
......@@ -116,7 +107,14 @@ class APNSHandler extends PushHandler {
* @return string
*/
protected function _buildNotification($deviceToken, $payload, array $arbitrary = [], $version = 1) {
$tokenLength = 32;
// set default arbitrary settings
$arbitrary = array_merge([
'expire' => 0,
'messageID' => 0,
'priority' => 10,
'command' => 1,
], $arbitrary);
// cleanup device tokens
$deviceToken = str_replace(' ', '', trim($deviceToken, '<> '));
......@@ -127,40 +125,26 @@ protected function _buildNotification($deviceToken, $payload, array $arbitrary =
$notification = pack('C', 1); // Command 1
$notification .= pack('N', (int) $arbitrary['messageID']); // notification id
$notification .= pack('N', ($arbitrary['expire'] > 0 ? time() + $arbitrary['expire'] : 0)); // expiration timestamps
$notification .= pack('n', $tokenLength); // token length
$notification .= pack('H*', $deviceToken); // device-token
$notification .= pack('n', strlen($payload)); // payload-length
$notification .= $payload; // payload
$notification .= pack('nH*', 32, $deviceToken); // device-token
$notification .= pack('n', strlen($payload)) . $payload; // payload
return $notification;
} elseif ((int) $version === 2) {
if (!isset($arbitrary['priority']) || !is_int($arbitrary['priority'])) {
$arbitrary['priority'] = 10;
}
// build notification first
$notification = pack('C', 1); // head
$notification .= pack('n', $tokenLength); // token length
$notification .= pack('H*', $deviceToken); // device-token
$notification .= pack('C', 2);
$notification .= pack('n', strlen($payload)); // payload-length
$notification .= pack('A*', $payload); // payload
$notification .= pack('Cn', 3, 4);
$notification .= pack('N', (int) $arbitrary['messageID']); // notification id
$notification .= pack('Cn', 4, 4);
$notification .= pack('N', ($arbitrary['expire'] > 0 ? time() + $arbitrary['expire'] : 0)); // expiration timestamps
$notification .= pack('Cn', 5, 1);
$notification .= pack('C', (int) $arbitrary['priority']); // notification priority
// build notification
$notification = pack('CnH*', 1, 32, $deviceToken); // device-token
$notification .= pack('CnA*', 2, strlen($payload), $payload); // payload
$notification .= pack('CnN', 3, 4, (int) $arbitrary['messageID']); // notification id
$notification .= pack('CnN', 4, 4, ($arbitrary['expire'] > 0 ? time() + $arbitrary['expire'] : 0)); // expiration timestamps
$notification .= pack('CnC', 5, 1, (int) $arbitrary['priority']); // notification priority
// pack notification into frame
$frame = pack('C', 2); // Command 2
$frame .= pack('N', strlen($notification)); // notification length
$frame .= $notification; // notification
$frame .= pack('N', strlen($notification)) . $notification; // notification
return $frame;
} else {
throw new \Exception('unknown Command version', 500);
throw new \Exception('Unknown Command Version', 500);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment