/**
  * Get value from cache for the given key
  * @param string $key
  */
 private static function getFromCache($key, $roleCacheDirtyAt)
 {
     if (!self::useCache()) {
         return null;
     }
     self::$cacheStores = array();
     foreach (self::$cacheLayers as $cacheLayer) {
         $cacheStore = kCacheManager::getCache($cacheLayer);
         if (!$cacheStore) {
             continue;
         }
         $value = $cacheStore->get(self::getCacheKeyPrefix() . $key);
         // try to fetch from cache
         if (!$value || !isset($value['updatedAt']) || $value['updatedAt'] < $roleCacheDirtyAt) {
             self::$cacheStores[] = $cacheStore;
             continue;
         }
         KalturaLog::debug("Found a cache value for key [{$key}] in layer [{$cacheLayer}]");
         self::storeInCache($key, $value);
         // store in lower cache layers
         self::$cacheStores[] = $cacheStore;
         // cache is updated - init from cache
         unset($value['updatedAt']);
         return $value;
     }
     KalturaLog::debug("No cache value found for key [{$key}]");
     return null;
 }
 public static function file_get_contents(FileSyncKey $key, $fetch_from_remote_if_no_local = true, $strict = true)
 {
     $cacheStore = kCacheManager::getCache(kCacheManager::MC_GLOBAL_FILESYNC);
     if ($cacheStore) {
         $cacheKey = self::CACHE_KEY_PREFIX . "{$key->object_id}_{$key->object_type}_{$key->object_sub_type}_{$key->version}";
         $result = $cacheStore->get($cacheKey);
         if ($result) {
             KalturaLog::log("returning from cache, key [{$cacheKey}] size [" . strlen($result) . "]");
             return $result;
         }
     }
     KalturaLog::log(__METHOD__ . " - key [{$key}], fetch_from_remote_if_no_local [{$fetch_from_remote_if_no_local}], strict [{$strict}]");
     list($file_sync, $local) = self::getReadyFileSyncForKey($key, $fetch_from_remote_if_no_local, $strict);
     if ($file_sync) {
         $file_sync = self::resolve($file_sync);
     }
     if ($file_sync) {
         $result = self::getContentsByFileSync($file_sync, $local, $fetch_from_remote_if_no_local, $strict);
         if ($cacheStore && $result && strlen($result) < self::MAX_CACHED_FILE_SIZE && !in_array($key->object_type, self::$uncachedObjectTypes)) {
             KalturaLog::log("saving to cache, key [{$cacheKey}] size [" . strlen($result) . "]");
             $cacheStore->set($cacheKey, $result, self::FILE_SYNC_CACHE_EXPIRY);
         }
         return $result;
     }
     KalturaLog::log(__METHOD__ . " - FileSync not found");
     return null;
 }
Esempio n. 3
0
 /**
  * @param string $key
  * @return NULL|kLock
  */
 public static function create($key)
 {
     $store = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_LOCK_KEYS);
     if (!$store) {
         return null;
     }
     return new kLock($store, $key);
 }
Esempio n. 4
0
 protected static function initGlobalMemcache()
 {
     if (self::$s_memcacheInited) {
         return;
     }
     self::$s_memcacheInited = true;
     self::$s_memcacheKeys = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_QUERY_CACHE_KEYS);
     if (self::$s_memcacheKeys === null) {
         // no reason to init the queries server, the query cache won't be used anyway
         return;
     }
     self::$s_memcacheQueries = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_QUERY_CACHE_QUERIES);
 }
Esempio n. 5
0
 /**
  * @return array<kBaseCacheWrapper>
  */
 protected static function getStores($cacheType = kCacheManager::CACHE_TYPE_RESPONSE_PROFILE)
 {
     if (isset(self::$cacheStores[$cacheType])) {
         return self::$cacheStores[$cacheType];
     }
     self::$cacheStores[$cacheType] = array();
     $cacheSections = kCacheManager::getCacheSectionNames($cacheType);
     if (is_array($cacheSections)) {
         foreach ($cacheSections as $cacheSection) {
             $cacheStore = kCacheManager::getCache($cacheSection);
             if ($cacheStore) {
                 self::$cacheStores[$cacheType][] = $cacheStore;
             }
         }
     }
     return self::$cacheStores[$cacheType];
 }
 private function calculateCacheKey()
 {
     $this->getKsData();
     $this->_playbackContext = isset($this->_params['playbackContext']) ? $this->_params['playbackContext'] : null;
     unset($this->_params['playbackContext']);
     $this->_params['___cache___protocol'] = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http";
     $this->_params['___cache___host'] = @$_SERVER['HTTP_HOST'];
     $this->_params['___cache___version'] = self::CACHE_VERSION;
     // take only the hostname part of the referrer parameter of baseEntry.getContextData
     if (isset($this->_params['referrer'])) {
         $referrer = base64_decode(str_replace(" ", "+", $this->_params['referrer']));
         if (!is_string($referrer)) {
             $referrer = "";
         }
         unset($this->_params['referrer']);
     } else {
         $referrer = self::getHttpReferrer();
     }
     $this->_referrers[] = $referrer;
     $this->finalizeCacheKey();
     $this->addExtraFields();
     $this->_cacheWrapper = kCacheManager::getCache(kCacheManager::FS_PLAY_MANIFEST);
 }
Esempio n. 7
0
 protected function warmCache($key)
 {
     if (self::$_cacheWarmupInitiated) {
         return;
     }
     self::$_cacheWarmupInitiated = true;
     $key = "cache-warmup-{$key}";
     $cacheSections = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_API_WARMUP);
     if (!$cacheSections) {
         return;
     }
     foreach ($cacheSections as $cacheSection) {
         $cacheStore = kCacheManager::getCache($cacheSection);
         if (!$cacheStore) {
             return;
         }
         // abort warming if a previous warmup started less than 10 seconds ago
         if ($cacheStore->get($key) !== false) {
             return;
         }
         // flag we are running a warmup for the current request
         $cacheStore->set($key, true, self::WARM_CACHE_TTL);
     }
     $uri = $_SERVER["REQUEST_URI"];
     $fp = fsockopen(kConf::get('api_cache_warmup_host'), 80, $errno, $errstr, 1);
     if ($fp === false) {
         error_log("warmCache - Couldn't open a socket [" . $uri . "]", 0);
         return;
     }
     $method = $_SERVER["REQUEST_METHOD"];
     $out = "{$method} {$uri} HTTP/1.1\r\n";
     $sentHeaders = self::getRequestHeaders();
     $sentHeaders["Connection"] = "Close";
     // mark request as a warm cache request in order to disable caching and pass the http/https protocol (the warmup always uses http)
     $sentHeaders[self::WARM_CACHE_HEADER] = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http";
     // if the request wasn't proxied pass the ip on the X-FORWARDED-FOR header
     $ipHeader = infraRequestUtils::getSignedIpAddressHeader();
     if ($ipHeader) {
         list($headerName, $headerValue) = $ipHeader;
         $sentHeaders[$headerName] = $headerValue;
     }
     foreach ($sentHeaders as $header => $value) {
         $out .= "{$header}:{$value}\r\n";
     }
     $out .= "\r\n";
     if ($method == "POST") {
         $postParams = array();
         foreach ($_POST as $key => &$val) {
             if (is_array($val)) {
                 $val = implode(',', $val);
             }
             $postParams[] = $key . '=' . urlencode($val);
         }
         $out .= implode('&', $postParams);
     }
     fwrite($fp, $out);
     fclose($fp);
 }
