Exemplo n.º 1
1
 public function validateApiAccessControl()
 {
     if (kIpAddressUtils::isInternalIp()) {
         return true;
     }
     if ($this->getEnforceHttpsApi() && infraRequestUtils::getProtocol() != infraRequestUtils::PROTOCOL_HTTPS) {
         KalturaLog::err('Action was accessed over HTTP while the partner is configured for HTTPS access only');
         return false;
     }
     $accessControl = $this->getApiAccessControl();
     if (is_null($accessControl)) {
         return true;
     }
     $context = new kEntryContextDataResult();
     $scope = new accessControlScope();
     $scope->setKs(kCurrentContext::$ks);
     $scope->setContexts(array(ContextType::PLAY));
     $disableCache = $accessControl->applyContext($context, $scope);
     if ($disableCache) {
         kApiCache::disableCache();
     }
     if (count($context->getMessages())) {
         header("X-Kaltura-API-Access-Control: " . implode(', ', $context->getMessages()));
     }
     if (count($context->getActions())) {
         $actions = $context->getActions();
         foreach ($actions as $action) {
             /* @var $action kAccessControlAction */
             if ($action->getType() == RuleActionType::BLOCK) {
                 KalturaLog::err('Action was blocked by API access control');
                 return false;
             }
         }
     }
     return true;
 }
Exemplo n.º 2
0
 protected function applyFilterFields(baseObjectFilter $filter)
 {
     /* @var $filter cuePointFilter */
     // Reduce the cache expiry when fetching live stream cuepoints
     $entryId = $filter->get('_in_entry_id');
     if ($entryId && strpos($entryId, ',') === false) {
         $entry = entryPeer::retrieveByPK($entryId);
         if ($entry && $entry->getType() == entryType::LIVE_STREAM) {
             kApiCache::setExpiry(self::LIVE_ENTRY_CUE_POINT_CACHE_EXPIRY_SECONDS);
         }
     }
     if ($filter->get('_free_text')) {
         $this->sphinxSkipped = false;
         $freeTexts = $filter->get('_free_text');
         KalturaLog::debug("Attach free text [{$freeTexts}]");
         $this->addFreeTextToMatchClauseByMatchFields($freeTexts, CuePointFilter::FREE_TEXT_FIELDS);
     }
     $filter->unsetByName('_free_text');
     if ($filter->get('_eq_is_public')) {
         $this->sphinxSkipped = false;
         $isPublic = $filter->get('_eq_is_public');
         $this->addCondition('is_public' . " = " . $isPublic);
     }
     $filter->unsetByName('_eq_is_public');
     return parent::applyFilterFields($filter);
 }
Exemplo n.º 3
0
 public function apply()
 {
     if ($this->_cacheStatus == self::CACHE_STATUS_DISABLED) {
         kApiCache::disableCache();
         return;
     }
     // common cache fields
     foreach ($this->_extraFields as $extraField) {
         call_user_func_array(array('kApiCache', 'addExtraField'), $extraField);
     }
     // anonymous cache fields
     if ($this->_expiry) {
         kApiCache::setExpiry($this->_expiry);
     }
     if ($this->_cacheStatus == self::CACHE_STATUS_ANONYMOUS_ONLY) {
         kApiCache::disableConditionalCache();
         return;
     }
     // conditional cache fields
     if ($this->_conditionalCacheExpiry) {
         kApiCache::setConditionalCacheExpiry($this->_conditionalCacheExpiry);
     }
     kApiCache::addInvalidationKeys(array_keys($this->_invalidationKeys), $this->_invalidationTime);
     kApiCache::addSqlQueryConditions($this->_sqlConditions);
 }
Exemplo n.º 4
0
 public function getFieldValue(kScope $scope)
 {
     kApiCache::addExtraField(kApiCache::ECF_COUNTRY, kApiCache::COND_MATCH, $this->getStringValues($scope));
     $ip = $scope->getIp();
     $ipGeo = kGeoCoderManager::getGeoCoder($this->getGeoCoderType());
     return $ipGeo->getCountry($ip);
 }
Exemplo n.º 5
0
 protected function getFieldValue(kScope $scope = null)
 {
     kApiCache::addExtraField(kApiCache::ECF_IP);
     if (!$scope) {
         $scope = new kScope();
     }
     return $scope->getIp();
 }
Exemplo n.º 6
0
 public function getFieldValue(kScope $scope)
 {
     kApiCache::addExtraField(kApiCache::ECF_COORDINATES, kApiCache::COND_GEO_DISTANCE, $this->getStringValues($scope));
     $ip = $scope->getIp();
     $ipGeo = kGeoCoderManager::getGeoCoder($this->getGeoCoderType());
     return array($ipGeo->getCoordinates($ip));
     // wrap in an array since otherwise the coordinates will be perceived as a list of two values
 }
Exemplo n.º 7
0
 protected function getFieldValue(kScope $scope = null)
 {
     kApiCache::addExtraField(kApiCache::ECF_USER_AGENT);
     if (!$scope) {
         $scope = new kScope();
     }
     return $scope->getUserAgent();
 }
