-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathortc.php
288 lines (223 loc) · 9.25 KB
/
ortc.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/*!
* Realtime Messaging PHP Library
* Copyright 2016, Realtime
*
* Date: Fri Oct 14 2016 v2.1.14
*/
class Request {
static function execute($method, $url, $data=array(), $referer='', $timeout=30, $user_agent='', $content_type='application/x-www-form-urlencoded'){
if($content_type=='application/json') {
$data = json_encode($data);
} else {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
}
// parse the given URL
$url = parse_url($url);
// extract host and path
$host = $url['host'];
$path = isset($url['path']) ? $url['path'] : '';
if (trim($host)=="" ){
return array(
'errcode' => -2,
'status' => 'err',
'error' => "Error: (parse_url) Host is empty"
);
}
if($url['scheme'] == 'http'){
$port = 80;
$protocol = '';
}else{
$port = 443;
$protocol = 'ssl://';
}
// open a socket connection - timeout: 30 sec
$fp = fsockopen($protocol.$host, $port, $errno, $errstr, $timeout);
if($fp) {
// send the request headers:
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "host: $host\r\n");
if($referer != '') fputs($fp, "Referer: $referer\r\n");
if($method == 'POST'){
fputs($fp, "Content-type: " . $content_type . "\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)){
// receive the results of the request
$result .= fgets($fp, 128);
}
} else {
return array(
'errcode' => -3,
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// be carefull with HTTP1.1 (nginx)
if(preg_match('/Transfer-Encoding: chunked/i', $header)){
$chunks = explode("\n", $content);
if (count($chunks)>1){
foreach($chunks as $line => $data){
if($line%2 == 0) unset($chunks[$line]);
}
array_pop($chunks);
$content = implode("\n", $chunks);
} else
$content = $result[1];
}
if( ! preg_match('/^HTTP\/1.1 ([0-9]{3})/', $header, $matches)){
return array(
'errcode' => -4,
'status' => 'err',
'error' => 'Error: failed to localize http 1.1 in the result header',
'response' => $result
);
};
if( !$matches[1] || $matches[1][0] !=='2' ){
return array(
'errcode' => -5,
'status' => 'err',
'error' => "Error: response was not successful",
'response' => $result
);
}
// return as structured array:
return array(
'errcode' => 0,
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
}
class Realtime{
private $ortc_data;
private $is_version_21;
function __construct($balancer, $app_key, $priv_key, $token){
$arg_list = func_get_args();
if (count($arg_list)<4) die("ERROR: Some of constructor's parameters are missing.");
foreach( $arg_list as $idx=>$val )
if ( trim($val) == "" )
die("ERROR: parameters '$idx' is not specified.");
$strpos_res = strpos( $balancer, "2.1" );
$this->is_version_21 = ($strpos_res > 0);
$this->ortc_data['balancer'] = $balancer;
$this->ortc_data['app_key'] = $app_key;
$this->ortc_data['priv_key'] = $priv_key;
$this->ortc_data['token'] = $token;
}
private function _get_server(){
$url = $this->ortc_data['balancer'].'?appkey='.$this->ortc_data['app_key'];
$balancer_response = Request::execute("GET",$url);
if($balancer_response['errcode'] != 0){
die('Error getting data from balancer! '.$balancer_response['error'].' Response: '.print_r($balancer_response['response'],true));
}
// error
if(!preg_match('/https?:\/\/[^\"]+/', $balancer_response['content'], $matches)){
return '';
}
if('http://undefined:undefined' == $matches[0]) return '';
// success
return $matches[0];
}
private function send_message_part($url, $channel, $msg, &$response = Array() ){
$message = array(
'AK' => $this->ortc_data['app_key'],
'PK' => $this->ortc_data['priv_key'],
'AT' => $this->ortc_data['token'],
'C' => $channel,
'M' => $msg
);
$content = Request::execute("POST",$url. '/send/', $message);
$response = $content;
return ( $content['errcode'] == 0 );
}
public function send($channel, $msg, &$response = Array() ){
$url = $this->_get_server();
if(!$url || $url == "") return false; // no server available
$numberOfParts = ((int)(strlen($msg) / 700)) + ((strlen($msg) % 700 == 0)? 0 : 1 );
$guid = substr(uniqid(),5,8);
if($numberOfParts > 1){
$part = 1;
while ($part <= $numberOfParts){
$ret = $this->send_message_part($url, $channel, $guid."_".$part."-".$numberOfParts."_".substr($msg,($part-1) * 699, 699), $response ); // $response returned used for debug purposes
if (!$ret) return false;
$part = $part + 1;
}
return true;
}
else
{
$ret = $this->send_message_part($url,$channel,$guid."_1-1_".$msg,$response); // returning $response for debug purpose
return $ret;
}
}
public function auth($channels, $private = 0, $expire_time = 180000, &$response = Array() ){
// post permissions
$fields = array(
'AK' => $this->ortc_data['app_key'],
'PK' => $this->ortc_data['priv_key'],
'AT' => $this->ortc_data['token'], //access token
'PVT' => $private,
'TTL' => $expire_time,
'TP' => count($channels) // total num of channels
);
foreach($channels as $channel => $perms){
$fields[$channel] = $perms;
}
$url = $this->_get_server();
if(!$url) return false; // no server available
$auth_path = '/authenticate';
$content = Request::execute('POST', $url.$auth_path, $fields, $referer='', 15, 'ortc-php'); // /auth or /authenticate depends on the server version
$response = $content;
return ( $content['errcode'] == 0 );
}
public function enable_presence($channel, $metadata = 1, &$response = Array() ){
$fields = array(
'privatekey' => $this->ortc_data['priv_key'],
'metadata' => $metadata
);
$url = $this->_get_server();
if(!$url) return false; // no server available
$enable_path = '/presence/enable/' . $this->ortc_data['app_key'] . '/' . $channel;
$content = Request::execute('POST', $url.$enable_path, $fields, $referer='', 15, 'ortc-php', 'application/json');
$response = $content;
return ( $content['errcode'] == 0 );
}
public function disable_presence($channel, &$response = Array() ){
$fields = array(
'privatekey' => $this->ortc_data['priv_key']
);
$url = $this->_get_server();
if(!$url) return false; // no server available
$enable_path = '/presence/disable/' . $this->ortc_data['app_key'] . '/' . $channel;
$content = Request::execute('POST', $url.$enable_path, $fields, $referer='', 15, 'ortc-php', 'application/json');
$response = $content;
return ( $content['errcode'] == 0 );
}
public function get_presence($channel, &$response = Array() ){
$fields = array(
);
$url = $this->_get_server();
if(!$url) return false; // no server available
$get_presence_path = '/presence/' . $this->ortc_data['app_key'] . '/' . $this->ortc_data['token'] . '/' . $channel;
$content = Request::execute('GET', $url.$get_presence_path, $fields, $referer='', 15, 'ortc-php');
if($content['errcode'] == 0){
$response = $content['content'];
} else {
$response = $content['response'][1];
}
return ( $content['errcode'] == 0 );
}
}
?>