-
Notifications
You must be signed in to change notification settings - Fork 35
/
Pusher.php
165 lines (133 loc) · 4.6 KB
/
Pusher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
Pusher PHP Library
/////////////////////////////////
This was a very simple PHP library to the Pusher API.
$pusher = new Pusher(APIKEY, SECRET, APP_ID, CHANNEL, [Debug: true/false, HOST, PORT]);
$pusher->trigger('my_event', 'test_channel', [socket_id, Debug: true/false]);
$pusher->socket_auth('socket_id');
Copyright 2010, Squeeks. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
Contrbutors:
+ Paul44 (http://github.com/Paul44)
+ Ben Pickles (http://github.com/benpickles)
+ Mastercoding (http://www.mastercoding.nl)
*/
class Pusher
{
private $settings = array ();
/**
* PHP5 Constructor.
*
* Initializes a new Pusher instance with key, secret , app ID and channel.
* You can optionally turn on debugging for all requests by setting debug to true.
*
* @param string $auth_key
* @param string $secret
* @param int $app_id
* @param string $channel [optional]
* @param bool $debug [optional]
* @param string $host [optional]
* @param int $port [optional]
* @param int $timeout [optional]
*/
public function __construct( $auth_key, $secret, $app_id, $channel = '', $debug = false, $host = 'http://api.pusherapp.com', $port = '80', $timeout = 30 )
{
// Check compatibility, disable for speed improvement
$this->check_compatibility();
// Setup defaults
$this->settings['server'] = $host;
$this->settings['port'] = $port;
$this->settings['auth_key'] = $auth_key;
$this->settings['secret'] = $secret;
$this->settings['app_id'] = $app_id;
$this->settings['channel'] = $channel;
$this->settings['url'] = '/apps/' . $this->settings['app_id'];
$this->settings['debug'] = $debug;
$this->settings['timeout'] = $timeout;
}
/**
* Check if the current PHP setup is sufficient to run this class
*/
private function check_compatibility()
{
// Check for dependent PHP extensions (JSON, cURL)
if ( ! extension_loaded( 'curl' ) || ! extension_loaded( 'json' ) )
{
die( 'There is missing dependant extensions - please ensure both cURL and JSON modules are installed' );
}
# Supports SHA256?
if ( ! in_array( 'sha256', hash_algos() ) )
{
die( 'SHA256 appears to be unsupported - make sure you have support for it, or upgrade your version of PHP.' );
}
}
/**
* Trigger an event by providing event name and payload.
* Optionally provide a socket ID to exclude a client (most likely the sender).
*
* @param string $event
* @param mixed $payload
* @param int $socket_id [optional]
* @param string $channel [optional]
* @param bool $debug [optional]
* @return bool|string
*/
public function trigger( $event, $payload, $socket_id = null, $channel = '', $debug = false )
{
# Check if we can initialize a cURL connection
$ch = curl_init();
if ( $ch === false )
{
die( 'Could not initialise cURL!' );
}
# Add channel to URL..
$s_url = $this->settings['url'] . '/channels/' . ($channel != '' ? $channel : $this->settings['channel']) . '/events';
# Build the request
$signature = "POST\n" . $s_url . "\n";
$payload_encoded = json_encode( $payload );
$query = "auth_key=" . $this->settings['auth_key'] . "&auth_timestamp=" . time() . "&auth_version=1.0&body_md5=" . md5( $payload_encoded ) . "&name=" . $event;
# Socket ID set?
if ( $socket_id !== null )
{
$query .= "&socket_id=" . $socket_id;
}
# Create the signed signature...
$auth_signature = hash_hmac( 'sha256', $signature . $query, $this->settings['secret'], false );
$signed_query = $query . "&auth_signature=" . $auth_signature;
$full_url = $this->settings['server'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query;
# Set cURL opts and execute request
curl_setopt( $ch, CURLOPT_URL, $full_url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array ( "Content-Type: application/json" ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload_encoded );
curl_setopt( $ch, CURLOPT_TIMEOUT, $this->settings['timeout'] );
$response = curl_exec( $ch );
curl_close( $ch );
if ( $response == "202 ACCEPTED\n" && $debug == false )
{
return true;
}
elseif ( $debug == true || $this->settings['debug'] == true )
{
return $response;
}
else
{
return false;
}
}
/**
* Creates a socket signature
*
* @param int $socket_id
* @return string
*/
public function socket_auth( $socket_id )
{
$signature = hash_hmac( 'sha256', $socket_id . ':' . $this->settings['channel'], $this->settings['secret'], false );
$signature = array ( 'auth' => $this->settings['auth_key'] . ':' . $signature );
return json_encode( $signature );
}
}
?>