Exemplo n.º 8
0
 public function getFieldValue(kScope $scope)
 {
     if ($this->getHttpHeader() || $this->getAcceptInternalIps()) {
         kApiCache::addExtraField(array("type" => kApiCache::ECF_IP, kApiCache::ECFD_IP_HTTP_HEADER => $this->getHttpHeader(), kApiCache::ECFD_IP_ACCEPT_INTERNAL_IPS => $this->getAcceptInternalIps()), kApiCache::COND_IP_RANGE, $this->getStringValues($scope));
         return infraRequestUtils::getIpFromHttpHeader($this->getHttpHeader(), $this->getAcceptInternalIps(), true);
     }
     kApiCache::addExtraField(kApiCache::ECF_IP, kApiCache::COND_IP_RANGE, $this->getStringValues($scope));
     return $scope->getIp();
 }
 public function buildServeFlavors()
 {
     if ($this->getManifestRedirect() && $this->getHostName() != $_SERVER['HTTP_HOST']) {
         kApiCache::setConditionalCacheExpiry(600);
         // the result contains a KS so we shouldn't cache it for a long time
         $parsedUrl = parse_url($this->getUrl());
         $flavor = array('urlPrefix' => $this->params->getMediaProtocol() . '://' . $parsedUrl['host'], 'url' => $_SERVER["REQUEST_URI"]);
         return array($flavor);
     }
     return parent::buildServeFlavors();
 }
Exemplo n.º 10
0
 protected function getFieldValue(kScope $scope = null)
 {
     kApiCache::addExtraField(kApiCache::ECF_COORDINATES);
     if (!$scope) {
         $scope = new kScope();
     }
     $ip = $scope->getIp();
     $ipGeo = kGeoCoderManager::getGeoCoder($this->getGeoCoderType());
     $coordinates = $ipGeo->getCoordinates($ip);
     return implode(",", $coordinates);
 }
Exemplo n.º 11
0
 protected function getFieldValue(kScope $scope = null)
 {
     kApiCache::addExtraField(kApiCache::ECF_COUNTRY);
     if (!$scope) {
         $scope = new kScope();
     }
     $ip = $scope->getIp();
     $ipGeo = kGeoCoderManager::getGeoCoder($this->getGeoCoderType());
     $country = $ipGeo->getCountry($ip);
     return trim(strtolower($country), " \n\r\t");
 }
Exemplo n.º 12
0
 public function get($obj_name)
 {
     if (!$this->m_cache) {
         return NULL;
     }
     kApiCache::disableConditionalCache();
     $value = $this->m_cache->get($this->m_namespace . $obj_name);
     if (!isset($value)) {
         return NULL;
     }
     return $value;
 }
 /**
  * @param string $playbackContext
  */
 public function output($playbackContext)
 {
     if ($this->tokenizer) {
         $this->tokenizer->setPlaybackContext($playbackContext);
     }
     $this->tokenizeUrls();
     $headers = $this->getHeaders();
     foreach ($headers as $header) {
         header($header);
     }
     requestUtils::sendCachingHeaders(kApiCache::hasExtraFields() ? 0 : $this->cachingHeadersAge);
     echo $this->getBody();
     die;
 }
Exemplo n.º 14
0
 /**
  * Looks for the time that is stored under ks privilege as reference time.
  * If not found, returns time().
  *
  * @param bool $notifyApiCache
  * @return int
  */
 public static function getTime($notifyApiCache = true)
 {
     if (kCurrentContext::$ks_object) {
         $referenceTime = kCurrentContext::$ks_object->getPrivilegeValue(ks::PRIVILEGE_REFERENCE_TIME);
         if ($referenceTime) {
             return (int) $referenceTime;
         }
     }
     if ($notifyApiCache) {
         return kApiCache::getTime();
     } else {
         return time();
     }
 }
Exemplo n.º 15
0
 protected function doCall($operation, array $params = array(), $type = null)
 {
     kApiCache::disableConditionalCache();
     $namespace = 'http://tempuri.org';
     $soapAction = '';
     $headers = array();
     $headers["KALTURA_SESSION_ID"] = (string) new UniqueId();
     $result = $this->call($operation, $params, $namespace, $soapAction, $headers);
     $this->throwError($result);
     if ($type) {
         return new $type($result);
     }
     return $result;
 }
 public function __construct()
 {
     $this->_cacheKeyPrefix = 'playManifest-';
     parent::__construct();
     if (!kConf::get('enable_cache')) {
         return;
     }
     $this->_params = requestUtils::getRequestParams();
     if (isset($this->_params['nocache'])) {
         return;
     }
     $this->calculateCacheKey();
     $this->enableCache();
 }
Exemplo n.º 17
0
 protected function internalFulfilled(kScope $scope)
 {
     $referrer = $scope->getReferrer();
     if ($this->getNot() === true && !$this->globalWhitelistDomainsAppended && strpos($referrer, "kwidget") === false && kConf::hasParam("global_whitelisted_domains")) {
         $ks = $scope->getKs();
         if (!$ks || !in_array($ks->partner_id, kConf::get('global_whitelisted_domains_exclude'))) {
             $this->globalWhitelistDomainsAppended = true;
             $globalWhitelistedDomains = kConf::get("global_whitelisted_domains");
             if (!is_array($globalWhitelistedDomains)) {
                 $globalWhitelistedDomains = explode(',', $globalWhitelistedDomains);
             }
             foreach ($globalWhitelistedDomains as $globalWhitelistedDomain) {
                 $this->values[] = new kStringValue($globalWhitelistedDomain);
             }
         }
     }
     kApiCache::addExtraField(kApiCache::ECF_REFERRER, kApiCache::COND_SITE_MATCH, $this->getStringValues($scope));
     return parent::internalFulfilled($scope);
 }