Esempio n. 8
0
function checkCache()
{
    $baseDir = "/tmp/cache_v2";
    $start_time = microtime(true);
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http";
    $host = "";
    if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
        $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
    } else {
        if (isset($_SERVER['HTTP_HOST'])) {
            $host = $_SERVER['HTTP_HOST'];
        }
    }
    $uri = $_SERVER["REQUEST_URI"];
    if (function_exists('apc_fetch')) {
        $url = apc_fetch("redirect-" . $protocol . $uri);
        if ($url) {
            sendCachingHeaders(60, true, time());
            header("X-Kaltura:cached-dispatcher-redirect");
            header("Location:{$url}");
            die;
        }
        $errorHeaders = apc_fetch("exterror-{$protocol}://{$host}{$uri}");
        if ($errorHeaders !== false) {
            sendCachingHeaders(60, true, time());
            foreach ($errorHeaders as $header) {
                header($header);
            }
            die;
        }
    }
    if (strpos($uri, "/playManifest") !== false) {
        require_once dirname(__FILE__) . "/../apps/kaltura/lib/cache/kPlayManifestCacher.php";
        $cache = kPlayManifestCacher::getInstance();
        $cache->checkOrStart();
    } else {
        if (strpos($uri, "/partnerservices2") !== false) {
            $params = $_GET + $_POST;
            unset($params['ks']);
            unset($params['kalsig']);
            $params['uri'] = $_SERVER['PATH_INFO'];
            $params['__protocol'] = $protocol;
            ksort($params);
            $keys = array_keys($params);
            $key = md5(implode("|", $params) . implode("|", $keys));
            $cache_filename = "{$baseDir}/cache-{$key}";
            if (file_exists($cache_filename)) {
                if (filemtime($cache_filename) + 600 < time()) {
                    @unlink($cache_filename);
                    @unlink($cache_filename . ".headers");
                    @unlink($cache_filename . ".log");
                } else {
                    $content_type = @file_get_contents("{$baseDir}/cache-{$key}.headers");
                    if ($content_type) {
                        header("Content-Type: {$content_type}");
                    }
                    $response = @file_get_contents("{$baseDir}/cache-{$key}");
                    if ($response) {
                        header("Access-Control-Allow-Origin:*");
                        // avoid html5 xss issues
                        if (strpos($uri, "/partnerservices2/executeplaylist") !== false) {
                            $max_age = 60;
                            sendCachingHeaders($max_age, true, time());
                        } else {
                            sendCachingHeaders(0);
                        }
                        $processing_time = microtime(true) - $start_time;
                        header("X-Kaltura:cached-dispatcher,{$key},{$processing_time}");
                        echo $response;
                        die;
                    }
                }
            }
        } else {
            if (strpos($uri, "/kwidget") !== false) {
                require_once dirname(__FILE__) . "/../apps/kaltura/lib/cache/kCacheManager.php";
                $cache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_PS2);
                if ($cache) {
                    // check if we cached the patched swf with flashvars
                    $uri = $protocol . $uri;
                    $cachedResponse = $cache->get("kwidgetswf{$uri}");
                    if ($cachedResponse) {
                        $max_age = 60 * 10;
                        header("X-Kaltura:cached-dispatcher");
                        header("Content-Type: application/x-shockwave-flash");
                        sendCachingHeaders($max_age, true, time());
                        header("Content-Length: " . strlen($cachedResponse));
                        echo $cachedResponse;
                        die;
                    }
                    $cachedResponse = $cache->get("kwidget{$uri}");
                    if ($cachedResponse) {
                        header("X-Kaltura:cached-dispatcher");
                        header("Expires: Sun, 19 Nov 2000 08:52:00 GMT");
                        header("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
                        header("Pragma", "no-cache");
                        if (strpos($uri, "nowrapper") !== false) {
                            header("Location:{$cachedResponse}");
                            die;
                        }
                        $referer = @$_SERVER['HTTP_REFERER'];
                        $externalInterfaceDisabled = strstr($referer, "bebo.com") === false && strstr($referer, "myspace.com") === false && strstr($referer, "current.com") === false && strstr($referer, "myyearbook.com") === false && strstr($referer, "facebook.com") === false && strstr($referer, "friendster.com") === false ? "" : "&externalInterfaceDisabled=1";
                        $noncached_params = $externalInterfaceDisabled . "&referer=" . urlencode($referer);
                        if (strpos($cachedResponse, "/swfparams/") > 0) {
                            $cachedResponse = substr($cachedResponse, 0, -4) . urlencode($noncached_params) . ".swf";
                        } else {
                            $cachedResponse .= $noncached_params;
                        }
                        header("Location:{$cachedResponse}");
                        die;
                    }
                }
            } else {
                if (strpos($uri, "/thumbnail") !== false) {
                    require_once dirname(__FILE__) . "/../apps/kaltura/lib/cache/kCacheManager.php";
                    $cache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_PS2);
                    if ($cache) {
                        require_once dirname(__FILE__) . '/../apps/kaltura/lib/renderers/kRendererDumpFile.php';
                        $cachedResponse = $cache->get("thumb{$uri}");
                        if ($cachedResponse && is_array($cachedResponse)) {
                            list($renderer, $invalidationKey, $cacheTime) = $cachedResponse;
                            $keysStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_QUERY_CACHE_KEYS);
                            if ($keysStore) {
                                $modifiedTime = $keysStore->get($invalidationKey);
                                if ($modifiedTime && $modifiedTime > $cacheTime) {
                                    return;
                                    // entry has changed (not necessarily the thumbnail)
                                }
                            }
                            require_once dirname(__FILE__) . '/../apps/kaltura/lib/monitor/KalturaMonitorClient.php';
                            KalturaMonitorClient::initApiMonitor(true, 'extwidget.thumbnail', $renderer->partnerId);
                            header("X-Kaltura:cached-dispatcher-thumb");
                            $renderer->output();
                            die;
                        }
                    }
                } else {
                    if (strpos($uri, "/embedIframe/") !== false) {
                        require_once dirname(__FILE__) . "/../apps/kaltura/lib/cache/kCacheManager.php";
                        $cache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_PS2);
                        if ($cache) {
                            // check if we cached the patched swf with flashvars
                            $cachedResponse = $cache->get("embedIframe{$uri}");
                            if ($cachedResponse) {
                                header("X-Kaltura:cached-dispatcher");
                                sendCachingHeaders(0);
                                header("Location:{$cachedResponse}");
                                die;
                            }
                        }
                    } else {
                        if (strpos($uri, "/serveFlavor/") !== false && function_exists('apc_fetch') && $_SERVER["REQUEST_METHOD"] == "GET") {
                            require_once dirname(__FILE__) . '/../apps/kaltura/lib/renderers/kRendererDumpFile.php';
                            require_once dirname(__FILE__) . '/../apps/kaltura/lib/renderers/kRendererString.php';
                            require_once dirname(__FILE__) . '/../apps/kaltura/lib/monitor/KalturaMonitorClient.php';
                            require_once dirname(__FILE__) . '/../apps/kaltura/lib/request/kIpAddressUtils.php';
                            $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
                            $cacheKey = 'dumpFile-' . kIpAddressUtils::isInternalIp($_SERVER['REMOTE_ADDR']) . '-' . $host . $uri;
                            $renderer = apc_fetch($cacheKey);
                            if ($renderer) {
                                KalturaMonitorClient::initApiMonitor(true, 'extwidget.serveFlavor', $renderer->partnerId);
                                header("X-Kaltura:cached-dispatcher");
                                $renderer->output();
                                die;
                            }
                        }
                    }
                }
            }
        }
    }
}
Esempio n. 9
0
 private function retrieveSphinxConnectionId()
 {
     $sphinxConnectionId = null;
     if (kConf::hasParam('exec_sphinx') && kConf::get('exec_sphinx')) {
         $sphinxConnection = DbManager::getSphinxConnection(false);
         $sphinxServerCacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_SPHINX_EXECUTED_SERVER);
         if ($sphinxServerCacheStore) {
             $sphinxConnectionId = $sphinxServerCacheStore->get(self::CACHE_PREFIX . $sphinxConnection->getHostName());
             if ($sphinxConnectionId) {
                 return $sphinxConnectionId;
             }
         }
         $sphinxServer = SphinxLogServerPeer::retrieveByLocalServer($sphinxConnection->getHostName());
         if ($sphinxServer) {
             $sphinxConnectionId = $sphinxServer->getId();
             if ($sphinxServerCacheStore) {
                 $sphinxServerCacheStore->set(self::CACHE_PREFIX . $sphinxConnection->getHostName(), $sphinxConnectionId);
             }
         }
     }
     return $sphinxConnectionId;
 }
 public function execute($limit = 0)
 {
     if ($this->executed) {
         return;
     }
     if ($limit) {
         $this->limit = $limit;
     }
     $microTimeStart = microtime(true);
     $renderer = KalturaSyndicationFeedFactory::getRendererByType($this->syndicationFeed->type);
     $renderer->init($this->syndicationFeed, $this->syndicationFeedDb, $this->mimeType);
     header($renderer->handleHttpHeader());
     echo $renderer->handleHeader();
     $cacheStore = null;
     if ($renderer->shouldEnableCache()) {
         $cacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_FEED_ENTRY);
     }
     $protocol = infraRequestUtils::getProtocol();
     $cachePrefix = "feed_{$this->syndicationFeed->id}/{$protocol}/entry_";
     $feedUpdatedAt = $this->syndicationFeedDb->getUpdatedAt(null);
     $e = null;
     $kalturaFeed = $this->syndicationFeed->type == KalturaSyndicationFeedType::KALTURA || $this->syndicationFeed->type == KalturaSyndicationFeedType::KALTURA_XSLT;
     $nextEntry = $this->getNextEntry();
     while ($nextEntry) {
         $this->enableApcProcessingFlag();
         $entry = $nextEntry;
         $nextEntry = $this->getNextEntry();
         // in case no video player is requested by user and the entry is mix, skip it
         if ($entry->getType() === entryType::MIX && !$this->syndicationFeed->allowEmbed) {
             continue;
         }
         $xml = false;
         // check cache
         $updatedAt = max($feedUpdatedAt, $entry->getUpdatedAt(null));
         if ($cacheStore) {
             $cacheKey = $cachePrefix . str_replace("_", "-", $entry->getId()) . self::CACHE_VERSION;
             // replace _ with - so cache folders will be created with random entry id and not 0_/1_
             $cacheTime = $cacheStore->get($cacheKey . self::CACHE_CREATION_TIME_SUFFIX);
             if ($cacheTime !== false && $cacheTime > $updatedAt + self::CACHE_CREATION_MARGIN) {
                 $xml = $cacheStore->get($cacheKey);
             }
         }
         if ($xml === false) {
             $e = null;
             if (!$kalturaFeed) {
                 $e = new KalturaMediaEntry();
                 $e->fromObject($entry);
             }
             $flavorAssetUrl = is_null($e) ? null : $this->getFlavorAssetUrl($e);
             if (!$kalturaFeed && $entry->getType() !== entryType::MIX && is_null($flavorAssetUrl)) {
                 $xml = "";
                 // cache empty result to avoid checking getFlavorAssetUrl next time
             } else {
                 $xml = $renderer->handleBody($entry, $e, $flavorAssetUrl);
             }
         }
         if ($cacheStore) {
             $cacheStore->set($cacheKey . self::CACHE_CREATION_TIME_SUFFIX, time(), self::CACHE_EXPIRY);
             $cacheStore->set($cacheKey, $xml, self::CACHE_EXPIRY);
         }
         echo $renderer->finalize($xml, $nextEntry !== false);
     }
     echo $renderer->handleFooter();
     if ($this->feedProcessingKey && function_exists('apc_delete')) {
         apc_delete($this->feedProcessingKey);
     }
     $microTimeEnd = microtime(true);
     KalturaLog::info("syndicationFeedRenderer- render time for ({$this->syndicationFeed->type}) is " . ($microTimeEnd - $microTimeStart));
 }
