Skip to content

Commit 5d6b0c3

Browse files
opentok
0 parents  commit 5d6b0c3

21 files changed

+1542
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

API_Config.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?PHP
2+
3+
/*!
4+
* OpenTok PHP Library
5+
* http://www.tokbox.com/
6+
*
7+
* Copyright 2010, TokBox, Inc.
8+
*
9+
*/
10+
11+
class API_Config {
12+
13+
// Replace this value with your TokBox API Partner Key
14+
const API_KEY = "11409442";
15+
16+
// Replace this value with your TokBox API Partner Secret
17+
const API_SECRET = "4551ad10b252ea8efb49caa951b85513e3baf922";
18+
19+
//const API_SERVER = "https://staging.tokbox.com/hl";
20+
// Uncomment this line when you launch your app
21+
const API_SERVER = "https://api.opentok.com/hl";
22+
23+
24+
const SDK_VERSION = "tbphp-v0.91.2011-10-12";
25+
26+
}

OpenTokSDK.php

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?PHP
2+
3+
/*!
4+
* OpenTok PHP Library
5+
* http://www.tokbox.com/
6+
*
7+
* Copyright 2010, TokBox, Inc.
8+
*
9+
* Last modified: 2011-10-12
10+
*/
11+
12+
require_once 'API_Config.php';
13+
require_once 'OpenTokSession.php';
14+
15+
//Generic OpenTok exception. Read the message to get more details
16+
class OpenTokException extends Exception { };
17+
//OpenTok exception related to authentication. Most likely an issue with your API key or secret
18+
class AuthException extends OpenTokException { };
19+
//OpenTok exception related to the HTTP request. Most likely due to a server error. (HTTP 500 error)
20+
class RequestException extends OpenTokException { };
21+
22+
class RoleConstants {
23+
const SUBSCRIBER = "subscriber"; //Can only subscribe
24+
const PUBLISHER = "publisher"; //Can publish, subscribe, and signal
25+
const MODERATOR = "moderator"; //Can do the above along with forceDisconnect and forceUnpublish
26+
};
27+
28+
class OpenTokSDK {
29+
30+
private $api_key;
31+
32+
private $api_secret;
33+
34+
public function __construct($api_key, $api_secret) {
35+
$this->api_key = $api_key;
36+
$this->api_secret = trim($api_secret);
37+
}
38+
39+
/** - Generate a token
40+
*
41+
* $session_id - If session_id is not blank, this token can only join the call with the specified session_id.
42+
* $role - One of the constants defined in RoleConstants. Default is publisher, look in the documentation to learn more about roles.
43+
* $expire_time - Optional timestamp to change when the token expires. See documentation on token for details.
44+
*/
45+
public function generate_token($session_id='', $role='', $expire_time=NULL, $connection_data='') {
46+
$create_time = time();
47+
48+
$nonce = microtime(true) . mt_rand();
49+
50+
if(!$role) {
51+
$role = RoleConstants::PUBLISHER;
52+
}
53+
54+
$data_string = "session_id=$session_id&create_time=$create_time&role=$role&nonce=$nonce";
55+
if(!is_null($expire_time)) {
56+
if(!is_numeric($expire_time))
57+
throw new OpenTokException("Expire time must be a number");
58+
if($expire_time < $create_time)
59+
throw new OpenTokException("Expire time must be in the future");
60+
if($expire_time > $create_time + 604800)
61+
throw new OpenTokException("Expire time must be in the next 7 days");
62+
$data_string .= "&expire_time=$expire_time";
63+
}
64+
if($connection_data != '') {
65+
if(strlen($connection_data) > 1000)
66+
throw new OpenTokException("Connection data must be less than 1000 characters");
67+
$data_string .= "&connection_data=" . urlencode($connection_data);
68+
}
69+
70+
$sig = $this->_sign_string($data_string, $this->api_secret);
71+
$api_key = $this->api_key;
72+
$sdk_version = API_Config::SDK_VERSION;
73+
74+
return "T1==" . base64_encode("partner_id=$api_key&sdk_version=$sdk_version&sig=$sig:$data_string");
75+
}
76+
77+
/**
78+
* Creates a new session.
79+
* $location - IP address to geolocate the call around.
80+
* $properties - Optional array, keys are defined in SessionPropertyConstants
81+
*/
82+
public function create_session($location, $properties=array()) {
83+
$properties["location"] = $location;
84+
$properties["api_key"] = $this->api_key;
85+
86+
$createSessionResult = $this->_do_request("/session/create", $properties);
87+
$createSessionXML = @simplexml_load_string($createSessionResult, 'SimpleXMLElement', LIBXML_NOCDATA);
88+
if(!$createSessionXML) {
89+
throw new OpenTokException("Failed to create session: Invalid response from server");
90+
}
91+
92+
$errors = $createSessionXML->xpath("//error");
93+
if($errors) {
94+
$errMsg = $errors[0]->xpath("//@message");
95+
if($errMsg) {
96+
$errMsg = (string)$errMsg[0]['message'];
97+
} else {
98+
$errMsg = "Unknown error";
99+
}
100+
throw new AuthException("Error " . $createSessionXML->error['code'] ." ". $createSessionXML->error->children()->getName() . ": " . $errMsg );
101+
}
102+
if(!isset($createSessionXML->Session->session_id)) {
103+
throw new OpenTokException("Failed to create session.");
104+
}
105+
$sessionId = $createSessionXML->Session->session_id;
106+
107+
return new OpenTokSession($sessionId, null);
108+
}
109+
110+
//////////////////////////////////////////////
111+
//Signing functions, request functions, and other utility functions needed for the OpenTok
112+
//Server API. Developers should not edit below this line. Do so at your own risk.
113+
//////////////////////////////////////////////
114+
115+
protected function _sign_string($string, $secret) {
116+
return hash_hmac("sha1", $string, $secret);
117+
}
118+
119+
protected function _do_request($url, $data) {
120+
$url = API_Config::API_SERVER . $url;
121+
122+
$dataString = "";
123+
foreach($data as $key => $value){
124+
$value = urlencode($value);
125+
$dataString .= "$key=$value&";
126+
}
127+
128+
$dataString = rtrim($dataString,"&");
129+
130+
//Use file_get_contents if curl is not available for PHP
131+
if(function_exists("curl_init")) {
132+
$ch = curl_init();
133+
134+
$api_key = $this->api_key;
135+
$api_secret = $this->api_secret;
136+
137+
curl_setopt($ch, CURLOPT_URL, $url);
138+
curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: application/x-www-form-urlencoded'));
139+
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("X-TB-PARTNER-AUTH: $this->api_key:$this->api_secret"));
140+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
141+
curl_setopt($ch, CURLOPT_HEADER, 0);
142+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
143+
curl_setopt($ch, CURLOPT_POST, 1);
144+
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
145+
146+
$res = curl_exec($ch);
147+
if(curl_errno($ch)) {
148+
throw new RequestException('Request error: ' . curl_error($ch));
149+
}
150+
151+
curl_close($ch);
152+
}
153+
else {
154+
if (function_exists("file_get_contents")) {
155+
$context_source = array ('http' => array (
156+
'method' => 'POST',
157+
'header'=> "Content-type: application/x-www-form-urlencoded\n"
158+
."X-TB-PARTNER-AUTH: $this->api_key:$this->api_secret\n"
159+
. "Content-Length: " . strlen($dataString) . "\n",
160+
'content' => $dataString
161+
)
162+
);
163+
$context = stream_context_create($context_source);
164+
$res = @file_get_contents( $url ,false, $context);
165+
}
166+
else{
167+
throw new RequestException("Your PHP installion neither supports the file_get_contents method nor cURL. Please enable one of these functions so that you can make API calls.");
168+
}
169+
}
170+
return $res;
171+
}
172+
}

