Skip to content

Commit 10b4e39

Browse files
committedOct 14, 2016
Added presence methods
1 parent 49468bc commit 10b4e39

File tree

1 file changed

+89
-41
lines changed

1 file changed

+89
-41
lines changed
 

‎ortc.php

+89-41
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
<?php
22

33
/*!
4-
* Open Realtime Connectivity JavaScript Library
4+
* Realtime Messaging PHP Library
5+
* Copyright 2016, Realtime
56
*
6-
* Copyright 2011, IBT, SA
7-
*
8-
* ORTC is part of our framework and it's powered by the world's first Real-Time Web Platform by IBT
9-
*
10-
* Join our movement to transform the World Wide Web into the Real-Time Web!
11-
*
12-
* Interested in Real-Time technology? See what we can do for your business today.
13-
*
14-
* Transform your old website into an exciting Real-Time experience!
15-
*
16-
* Improve the interaction with your visitor to a level never seen before
17-
* and increase visitor recurrency and loyalty.
18-
*
19-
* Visit us at www.realtime.co and learn more.
20-
*
21-
* Date: Thu Dec 05 2013 v2.1.12
7+
* Date: Fri Oct 14 2016 v2.1.14
228
*/
239

2410
class Request {
25-
static function execute($method, $url, $data=array(), $referer='', $timeout=30, $user_agent=''){
26-
// Convert the data array into URL Parameters like a=b&foo=bar etc.
27-
$data = http_build_query($data);
11+
static function execute($method, $url, $data=array(), $referer='', $timeout=30, $user_agent='', $content_type='application/x-www-form-urlencoded'){
12+
13+
if($content_type=='application/json') {
14+
$data = json_encode($data);
15+
} else {
16+
// Convert the data array into URL Parameters like a=b&foo=bar etc.
17+
$data = http_build_query($data);
18+
}
19+
2820
// parse the given URL
2921
$url = parse_url($url);
3022
// extract host and path
@@ -49,19 +41,19 @@ static function execute($method, $url, $data=array(), $referer='', $timeout=30,
4941
// open a socket connection - timeout: 30 sec
5042
$fp = fsockopen($protocol.$host, $port, $errno, $errstr, $timeout);
5143
if($fp) {
52-
// send the request headers:
44+
// send the request headers:
5345
fputs($fp, "$method $path HTTP/1.1\r\n");
5446
fputs($fp, "host: $host\r\n");
5547
if($referer != '') fputs($fp, "Referer: $referer\r\n");
5648
if($method == 'POST'){
57-
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
49+
fputs($fp, "Content-type: " . $content_type . "\r\n");
5850
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
5951
}
6052
fputs($fp, "Connection: close\r\n\r\n");
6153
fputs($fp, $data);
6254
$result = '';
6355
while(!feof($fp)){
64-
// receive the results of the request
56+
// receive the results of the request
6557
$result .= fgets($fp, 128);
6658
}
6759
} else {
@@ -112,7 +104,7 @@ static function execute($method, $url, $data=array(), $referer='', $timeout=30,
112104
);
113105
}
114106

115-
// return as structured array:
107+
// return as structured array:
116108
return array(
117109
'errcode' => 0,
118110
'status' => 'ok',
@@ -124,7 +116,7 @@ static function execute($method, $url, $data=array(), $referer='', $timeout=30,
124116
}
125117

126118
class Realtime{
127-
private $ortc_data;
119+
private $ortc_data;
128120
private $is_version_21;
129121

130122
function __construct($balancer, $app_key, $priv_key, $token){
@@ -157,7 +149,7 @@ private function _get_server(){
157149
}
158150
if('http://undefined:undefined' == $matches[0]) return '';
159151

160-
// success
152+
// success
161153
return $matches[0];
162154
}
163155

@@ -192,26 +184,26 @@ public function send($channel, $msg, &$response = Array() ){
192184

193185
while ($part <= $numberOfParts){
194186

195-
$ret = $this->send_message_part($url, $channel, $guid."_".$part."-".$numberOfParts."_".substr($msg,($part-1) * 699, 699), $response ); // $response returned used for debug purposes
196-
if (!$ret) return false;
187+
$ret = $this->send_message_part($url, $channel, $guid."_".$part."-".$numberOfParts."_".substr($msg,($part-1) * 699, 699), $response ); // $response returned used for debug purposes
188+
if (!$ret) return false;
197189

198-
$part = $part + 1;
190+
$part = $part + 1;
199191

200192
}
201193

202194
return true;
203195
}
204196
else
205197
{
206-
$ret = $this->send_message_part($url,$channel,$guid."_1-1_".$msg,$response); // returning $response for debug purpose
198+
$ret = $this->send_message_part($url,$channel,$guid."_1-1_".$msg,$response); // returning $response for debug purpose
207199
return $ret;
208200
}
209201
}
210202

211203

212204
public function auth($channels, $private = 0, $expire_time = 180000, &$response = Array() ){
213205

214-
// post permissions
206+
// post permissions
215207
$fields = array(
216208
'AK' => $this->ortc_data['app_key'],
217209
'PK' => $this->ortc_data['priv_key'],
@@ -225,16 +217,72 @@ public function auth($channels, $private = 0, $expire_time = 180000, &$response
225217
$fields[$channel] = $perms;
226218
}
227219
$url = $this->_get_server();
228-
if(!$url) return false; // no server available
229-
230-
$auth_path = '/authenticate';
231-
232-
$content = Request::execute('POST', $url.$auth_path, $fields, $referer='', 15, 'ortc-php'); // /auth or /authenticate depends on the server version
233-
234-
$response = $content;
235-
236-
return ( $content['errcode'] == 0 );
237-
}
220+
if(!$url) return false; // no server available
221+
222+
$auth_path = '/authenticate';
223+
224+
$content = Request::execute('POST', $url.$auth_path, $fields, $referer='', 15, 'ortc-php'); // /auth or /authenticate depends on the server version
225+
226+
$response = $content;
227+
228+
return ( $content['errcode'] == 0 );
229+
}
230+
231+
232+
public function enable_presence($channel, $metadata = 1, &$response = Array() ){
233+
234+
$fields = array(
235+
'privatekey' => $this->ortc_data['priv_key'],
236+
'metadata' => $metadata
237+
);
238+
239+
$url = $this->_get_server();
240+
if(!$url) return false; // no server available
241+
242+
$enable_path = '/presence/enable/' . $this->ortc_data['app_key'] . '/' . $channel;
243+
$content = Request::execute('POST', $url.$enable_path, $fields, $referer='', 15, 'ortc-php', 'application/json');
244+
$response = $content;
245+
246+
return ( $content['errcode'] == 0 );
247+
}
248+
249+
250+
public function disable_presence($channel, &$response = Array() ){
251+
252+
$fields = array(
253+
'privatekey' => $this->ortc_data['priv_key']
254+
);
255+
256+
$url = $this->_get_server();
257+
if(!$url) return false; // no server available
258+
259+
$enable_path = '/presence/disable/' . $this->ortc_data['app_key'] . '/' . $channel;
260+
$content = Request::execute('POST', $url.$enable_path, $fields, $referer='', 15, 'ortc-php', 'application/json');
261+
$response = $content;
262+
263+
return ( $content['errcode'] == 0 );
264+
}
265+
266+
267+
public function get_presence($channel, &$response = Array() ){
268+
269+
$fields = array(
270+
);
271+
272+
$url = $this->_get_server();
273+
if(!$url) return false; // no server available
274+
275+
$get_presence_path = '/presence/' . $this->ortc_data['app_key'] . '/' . $this->ortc_data['token'] . '/' . $channel;
276+
$content = Request::execute('GET', $url.$get_presence_path, $fields, $referer='', 15, 'ortc-php');
277+
278+
if($content['errcode'] == 0){
279+
$response = $content['content'];
280+
} else {
281+
$response = $content['response'][1];
282+
}
283+
284+
return ( $content['errcode'] == 0 );
285+
}
238286

239287
}
240288
?>

0 commit comments

Comments
 (0)
Please sign in to comment.