Exemplo n.º 18
0
 public function execute($input_parameters = null)
 {
     if (!kQueryCache::isCurrentQueryHandled()) {
         kApiCache::disableConditionalCache();
     }
     $search = array();
     $replace = array();
     if (is_null($input_parameters)) {
         $search = array_reverse(array_keys($this->values));
         $replace = array_reverse($this->values);
     } else {
         $i = 1;
         foreach ($input_parameters as $value) {
             $search[] = ':p' . $i++;
             if (is_null($value)) {
                 $replace[] = "NULL";
             } else {
                 $replace[] = "'{$value}'";
             }
         }
         $search = array_reverse($search);
         $replace = array_reverse($replace);
     }
     $sql = str_replace($search, $replace, $this->queryString);
     KalturaLog::debug($sql);
     $sqlStart = microtime(true);
     if (self::$dryRun && !preg_match('/^(\\/\\*.+\\*\\/ )?SELECT/i', $sql)) {
         KalturaLog::debug("Sql dry run - " . (microtime(true) - $sqlStart) . " seconds");
     } else {
         try {
             parent::execute($input_parameters);
         } catch (PropelException $pex) {
             KalturaLog::alert($pex->getMessage());
             throw new PropelException("Database error");
         }
         $sqlTook = microtime(true) - $sqlStart;
         KalturaLog::debug("Sql took - " . $sqlTook . " seconds");
         KalturaMonitorClient::monitorDatabaseAccess($sql, $sqlTook);
     }
 }
