<?
function img_preg($arr)
{
	if (!preg_match('/exit/i', $arr[1]))
	return '<img style="max-width:100px;" src="' . text($arr[1]) . '" alt="img" />';
	else
	return '<img style="max-width:100px;" src="/style/no_image.png" alt="No Image" />';	
}

function links_preg1($arr)
{
	global $set;

	if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[1]) || !preg_match('#://#',$arr[1]))
	return '<a href="' . $arr[1] . '">' . $arr[2] . '</a>';
	else
	return '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[1])) . '">' . $arr[2] . '</a>';

}

function vremja_duration ($duration = 0)
{
	$hours = floor($duration / 3600);
	if ($hours > 0)$hours = $hours . ':';
	else $hours = null;
	$min = floor($minutes = ($duration / 3600 - $hours)*60) . ':';
	$sec = $seconds = ceil(($minutes - floor($minutes)) * 60);
	return $hours . $min . $sec;
} 

function links_preg2($arr)
{
	global $set;
	if (preg_match('#^http://' . preg_quote($_SERVER['HTTP_HOST']) . '#',$arr[2]))
	return $arr[1] . '<a href="' . $arr[2] . '">' . $arr[2] . '</a>' . $arr[4];
	elseif (preg_replace('#http://(.*)(v=)#isU', '', $arr[2]))
    {
        $code_video = preg_replace('#http://(.*)(v=)#isU', '', $arr[2]); 
        
        if (is_file(H.'sys/cache/youtube/video-' . $code_video . '.dat')){
            $array = unserialize(file_get_contents(H.'sys/cache/youtube/video-' . $code_video . '.dat'));
        } else {
            if (strlen2($code_video) == 11) {
            	$code_video = text($code_video);
				
            	
            	$file = @file_get_contents('http://gdata.youtube.com/feeds/api/videos/' . $code_video);
            	$entry = new SimpleXMLElement($file);

            	// Продолжительность видео 
            	$durationEl = @reset($entry->xpath('media:group/yt:duration[@seconds]'));
            	$seconds = (int) $durationEl->attributes()->seconds;

            	/*
            	* Если не писали название видео
            	* то берем его с Youtube
            	*/

            	if (!isset($err))
            	{
            		$file = file_get_contents('http://gdata.youtube.com/feeds/api/videos/' . $code_video);
                    $screen = 'http://i1.ytimg.com/vi/' . $code_video . '/default.jpg';
            		$name = preg_replace("#(<)(.*)(<title type='text'>)#isU", "", $file); 
            		$name = preg_replace('#(</title>)(.*)(</entry>)#isU', '', $name); 
            		$name = htmlspecialchars($name);
            		
            		if ($name)
            		{
            			$array = array('name' => $name, 'code' => $code_video, 'seconds' => $seconds);
                      @file_put_contents(H.'sys/cache/youtube/video-' . $code_video . '.dat', serialize($array));
                        
                  if ($screen)
			                @copy($screen, H.'sys/cache/youtube/video-' . $code_video . '.jpg');
            		}
            	}
            }
        }
        
        if (isset($array)){
            return '<a id="youtube-' . text($array['code']) . '" href="' . $arr[2] . '" ><div style="position: relative; padding: 0; margin: 0; width: 128px;"><img src="/sys/cache/youtube/video-' . text($array['code']) . '.jpg" style="width: 128px; padding: 0; margin: 0;" /><span style="color: #fff; background: #000; padding: 2px; font-size: small; position: absolute; bottom: 0px; right: 0px;">' . vremja_duration($array['seconds']) . '</span></div><img src="/plugins/video/img/video.png" /> ' . text($array['name']) . '</a><br />';
        }
         else
        return '<a href="' . $arr[2] . '">' . $code_video . '</a>';
    }
    else
    {
        return $arr[1] . '<a' . ($set['web'] ? ' target="_blank"' : null) . ' href="http://' . $_SERVER['HTTP_HOST'] . '/go.php?go=' . base64_encode(html_entity_decode($arr[2])) . '">Ссылка</a>' . $arr[4];
    }
	
}

function links($msg)
{
	global $set;
	if ($set['bb_img'])$msg = preg_replace_callback('/\[img\](.+)\[\/img\]/isU', 'img_preg', $msg);
	if ($set['bb_url'])$msg = preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU', 'links_preg1', $msg);
	if ($set['bb_http'])$msg = preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu', 'links_preg2', $msg);
	return $msg;
}
?>