protected function renderDataInRequestedFormat($response_params, $return_value = false)
 {
     $renderer = new kalturaWebserviceRenderer($this);
     list($response, $content_type) = $renderer->renderDataInRequestedFormat($response_params, $this->response_type, self::$escape_text);
     $this->getResponse()->setHttpHeader("Content-Type", $content_type);
     if ($return_value) {
         return $response;
     } else {
         return $this->renderText($response);
     }
 }
 public function execute($add_extra_debug_data = true)
 {
     date_default_timezone_set(kConf::get("date_default_timezone"));
     // TODO - remove for production - use some configuration to determine
     kConfigTable::$should_use_cache = false;
     $start_impl = $end_impl = 0;
     $nocache = false;
     if ($this->getP("nocache")) {
         $nocache = true;
         $this->logMessage("Not using cache!");
         objectWrapperBase::useCache(false);
     }
     $add_benchmarks = $this->getP("add_benchmarks", false);
     // while testing our system - will match each service agains its description
     // $this->matchInDescription();
     $this->msg = array();
     $this->error = array();
     $this->debug = array();
     $start_time = microtime(true);
     $start = microtime(true);
     $this->benchmarkStart("beforeImpl");
     $this->response_type = $this->getP("format", self::DEFAULT_FORMAT);
     //
     /*
     		$should_debug = $this->getP ( "should_debug" , true );
     		if ( $should_debug == "false" ) $this->should_debug  = false;
     */
     if ($this->should_debug && $add_extra_debug_data) {
         $this->benchmarkStart("signature");
         $sig_type = $this->getP("sigtype", self::SIG_TYPE_POST);
         $signature_params = self::getParamsFromSigType($sig_type);
         $signatura_valid = self::validateSignature($signature_params);
         $this->benchmarkEnd("signature");
         $this->addDebug("sigtype", $sig_type);
         $this->addDebug("validateSignature", $signatura_valid);
         $this->addDebug("signature", self::signature($signature_params));
         //			$this->addDebug( "rawsignature" , self::signature( $signature_params , false ) );
     }
     $partner_id = $this->getP("partner_id");
     if (!$partner_id) {
         $partner_id = $this->getP("partnerId");
     }
     $subp_id = $this->getP("subp_id");
     if (!$subp_id) {
         $subp_id = $this->getP("subpId");
     }
     $puser_id = $this->getP("uid");
     $ks_str = $this->getP("ks");
     if ($ks_str == "{ks}") {
         $ks_str = "";
     }
     // if the client DIDN'T replace the dynamic ks - ignore it
     // the $execution_cache_key can be used by services to cache the results depending on the inpu parameters
     // if the $execution_cache_key is not null, the rendere will search for the result of the rendering depending on the $execution_cache_key
     // if it doesn't find it - it will create it (per format) and store it for next time
     $execution_cache_key = null;
     // moved the renderer here to see if has the $execution_cache_key and if so - skip the implementation
     $renderer = new kalturaWebserviceRenderer($this->response_context);
     $private_partner_data = false;
     try {
         try {
             $arr = list($partner_id, $subp_id, $uid, $private_partner_data) = $this->validateTicketSetPartner($partner_id, $subp_id, $puser_id, $ks_str);
         } catch (Exception $ex) {
             KalturaLog::log('validateTicketSetPartner failed - not caching response');
             defPartnerservices2baseAction::disableCache();
             throw $ex;
         }
         // if PS2 permission validation is enabled for the current partner, only the actions defined in kConf's parameter "ps2_actions_not_blocked_by_permissions" will be allowed
         $currentPartner = $this->getPartner();
         if ($currentPartner && $currentPartner->getEnabledService(PermissionName::FEATURE_PS2_PERMISSIONS_VALIDATION)) {
             if (!in_array(strtolower(get_class($this)), kConf::get('ps2_actions_not_blocked_by_permissions'))) {
                 KalturaLog::log('PS2 action ' . get_class($this) . ' is being blocked for partner ' . $currentPartner->getId() . ' defined with FEATURE_PS2_PERMISSIONS_VALIDATION enabled');
                 $this->addException(APIErrors::SERVICE_FORBIDDEN, get_class($this));
             }
         }
         $this->private_partner_data = $private_partner_data;
         //print_r ( $arr );
         // TODO - validate the matchIp is ok with the user's IP
         $this->validateIp();
         // most services should not attempt to cache the results - for them this will return null
         $execution_cache_key = $this->getExecutionCacheKeyWrapper($partner_id, $subp_id, $puser_id);
         // if the key is not null - it will be used in the renderer for using the cotent from the cache
         if ($nocache) {
             $renderer->deleteCacheKey($execution_cache_key, $this->response_type);
         } else {
             $renderer->setCacheKey($execution_cache_key);
         }
         if (!$renderer->hasContentForCacheKey($this->response_type)) {
             $this->benchmarkStart("applyPartnerFilters");
             //init entitlement before set the default criteire by myPartnerUtils::applyPartnerFilters
             kEntitlementUtils::initEntitlementEnforcement();
             // apply filters for Criteria so there will be no chance of exposure of date from other partners !
             // TODO - add the parameter for allowing kaltura network
             myPartnerUtils::applyPartnerFilters($partner_id, $private_partner_data, $this->partnerGroup2(), $this->kalturaNetwork2());
             $this->benchmarkEnd("applyPartnerFilters");
             $this->benchmarkStart("puserKuser");
             list($partner_id, $subp_id, $puser_id, $partner_prefix) = $this->preparePartnerPuserDetails($partner_id, $subp_id, $puser_id);
             $puser_kuser = $this->getPuserKuser($partner_id, $subp_id, $puser_id);
             $this->benchmarkEnd("puserKuser");
             $this->benchmarkEnd("beforeImpl");
             // ----------------------------- impl --------------------------
             $start_impl = microtime(true);
             $result = $this->executeImpl($partner_id, $subp_id, $puser_id, $partner_prefix, $puser_kuser);
             $end_impl = microtime(true);
             kEventsManager::flushEvents();
         } else {
             /// the renderer claims to have the desired result ! just flow down the code ... ;)
         }
         // ----------------------------- impl --------------------------
     } catch (APIException $api_ex) {
         $message = $api_ex->getMessage();
         if ($this->should_debug && $message) {
             $this->addError(APIErrors::SERVERL_ERROR, "[{$message}]");
         } elseif ($api_ex->api_code) {
             call_user_func_array(array(&$this, 'addError'), $api_ex->extra_data);
             //				$this->addError ( $api_ex->api_code ,$api_ex->extra_data );
         }
     } catch (PropelException $pex) {
         KalturaLog::alert($pex->getMessage());
         $this->addError(APIErrors::INTERNAL_DATABASE_ERROR);
     } catch (Exception $ex) {
         $this->addError(APIErrors::INTERNAL_SERVERL_ERROR, $ex->getMessage());
         KalturaLog::err($ex->getMessage());
     }
     $execute_impl_end_time = microtime(true);
     // render according to the format_type
     $res = array();
     $this->addMsg("serverTime", time());
     $res['result'] = $this->msg;
     $res['error'] = $this->error;
     if ($this->should_debug) {
         // this specific debug line should be used
         $this->addDebug("execute_impl_time", $end_impl - $start_impl);
         $this->addDebug("execute_time", $execute_impl_end_time - $start_time);
         // will be used as a place holder and will be replaced after the rendering.
         if ($add_extra_debug_data) {
             $this->addDebug("total_time", self::__TOTAL_TIME__);
         }
         if ($add_benchmarks && count($this->benchmarks) > 0) {
             $this->addDebug("host", @$_ENV["HOSTNAME"]);
             $this->addDebug("benchmarks", $this->getBenchmarks());
         }
         $res['debug'] = $this->debug;
     }
     // ignore all the errors and debug - the first msg is the only html used
     if ($this->response_type == kalturaWebserviceRenderer::RESPONSE_TYPE_HTML) {
         $res = "<html>";
         foreach ($this->msg as $html_bit) {
             $res .= $html_bit;
         }
         $res .= "</html>";
     }
     if ($this->response_type == kalturaWebserviceRenderer::RESPONSE_TYPE_MRSS) {
         // in case of mRss - render only the result not the errors ot the debug
         list($response, $content_type) = $renderer->renderDataInRequestedFormat($res['result'], $this->response_type, true, self::$escape_text);
     } else {
         list($response, $content_type) = $renderer->renderDataInRequestedFormat($res, $this->response_type, true, self::$escape_text);
     }
     $end_time = microtime(true);
     if (is_string($response)) {
         $this->logMessage("Rendereing took: [" . ($end_time - $start_time) . "] seconds. Response size [" . strlen($response) . "]", SF_LOG_WARNING);
         $this->logMessage($response, SF_LOG_WARNING);
     } else {
         $this->logMessage("Rendereing took: [" . ($end_time - $start_time) . "]");
     }
     if ($this->should_debug && $add_extra_debug_data) {
         // fix the total time including the render time
         $str_time = (string) ($end_time - $start_time);
         if ($this->response_type == kalturaWebserviceRenderer::RESPONSE_TYPE_PHP) {
             // replcate the placehoder with the real execution time
             // this is a nasty hack - we replace the serialized PHP value - the length of the placeholder is 14 characters
             // the length of the str_time can be less - replace the whole string phrase
             $replace_string = 's:' . strlen($str_time) . ':"' . $str_time;
             $response = str_replace('s:14:"' . self::__TOTAL_TIME__, $replace_string, $response);
         } elseif ($this->response_type == kalturaWebserviceRenderer::RESPONSE_TYPE_PHP_ARRAY || $this->response_type == kalturaWebserviceRenderer::RESPONSE_TYPE_PHP_OBJECT) {
             // the $response is not a string - we can't just replace it
             $res["debug"]["total_time"] = $str_time;
         } elseif ($this->response_type == kalturaWebserviceRenderer::RESPONSE_TYPE_MRSS) {
             // do nothing to the result
         } else {
             $response = str_replace(self::__TOTAL_TIME__, $str_time, $response);
         }
     }
     header("Access-Control-Allow-Origin:*");
     // avoid html5 xss issues
     $this->setContentType($content_type);
     // while testing our system - will match each service agains its description
     // $this->matchOutDescription();
     return $response;
 }