Exemplo n.º 19
0
 public function unsetMediaServer($index, $hostname)
 {
     $server = $this->getFromCustomData("server-{$index}", LiveEntry::CUSTOM_DATA_NAMESPACE_MEDIA_SERVERS);
     if ($server && $server->getHostname() == $hostname) {
         $server = $this->removeFromCustomData("server-{$index}", LiveEntry::CUSTOM_DATA_NAMESPACE_MEDIA_SERVERS);
         $this->setLastBroadcastEndTime(kApiCache::getTime());
     }
     if (!$this->hasMediaServer()) {
         $this->setLiveStatus(LiveEntryStatus::STOPPED);
     }
 }
 /**
  * Will forward to the regular swf player according to the widget_id 
  */
 public function execute()
 {
     myDbHelper::$use_alternative_con = myDbHelper::DB_HELPER_CONN_PROPEL2;
     requestUtils::handleConditionalGet();
     ignore_user_abort();
     $entry_id = $this->getRequestParameter("entry_id");
     $widget_id = $this->getRequestParameter("widget_id", 0);
     $upload_token_id = $this->getRequestParameter("upload_token_id");
     $version = $this->getIntRequestParameter("version", null, 0, 10000000);
     $type = $this->getIntRequestParameter("type", 1, 1, 5);
     //Hack: if KMS sends thumbnail request containing "!" char, the type should be treated as 5.
     $width = $this->getRequestParameter("width", -1);
     $height = $this->getRequestParameter("height", -1);
     if (strpos($width, "!") || strpos($height, "!")) {
         $type = 5;
     }
     $width = $this->getFloatRequestParameter("width", -1, -1, 10000);
     $height = $this->getFloatRequestParameter("height", -1, -1, 10000);
     $crop_provider = $this->getRequestParameter("crop_provider", null);
     $quality = $this->getIntRequestParameter("quality", 0, 0, 100);
     $src_x = $this->getFloatRequestParameter("src_x", 0, 0, 10000);
     $src_y = $this->getFloatRequestParameter("src_y", 0, 0, 10000);
     $src_w = $this->getFloatRequestParameter("src_w", 0, 0, 10000);
     $src_h = $this->getFloatRequestParameter("src_h", 0, 0, 10000);
     $vid_sec = $this->getFloatRequestParameter("vid_sec", -1, -1);
     $vid_slice = $this->getRequestParameter("vid_slice", -1);
     $vid_slices = $this->getRequestParameter("vid_slices", -1);
     $density = $this->getFloatRequestParameter("density", 0, 0);
     $stripProfiles = $this->getRequestParameter("strip", null);
     $flavor_id = $this->getRequestParameter("flavor_id", null);
     $file_name = $this->getRequestParameter("file_name", null);
     $file_name = basename($file_name);
     // actual width and height of image from which the src_* values were taken.
     // these will be used to multiply the src_* parameters to make them relate to the original image size.
     $rel_width = $this->getFloatRequestParameter("rel_width", -1, -1, 10000);
     $rel_height = $this->getFloatRequestParameter("rel_height", -1, -1, 10000);
     if ($width == -1 && $height == -1) {
         $width = 120;
         $height = 90;
     } else {
         if ($width == -1) {
             // if only either width or height is missing reset them to zero, and convertImage will handle them
             $width = 0;
         } else {
             if ($height == -1) {
                 $height = 0;
             }
         }
     }
     $bgcolor = $this->getRequestParameter("bgcolor", "ffffff");
     $partner = null;
     // validating the inputs
     if (!is_numeric($quality) || $quality < 0 || $quality > 100) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'quality must be between 20 and 100');
     }
     if (!is_numeric($src_x) || $src_x < 0 || $src_x > 10000) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'src_x must be between 0 and 10000');
     }
     if (!is_numeric($src_y) || $src_y < 0 || $src_y > 10000) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'src_y must be between 0 and 10000');
     }
     if (!is_numeric($src_w) || $src_w < 0 || $src_w > 10000) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'src_w must be between 0 and 10000');
     }
     if (!is_numeric($src_h) || $src_h < 0 || $src_h > 10000) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'src_h must be between 0 and 10000');
     }
     if (!is_numeric($width) || $width < 0 || $width > 10000) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'width must be between 0 and 10000');
     }
     if (!is_numeric($height) || $height < 0 || $height > 10000) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'height must be between 0 and 10000');
     }
     if (!is_numeric($density) || $density < 0) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'density must be positive');
     }
     if (!is_numeric($vid_sec) || $vid_sec < -1) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'vid_sec must be positive');
     }
     if (!preg_match('/^[0-9a-fA-F]{1,6}$/', $bgcolor)) {
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY, 'bgcolor must be six hexadecimal characters');
     }
     if ($upload_token_id) {
         $upload_token = UploadTokenPeer::retrieveByPK($upload_token_id);
         if ($upload_token) {
             $partnerId = $upload_token->getPartnerId();
             $partner = PartnerPeer::retrieveByPK($partnerId);
             if ($density == 0) {
                 $density = $partner->getDefThumbDensity();
             }
             if (is_null($stripProfiles)) {
                 $stripProfiles = $partner->getStripThumbProfile();
             }
             $thumb_full_path = myContentStorage::getFSCacheRootPath() . myContentStorage::getGeneralEntityPath("uploadtokenthumb", $upload_token->getIntId(), $upload_token->getId(), $upload_token->getId() . ".jpg");
             kFile::fullMkdir($thumb_full_path);
             if (file_exists($upload_token->getUploadTempPath())) {
                 $src_full_path = $upload_token->getUploadTempPath();
                 $valid_image_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_WBMP);
                 $image_type = exif_imagetype($src_full_path);
                 if (!in_array($image_type, $valid_image_types)) {
                     // capture full frame
                     myFileConverter::captureFrame($src_full_path, $thumb_full_path, 1, "image2", -1, -1, 3);
                     if (!file_exists($thumb_full_path)) {
                         myFileConverter::captureFrame($src_full_path, $thumb_full_path, 1, "image2", -1, -1, 0);
                     }
                     $src_full_path = $thumb_full_path;
                 }
                 // and resize it
                 myFileConverter::convertImage($src_full_path, $thumb_full_path, $width, $height, $type, $bgcolor, true, $quality, $src_x, $src_y, $src_w, $src_h, $density, $stripProfiles);
                 kFile::dumpFile($thumb_full_path);
             } else {
                 KalturaLog::debug("token_id [{$upload_token_id}] not found in DC [" . kDataCenterMgr::getCurrentDcId() . "]. dump url to romote DC");
                 $remoteUrl = kDataCenterMgr::getRemoteDcExternalUrlByDcId(1 - kDataCenterMgr::getCurrentDcId()) . $_SERVER['REQUEST_URI'];
                 kFile::dumpUrl($remoteUrl);
             }
         }
     }
     if ($entry_id) {
         $entry = entryPeer::retrieveByPKNoFilter($entry_id);
         if (!$entry) {
             // problem could be due to replication lag
             kFile::dumpApiRequest(kDataCenterMgr::getRemoteDcExternalUrlByDcId(1 - kDataCenterMgr::getCurrentDcId()));
         }
     } else {
         // get the widget
         $widget = widgetPeer::retrieveByPK($widget_id);
         if (!$widget) {
             KExternalErrors::dieError(KExternalErrors::ENTRY_AND_WIDGET_NOT_FOUND);
         }
         // get the kshow
         $kshow_id = $widget->getKshowId();
         $kshow = kshowPeer::retrieveByPK($kshow_id);
         if ($kshow) {
             $entry_id = $kshow->getShowEntryId();
         } else {
             $entry_id = $widget->getEntryId();
         }
         $entry = entryPeer::retrieveByPKNoFilter($entry_id);
         if (!$entry) {
             KExternalErrors::dieError(KExternalErrors::ENTRY_NOT_FOUND);
         }
     }
     $partner = $entry->getPartner();
     if ($density == 0) {
         $density = $partner->getDefThumbDensity();
     }
     $thumbParams = new kThumbnailParameters();
     $thumbParams->setSupportAnimatedThumbnail($partner->getSupportAnimatedThumbnails());
     if (is_null($stripProfiles)) {
         $stripProfiles = $partner->getStripThumbProfile();
     }
     //checks whether the thumbnail display should be restricted by KS
     $base64Referrer = $this->getRequestParameter("referrer");
     $referrer = base64_decode($base64Referrer);
     if (!is_string($referrer)) {
         $referrer = "";
     }
     // base64_decode can return binary data
     if (!$referrer) {
         $referrer = kApiCache::getHttpReferrer();
     }
     $ksStr = $this->getRequestParameter("ks");
     $securyEntryHelper = new KSecureEntryHelper($entry, $ksStr, $referrer, accessControlContextType::THUMBNAIL);
     $securyEntryHelper->validateForPlay();
     // multiply the passed $src_* values so that they will relate to the original image size, according to $src_display_*
     if ($rel_width != -1) {
         $widthRatio = $entry->getWidth() / $rel_width;
         $src_x = $src_x * $widthRatio;
         $src_w = $src_w * $widthRatio;
     }
     if ($rel_height != -1) {
         $heightRatio = $entry->getHeight() / $rel_height;
         $src_y = $src_y * $heightRatio;
         $src_h = $src_h * $heightRatio;
     }
     $subType = entry::FILE_SYNC_ENTRY_SUB_TYPE_THUMB;
     if ($entry->getMediaType() == entry::ENTRY_MEDIA_TYPE_IMAGE) {
         $subType = entry::FILE_SYNC_ENTRY_SUB_TYPE_DATA;
     }
     KalturaLog::debug("get thumbnail filesyncs");
     $dataKey = $entry->getSyncKey($subType);
     list($file_sync, $local) = kFileSyncUtils::getReadyFileSyncForKey($dataKey, true, false);
     $tempThumbPath = null;
     $entry_status = $entry->getStatus();
     // both 640x480 and 0x0 requests are probably coming from the kdp
     // 640x480 - old kdp version requesting thumbnail
     // 0x0 - new kdp version requesting the thumbnail of an unready entry
     // we need to distinguish between calls from the kdp and calls from a browser: <img src=...>
     // that can't handle swf input
     if (($width == 640 && $height == 480 || $width == 0 && $height == 0) && ($entry_status == entryStatus::PRECONVERT || $entry_status == entryStatus::IMPORT || $entry_status == entryStatus::ERROR_CONVERTING || $entry_status == entryStatus::DELETED)) {
         $contentPath = myContentStorage::getFSContentRootPath();
         $msgPath = $contentPath . "content/templates/entry/bigthumbnail/";
         if ($entry_status == entryStatus::DELETED) {
             $msgPath .= $entry->getModerationStatus() == moderation::MODERATION_STATUS_BLOCK ? "entry_blocked.swf" : "entry_deleted.swf";
         } else {
             $msgPath .= $entry_status == entryStatus::ERROR_CONVERTING ? "entry_error.swf" : "entry_converting.swf";
         }
         kFile::dumpFile($msgPath, null, 0);
     }
     if (!$file_sync) {
         $tempThumbPath = $entry->getLocalThumbFilePath($entry, $version, $width, $height, $type, $bgcolor, $crop_provider, $quality, $src_x, $src_y, $src_w, $src_h, $vid_sec, $vid_slice, $vid_slices, $density, $stripProfiles, $flavor_id, $file_name);
         if (!$tempThumbPath) {
             KExternalErrors::dieError(KExternalErrors::MISSING_THUMBNAIL_FILESYNC);
         }
     }
     if (!$local && !$tempThumbPath && $file_sync) {
         if (!in_array($file_sync->getDc(), kDataCenterMgr::getDcIds())) {
             $remoteUrl = $file_sync->getExternalUrl($entry->getId());
             header("Location: {$remoteUrl}");
             die;
         }
         $remoteUrl = kDataCenterMgr::getRedirectExternalUrl($file_sync, $_SERVER['REQUEST_URI']);
         kFile::dumpUrl($remoteUrl);
     }
     // if we didnt return a template for the player die and dont return the original deleted thumb
     if ($entry_status == entryStatus::DELETED) {
         KExternalErrors::dieError(KExternalErrors::ENTRY_DELETED_MODERATED);
     }
     if (!$tempThumbPath) {
         try {
             $tempThumbPath = myEntryUtils::resizeEntryImage($entry, $version, $width, $height, $type, $bgcolor, $crop_provider, $quality, $src_x, $src_y, $src_w, $src_h, $vid_sec, $vid_slice, $vid_slices, null, $density, $stripProfiles, $thumbParams);
         } catch (Exception $ex) {
             if ($ex->getCode() != kFileSyncException::FILE_DOES_NOT_EXIST_ON_CURRENT_DC) {
                 KalturaLog::log("Error - resize image failed");
                 KExternalErrors::dieError(KExternalErrors::MISSING_THUMBNAIL_FILESYNC);
             }
             // get original flavor asset
             $origFlavorAsset = assetPeer::retrieveOriginalByEntryId($entry_id);
             if (!$origFlavorAsset) {
                 KalturaLog::log("Error - no original flavor for entry [{$entry_id}]");
                 KExternalErrors::dieError(KExternalErrors::FLAVOR_NOT_FOUND);
             }
             $syncKey = $origFlavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET);
             $remoteFileSync = kFileSyncUtils::getOriginFileSyncForKey($syncKey, false);
             if (!$remoteFileSync) {
                 // file does not exist on any DC - die
                 KalturaLog::log("Error - no FileSync for entry [{$entry_id}]");
                 KExternalErrors::dieError(KExternalErrors::MISSING_THUMBNAIL_FILESYNC);
             }
             if ($remoteFileSync->getDc() == kDataCenterMgr::getCurrentDcId()) {
                 KalturaLog::log("ERROR - Trying to redirect to myself - stop here.");
                 KExternalErrors::dieError(KExternalErrors::MISSING_THUMBNAIL_FILESYNC);
             }
             if (!in_array($remoteFileSync->getDc(), kDataCenterMgr::getDcIds())) {
                 KExternalErrors::dieError(KExternalErrors::MISSING_THUMBNAIL_FILESYNC);
             }
             $remoteUrl = kDataCenterMgr::getRedirectExternalUrl($remoteFileSync);
             kFile::dumpUrl($remoteUrl);
         }
     }
     $nocache = strpos($tempThumbPath, "_NOCACHE_") !== false;
     if ($securyEntryHelper->shouldDisableCache() || kApiCache::hasExtraFields() || !$securyEntryHelper->isKsWidget() && $securyEntryHelper->hasRules()) {
         $nocache = true;
     }
     // notify external proxy, so it'll cache this url
     if (!$nocache && requestUtils::getHost() == kConf::get("apphome_url") && file_exists($tempThumbPath)) {
         self::notifyProxy($_SERVER["REQUEST_URI"]);
     }
     // cache result
     if (!$nocache) {
         $requestKey = $_SERVER["REQUEST_URI"];
         $cache = new myCache("thumb", 86400 * 30);
         // 30 days
         $cache->put($requestKey, $tempThumbPath);
     }
     kFile::dumpFile($tempThumbPath, null, $nocache ? 0 : null);
     // TODO - can delete from disk assuming we caneasily recreate it and it will anyway be cached in the CDN
     // however dumpfile dies at the end so we cant just write it here (maybe register a shutdown callback)
 }