OpenTokSession.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*!
4+
* OpenTok PHP Library
5+
* http://www.tokbox.com/
6+
*
7+
* Copyright 2010, TokBox, Inc.
8+
*/
9+
10+
11+
class OpenTokSession {
12+
13+
public $sessionId;
14+
15+
public $sessionProperties;
16+
17+
function __construct($sessionId, $properties) {
18+
$this->sessionId = $sessionId;
19+
$this->sessionProperties = $properties;
20+
}
21+
22+
public function getSessionId() {
23+
return $this->sessionId;
24+
}
25+
}

Session.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*!
4+
* OpenTok PHP Library v0.90.0
5+
* http://www.tokbox.com/
6+
*
7+
* Copyright 2010, TokBox, Inc.
8+
*
9+
* Date: November 05 14:50:00 2010
10+
*/
11+
12+
13+
class Session {
14+
15+
private $sessionId;
16+
17+
private $primaryMediaServer;
18+
19+
private $sessionProperties;
20+
21+
function __construct($sessionId, $primaryMediaServer, $properties) {
22+
$this->sessionId = $sessionId;
23+
$this->primaryMediaServer = $primaryMediaServer;
24+
$this->sessionProperties = $properties;
25+
}
26+
27+
public function getSessionId() {
28+
return $this->sessionId;
29+
}
30+
31+
public static function parseSession($sessionXml) {
32+
33+
34+
}
35+
}

SessionPropertyConstants.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?PHP
2+
/*!
3+
* OpenTok PHP Library
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*
8+
*/
9+
10+
class SessionPropertyConstants {
11+
const ECHOSUPPRESSION_ENABLED = "echoSuppression.enabled"; //Boolean
12+
const MULTIPLEXER_NUMOUTPUTSTREAMS = "multiplexer.numOutputStreams"; //Integer
13+
const MULTIPLEXER_SWITCHTYPE = "multiplexer.switchType"; //Integer
14+
const MULTIPLEXER_SWITCHTIMEOUT = "multiplexer.switchTimeout"; //Integer
15+
const MULTIPLEXER_TRANSITIONDURATION = "multiplexer.transitionDuration"; //Integer
16+
const P2P_PREFERENCE = "p2p.preference"; //String
17+
}

TestOpenTokSDK.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?PHP
2+
/*!
3+
* OpenTok PHP Library v0.90.0
4+
* http://www.tokbox.com/
5+
*
6+
* Copyright 2010, TokBox, Inc.
7+
*
8+
* Date: November 05 14:50:00 2010
9+
*/
10+
11+
require_once 'OpenTokSDK.php';
12+
$a = new OpenTokSDK(API_Config::API_KEY,API_Config::API_SECRET);
13+
print $a->generate_token();
14+
print "\n";
15+
print $a->generate_token('mysession');
16+
print "\n";
17+
print $a->generate_token('mysession', RoleConstants::MODERATOR);
18+
print "\n";
19+
try {
20+
print $a->create_session('127.0.0.1')->getSessionId();
21+
}catch(OpenTokException $e) {
22+
print $e->getMessage();
23+
}
24+
25+
print "\n";
26+

0 commit comments

Comments
 (0)