You are here

class KalturaClientBase in Kaltura 6

Same name and namespace in other branches
  1. 5 kaltura_client/kaltura_client_base.php \KalturaClientBase
  2. 6.2 kaltura_client/KalturaClientBase.php \KalturaClientBase

Hierarchy

Expanded class hierarchy of KalturaClientBase

File

kaltura_client/kaltura_client_base.php, line 28

View source
class KalturaClientBase {

  /**
   * @var KalturaConfiguration
   */
  var $config;

  /**
   * @var string
   */
  var $ks;

  /**
   * @var boolean
   */
  var $shouldLog = true;

  /**
   * Kaltura client constuctor, expecting configuration object
   *
   * @param KalturaConfiguration $config
   */
  function KalturaClientBase($config) {
    $this->config = $config;
    $logger = $this->config
      ->getLogger();
    if ($logger) {
      $this->shouldLog = true;
    }
  }
  function http_parse_query($array = null, $convention = "%s") {
    if (!$array || count($array) == 0) {
      return '';
    }
    $query = '';
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $new_convention = sprintf($convention, $key) . '[%s]';
        $query .= http_parse_query($value, $new_convention);
      }
      else {
        $key = urlencode($key);
        $value = urlencode($value);
        $query .= sprintf($convention, $key) . "={$value}&";
      }
    }
    return $query;
  }
  function do_post_request($url, $data, $optional_headers = null) {
    if (!function_exists('fsockopen')) {
      return null;
    }
    $start = strpos($url, '//') + 2;
    $end = strpos($url, '/', $start);
    $host = substr($url, $start, $end - $start);
    $domain = substr($url, $end);
    $fp = fsockopen($host, 80);
    if (!$fp) {
      return null;
    }
    fputs($fp, "POST {$domain} HTTP/1.1\n");
    fputs($fp, "Host: {$host}\n");
    if ($optional_headers) {
      fputs($fp, $optional_headers);
    }
    fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
    fputs($fp, "Content-length: " . strlen($data) . "\n\n");
    fputs($fp, "{$data}\n\n");
    $response = "";
    while (!feof($fp)) {
      $response .= fread($fp, 32768);
    }
    $pos = strpos($response, "\r\n\r\n");
    if ($pos) {
      $response = substr($response, $pos + 4);
    }
    else {
      $response = "";
    }
    fclose($fp);
    return $response;
  }
  function hit($method, $session_user, $params) {
    $start_time = microtime(true);
    $this
      ->log("service url: [" . $this->config->serviceUrl . "]");
    $this
      ->log("trying to call method: [" . $method . "] for user id: [" . $session_user->userId . "] using session: [" . $this->ks . "]");

    // append the basic params
    $params["kaltura_api_version"] = KALTURA_API_VERSION;
    $params["partner_id"] = $this->config->partnerId;
    $params["subp_id"] = $this->config->subPartnerId;
    $params["format"] = $this->config->format;
    $params["uid"] = $session_user->userId;
    $this
      ->addOptionalParam($params, "user_name", $session_user->screenName);
    $this
      ->addOptionalParam($params, "ks", $this->ks);
    $url = $this->config->serviceUrl . "/index.php/partnerservices2/" . $method;
    $this
      ->log("full reqeust url: [" . $url . "]");
    if (function_exists("curl_init")) {
      $this
        ->log("using curl");
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_USERAGENT, "Kaltura PHP4 Client (API version " . KALTURA_API_VERSION . "; curl; PHP " . phpversion() . ")");
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      $signature = $this
        ->signature($params);
      $params["kalsig"] = $signature;
      $http_result = curl_exec($ch);
      $curl_error = curl_error($ch);
    }
    else {
      $this
        ->log("not using curl");
      $curl_error = "";
      $params_string = $this
        ->http_parse_query($params);
      $http_result = $this
        ->do_post_request($url, $params_string);
    }
    if ($curl_error) {
      $result["error"] = array(
        array(
          "code" => "CURL_ERROR",
          "desc" => $curl_error,
        ),
      );
    }
    else {
      $this
        ->log("result (serialized): [" . $http_result . "]");
      if ($this->config->format == KALTURA_SERVICE_FORMAT_PHP) {
        $result = @unserialize($http_result);
        if (!$result) {
          $result["result"] = null;
          $result["error"] = array(
            array(
              "code" => "SERIALIZE_ERROR",
              "desc" => "failed to serialize server result",
            ),
          );
        }

        //$dump = print_r($result, true);

        //$this->log("result (object dump): " . $dump);
      }
      else {
        $result["error"] = array(
          array(
            "code" => "UNSUPPORTED_FORMAT",
            "desc" => "unsuppoted format [" . $this->config->format . "]",
          ),
        );
      }
    }
    $end_time = microtime(true);
    $this
      ->log("execution time for method [" . $method . "]: [" . ($end_time - $start_time) . "]");
    return $result;
  }
  function start($session_user, $secret, $admin = null, $privileges = null, $expiry = 86400) {
    $result = $this
      ->startsession($session_user, $secret, $admin, $privileges, $expiry);
    $this->ks = @$result["result"]["ks"];
    return $result;
  }
  function signature($params) {
    ksort($params);
    $str = "";
    foreach ($params as $k => $v) {
      $str .= $k . $v;
    }
    return md5($str);
  }
  function getKs() {
    return $this->ks;
  }
  function setKs($ks) {
    $this->ks = $ks;
  }
  function addOptionalParam(&$params, $paramName, $paramValue) {
    if ($paramValue !== null) {
      $params[$paramName] = $paramValue;
    }
  }
  function log($msg) {
    if ($this->shouldLog) {
      $logger = $this->config
        ->getLogger();
      $logger
        ->log($msg);
    }
  }

}

Members