Esempio n. 11
0
        return number_format($size / (1 << 20), 2) . "MB";
    }
    if ($size >= 1 << 10) {
        return number_format($size / (1 << 10), 2) . "KB";
    }
    return number_format($size) . " bytes";
}
function writeOutput($msg)
{
    fwrite(STDERR, $msg);
}
$iniDir = __DIR__ . '/../../../configurations/batch/';
if (isset($argc) && $argc > 2) {
    $iniDir = $argv[2];
}
$keysCache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_QUERY_CACHE_KEYS);
if (!$keysCache) {
    die('failed to get keys cache');
}
// get the max id / last id
$maxId = $keysCache->get(MAX_FILESYNC_ID_PREFIX . kDataCenterMgr::getCurrentDcId());
writeOutput('Max id for dc [' . kDataCenterMgr::getCurrentDcId() . '] is [' . $maxId . "]\n");
$excludeFileSyncMap = getExcludeFileSyncMap();
FileSyncPeer::setUseCriteriaFilter(false);
$fileSyncWorkers = getFileSyncWorkers($iniDir);
foreach ($fileSyncWorkers as $fileSyncWorker) {
    $workerId = $fileSyncWorker['id'];
    $workerName = $fileSyncWorker['name'];
    $filter = $fileSyncWorker['filter'];
    $lastId = $keysCache->get(LAST_FILESYNC_ID_PREFIX . $workerId);
    // build the base criteria
 protected function storeExtraFields()
 {
     if (!$this->_cacheKeyDirty) {
         return;
     }
     // no extra fields were added to the cache
     $extraFieldsCache = kCacheManager::getCache(kCacheManager::APC);
     if (!$extraFieldsCache) {
         self::disableCache();
         return;
     }
     if ($extraFieldsCache->set(self::EXTRA_KEYS_PREFIX . $this->_originalCacheKey, $this->_extraFields, self::CONDITIONAL_CACHE_EXPIRY) === false) {
         self::disableCache();
         return;
     }
     $this->finalizeCacheKey();
     // update the cache key to include the extra fields
 }
Esempio n. 13
0
 public static function file_get_contents(FileSyncKey $key, $fetch_from_remote_if_no_local = true, $strict = true, $max_file_size = 0)
 {
     $cacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_FILE_SYNC);
     if ($cacheStore) {
         $cacheKey = self::CACHE_KEY_PREFIX . "{$key->object_id}_{$key->object_type}_{$key->object_sub_type}_{$key->version}";
         $result = $cacheStore->get($cacheKey);
         if ($result) {
             KalturaLog::info("returning from cache, key [{$cacheKey}] size [" . strlen($result) . "]");
             return $result;
         }
     }
     KalturaLog::debug("key [{$key}], fetch_from_remote_if_no_local [{$fetch_from_remote_if_no_local}], strict [{$strict}]");
     list($file_sync, $local) = self::getReadyFileSyncForKey($key, $fetch_from_remote_if_no_local, $strict);
     if ($file_sync) {
         $file_sync = self::resolve($file_sync);
     }
     if ($file_sync) {
         if ($max_file_size && $file_sync->getFileSize() > $max_file_size) {
             KalturaLog::err('FileSync size [' . $file_sync->getFileSize() . '] exceeds the limit [' . $max_file_size . ']');
             return null;
         }
         $result = self::getContentsByFileSync($file_sync, $local, $fetch_from_remote_if_no_local, $strict);
         if ($cacheStore && $result && strlen($result) < self::MAX_CACHED_FILE_SIZE && !in_array($key->object_type, self::$uncachedObjectTypes)) {
             KalturaLog::info("saving to cache, key [{$cacheKey}] size [" . strlen($result) . "]");
             $cacheStore->set($cacheKey, $result, self::FILE_SYNC_CACHE_EXPIRY);
         }
         return $result;
     }
     KalturaLog::info("FileSync not found");
     return null;
 }
 /**
  * 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);
     }
 }
Esempio n. 15
0
 public function __construct($namespace, $expiry = NULL)
 {
     $this->m_namespace = $namespace;
     $this->m_expiry = $expiry ? $expiry : self::DEFAULT_EXPIRY_IN_SECONDS;
     $this->m_cache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_PS2);
 }
Esempio n. 16
0
 /**
  * Will forward to the regular swf player according to the widget_id 
  */
 public function execute()
 {
     // check if this is a request for the kdp without a wrapper
     // in case of an application loading the kdp (e.g. kmc)
     $nowrapper = $this->getRequestParameter("nowrapper", false);
     // allow caching if either the cache start time (cache_st) parameter
     // wasn't specified or if it is past the specified time
     $cache_st = $this->getRequestParameter("cache_st");
     $allowCache = !$cache_st || $cache_st < time();
     $referer = @$_SERVER['HTTP_REFERER'];
     $externalInterfaceDisabled = strstr($referer, "bebo.com") === false && strstr($referer, "myspace.com") === false && strstr($referer, "current.com") === false && strstr($referer, "myyearbook.com") === false && strstr($referer, "facebook.com") === false && strstr($referer, "friendster.com") === false ? "" : "&externalInterfaceDisabled=1";
     // if there is no wrapper the loader is responsible for setting extra params to the kdp
     $noncached_params = "";
     if (!$nowrapper) {
         $noncached_params = $externalInterfaceDisabled . "&referer=" . urlencode($referer);
     }
     $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http";
     $requestKey = $protocol . $_SERVER["REQUEST_URI"];
     // check if we cached the redirect url
     $cache_redirect = new myCache("kwidget", 10 * 60);
     // 10 minutes
     $cachedResponse = $cache_redirect->get($requestKey);
     if ($allowCache && $cachedResponse) {
         header("X-Kaltura:cached-action");
         header("Expires: Sun, 19 Nov 2000 08:52:00 GMT");
         header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
         header("Pragma: no-cache");
         header("Location:{$cachedResponse}" . $noncached_params);
         KExternalErrors::dieGracefully();
     }
     // check if we cached the patched swf with flashvars
     $cache_swfdata = new myCache("kwidgetswf", 10 * 60);
     // 10 minutes
     $cachedResponse = $cache_swfdata->get($requestKey);
     if ($allowCache && $cachedResponse) {
         header("X-Kaltura:cached-action");
         requestUtils::sendCdnHeaders("swf", strlen($cachedResponse), 60 * 10, null, true, time());
         echo $cachedResponse;
         KExternalErrors::dieGracefully();
     }
     $widget_id = $this->getRequestParameter("wid");
     $show_version = $this->getRequestParameter("v");
     $debug_kdp = $this->getRequestParameter("debug_kdp", false);
     $widget = widgetPeer::retrieveByPK($widget_id);
     if (!$widget) {
         KExternalErrors::dieGracefully();
     }
     myPartnerUtils::blockInactivePartner($widget->getPartnerId());
     // because of the routing rule - the entry_id & kmedia_type WILL exist. be sure to ignore them if smaller than 0
     $kshow_id = $widget->getKshowId();
     $entry_id = $widget->getEntryId();
     $gallery_widget = !$kshow_id && !$entry_id;
     if (!$entry_id) {
         $entry_id = -1;
     }
     if ($widget->getSecurityType() != widget::WIDGET_SECURITY_TYPE_TIMEHASH) {
         // try eid - if failed entry_id
         $eid = $this->getRequestParameter("eid", $this->getRequestParameter("entry_id"));
         // try kid - if failed kshow_id
         $kid = $this->getRequestParameter("kid", $this->getRequestParameter("kshow_id"));
         if ($eid != null) {
             $entry_id = $eid;
         } elseif ($kid != null) {
             $kshow_id = $kid;
         }
     }
     if ($widget->getSecurityType() == widget::WIDGET_SECURITY_TYPE_MATCH_IP) {
         $allowCache = false;
         // here we'll attemp to match the ip of the request with that from the customData of the widget
         $custom_data = $widget->getCustomData();
         $valid_country = false;
         if ($custom_data) {
             // in this case the custom_data should be of format:
             //  valid_county_1,valid_country_2,...,valid_country_n;falback_entry_id
             $arr = explode(";", $custom_data);
             $countries_str = $arr[0];
             $fallback_entry_id = isset($arr[1]) ? $arr[1] : null;
             $fallback_kshow_id = isset($arr[2]) ? $arr[2] : null;
             $current_country = "";
             $valid_country = requestUtils::matchIpCountry($countries_str, $current_country);
             if (!$valid_country) {
                 KalturaLog::log("kwidgetAction: Attempting to access widget [{$widget_id}] and entry [{$entry_id}] from country [{$current_country}]. Retrning entry_id: [{$fallback_entry_id}] kshow_id [{$fallback_kshow_id}]");
                 $entry_id = $fallback_entry_id;
                 $kshow_id = $fallback_kshow_id;
             }
         }
     } elseif ($widget->getSecurityType() == widget::WIDGET_SECURITY_TYPE_FORCE_KS) {
     }
     $kmedia_type = -1;
     // support either uiconf_id or ui_conf_id
     $uiconf_id = $this->getRequestParameter("uiconf_id");
     if (!$uiconf_id) {
         $uiconf_id = $this->getRequestParameter("ui_conf_id");
     }
     if ($uiconf_id) {
         $widget_type = $uiconf_id;
         $uiconf_id_str = "&uiconf_id={$uiconf_id}";
     } else {
         $widget_type = $widget->getUiConfId();
         $uiconf_id_str = "";
     }
     if (empty($widget_type)) {
         $widget_type = 3;
     }
     $kdata = $widget->getCustomData();
     $partner_host = myPartnerUtils::getHost($widget->getPartnerId());
     $partner_cdnHost = myPartnerUtils::getCdnHost($widget->getPartnerId());
     $host = $partner_host;
     if ($widget_type == 10) {
         $swf_url = $host . "/swf/weplay.swf";
     } else {
         $swf_url = $host . "/swf/kplayer.swf";
     }
     $partner_id = $widget->getPartnerId();
     $subp_id = $widget->getSubpId();
     if (!$subp_id) {
         $subp_id = 0;
     }
     $uiConf = uiConfPeer::retrieveByPK($widget_type);
     // new ui_confs which are deleted should stop the script
     // the check for >100000 is for supporting very old mediawiki and such players
     if (!$uiConf && $widget_type > 100000) {
         KExternalErrors::dieGracefully();
     }
     if ($uiConf) {
         $ui_conf_swf_url = $uiConf->getSwfUrl();
         if (kString::beginsWith($ui_conf_swf_url, "http")) {
             $swf_url = $ui_conf_swf_url;
             // absolute URL
         } else {
             $use_cdn = $uiConf->getUseCdn();
             $host = $use_cdn ? $partner_cdnHost : $partner_host;
             $swf_url = $host . myPartnerUtils::getUrlForPartner($partner_id, $subp_id) . $ui_conf_swf_url;
         }
         if ($debug_kdp) {
             $swf_url = str_replace("/kdp/", "/kdp_debug/", $swf_url);
         }
     }
     if ($show_version < 0) {
         $show_version = null;
     }
     $ip = requestUtils::getRemoteAddress();
     // to convert back, use long2ip
     // the widget log should change to reflect the new data, but for now - i used $widget_id instead of the widgget_type
     //		WidgetLog::createWidgetLog( $referer , $ip , $kshow_id , $entry_id , $kmedia_type , $widget_id );
     if ($entry_id == -1) {
         $entry_id = null;
     }
     $kdp3 = false;
     $base_wrapper_swf = myContentStorage::getFSFlashRootPath() . "/kdpwrapper/" . kConf::get('kdp_wrapper_version') . "/kdpwrapper.swf";
     $widgetIdStr = "widget_id={$widget_id}";
     $partnerIdStr = "partner_id={$partner_id}&subp_id={$subp_id}";
     $entryVarName = 'entryId';
     if ($widget->getIsPlayList()) {
         $entryVarName = 'playlistId';
     }
     if ($uiConf) {
         $ks_flashvars = "";
         $conf_vars = $uiConf->getConfVars();
         if ($conf_vars) {
             $conf_vars = "&" . $conf_vars;
         }
         $wrapper_swf = $base_wrapper_swf;
         $partner = PartnerPeer::retrieveByPK($partner_id);
         if ($partner) {
             $partner_type = $partner->getType();
         }
         if (version_compare($uiConf->getSwfUrlVersion(), "3.0", ">=")) {
             $kdp3 = true;
             // further in the code, $wrapper_swf is being used and not $base_wrapper_swf
             $wrapper_swf = $base_wrapper_swf = myContentStorage::getFSFlashRootPath() . '/kdp3wrapper/' . kConf::get('kdp3_wrapper_version') . '/kdp3wrapper.swf';
             $widgetIdStr = "widgetId={$widget_id}";
             $uiconf_id_str = "&uiConfId={$uiconf_id}";
             $partnerIdStr = "partnerId={$partner_id}&subpId={$subp_id}";
         }
         // if we are loaded without a wrapper (directly in flex)
         // 1. dont create the ks - keep url the same for caching
         // 2. dont patch the uiconf - patching is done only to wrapper anyway
         if ($nowrapper) {
             $dynamic_date = $widgetIdStr . "&host=" . str_replace("http://", "", str_replace("https://", "", $partner_host)) . "&cdnHost=" . str_replace("http://", "", str_replace("https://", "", $partner_cdnHost)) . $uiconf_id_str . $conf_vars;
             $url = "{$swf_url}?{$dynamic_date}";
         } else {
             $swf_data = null;
             // if kdp version >= 2.5
             if (version_compare($uiConf->getSwfUrlVersion(), "2.5", ">=")) {
                 // create an anonymous session
                 $ks = "";
                 $privileges = "view:*,widget:1";
                 if ($widget->getIsPlayList()) {
                     $privileges = "list:*,widget:1";
                 }
                 if (PermissionPeer::isValidForPartner(PermissionName::FEATURE_ENTITLEMENT, $partner_id) && !$widget->getEnforceEntitlement() && $widget->getEntryId()) {
                     $privileges .= ',' . kSessionBase::PRIVILEGE_DISABLE_ENTITLEMENT_FOR_ENTRY . ':' . $widget->getEntryId();
                 }
                 if (PermissionPeer::isValidForPartner(PermissionName::FEATURE_ENTITLEMENT, $partner_id) && !is_null($widget->getPrivacyContext()) && $widget->getPrivacyContext() != '') {
                     $privileges .= ',' . kSessionBase::PRIVILEGE_PRIVACY_CONTEXT . ':' . $widget->getPrivacyContext();
                 }
                 $result = kSessionUtils::createKSessionNoValidations($partner_id, 0, $ks, 86400, false, "", $privileges);
                 $ks_flashvars = "&{$partnerIdStr}&uid=0&ts=" . microtime(true);
                 if ($widget->getSecurityType() != widget::WIDGET_SECURITY_TYPE_FORCE_KS) {
                     $ks_flashvars = "&ks={$ks}" . $ks_flashvars;
                 }
                 // patch kdpwrapper with getwidget and getuiconf
                 $root = myContentStorage::getFSContentRootPath();
                 $confFile_mtime = $uiConf->getUpdatedAt(null);
                 $swf_key = "widget_{$widget_id}_{$widget_type}_{$confFile_mtime}_" . md5($base_wrapper_swf . $swf_url) . ".swf";
                 $cache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_KWIDGET_SWF);
                 if ($cache) {
                     $swf_data = $cache->get($swf_key);
                 }
                 if (!$swf_data) {
                     require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "api_v3" . DIRECTORY_SEPARATOR . "bootstrap.php";
                     $dispatcher = KalturaDispatcher::getInstance();
                     try {
                         $widget_result = $dispatcher->dispatch("widget", "get", array("ks" => $ks, "id" => $widget_id));
                         $ui_conf_result = $dispatcher->dispatch("uiConf", "get", array("ks" => $ks, "id" => $widget_type));
                     } catch (Exception $ex) {
                         KExternalErrors::dieGracefully();
                     }
                     if (!$ui_conf_result->confFile) {
                         KExternalErrors::dieGracefully();
                     }
                     $serializer = new KalturaXmlSerializer(false);
                     $widget_xml = $serializer->serialize($widget_result);
                     $serializer = new KalturaXmlSerializer(false);
                     $ui_conf_xml = $serializer->serialize($ui_conf_result);
                     $result = "<xml><result>{$widget_xml}</result><result>{$ui_conf_xml}</result></xml>";
                     $patcher = new kPatchSwf(file_get_contents($root . $base_wrapper_swf));
                     $swf_data = $patcher->patch($result);
                     if ($cache) {
                         $cache->set($swf_key, $swf_data);
                     }
                 }
             }
             $kdp_version_2 = strpos($swf_url, "kdp/v2.") > 0;
             if ($partner_host == "http://www.kaltura.com" && !$kdp_version_2 && !$kdp3) {
                 $partner_host = 1;
                 // otherwise the kdp will try going to cdnwww.kaltura.com
             }
             $track_wrapper = '';
             if (kConf::get('track_kdpwrapper') && kConf::get('kdpwrapper_track_url')) {
                 $track_wrapper = "&wrapper_tracker_url=" . urlencode(kConf::get('kdpwrapper_track_url') . "?activation_key=" . kConf::get('kaltura_activation_key') . "&package_version=" . kConf::get('kaltura_version'));
             }
             $optimizedConfVars = null;
             $optimizedHost = null;
             if (kConf::hasMap("optimized_playback")) {
                 $optimizedPlayback = kConf::getMap("optimized_playback");
                 if (array_key_exists($partner_id, $optimizedPlayback)) {
                     // force a specific kdp for the partner
                     $params = $optimizedPlayback[$partner_id];
                     if (array_key_exists('kdp_version', $params)) {
                         $swf_url = $partner_cdnHost . myPartnerUtils::getUrlForPartner($partner_id, $subp_id) . "/flash/kdp3/" . $params['kdp_version'] . "/kdp3.swf";
                     }
                     if (array_key_exists('conf_vars', $params)) {
                         $optimizedConfVars = $params['conf_vars'];
                     }
                     if (array_key_exists('host', $params)) {
                         $optimizedHost = $params['host'];
                     }
                     // cache immidiately
                     $cache_st = 0;
                     $allowCache = true;
                 }
             }
             if ($optimizedConfVars === null) {
                 $optimizedConfVars = "clientDefaultMethod=GET";
             }
             $conf_vars = "&{$optimizedConfVars}&" . $conf_vars;
             $stats_host = $protocol == "https" ? kConf::get("stats_host_https") : kConf::get("stats_host");
             $wrapper_stats = kConf::get('kdp3_wrapper_stats_url') ? "&wrapper_stats_url={$protocol}://{$stats_host}" . urlencode(str_replace("{partnerId}", $partner_id, kConf::get('kdp3_wrapper_stats_url'))) : "";
             $partner_host = str_replace("http://", "", str_replace("https://", "", $partner_host));
             // if the host is the default www domain use the cdn api domain
             if ($partner_host == kConf::get("www_host") && $optimizedHost === null) {
                 $partner_host = kConf::get("cdn_api_host");
             } else {
                 if ($optimizedHost) {
                     $partner_host = $optimizedHost;
                 }
             }
             if ($protocol == "https" && ($partner_host = kConf::get("cdn_api_host"))) {
                 $partner_host = kConf::get("cdn_api_host_https");
             }
             $dynamic_date = $widgetIdStr . $track_wrapper . $wrapper_stats . "&kdpUrl=" . urlencode($swf_url) . "&host=" . $partner_host . "&cdnHost=" . str_replace("http://", "", str_replace("https://", "", $partner_cdnHost)) . "&statistics.statsDomain={$stats_host}" . ($show_version ? "&entryVersion={$show_version}" : "") . ($kshow_id ? "&kshowId={$kshow_id}" : "") . ($entry_id ? "&{$entryVarName}={$entry_id}" : "") . $uiconf_id_str . $ks_flashvars . ($cache_st ? "&clientTag=cache_st:{$cache_st}" : "") . $conf_vars;
             // patch wrapper with flashvars and dump to browser
             if (version_compare($uiConf->getSwfUrlVersion(), "2.6.6", ">=")) {
                 $startTime = microtime(true);
                 $patcher = new kPatchSwf($swf_data, "KALTURA_FLASHVARS_DATA");
                 $wrapper_data = $patcher->patch($dynamic_date . "&referer=" . urlencode($referer));
                 KalturaLog::log('Patching took ' . (microtime(true) - $startTime));
                 requestUtils::sendCdnHeaders("swf", strlen($wrapper_data), $allowCache ? 60 * 10 : 0, null, true, time());
                 if ($_SERVER["REQUEST_METHOD"] == "HEAD") {
                     header('Content-Length: ' . strlen($wrapper_data));
                 } else {
                     echo $wrapper_data;
                 }
                 if ($allowCache) {
                     $cache_swfdata->put($requestKey, $wrapper_data);
                 }
                 KExternalErrors::dieGracefully();
             }
             if ($swf_data) {
                 $md5 = md5($swf_key);
                 $wrapper_swf = "content/cacheswf/" . substr($md5, 0, 2) . "/" . substr($md5, 2, 2) . "/" . $swf_key;
                 $wrapper_swf_path = "{$root}/{$wrapper_swf}";
                 if (!file_exists($wrapper_swf_path)) {
                     kFile::fullMkdir($wrapper_swf_path);
                     file_put_contents($wrapper_swf_path, $swf_data);
                 }
             }
             // for now changed back to $host since kdp version prior to 1.0.15 didnt support loading by external domain kdpwrapper
             $url = $host . myPartnerUtils::getUrlForPartner($partner_id, $subp_id) . "/{$wrapper_swf}?{$dynamic_date}";
         }
     } else {
         $dynamic_date = "kshowId={$kshow_id}" . "&host=" . requestUtils::getRequestHostId() . ($show_version ? "&entryVersion={$show_version}" : "") . ($entry_id ? "&{$entryVarName}={$entry_id}" : "") . ($entry_id ? "&KmediaType={$kmedia_type}" : "");
         $dynamic_date .= "&isWidget={$widget_type}&referer=" . urlencode($referer);
         $dynamic_date .= "&kdata={$kdata}";
         $url = "{$swf_url}?{$dynamic_date}";
     }
     // if referer has a query string an IE bug will prevent out flashvars to propagate
     // when nowrapper is true we cant use /swfparams either as there isnt a kdpwrapper
     if (!$nowrapper && $uiConf && version_compare($uiConf->getSwfUrlVersion(), "2.6.6", ">=")) {
         // apart from the /swfparam/ format, add .swf suffix to the end of the stream in case
         // a corporate firewall looks at the file suffix
         $pos = strpos($url, "?");
         $url = substr($url, 0, $pos) . "/swfparams/" . urlencode(substr($url, $pos + 1)) . ".swf";
     }
     if ($allowCache) {
         $cache_redirect->put($requestKey, $url);
     }
     if (strpos($url, "/swfparams/") > 0) {
         $url = substr($url, 0, -4) . urlencode($noncached_params) . ".swf";
     } else {
         $url .= $noncached_params;
     }
     KExternalErrors::terminateDispatch();
     $this->redirect($url);
 }
 /**
  * @return KalturaPDO
  */
 public static function getSphinxConnection($read = true)
 {
     if (self::$sphinxConnection) {
         return self::$sphinxConnection;
     }
     $sphinxDS = isset(self::$config['sphinx_datasources']['datasources']) ? self::$config['sphinx_datasources']['datasources'] : array(self::DB_CONFIG_SPHINX);
     $cacheExpiry = isset(self::$config['sphinx_datasources']['cache_expiry']) ? self::$config['sphinx_datasources']['cache_expiry'] : 300;
     $connectTimeout = isset(self::$config['sphinx_datasources']['connect_timeout']) ? self::$config['sphinx_datasources']['connect_timeout'] : 1;
     $stickySessionExpiry = isset(self::$config['sphinx_datasources']['sticky_session_timeout']) ? self::$config['sphinx_datasources']['sticky_session_timeout'] : 600;
     $stickySessionKey = 'StickySession:' . kCurrentContext::$user_ip;
     $cache = kCacheManager::getCache(kCacheManager::MC_GLOBAL_QUERIES);
     if ($cache) {
         $key = $cache->get($stickySessionKey);
         if ($key) {
             $connection = self::getConnection($key, $cacheExpiry, $connectTimeout);
             if (!is_null($connection)) {
                 return $connection;
             }
         }
     }
     // loop twice, on first iteration try only connections not marked as failed
     // in case all connections failed, try all connections on second iteration
     $iteration = 2;
     while ($iteration--) {
         $count = count($sphinxDS);
         $offset = mt_rand(0, $count - 1);
         while ($count--) {
             $key = $sphinxDS[($count + $offset) % count($sphinxDS)];
             $cacheKey = "sphinxCon:" . $key;
             if (function_exists('apc_fetch')) {
                 if (!$iteration) {
                     // on second iteration reset failed connection flag
                     apc_store($cacheKey, 0);
                 } else {
                     if (apc_fetch($cacheKey)) {
                         // if connection failed to connect in the past mark it
                         continue;
                     }
                 }
             }
             $connection = self::getConnection($key, $cacheExpiry, $connectTimeout);
             if (!$read && $cache) {
                 $cache->set($stickySessionKey, $key, $stickySessionExpiry);
             }
             if (!is_null($connection)) {
                 return $connection;
             }
         }
     }
     KalturaLog::debug("getSphinxConnection: Failed to connect to any Sphinx config");
     throw new Exception('Failed to connect to any Sphinx config');
 }
