1
1
<?php
2
2
3
3
/*!
4
- * Open Realtime Connectivity JavaScript Library
4
+ * Realtime Messaging PHP Library
5
+ * Copyright 2016, Realtime
5
6
*
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
22
8
*/
23
9
24
10
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
+
28
20
// parse the given URL
29
21
$ url = parse_url ($ url );
30
22
// extract host and path
@@ -49,19 +41,19 @@ static function execute($method, $url, $data=array(), $referer='', $timeout=30,
49
41
// open a socket connection - timeout: 30 sec
50
42
$ fp = fsockopen ($ protocol .$ host , $ port , $ errno , $ errstr , $ timeout );
51
43
if ($ fp ) {
52
- // send the request headers:
44
+ // send the request headers:
53
45
fputs ($ fp , "$ method $ path HTTP/1.1 \r\n" );
54
46
fputs ($ fp , "host: $ host \r\n" );
55
47
if ($ referer != '' ) fputs ($ fp , "Referer: $ referer \r\n" );
56
48
if ($ method == 'POST ' ){
57
- fputs ($ fp , "Content-type: application/x-www-form-urlencoded \r\n" );
49
+ fputs ($ fp , "Content-type: " . $ content_type . " \r\n" );
58
50
fputs ($ fp , "Content-length: " . strlen ($ data ) . "\r\n" );
59
51
}
60
52
fputs ($ fp , "Connection: close \r\n\r\n" );
61
53
fputs ($ fp , $ data );
62
54
$ result = '' ;
63
55
while (!feof ($ fp )){
64
- // receive the results of the request
56
+ // receive the results of the request
65
57
$ result .= fgets ($ fp , 128 );
66
58
}
67
59
} else {
@@ -112,7 +104,7 @@ static function execute($method, $url, $data=array(), $referer='', $timeout=30,
112
104
);
113
105
}
114
106
115
- // return as structured array:
107
+ // return as structured array:
116
108
return array (
117
109
'errcode ' => 0 ,
118
110
'status ' => 'ok ' ,
@@ -124,7 +116,7 @@ static function execute($method, $url, $data=array(), $referer='', $timeout=30,
124
116
}
125
117
126
118
class Realtime{
127
- private $ ortc_data ;
119
+ private $ ortc_data ;
128
120
private $ is_version_21 ;
129
121
130
122
function __construct ($ balancer , $ app_key , $ priv_key , $ token ){
@@ -157,7 +149,7 @@ private function _get_server(){
157
149
}
158
150
if ('http://undefined:undefined ' == $ matches [0 ]) return '' ;
159
151
160
- // success
152
+ // success
161
153
return $ matches [0 ];
162
154
}
163
155
@@ -192,26 +184,26 @@ public function send($channel, $msg, &$response = Array() ){
192
184
193
185
while ($ part <= $ numberOfParts ){
194
186
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 ;
197
189
198
- $ part = $ part + 1 ;
190
+ $ part = $ part + 1 ;
199
191
200
192
}
201
193
202
194
return true ;
203
195
}
204
196
else
205
197
{
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
207
199
return $ ret ;
208
200
}
209
201
}
210
202
211
203
212
204
public function auth ($ channels , $ private = 0 , $ expire_time = 180000 , &$ response = Array () ){
213
205
214
- // post permissions
206
+ // post permissions
215
207
$ fields = array (
216
208
'AK ' => $ this ->ortc_data ['app_key ' ],
217
209
'PK ' => $ this ->ortc_data ['priv_key ' ],
@@ -225,16 +217,72 @@ public function auth($channels, $private = 0, $expire_time = 180000, &$response
225
217
$ fields [$ channel ] = $ perms ;
226
218
}
227
219
$ 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
+ }
238
286
239
287
}
240
288
?>
0 commit comments