diff --git a/alpha/apps/kaltura/lib/storage/urlTokenizers/kL3UrlTokenizer.php b/alpha/apps/kaltura/lib/storage/urlTokenizers/kL3UrlTokenizer.php new file mode 100644 index 00000000000..3c1497ca95f --- /dev/null +++ b/alpha/apps/kaltura/lib/storage/urlTokenizers/kL3UrlTokenizer.php @@ -0,0 +1,173 @@ +calculateToken($urlToToken) . $restUrl; + } + + /** + * @param string $baseUrl + * @param array $flavors + */ + public function tokenizeMultiUrls(&$baseUrl, &$flavors) + { + $flavorRestUrl = ''; + $commonPartUrl = ''; + if (count($flavors) > 1) + { + if (isset($flavors[0][self::URL])) + { + $url = self::removeTokenWord($flavors[0][self::URL]); + $commonPartUrl = self::getCommonPartUrl($url); + } + } + else if (count($flavors) == 1) + { + if (isset($flavors[0][self::URL])) + { + $url = self::removeTokenWord($flavors[0][self::URL]); + list($commonPartUrl,$flavorRestUrl) = self::splitUrlFlavorParts($url); + } + } + $urlTokenized = $this->calculateToken($commonPartUrl); + self::setFlavorsUrlWithToken($flavors, $urlTokenized, $flavorRestUrl); + } + + public static function setFlavorsUrlWithToken(&$flavors, $urlTokenized, $flavorRestUrl) + { + if ($urlTokenized === '') + { + return; + } + foreach($flavors as $flavorKey => $flavor) + { + if (isset($flavor[self::URL])) + { + if ($flavorRestUrl !== '') + { + $restUrl = $flavorRestUrl; + } + else + { + $restUrl = self::getRestUrl($flavor); + } + $flavors[$flavorKey][self::URL] = $urlTokenized . $restUrl; + } + } + } + + public static function getRestUrl($flavor) + { + $restUrl = ''; + $flavorIdPos = strpos($flavor[self::URL], self::FLAVOR_ID . self::SLASH); + if ($flavorIdPos !== false) + { + $restUrl = substr($flavor[self::URL], $flavorIdPos + strlen(self::FLAVOR_ID . self::SLASH)); + } + return $restUrl; + } + + public function calculateToken($urlToToken) + { + if ($urlToToken === '') + { + return ''; + } + $nva = time() + $this->window; + $path = self::SLASH . trim($urlToToken, self::SLASH) . self::SLASH; + $dirs = substr_count($path, self::SLASH) - 1; + $tokenParams = self::NVA . self::EQUAL . $nva . self::AMPERSAND . self::DIRS . self::EQUAL . $dirs; + $uri = $path. self::QUESTION_MARK . $tokenParams; + $hash = $this->gen . substr(hash_hmac(self::SECURE_HASH_ALGO, $uri, $this->key), 0, 20); + $tokenParams .= self::AMPERSAND . self::HASH . self::EQUAL . $hash; + $token = str_replace(self::AMPERSAND, self::TILDA , $tokenParams); + return self::TOKEN . self::EQUAL . $token . $path; + } + + public static function removeTokenWord($url) + { + $tokenPos = strpos($url, self::SLASH . self::TOKEN); + if ($tokenPos !== false) + { + $url = substr($url, $tokenPos + strlen(self::SLASH . self::TOKEN)); + } + return $url; + } + + /* + * In this case the flavor url has one flavor, + * the tokenize url should be till the last slash Occurrence + * splitting url into $urlToTokenize and $restUrl + * $urlToTokenize: until the last slash Occurrence + * $restUrl: from the last slash Occurrence + */ + public static function splitUrlFlavorParts($url) + { + $lastSlashOccurrence = strrpos ($url, self::SLASH); + if ($lastSlashOccurrence === false) + { + return array('', ''); + } + $restUrl = substr($url, $lastSlashOccurrence + 1); + $urlToTokenize = substr($url, 0, $lastSlashOccurrence + 1); + return array($urlToTokenize, $restUrl); + } + + /* + * In this case the flavor url has several flavors, + * the tokenize url should be till the 'flavorId/' + */ + public static function getCommonPartUrl($url) + { + $commonPartUrl = $url; + $flavorIdPos = strpos($url, self::FLAVOR_ID . self::SLASH); + if ($flavorIdPos !== false) + { + $commonPartUrl = substr($url, 0, $flavorIdPos) . self::FLAVOR_ID . self::SLASH; + } + return $commonPartUrl; + } + + /** + * @return string $gen + */ + public function getGen() + { + return $this->gen; + } + + /** + * @param string $gen + */ + public function setGen($gen) + { + $this->gen = $gen; + } +} \ No newline at end of file diff --git a/api_v3/lib/types/delivery/KalturaDeliveryFactory.php b/api_v3/lib/types/delivery/KalturaDeliveryFactory.php index 93da7f8e3ee..d5793f58639 100644 --- a/api_v3/lib/types/delivery/KalturaDeliveryFactory.php +++ b/api_v3/lib/types/delivery/KalturaDeliveryFactory.php @@ -62,6 +62,8 @@ public static function getTokenizerInstanceByType($type) { switch ($type) { case 'kLevel3UrlTokenizer': return new KalturaUrlTokenizerLevel3(); + case 'kL3UrlTokenizer': + return new KalturaUrlTokenizerL3(); case 'kLimeLightUrlTokenizer': return new KalturaUrlTokenizerLimeLight(); case 'kAkamaiHttpUrlTokenizer': diff --git a/api_v3/lib/types/delivery/KalturaUrlTokenizerL3.php b/api_v3/lib/types/delivery/KalturaUrlTokenizerL3.php new file mode 100644 index 00000000000..28a1d66ff6d --- /dev/null +++ b/api_v3/lib/types/delivery/KalturaUrlTokenizerL3.php @@ -0,0 +1,33 @@ +