Esempio n. 18
0
 protected function isKSInvalidated()
 {
     if (strpos($this->privileges, self::PRIVILEGE_ACTIONS_LIMIT) !== false) {
         return null;
     }
     // cannot validate action limited KS at this level
     $memcache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_QUERY_CACHE_KEYS);
     if (!$memcache) {
         return null;
     }
     // failed to connect to memcache or memcache not enabled
     $ksKey = self::INVALID_SESSION_KEY_PREFIX . $this->hash;
     $keysToGet = array(self::INVALID_SESSIONS_SYNCED_KEY, $ksKey);
     $sessionIdKey = $this->getSessionIdHash();
     if ($sessionIdKey) {
         $sessionIdKey = self::INVALID_SESSION_KEY_PREFIX . $sessionIdKey;
         $keysToGet[] = $sessionIdKey;
     }
     $cacheResult = $memcache->multiGet($keysToGet);
     if ($cacheResult === false) {
         return null;
     }
     // failed to get the keys
     if (!array_key_exists(self::INVALID_SESSIONS_SYNCED_KEY, $cacheResult) || !$cacheResult[self::INVALID_SESSIONS_SYNCED_KEY]) {
         return null;
     }
     // invalid sessions not synched to memcache
     unset($cacheResult[self::INVALID_SESSIONS_SYNCED_KEY]);
     if ($cacheResult) {
         return true;
     }
     // the session is invalid
     return false;
 }