Exemplo n.º 21
0
 /**
  * batch extendFileSyncLock action extends the expiration of a file sync lock
  *
  * @action extendFileSyncLock
  * @param int $id The id of the file sync 
  */
 function extendFileSyncLockAction($id)
 {
     // need to explicitly disable the cache since this action does not perform any queries
     kApiCache::disableConditionalCache();
     $lockCache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_LOCK_KEYS);
     if (!$lockCache) {
         throw new KalturaAPIException(MultiCentersErrors::GET_LOCK_CACHE_FAILED);
     }
     if (!$lockCache->set(self::LOCK_KEY_PREFIX . $id, true, self::LOCK_EXPIRY)) {
         throw new KalturaAPIException(MultiCentersErrors::EXTEND_FILESYNC_LOCK_FAILED);
     }
 }
Exemplo n.º 22
0
 protected function isAnonymous($ks)
 {
     if (parent::isAnonymous($ks)) {
         return true;
     } else {
         if (!$ks) {
             return false;
         }
     }
     if ($this->clientTag && strpos($this->clientTag, 'kmc') === 0) {
         return false;
     }
     // force caching of actions listed in kConf even if admin ks is used
     if (!kConf::hasParam('v3cache_ignore_admin_ks')) {
         return false;
     }
     $v3cacheIgnoreAdminKS = kConf::get('v3cache_ignore_admin_ks');
     if (!isset($v3cacheIgnoreAdminKS[$ks->partner_id])) {
         return false;
     }
     $actions = explode(',', $v3cacheIgnoreAdminKS[$ks->partner_id]);
     foreach ($actions as $action) {
         list($serviceId, $actionId) = explode('.', $action);
         if ($this->_params['service'] == $serviceId && $this->_params['action'] == $actionId) {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 23
0
 private static function addSchedulingToCriteria(Criteria $c)
 {
     $startDateCriterion = $c->getNewCriterion(entryPeer::START_DATE, kApiCache::getTime(), Criteria::LESS_EQUAL);
     $startDateCriterion->addOr($c->getNewCriterion(entryPeer::START_DATE, null));
     $endDateCriterion = $c->getNewCriterion(entryPeer::END_DATE, kApiCache::getTime(), Criteria::GREATER_EQUAL);
     $endDateCriterion->addOr($c->getNewCriterion(entryPeer::END_DATE, null));
     $c->addAnd($startDateCriterion);
     $c->addAnd($endDateCriterion);
 }
Exemplo n.º 24
0
 public function query()
 {
     kApiCache::disableConditionalCache();
     $args = func_get_args();
     $sql = $args[0];
     KalturaLog::debug($sql);
     $comment = $this->getCommentWrapped();
     $sql = $comment . $sql;
     $sqlStart = microtime(true);
     try {
         if (version_compare(PHP_VERSION, '5.3', '<')) {
             $result = call_user_func_array(array($this, 'parent::query'), $args);
         } else {
             $result = call_user_func_array('parent::query', $args);
         }
     } catch (PropelException $pex) {
         KalturaLog::alert($pex->getMessage());
         throw new PropelException("Database error");
     }
     $sqlTook = microtime(true) - $sqlStart;
     KalturaLog::debug("Sql took - " . $sqlTook . " seconds");
     KalturaMonitorClient::monitorDatabaseAccess($sql, $sqlTook, $this->hostName);
     return $result;
 }
Exemplo n.º 25
0
 public static function getCachedQueryResults(Criteria $criteria, $queryType, $peerClassName, &$cacheKey, &$queryDB)
 {
     if (!kConf::get("query_cache_enabled")) {
         return null;
     }
     // if the criteria has an empty IN, no need to go to the DB or memcache - return an empty array
     foreach ($criteria->getMap() as $criterion) {
         if (in_array(Criterion::ODER, $criterion->getConjunctions())) {
             continue;
         }
         if ($criterion->getComparison() == Criteria::IN && !$criterion->getValue()) {
             KalturaLog::debug("kQueryCache: criteria has empty IN, returning empty result set, peer={$peerClassName}");
             return array();
         }
     }
     // initialize
     $invalidationKeyRules = call_user_func(array($peerClassName, 'getCacheInvalidationKeys'));
     $invalidationKeys = self::getInvalidationKeysForQuery($invalidationKeyRules, $criteria);
     if (!$invalidationKeys) {
         return null;
     }
     self::initGlobalMemcache();
     if (self::$s_memcacheQueries === null) {
         return null;
     }
     // build memcache query
     foreach ($invalidationKeys as $index => $invalidationKey) {
         $invalidationKeys[$index] = self::CACHE_PREFIX_INVALIDATION_KEY . $invalidationKey;
     }
     $keysToGet = $invalidationKeys;
     $keysToGet[] = self::DONT_CACHE_KEY;
     $keysToGet[] = self::MAX_SLAVE_LAG_KEY;
     $queryStart = microtime(true);
     $cacheResult = self::$s_memcacheKeys->multiGet($keysToGet);
     KalturaLog::debug("kQueryCache: keys query took " . (microtime(true) - $queryStart) . " seconds");
     if ($cacheResult === false) {
         KalturaLog::log("kQueryCache: failed to query keys memcache, not using query cache");
         return null;
     }
     // get max slave lag
     $queryMasterThreshold = self::MAX_QUERY_MASTER_TIME_MARGIN_SEC;
     $maxSlaveLag = null;
     if (array_key_exists(self::MAX_SLAVE_LAG_KEY, $cacheResult) && strlen($cacheResult[self::MAX_SLAVE_LAG_KEY]) && is_numeric($cacheResult[self::MAX_SLAVE_LAG_KEY])) {
         $maxSlaveLag = $cacheResult[self::MAX_SLAVE_LAG_KEY];
         $maxSlaveLag += self::SLAVE_LAG_TIME_MARGIN_SEC;
         $queryMasterThreshold = min($maxSlaveLag, $queryMasterThreshold);
     }
     unset($cacheResult[self::MAX_SLAVE_LAG_KEY]);
     // don't cache the result if the 'dont cache' flag is enabled
     $cacheQuery = true;
     if (array_key_exists(self::DONT_CACHE_KEY, $cacheResult) && $cacheResult[self::DONT_CACHE_KEY]) {
         KalturaLog::log("kQueryCache: dontCache key is set -> not caching the result");
         $cacheQuery = false;
     }
     unset($cacheResult[self::DONT_CACHE_KEY]);
     // get max invalidation time
     $maxInvalidationTime = null;
     $maxInvalidationKey = null;
     if (count($cacheResult)) {
         $maxInvalidationTime = max($cacheResult);
         $maxInvalidationKey = array_search($maxInvalidationTime, $cacheResult);
     }
     // check whether we should query the master
     $queryDB = self::QUERY_DB_SLAVE;
     $currentTime = time();
     if (!is_null($maxInvalidationTime) && $currentTime < $maxInvalidationTime + $queryMasterThreshold) {
         KalturaLog::debug("kQueryCache: changed recently -> query master, peer={$peerClassName}, invkey={$maxInvalidationKey} querytime={$currentTime} invtime={$maxInvalidationTime} threshold={$queryMasterThreshold}");
         $queryDB = self::QUERY_DB_MASTER;
         if ($currentTime < $maxInvalidationTime + self::CLOCK_SYNC_TIME_MARGIN_SEC) {
             return null;
             // The query won't be cached since cacheKey is null, it's ok cause it won't be used anyway
         }
     }
     if ($queryDB == self::QUERY_DB_SLAVE && !is_null($maxInvalidationTime) && $currentTime < $maxInvalidationTime + $maxSlaveLag) {
         KalturaLog::debug("kQueryCache: using an out of date slave -> not caching the result, peer={$peerClassName}, invkey={$maxInvalidationKey} querytime={$currentTime} invtime={$maxInvalidationTime} slavelag={$maxSlaveLag}");
         $cacheQuery = false;
     }
     // get the cache key and update the api cache
     $origCacheKey = self::CACHE_PREFIX_QUERY . $queryType . md5(serialize($criteria) . self::CACHE_VERSION);
     if ($cacheQuery) {
         kApiCache::addInvalidationKeys($invalidationKeys, $maxInvalidationTime);
         $cacheKey = new kQueryCacheKey($origCacheKey);
     } else {
         kApiCache::disableConditionalCache();
     }
     // check whether we have a valid cached query
     $queryStart = microtime(true);
     $queryResult = self::$s_memcacheQueries->get($origCacheKey);
     KalturaLog::debug("kQueryCache: query took " . (microtime(true) - $queryStart) . " seconds");
     if (!$queryResult) {
         KalturaLog::debug("kQueryCache: cache miss, peer={$peerClassName}, key={$origCacheKey}");
         return null;
     }
     list($queryResult, $queryTime, $debugInfo) = $queryResult;
     if (!is_null($maxInvalidationTime) && $queryTime < $maxInvalidationTime + self::CLOCK_SYNC_TIME_MARGIN_SEC) {
         KalturaLog::debug("kQueryCache: cached query invalid, peer={$peerClassName}, key={$origCacheKey}, invkey={$maxInvalidationKey} querytime={$queryTime} debugInfo={$debugInfo} invtime={$maxInvalidationTime}");
         return null;
     }
     // return from memcache
     $existingInvKeys = array();
     foreach ($cacheResult as $invalidationKey => $invalidationTime) {
         $existingInvKeys[] = "{$invalidationKey}:{$invalidationTime}";
     }
     $existingInvKeys = implode(',', $existingInvKeys);
     KalturaLog::debug("kQueryCache: returning from memcache, peer={$peerClassName}, key={$origCacheKey} queryTime={$queryTime} debugInfo={$debugInfo} invkeys=[{$existingInvKeys}]");
     return $queryResult;
 }
Exemplo n.º 26
0
 public final function output()
 {
     $this->prepareFlavors();
     if ($this->deliveryCode) {
         $this->replaceDeliveryCode();
     }
     $this->tokenizeUrls();
     $headers = $this->getHeaders();
     $headers[] = "Access-Control-Allow-Origin:*";
     $headers[] = "Access-Control-Expose-Headers: Server,range,Content-Length,Content-Range";
     foreach ($headers as $header) {
         header($header);
     }
     if (kApiCache::hasExtraFields() && !$this->forceCachingHeaders) {
         $this->cachingHeadersAge = 0;
     }
     infraRequestUtils::sendCachingHeaders($this->cachingHeadersAge, true, $this->lastModified);
     $header = $this->getManifestHeader();
     $footer = $this->getManifestFooter();
     $flavors = $this->getManifestFlavors();
     foreach ($this->contributors as $contributorInstance) {
         /* @var $contributorInstance BaseManifestEditor */
         $header = $contributorInstance->editManifestHeader($header);
         $footer = $contributorInstance->editManifestFooter($footer);
         $flavors = $contributorInstance->editManifestFlavors($flavors);
     }
     $separator = $this->getSeparator();
     $content = $header;
     if ($content) {
         $content .= $separator;
     }
     $content .= implode($separator, $flavors);
     $content .= $separator . $footer;
     header('Content-Length: ' . strlen($content));
     // avoid chunked encoding
     echo $content;
     die;
 }
Exemplo n.º 27
0
 private static function addSchedulingToCriteria(Criteria $c, entryFilter $filter = null)
 {
     $min = 0;
     $max = kApiCache::getTime();
     $allowNull = true;
     if ($filter) {
         if ($filter->is_set('_lteornull_start_date')) {
             $max = min($max, $filter->get('_lteornull_start_date'));
             $filter->unsetByName('_lteornull_start_date');
         }
         if ($filter->is_set('_gteornull_start_date')) {
             $min = max($min, $filter->get('_gteornull_start_date'));
             $filter->unsetByName('_gteornull_start_date');
         }
         if ($filter->is_set('_lte_start_date')) {
             $max = min($max, $filter->get('_lte_start_date'));
             $allowNull = false;
             $filter->unsetByName('_lte_start_date');
         }
         if ($filter->is_set('_gte_start_date')) {
             $min = max($min, $filter->get('_gte_start_date'));
             $allowNull = false;
             $filter->unsetByName('_gte_start_date');
         }
     }
     self::addSchedulingCriterion($c, entryPeer::START_DATE, $min, $max, $allowNull);
     $min = kApiCache::getTime();
     $max = 0;
     $allowNull = true;
     if ($filter) {
         if ($filter->is_set('_lteornull_end_date')) {
             $max = min($max, $filter->get('_lteornull_end_date'));
             $filter->unsetByName('_lteornull_end_date');
         }
         if ($filter->is_set('_gteornull_end_date')) {
             $min = max($min, $filter->get('_gteornull_end_date'));
             $filter->unsetByName('_gteornull_end_date');
         }
         if ($filter->is_set('_lte_end_date')) {
             $max = min($max, $filter->get('_lte_end_date'));
             $allowNull = false;
             $filter->unsetByName('_lte_end_date');
         }
         if ($filter->is_set('_gte_end_date')) {
             $min = max($min, $filter->get('_gte_end_date'));
             $allowNull = false;
             $filter->unsetByName('_gte_end_date');
         }
     }
     self::addSchedulingCriterion($c, entryPeer::END_DATE, $min, $max, $allowNull);
 }
Exemplo n.º 28
0
 private static function executeQuery($query)
 {
     kApiCache::disableConditionalCache();
     $mysql_function = 'mysqli';
     $db_config = kConf::get("reports_db_config");
     if (!isset($db_config["port"])) {
         if (ini_get("mysqli.default_port") !== null) {
             $db_config["port"] = ini_get("mysqli.default_port");
         } else {
             $db_config["port"] = 3306;
         }
     }
     $timeout = isset($db_config["timeout"]) ? $db_config["timeout"] : 40;
     ini_set('mysql.connect_timeout', $timeout);
     $host = $db_config["host"];
     if (isset($db_config["port"]) && $db_config["port"] && $mysql_function != 'mysqli') {
         $host .= ":" . $db_config["port"];
     }
     $connect_function = $mysql_function . '_connect';
     $link = $connect_function($host, $db_config["user"], $db_config["password"], null, $db_config["port"]);
     KalturaLog::log("Reports query using database host: [{$host}] user [" . $db_config["user"] . "]");
     if ($mysql_function == 'mysql') {
         $db_selected = mysql_select_db($db_config["db_name"], $link);
     } else {
         $db_selected = mysqli_select_db($link, $db_config["db_name"]);
     }
     $error_function = $mysql_function . '_error';
     if (!$db_selected) {
         throw new kCoreException('Can\'t use foo : ' . $error_function($link), kCoreException::INVALID_QUERY);
     }
     if ($mysql_function == 'mysql') {
         $result = mysql_query($query);
     } else {
         $result = mysqli_query($link, $query);
     }
     // Check result
     // This shows the actual query sent to MySQL, and the error. Useful for debugging.
     if (!$result) {
         KalturaLog::err('Invalid query: ' . $error_function($link));
         $message = 'Invalid query';
         throw new kCoreException($message, kCoreException::INVALID_QUERY);
     }
     $res = array();
     $fetch_function = $mysql_function . '_fetch_assoc';
     while ($row = $fetch_function($result)) {
         $res[] = $row;
     }
     $free_result_func = $mysql_function . '_free_result';
     $free_result_func($result);
     $close_function = $mysql_function . '_close';
     $close_function($link);
     return $res;
 }
Exemplo n.º 29
0
 public function isScheduledNow($time = null)
 {
     if (is_null($time)) {
         $time = time();
         // entry scheduling status changes within 24H
         if ($this->getStartDate() && abs($this->getStartDate(null) - time()) <= 86400 || $this->getEndDate() && abs($this->getEndDate(null) - time()) <= 86400) {
             kApiCache::setConditionalCacheExpiry(600);
         }
         // entry scheduling status changes within 10 min
         if ($this->getStartDate() && abs($this->getStartDate(null) - time()) <= 600 || $this->getEndDate() && abs($this->getEndDate(null) - time()) <= 600) {
             kApiCache::setExpiry(60);
             kApiCache::setConditionalCacheExpiry(60);
         }
     }
     $startDateCheck = !$this->getStartDate() || $this->getStartDate(null) <= $time;
     $endDateCheck = !$this->getEndDate() || $this->getEndDate(null) >= $time;
     return $startDateCheck && $endDateCheck;
 }
Exemplo n.º 30
0
 /**
  * @param FileSync $fileSync
  * @param bool $tokenizeUrl
  * @return string
  */
 public function getFileSyncUrl(FileSync $fileSync, $tokenizeUrl = true)
 {
     $fileSync = kFileSyncUtils::resolve($fileSync);
     $url = $this->doGetFileSyncUrl($fileSync);
     $url = str_replace('\\', '/', $url);
     if ($tokenizeUrl) {
         $tokenizer = $this->getTokenizer();
         if ($tokenizer) {
             $url = $tokenizer->tokenizeSingleUrl($url, $this->getUrlPrefix());
             kApiCache::disableCache();
         }
     }
     return $url;
 }