View source
<?php
class KalturaClientBase {
const KALTURA_API_VERSION = "3.0";
const KALTURA_SERVICE_FORMAT_JSON = 1;
const KALTURA_SERVICE_FORMAT_XML = 2;
const KALTURA_SERVICE_FORMAT_PHP = 3;
private $config;
private $ks;
private $shouldLog = false;
private $isMultiRequest = false;
private $callsQueue = array();
public function __construct(KalturaConfiguration $config = null) {
if ($config != null) {
$this->config = $config;
$logger = $this->config
->getLogger();
if ($logger) {
$this->shouldLog = true;
}
}
}
public function queueServiceActionCall($service, $action, $params = array(), $files = array()) {
if (!isset($params["partnerId"]) || $params["partnerId"] === -1) {
$params["partnerId"] = $this->config->partnerId;
}
$this
->addParam($params, "ks", $this->ks);
$call = new KalturaServiceActionCall($service, $action, $params, $files);
$this->callsQueue[] = $call;
}
public function doQueue() {
if (count($this->callsQueue) == 0) {
return null;
}
$startTime = microtime(true);
$params = array();
$files = array();
$this
->log("service url: [" . $this->config->serviceUrl . "]");
$this
->addParam($params, "apiVersion", self::KALTURA_API_VERSION);
$this
->addParam($params, "format", $this->config->format);
$this
->addParam($params, "clientTag", $this->config->clientTag);
$url = $this->config->serviceUrl . "/api_v3/index.php?service=";
if ($this->isMultiRequest) {
$url .= "multirequest";
$i = 1;
foreach ($this->callsQueue as $call) {
$callParams = $call
->getParamsForMultiRequest($i++);
$params = array_merge($params, $callParams);
$files = array_merge($files, $call->files);
}
}
else {
$call = $this->callsQueue[0];
$url .= $call->service . "&action=" . $call->action;
$params = array_merge($params, $call->params);
$files = $call->files;
}
$this->callsQueue = array();
$this->isMultiRequest = false;
$signature = $this
->signature($params);
$this
->addParam($params, "kalsig", $signature);
list($postResult, $error) = $this
->doHttpRequest($url, $params, $files);
if ($error) {
throw new Exception($error);
}
else {
$this
->log("result (serialized): " . $postResult);
if ($this->config->format == self::KALTURA_SERVICE_FORMAT_PHP) {
$result = @unserialize($postResult);
if ($result === false && serialize(false) !== $postResult) {
throw new Exception("failed to serialize server result\n{$postResult}");
}
$dump = print_r($result, true);
$this
->log("result (object dump): " . $dump);
}
else {
throw new Exception("unsupported format");
}
}
$endTime = microtime(true);
$this
->log("execution time for [" . $url . "]: [" . ($endTime - $startTime) . "]");
return $result;
}
private function signature($params) {
ksort($params);
$str = "";
foreach ($params as $k => $v) {
$str .= $k . $v;
}
return md5($str);
}
private function doHttpRequest($url, $params = array(), $files = array()) {
if (function_exists('curl_init')) {
return $this
->doCurl($url, $params, $files);
}
else {
return $this
->doPostRequest($url, $params, $files);
}
}
private function doCurl($url, $params = array(), $files = array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if (count($files) > 0) {
foreach ($files as &$file) {
$file = "@" . $file;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge($params, $files));
}
else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, "&"));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, '');
if (count($files) > 0) {
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
}
else {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->config->curlTimeout);
}
$result = curl_exec($ch);
$curlError = curl_error($ch);
curl_close($ch);
return array(
$result,
$curlError,
);
}
private function doPostRequest($url, $params = array(), $files = array()) {
if (count($files) > 0) {
throw new Exception("Uploading files is not supported with stream context http request, please use curl");
}
$formattedData = http_build_query($params, "", "&");
$params = array(
'http' => array(
"method" => "POST",
"Accept-language: en\r\n" . "Content-type: application/x-www-form-urlencoded\r\n",
"content" => $formattedData,
),
);
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
$phpErrorMsg = "";
throw new Exception("Problem with {$url}, {$phpErrorMsg}");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from {$url}, {$phpErrorMsg}");
}
return array(
$response,
'',
);
}
public function getKs() {
return $this->ks;
}
public function setKs($ks) {
$this->ks = $ks;
}
public function getConfig() {
return $this->config;
}
public function setConfig(KalturaConfiguration $config) {
$this->config = $config;
$logger = $this->config
->getLogger();
if ($logger instanceof IKalturaLogger) {
$this->shouldLog = true;
}
}
public function addParam(&$params, $paramName, $paramValue) {
if ($paramValue !== null) {
$params[$paramName] = $paramValue;
}
}
public function throwExceptionIfError($resultObject) {
if ($this
->isError($resultObject)) {
throw new KalturaException($resultObject["message"], $resultObject["code"]);
}
}
public function isError($resultObject) {
return is_array($resultObject) && isset($resultObject["message"]) && isset($resultObject["code"]);
}
public function validateObjectType($resultObject, $objectType) {
if (is_object($resultObject)) {
if (!$resultObject instanceof $objectType) {
throw new Exception("Invalid object type");
}
}
else {
if (gettype($resultObject) !== "NULL" && gettype($resultObject) !== $objectType) {
throw new Exception("Invalid object type");
}
}
}
public function startMultiRequest() {
$this->isMultiRequest = true;
}
public function doMultiRequest() {
return $this
->doQueue();
}
public function isMultiRequest() {
return $this->isMultiRequest;
}
protected function log($msg) {
if ($this->shouldLog) {
$this->config
->getLogger()
->log($msg);
}
}
}
class KalturaServiceActionCall {
public $service;
public $action;
public $params;
public $files;
public function __construct($service, $action, $params = array(), $files = array()) {
$this->service = $service;
$this->action = $action;
$newParams = array();
foreach ($params as $key => $val) {
if (is_array($val)) {
foreach ($val as $subKey => $subVal) {
$newParams[$key . ":" . $subKey] = $subVal;
}
}
else {
$newParams[$key] = $val;
}
}
$this->params = $newParams;
$this->files = $files;
}
public function getParamsForMultiRequest($multiRequestIndex) {
$multiRequestParams = array();
$multiRequestParams[$multiRequestIndex . ":service"] = $this->service;
$multiRequestParams[$multiRequestIndex . ":action"] = $this->action;
foreach ($this->params as $key => $val) {
$multiRequestParams[$multiRequestIndex . ":" . $key] = $val;
}
return $multiRequestParams;
}
}
abstract class KalturaServiceBase {
protected $client;
public function __construct(KalturaClient $client) {
$this->client = $client;
}
}
abstract class KalturaObjectBase {
protected function addIfNotNull(&$params, $paramName, $paramValue) {
if ($paramValue !== null) {
$params[$paramName] = $paramValue;
}
}
public function toParams() {
$params = array();
$params["objectType"] = get_class($this);
foreach ($this as $prop => $val) {
$this
->addIfNotNull($params, $prop, $val);
}
return $params;
}
}
class KalturaException extends Exception {
protected $code;
public function __construct($message, $code) {
$this->code = $code;
parent::__construct($message);
}
}
class KalturaConfiguration {
private $logger;
public $serviceUrl = "http://www.kaltura.com/";
public $partnerId = null;
public $format = 3;
public $clientTag = "php5";
public $curlTimeout = 10;
public function __construct($partnerId = -1) {
if (!is_numeric($partnerId)) {
throw new Exception("Invalid partner id");
}
$this->partnerId = $partnerId;
}
public function setLogger(IKalturaLogger $log) {
$this->logger = $log;
}
public function getLogger() {
return $this->logger;
}
}
interface IKalturaLogger {
function log($msg);
}