Esempio n. 19
0
 protected function getKSVersionAndSecret($partnerId)
 {
     $result = parent::getKSVersionAndSecret($partnerId);
     if ($result) {
         return $result;
     }
     $partner = PartnerPeer::retrieveByPK($partnerId);
     if (!$partner) {
         return array(1, null);
     }
     // VERY big problem
     $ksVersion = $partner->getKSVersion();
     $cacheKey = self::getSecretsCacheKey($partnerId);
     $cacheSections = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_PARTNER_SECRETS);
     foreach ($cacheSections as $cacheSection) {
         $cacheStore = kCacheManager::getCache($cacheSection);
         if (!$cacheStore) {
             continue;
         }
         $cacheStore->set($cacheKey, array($partner->getAdminSecret(), $partner->getSecret(), $ksVersion));
     }
     return array($ksVersion, $partner->getAdminSecret());
 }
 protected function handleEntries($context, $feed, array $entries)
 {
     $protocol = infraRequestUtils::getProtocol();
     $cachePrefix = "dist_" . $this->profile->getId() . "/{$protocol}/entry_";
     $profileUpdatedAt = $this->profile->getUpdatedAt(null);
     $extendItems = $this->profile->getItemXpathsToExtend();
     $enableCache = empty($extendItems);
     $cacheStore = null;
     if ($enableCache) {
         $cacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_FEED_ENTRY);
         if (is_null($cacheStore)) {
             $enableCache = false;
         }
     }
     $counter = 0;
     foreach ($entries as $entry) {
         $xml = null;
         $cacheKey = $cachePrefix . str_replace("_", "-", $entry->getId());
         // replace _ with - so cache folders will be created with random entry id and not 0_/1_
         if ($enableCache) {
             $cacheTime = $cacheStore->get($cacheKey . self::CACHE_CREATION_TIME_SUFFIX);
             $updatedAt = max($profileUpdatedAt, $entry->getUpdatedAt(null));
             if ($updatedAt + self::CACHE_CREATION_MARGIN < $cacheTime) {
                 $xml = $cacheStore->get($cacheKey);
             }
         }
         if (!$xml) {
             $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $this->profile->getId());
             if (!$entryDistribution) {
                 KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $this->profile->getId() . ']');
                 continue;
             }
             $xml = $this->handleEntry($context, $feed, $entry, $entryDistribution);
             if (!is_null($xml) && $enableCache) {
                 $cacheStore->set($cacheKey . self::CACHE_CREATION_TIME_SUFFIX, time());
                 $cacheStore->set($cacheKey, $xml);
             }
         }
         $feed->addItemXml($xml);
         $counter++;
         //to avoid the cache exceeding the memory size
         if ($counter % self::CACHE_SIZE == 0) {
             kMemoryManager::clearMemory();
         }
     }
 }
