function getMediaThumb($id, $type = '')
{
switch ($type) {
case 'vimeo':
$xml = simplexml_load_file('http://vimeo.com/api/v2/video/'.$id.'.xml');
return (string) $xml->video->thumbnail_small;
break;
case 'rutube':
$xml = simplexml_load_file(rawurlencode('http://rutube.ru/api/video/'.$id.'/?format=xml'));
return (string) $xml->thumbnail_url;
break;
case 'xyu':
$arg1 = mb_substr($id, 0, 2);
$arg2 = mb_substr($id, 2, 2);
$img_src = "http://cdn-i2.xyu.tv/{$arg1}/{$arg2}/{$id}/160x120/0000000001.jpg";
return (string) $img_src;
break;
case 'red':
$xml = simplexml_load_file('http://api.redtube.com/?data=redtube.Videos.getVideoById&video_id='.$id.'&output=xml&thumbsize=small','SimpleXMLElement', LIBXML_NOCDATA);
$img_ary = array();
foreach ($xml->thumbs as $item) {
$img_ary[] = object2array($item[0]);
}
return (string) $img_ary[0]['thumb'][0];
break;
case 'xhamster':
return 'images/xhm_no_th.jpg';
break;
default:
return 'images/no_th.jpg';
}
}
function parseMedia($text)
{
$img_src = 'images/no_th.jpg';
// Start parse and replase YouTube
$patterns = '~
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\-\s] # but char before ID is non-ID char.
) # End host alternatives.
([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w\-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w-]?(.*?)? # Consume any URL (query) remainder.
~ix';
$replacements = '<img onclick="$(this).hide(); $(this).next(\'div\').css({\'display\' : \'block\'});" style="background:transparent;border:6px solid #ABABAB;border-radius:3px;padding:1px;margin:3px 3px 3px 0;" class="clickable" src="http://img.youtube.com/vi/\\1/3.jpg" /><div style="display: none;"><object width="640" height="360"><param name="movie" value="http://www.youtube.com/v/\\1"></param><embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" width="640" height="360"></embed></object></div>';
$text = preg_replace($patterns, $replacements, $text);
// End parse and replase YouTube
// Start parse and replase vimeo
$patterns_vimeo = '~https?:\/\/(?:[\w]+\.)*vimeo\.com(?:[\/\w]*\/videos?)?\/([0-9]+)[^\s]*~ix';
if(preg_match($patterns_vimeo, $text, $out)) $img_src = getMediaThumb($out[1], 'vimeo');
$patterns = '~https?:\/\/(?:[\w]+\.)*vimeo\.com(?:[\/\w]*\/videos?)?\/([0-9]+)[^\s]*~ix';
$replacements = '<img width="120px" height="90px" onclick="$(this).hide(); $(this).next(\'div\').css({\'display\' : \'block\'});" style="background:transparent;border:6px solid #ABABAB;border-radius:3px;padding:1px;margin:3px 3px 3px 0;" class="clickable" src="'.$img_src.'" /><div style="display: none;"><iframe src="http://player.vimeo.com/video/\\1?byline=0&portrait=0&badge=0" width="640" height="360" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>';
$text = preg_replace($patterns, $replacements, $text);
// End parse and replase vimeo
// Start parse and replase rutube
$patterns_rutube = '~https?:\/\/(?:[\w]+\.)*rutube\.ru/video/([A-Za-z0-9-_]{5,40})/~ix';
if(preg_match($patterns_rutube, $text, $out)) $img_src = getMediaThumb($out[1], 'rutube');
$patterns = '~https?:\/\/(?:[\w]+\.)*rutube\.ru/video/([A-Za-z0-9-_]{5,40})/~ix';
$replacements = '<img width="120px" height="90px" onclick="$(this).hide(); $(this).next(\'div\').css({\'display\' : \'block\'});" style="background:transparent;border:6px solid #ABABAB;border-radius:3px;padding:1px;margin:3px 3px 3px 0;" class="clickable" src="'.$img_src.'" /><div style="display: none;"><iframe width="640" height="360" src="http://rutube.ru/video/embed/\\1" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen scrolling="no"></iframe></div>';
$text = preg_replace($patterns, $replacements, $text);
// End parse and replase rutube
// Start parse and replase xyu.tv
$patterns_xuy = '~https?:\/\/(?:[\w]+\.)*xyu\.tv/video/([0-9]{1,10})(.*)~ix';
if(preg_match_all($patterns_xuy, $text, $out, PREG_SET_ORDER)) $img_src = getMediaThumb($out[0][1], 'xyu');
$patterns = '~https?:\/\/(?:[\w]+\.)*xyu\.tv/video/([0-9]{1,10})(.*)~ix';
$replacements = '<img width="120px" height="90px" onclick="$(this).hide(); $(this).next(\'div\').css({\'display\' : \'block\'});" style="background:transparent;border:6px solid #ABABAB;border-radius:3px;padding:1px;margin:3px 3px 3px 0;" class="clickable" src="'.$img_src.'" /><div style="display: none;"><iframe width="640" height="360" src="http://xuy.tv/embed/?id=\\1&autoplay=0&color=white&related=0" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen scrolling="no"></iframe></div>';
$text = preg_replace($patterns, $replacements, $text);
// End parse and replase xyu.tv
// Start parse and replase redtube.com
$patterns_red = '~https?:\/\/(?:[\w]+\.)*redtube\.com/([0-9]{1,7})(.*)~ix';
if(preg_match_all($patterns_red, $text, $out, PREG_SET_ORDER)) $img_src = getMediaThumb($out[0][1], 'red');
$patterns = '~https?:\/\/(?:[\w]+\.)*redtube\.com/([0-9]{1,7})(.*)~ix';
$replacements = '<img width="120px" height="90px" onclick="$(this).hide(); $(this).next(\'div\').css({\'display\' : \'block\'});" style="background:transparent;border:6px solid #ABABAB;border-radius:3px;padding:1px;margin:3px 3px 3px 0;" class="clickable" src="'.$img_src.'" /><div style="display: none;"><object height="360" width="640"><param name="allowfullscreen" value="true"><param name="AllowScriptAccess" value="always"><param name="movie" value="http://embed.redtube.com/player/"><param name="FlashVars" value="id=\\1&style=redtube&autostart=false"><embed src="http://embed.redtube.com/player/?id=\\1&style=redtube" allowfullscreen="true" AllowScriptAccess="always" flashvars="autostart=false" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" height="360" width="640" /></object></div>';
$text = preg_replace($patterns, $replacements, $text);
// End parse and replase redtube.com
// Start parse and replase xhamster.com
$patterns_xhm = '~https?:\/\/(?:[\w]+\.)*xhamster\.com/movies/([0-9]{1,10})/?(.*?)~ix';
if(preg_match_all($patterns_xhm, $text, $out, PREG_SET_ORDER)) $img_src = getMediaThumb($out[0][1], 'xhamster');
$patterns = '~https?:\/\/(?:[\w]+\.)*xhamster\.com/movies/([0-9]{1,10})/?(.*)~ix';
$replacements = '<img width="120px" height="90px" onclick="$(this).hide(); $(this).next(\'div\').css({\'display\' : \'block\'});" style="background:transparent;border:6px solid #ABABAB;border-radius:3px;padding:1px;margin:3px 3px 3px 0;" class="clickable" src="'.$img_src.'" /><div style="display: none;"><iframe width="640" height="360" src="http://xhamster.com/xembed.php?video=\\1" frameborder="0" scrolling="no"></iframe></div>';
$text = preg_replace($patterns, $replacements, $text);
// End parse and replase xhamster.com
return $text;
}