|
| 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 | +} |
0 commit comments