Esempio n. 21
0
 protected function countEntriesByCriteria($entryIds, $directOnly = false)
 {
     // Try to retrieve from cache
     $cacheKey = category::EXCEEDED_ENTRIES_COUNT_CACHE_PREFIX . $this->getId();
     $cacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_LOCK_KEYS);
     if ($cacheStore) {
         $countExceeded = $cacheStore->get($cacheKey);
         if ($countExceeded) {
             return null;
         }
     }
     // Query for entry count
     $baseCriteria = KalturaCriteria::create(entryPeer::OM_CLASS);
     $filter = new entryFilter();
     $filter->setPartnerSearchScope(baseObjectFilter::MATCH_KALTURA_NETWORK_AND_PRIVATE);
     if ($directOnly) {
         $filter->setCategoriesIdsMatchAnd($this->getId());
     } else {
         $filter->setCategoryAncestorId($this->getId());
     }
     if ($entryIds) {
         $filter->setIdNotIn($entryIds);
     }
     $filter->setLimit(1);
     $filter->attachToCriteria($baseCriteria);
     $baseCriteria->applyFilters();
     $count = $baseCriteria->getRecordsCount();
     // Save the result within the cache
     if ($count >= category::MAX_NUMBER_OF_ENTRIES_PER_CATEGORY) {
         if ($cacheStore) {
             $cacheStore->set($cacheKey, true, category::EXCEEDED_ENTRIES_COUNT_CACHE_EXPIRY);
         }
     }
     return $count;
 }
