|
|
@@ -14,7 +14,7 @@ class Pusher implements LoggerAwareInterface
|
|
|
/**
|
|
|
* @var string Version
|
|
|
*/
|
|
|
- public static $VERSION = '3.2.0';
|
|
|
+ public static $VERSION = '3.3.1';
|
|
|
|
|
|
/**
|
|
|
* @var null|PusherCrypto
|
|
|
@@ -55,6 +55,9 @@ class Pusher implements LoggerAwareInterface
|
|
|
* useTLS - quick option to use scheme of https and port 443.
|
|
|
* encrypted - deprecated; renamed to `useTLS`.
|
|
|
* cluster - cluster name to connect to.
|
|
|
+ * encryption_master_key - a 32 char long key. This key, along with the channel name, are used to derive per-channel encryption keys. Per-channel keys are used encrypt event data on encrypted channels.
|
|
|
+ * debug - (default `false`) if `true`, every `trigger()` and `triggerBatch()` call will return a `$response` object, useful for logging/inspection purposes.
|
|
|
+ * curl_options - wrapper for curl_setopt, more here: http://php.net/manual/en/function.curl-setopt.php
|
|
|
* notification_host - host to connect to for native notifications.
|
|
|
* notification_scheme - scheme for the notification_host.
|
|
|
* @param string $host [optional] - deprecated
|
|
|
@@ -373,8 +376,12 @@ class Pusher implements LoggerAwareInterface
|
|
|
$response['body'] = curl_exec($ch);
|
|
|
$response['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
|
|
- if ($response['body'] === false || $response['status'] < 200 || 400 <= $response['status']) {
|
|
|
+ if ($response['body'] === false) {
|
|
|
$this->log('exec_curl error: {error}', array('error' => curl_error($ch)), LogLevel::ERROR);
|
|
|
+ } elseif ($response['status'] < 200 || 400 <= $response['status']) {
|
|
|
+ $this->log('exec_curl {status} error from server: {body}', $response, LogLevel::ERROR);
|
|
|
+ } else {
|
|
|
+ $this->log('exec_curl {status} response: {body}', $response);
|
|
|
}
|
|
|
|
|
|
$this->log('exec_curl response: {response}', array('response' => print_r($response, true)));
|
|
|
@@ -393,11 +400,11 @@ class Pusher implements LoggerAwareInterface
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Build the ddn domain.
|
|
|
+ * Build the Channels domain.
|
|
|
*
|
|
|
* @return string
|
|
|
*/
|
|
|
- private function ddn_domain()
|
|
|
+ private function channels_domain()
|
|
|
{
|
|
|
return $this->settings['scheme'].'://'.$this->settings['host'].':'.$this->settings['port'];
|
|
|
}
|
|
|
@@ -490,21 +497,35 @@ class Pusher implements LoggerAwareInterface
|
|
|
$this->validate_channels($channels);
|
|
|
$this->validate_socket_id($socket_id);
|
|
|
|
|
|
+ $has_encrypted_channel = false;
|
|
|
+ foreach ($channels as $chan) {
|
|
|
+ if (PusherCrypto::is_encrypted_channel($chan)) {
|
|
|
+ $has_encrypted_channel = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($has_encrypted_channel) {
|
|
|
+ if (count($channels) > 1) {
|
|
|
+ // For rationale, see limitations of end-to-end encryption in the README
|
|
|
+ throw new PusherException('You cannot trigger to multiple channels when using encrypted channels');
|
|
|
+ } else {
|
|
|
+ $data_encoded = $this->crypto->encrypt_payload($channels[0], $already_encoded ? $data : json_encode($data));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $data_encoded = $already_encoded ? $data : json_encode($data);
|
|
|
+ }
|
|
|
+
|
|
|
$query_params = array();
|
|
|
|
|
|
$s_url = $this->settings['base_path'].'/events';
|
|
|
|
|
|
- $data_encoded = $already_encoded ? $data : json_encode($data);
|
|
|
-
|
|
|
// json_encode might return false on failure
|
|
|
if (!$data_encoded) {
|
|
|
$this->log('Failed to perform json_encode on the the provided data: {error}', array(
|
|
|
'error' => print_r($data, true),
|
|
|
), LogLevel::ERROR);
|
|
|
}
|
|
|
- if (PusherCrypto::is_encrypted_channel($channels[0])) {
|
|
|
- $data_encoded = $this->crypto->encrypt_payload($channels[0], $data_encoded);
|
|
|
- }
|
|
|
+
|
|
|
$post_params = array();
|
|
|
$post_params['name'] = $event;
|
|
|
$post_params['data'] = $data_encoded;
|
|
|
@@ -518,7 +539,7 @@ class Pusher implements LoggerAwareInterface
|
|
|
|
|
|
$query_params['body_md5'] = md5($post_value);
|
|
|
|
|
|
- $ch = $this->create_curl($this->ddn_domain(), $s_url, 'POST', $query_params);
|
|
|
+ $ch = $this->create_curl($this->channels_domain(), $s_url, 'POST', $query_params);
|
|
|
|
|
|
$this->log('trigger POST: {post_value}', compact('post_value'));
|
|
|
|
|
|
@@ -526,14 +547,14 @@ class Pusher implements LoggerAwareInterface
|
|
|
|
|
|
$response = $this->exec_curl($ch);
|
|
|
|
|
|
- if ($response['status'] === 200 && $debug === false) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
if ($debug === true || $this->settings['debug'] === true) {
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
+ if ($response['status'] === 200) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -550,26 +571,33 @@ class Pusher implements LoggerAwareInterface
|
|
|
*/
|
|
|
public function triggerBatch($batch = array(), $debug = false, $already_encoded = false)
|
|
|
{
|
|
|
- $query_params = array();
|
|
|
+ foreach ($batch as $key => $event) {
|
|
|
+ $this->validate_channel($event['channel']);
|
|
|
+ if (isset($event['socket_id'])) {
|
|
|
+ $this->validate_socket_id($event['socket_id']);
|
|
|
+ }
|
|
|
|
|
|
- $s_url = $this->settings['base_path'].'/batch_events';
|
|
|
+ $data = $event['data'];
|
|
|
+ if (!is_string($data)) {
|
|
|
+ $data = $already_encoded ? $data : json_encode($data);
|
|
|
+ }
|
|
|
|
|
|
- if (!$already_encoded) {
|
|
|
- foreach ($batch as $key => $event) {
|
|
|
- if (!is_string($event['data'])) {
|
|
|
- $batch[$key]['data'] = json_encode($event['data']);
|
|
|
- }
|
|
|
+ if (PusherCrypto::is_encrypted_channel($event['channel'])) {
|
|
|
+ $batch[$key]['data'] = $this->crypto->encrypt_payload($event['channel'], $data);
|
|
|
+ } else {
|
|
|
+ $batch[$key]['data'] = $data;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$post_params = array();
|
|
|
$post_params['batch'] = $batch;
|
|
|
-
|
|
|
$post_value = json_encode($post_params);
|
|
|
|
|
|
+ $query_params = array();
|
|
|
$query_params['body_md5'] = md5($post_value);
|
|
|
+ $s_url = $this->settings['base_path'].'/batch_events';
|
|
|
|
|
|
- $ch = $this->create_curl($this->ddn_domain(), $s_url, 'POST', $query_params);
|
|
|
+ $ch = $this->create_curl($this->channels_domain(), $s_url, 'POST', $query_params);
|
|
|
|
|
|
$this->log('trigger POST: {post_value}', compact('post_value'));
|
|
|
|
|
|
@@ -577,14 +605,14 @@ class Pusher implements LoggerAwareInterface
|
|
|
|
|
|
$response = $this->exec_curl($ch);
|
|
|
|
|
|
- if ($response['status'] === 200 && $debug === false) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
if ($debug === true || $this->settings['debug'] === true) {
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
+ if ($response['status'] === 200) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -649,7 +677,7 @@ class Pusher implements LoggerAwareInterface
|
|
|
{
|
|
|
$s_url = $this->settings['base_path'].$path;
|
|
|
|
|
|
- $ch = $this->create_curl($this->ddn_domain(), $s_url, 'GET', $params);
|
|
|
+ $ch = $this->create_curl($this->channels_domain(), $s_url, 'GET', $params);
|
|
|
|
|
|
$response = $this->exec_curl($ch);
|
|
|
|