Обработчик плейлиста Uppod

iskiz

Новичок
Здравствуйте посетители форума.
Постараюсь вкратце описать свой вопрос :)

Есть ПХП скрипт:

PHP:
<?php
/////////////////////

// Parameters need to be passed in through the URL\'s query string:
// pl	 absolute path of local playlistfile starting with \"/\" (e.g. /images/toast.jpg)

/////////////////////


/////////////////////
// CODE STARTS HERE
/////////////////////

if (!isset($_GET['pl']))
{
header('HTTP/1.1 400 Bad Request');
echo "Error: no playlist was specified";
exit();
}

define('MEMORY_TO_ALLOCATE',	'8M');
define('CURRENT_DIR',	 dirname(__FILE__));
define('DOCUMENT_ROOT',	 $_SERVER['DOCUMENT_ROOT']);

// Playlists must be local files, so for convenience we strip the domain if it\'s there
$playlist	 = preg_replace("/^(s?f|ht)tps?:\/\/[^\/]+/i", '', (string) $_GET['pl']);

// For security, directories cannot contain \':\', playlist files cannot contain \'..\' or \'<\', and
// images must start with \'/\'
if ($playlist{0} != '/' || strpos(dirname($playlist), ':') || preg_match('/(\.\.|<|>)/', $playlist))
{
header('HTTP/1.1 400 Bad Request');
echo "Error: malformed playlist path. Playlist paths must begin with /";
exit();
}

// If the playlist doesn\'t exist, or we haven\'t been told what it is, there\'s nothing
// that we can do
if (!$playlist)
{
header('HTTP/1.1 400 Bad Request');
echo "Error: no playlist file was specified";
exit();
}

// Strip the possible trailing slash off the document root
$docRoot	= preg_replace("/\/$/", '', DOCUMENT_ROOT);

if (!file_exists($docRoot . $playlist))
{
header('HTTP/1.1 404 Not Found');
echo 'Error: playlist file does not exist: ' . $docRoot . $playlist;
exit();
}

$playlist_string = file_get_contents( 'http://' . 'site.ru' . $playlist);

$decoded_playlist = json_decode($playlist_string, true);

// для дебага
// echo \'<h2>Декодированный плейлист</h2><pre>\';
// print_r ($decoded_playlist);
// echo \'</pre>\';


function encode_urls($decoded_playlist) {
foreach ($decoded_playlist as &$episode) {
foreach ($episode as &$url) {
$secret = "yoursecretword";
$uri_prefix = "/video/";
$fname = basename($url[\'file\']);
$ts = sprintf(\"%08x\",strtotime(\"now\"));
$dirname = dirname($url[\'file\']);
$url[\'file\'] = $dirname . \'/\' . md5($fname . $ts . $secret) . \"/{$ts}/{$fname}\";
}
}
return $decoded_playlist;
}

$decoded_playlist = encode_urls($decoded_playlist);


// для дебага
// echo \'<br /> <br />\';
// 
// foreach ($decoded_playlist as &$episode) { 
// echo <pre>;
// foreach ($episode as &$url) {
// print_r($url);
// }
//  echo '</pre>';
// }

// для дебага
// echo \'<h2>Декодированный плейлист 2</h2><pre>\';
// print_r ($decoded_playlist);
// echo \'</pre>\';
// 
// echo \'<h2>Закодированный JSON плейлист 2</h2><pre>\';
print_r (json_encode($decoded_playlist));
// echo \'</pre>\';
?>
Выше указанный скрипт плейлисты такого вида:

PHP:
{"playlist":[
{"comment":"Серия 1","file":"/Serials/BlueMauntin/01/s01e01.flv"},
{"comment":"Серия 2","file":"/Serials/BlueMauntin/01/s01e02.flv"}]}
После запроса к строке "site.ru/playlist.php=/playlists/BlueMauntin.txt" приводит плейлист к такому виду:

PHP:
{"playlist":[
{"comment":"Серия 1","file":"http://site.ru/video/c04bd114356356854dd6587847eb884c/4f2c74d9/Serials/BlueMauntin/01/s01e01.flv"},
{"comment":"Серия 2","file":"http://site.ru/video/c04bd114356356854dd6587847eb884c/4f2c74d9/Serials/BlueMauntin/01/s01e02.flv"}]}
А если плейлист будет с вложенным разделом, например:

PHP:
{"playlist":[
{"comment":"Сезон 1","playlist":[
{"comment":"Серия 1","file":"/Serials/BlueMauntin/01/s01e01.flv"},
{"comment":"Серия 2","file":"/Serials/BlueMauntin/01/s01e02.flv"}]},
{"comment":"Сезон 2","playlist":[
{"comment":"Серия 1","file":"/Serials/BlueMauntin/01/s01e01.flv"},
{"comment":"Серия 2","file":"/Serials/BlueMauntin/01/s01e02.flv"}]}]}
То скрипт обрабатывает только верхний playlist, а внутренние нет - на выдаче получается что-то вроде:

PHP:
{"playlist":[
{"comment":"Сезон 1","playlist":[
{"comment":"Серия 1","file":"/Serials/BlueMauntin/01/s01e01.flv"},
{"comment":"Серия 2","file":"/Serials/BlueMauntin/01/s01e02.flv"}],
"file":"http://site.ru/video/70db214bbb140712e17d557d4ef6b143/4f2c77ca/"},
{"comment":"Сезон 2","playlist":[
{"comment":"Серия 1","file":"/Serials/BlueMauntin/01/s01e01.flv"},
{"comment":"Серия 2","file":"/Serials/BlueMauntin/01/s01e02.flv"}],
"file":"http://site.ru/video/70db214bbb140712e17d557d4ef6b143/4f2c77ca/"}]}
Подскажите что подправить, чтобы обрабатывало и внутренние. С ПХП только начал дружиться. Подозреваю, что нужно править "preg_replace". Или лучше было бы подвязать к переменной плейлиста "file":". Старался быть краток, надеюсь на Вашу помощь. Спасибо.
 
Сверху