Esempio n. 22
0
 protected static function getSphinxConnIndexFromCache()
 {
     self::$sphinxCache = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_SPHINX_STICKY_SESSIONS);
     if (!self::$sphinxCache) {
         return false;
     }
     self::$stickySessionKey = self::getStickySessionKey();
     $preferredIndex = self::$sphinxCache->get(self::$stickySessionKey);
     if ($preferredIndex === false) {
         return false;
     }
     self::$cachedConnIndex = (int) $preferredIndex;
     //$preferredIndex returns from self::$sphinxCache->get(..) in type string
     return $preferredIndex;
 }
Esempio n. 23
0
 /**
  * Get value from cache for the given key
  * @param string $key
  */
 private static function getFromCache($key, $roleCacheDirtyAt)
 {
     if (!self::useCache()) {
         return null;
     }
     self::$cacheStores = array();
     $cacheLayers = kCacheManager::getCacheSectionNames(kCacheManager::CACHE_TYPE_PERMISSION_MANAGER);
     foreach ($cacheLayers as $cacheLayer) {
         $cacheStore = kCacheManager::getCache($cacheLayer);
         if (!$cacheStore) {
             continue;
         }
         $cacheRole = $cacheStore->get(self::getCacheKeyPrefix() . $key);
         // try to fetch from cache
         if (!$cacheRole || !isset($cacheRole['updatedAt']) || $cacheRole['updatedAt'] < $roleCacheDirtyAt) {
             self::$cacheStores[] = $cacheStore;
             continue;
         }
         $map = $cacheStore->get(self::getCacheKeyPrefix() . $cacheRole['mapHash']);
         // try to fetch from cache
         if (!$map) {
             self::$cacheStores[] = $cacheStore;
             continue;
         }
         KalturaLog::debug("Found a cache value for key [{$key}] map hash [" . $cacheRole['mapHash'] . "] in layer [{$cacheLayer}]");
         self::storeInCache($key, $cacheRole, $map);
         // store in lower cache layers
         self::$cacheStores[] = $cacheStore;
         return $map;
     }
     KalturaLog::debug("No cache value found for key [{$key}]");
     return null;
 }
Esempio n. 24
0
 /**
  *
  * Store given value in cache for with the given key as an identifier
  * @param string $key
  */
 private function storeInCache($key)
 {
     $cacheType = self::getCacheType();
     $cacheStore = kCacheManager::getSingleLayerCache($cacheType);
     if (!$cacheStore) {
         KalturaLog::debug("cacheStore is null. cacheType: {$cacheType} . returning false");
         return false;
     }
     return $cacheStore->set($key, true, kConf::get('media_server_cache_expiry', 'local', self::DEFAULT_CACHE_EXPIRY));
 }
 private static function getMaxInvalidationTime($invalidationKeys)
 {
     $memcache = kCacheManager::getCache(kCacheManager::MC_GLOBAL_KEYS);
     if (!$memcache) {
         return null;
     }
     $cacheResult = $memcache->multiGet($invalidationKeys);
     if ($cacheResult === false) {
         return null;
     }
     // failed to get the invalidation keys
     if (!$cacheResult) {
         return 0;
     }
     // no invalidation keys - no changes occured
     return max($cacheResult);
 }