<?php
function validId($cid, $vid, $type) {
switch($type) {
case 'channel': // live stream
$response = drupal_http_request('http://x'.$cid.'x.' . MEDIA_LIVESTREAM_REST_CHANNEL_API . 'info.json'); //from Livestream submodule getChannelProperties
break;
case 'video': // on-demand video clip
$response = drupal_http_request('http://x'.$cid.'x.' . MEDIA_LIVESTREAM_REST_CHANNEL_API . 'clipdetails.json?id='.$vid); //from getVideoProperties
break;
}
return ($response->code == 200);
}
function parse($embedCode) {
$embedCode = trim($embedCode);
$match = FALSE;
// The Livestream short code.
if (!$match && preg_match('@livestre\.am/([0-9a-zA-Z\-]+)$@i', $embedCode, $matches)) {
$headers = media_livestream_get_short_code($matches[0]);
$embedCode = $headers['url'];
}
// This one checks for a channel.
// E.g. livestream.com/channelname.
if (!$match && preg_match('@livestream\.com/([0-9a-zA-Z\-]+)$@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'channel';
$channel_id = $matches[1];
$video_id = $matches[1];
}
// This one checks for a channel iframe.
// E.g. livestream.com/embed/channelname. (And no clip (id) set).
elseif (!$match && preg_match('@livestream\.com/embed/([0-9a-zA-Z\-]+)?((?!clip|clipId).)*$@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'channel';
$channel_id = $matches[1];
$video_id = $matches[1];
}
// This one works for 'video' and 'share' on a new style link.
// E.g. livestream.com/channelname/video(or)share/clip(id).
elseif (!$match && preg_match('@livestream\.com/([0-9a-zA-Z\-]+).*/(video|share).*((clipId|clip)=([^\&\'" ]+))@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'video';
$channel_id = $matches[1];
$video_id = $matches[5];
}
// This one matches the new iframe embed for videos.
// E.g. livestream.com/embed/channelname/clip(id).
elseif (!$match && preg_match('@livestream\.com/(embed)/([0-9a-zA-Z\-]+).*((clipId|clip)=([^\&\'" ]+))@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'video';
$channel_id = $matches[2];
$video_id = $matches[5];
}
// This one also checks for a channel, but this link is almost never used, but it does exist.
// E.g. livestream.com/channelname/share.
elseif (!$match && preg_match('@livestream\.com/([0-9a-zA-Z\-]+)/(share)@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'channel';
$channel_id = $matches[1];
$video_id = $matches[1];
}
// This one matches the old flash embed for videos.
// E.g. livestream.com/grid/LSPlayer.swf?channel=channelname/clip(id).
elseif (!$match && preg_match('@livestream\.com/(grid)/LSPlayer.swf\?(channel)=([0-9a-zA-Z\-]+)&(clipId|clip)=([^\&\'" ]+)@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'video';
$channel_id = $matches[3];
$video_id = $matches[5];
}
// This one matches the old flash embed for channels.
// E.g. livestream.com/grid/LSPlayer.swf?channel=channelname.
elseif (!$match && preg_match('@livestream\.com/grid/LSPlayer\.swf.?channel=([0-9a-zA-Z\-]+)@i', $embedCode, $matches)) {
$match = TRUE;
$type = 'channel';
$channel_id = $matches[1];
$video_id = $matches[1];
}
//do something with the match (along the lines of validate and return)
return array($channel_id, $video_id);
}
/**
* Resolve the Livestream shortcode to get the
* full url to the correct channel/video.
*/
function media_livestream_get_short_code($shortcode) {
// Set options to fetch the header redirects Livestream trows at us.
$options = array(
CURLOPT_HEADER => FALSE, // Don not return headers. Not sure.
CURLOPT_FOLLOWLOCATION => TRUE, // Follow the link.
CURLOPT_RETURNTRANSFER => TRUE, // This is not what you think.
// (see the manual, ^^ still fetches the whole page if i disable it)
CURLOPT_ENCODING => "", // Do not specify encoding.
CURLOPT_CONNECTTIMEOUT => 100, // Connect timeout.
CURLOPT_TIMEOUT => 100, // Response timeout.
CURLOPT_MAXREDIRS => 2, // Stop after 2 redirects.
CURLOPT_AUTOREFERER => TRUE, // Set the referer on redirect.
CURLOPT_USERAGENT => "Drupal", // Browser ID.
);
// Initialise curl request.
$handler = curl_init($shortcode);
curl_setopt_array($handler, $options);
// Execute our curl request with our Livesteam shortcode url.
$page = curl_exec($handler);
$header = curl_getinfo($handler);
$errornr = curl_errno($handler);
$errormsg = curl_error($handler);
// Close request.
curl_close($handler);
// We don't really need the page content, we're here for the redirect url.
$header['page'] = $page;
// And we also do not use errors at the moment, but maybe we should.
$header['errornr'] = $errornr;
$header['errormsg'] = $errormsg;
return $header;
}
print_r(parse('livestream.com/grid/LSPlayer.swf?channel=channelname'));