<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function url2html ($message, $minorArea=1)
{
    $msgs = explode("&nbsp;", $message);
    foreach ($msgs as $key => $msg)
    {
        $msgs[$key] = trim(preg_replace("/(?<!&quot;)(?<=[^\]a-z0-9-=\"'\\/])(https?:\/\/)([a-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)/ie", "url2html_url('\\0', $minorArea)", ' '.$msg));
    }
    $message = implode("&nbsp;", $msgs);
    $msgs = explode("&nbsp;", $message);
    foreach ($msgs as $key => $msg)
    {
        $msgs[$key] = trim(preg_replace("/(?<!&quot;)(?<=[^\]\>a-z0-9-=\"'\\/])(https?:\/\/)([a-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)/ie", "url2html_link('\\0')", ' '.$msg));
    }
    $message = implode("&nbsp;", $msgs);
    return $message;
}

function url2html_url ($url, $minorArea=1) {
    $url = str_replace("&amp;", "&", $url);
    $url_section = parse_url($url);
    $host = $url_section["host"];
    $path = $url_section["path"];
    $query = $url_section["query"];
    if (preg_match("/\.(?:jpe?g|png|gif)$/i", $path, $matches))
    {
        $html = url2html_img($url);    
    }
    elseif (preg_match("/\.pdf$/i", $path, $matches))
    {
        $html = url2html_pdf($url, $minorArea);    
    }
    elseif (preg_match("/\.(mp3|wav|wma)$/i", $path, $matches))
    {
        $html = url2html_audio($url);    
    }
    elseif (preg_match("/\.(mp4|webm|ogv|flv|wmv)$/i", $path, $matches))
    {
        $html = url2html_video($url);    
    }
    elseif (preg_match("/\byoutube\.com$/i", $host, $matches))
    {
        $html = url2html_youtube($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\byoutu\.be$/i", $host, $matches))
    {
        $html = url2html_youtube($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\bdailymotion\.com$/i", $host, $matches))
    {
        $html = url2html_dailymotion($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\bdai\.ly$/i", $host, $matches))
    {
        $html = url2html_dailymotion($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\bvimeo\.com$/i", $host, $matches))
    {
        $html = url2html_vimeo($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/(^|\.)56\.com$/i", $host, $matches))
    {
        $html = url2html_56($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\bbilibili\.com$/i", $host, $matches))
    {
        $html = url2html_bilibili($url, $minorArea, $path, $query);
    }
    // http://static.hdslb.com/miniloader.swf?aid=1286985&page=2
    elseif (preg_match("/static\.hdslb\.com$/i", $host, $matches))
    {
        $html = url2html_bilibili_swf($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\.ku6\.com$/i", $host, $matches))
    {
        $html = url2html_ku6($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/v\.ifeng\.com$/i", $host, $matches))
    {
        $html = url2html_ifeng($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/player\.video\.qiyi\.com$/i", $host, $matches))
    {
        $html = url2html_iqiyi($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/v\.qq\.com$/i", $host, $matches)) 
    {
        $html = url2html_qq($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/video\.sina\.com\.cn$/i", $host, $matches))
    {
        $html = url2html_sina($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/share\.vrs\.sohu\.com$/i", $host, $matches))
    {
        $html = url2html_sohu($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\btudou\.com$/i", $host, $matches))
    {
        $html = url2html_tudou($url, $minorArea, $path, $query);
    }
    elseif (preg_match("/\byouku\.com$/i", $host, $matches))
    {
        $html = url2html_youku($url, $minorArea, $path, $query);
    }
    // http://xlkezhan.com/wizard/posts/Iz1v9ib9wmrHmmxDiS4y.html
    elseif (preg_match("/\bxlkezhan\.com$/i", $host, $matches))
    {
        if (preg_match("/^\/wizard\/posts\/([a-z0-9\_\-\=]+.html)$/i", $path, $matches))
        {
            $html = url2html_xiaojingling($url);  
        }
    }
    elseif (preg_match("/\.swf$/i", $url, $matches))
    {
        $html = url2html_swf($url);
    }
    if (!isset($html)) $html = $url;
    return str_replace(array("\t", "\r\n", "\n", "   ", "  "), ' ', $html);
}

function url2html_img($url) {
    return "<a href='$url' target='_blank' style='vertical-align: top;'>
            <img src='$url' /></a>";
}

function url2html_pdf($url, $minorArea) {
    $width = $minorArea?400:740;
    $height = $minorArea?250:926;
    if (_url2html_isNoFlashDevice()) $url = "https://docs.google.com/gview?url=".$url."&embedded=true";
    return '<iframe src="'.$url.'" frameborder=0 width='.$width.' height='.$height.'></iframe>';
}

// e.g. http://www.vsread.com/bbsuploadimg/2012-02-09/20120209205733_84209.swf
function url2html_swf($url, $width=null, $height=null) {
    if (_url2html_isNoFlashDevice()) return null;
    if (!isset($width) || !isset($height)) {
        list($width, $height) = getimagesize($url);
    }
    return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
             codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,18,0"
             width="'.$width.'"  height="'.$height.'" align="middle">
                 <param name="allowFullScreen" value="true" />
                 <param name="movie" value="'.$url.'" />
                 <embed src="'.$url.'" allowFullScreen="true" width="'.$width.'" height="'.$height.'"
                  align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
            </object>';
}

function url2html_audio($url) {
    $randomid = 'audio_'.random(3);
    return '<audio id="'.$randomid.'" controls preload width="180" src="'.$url.'"></audio>
            <script type="text/javascript">jqm("#'.$randomid.'").mediaelementplayer({audioVolume: "vertical"});</script>';
}

function url2html_video($url) {
    $randomid = 'video_'.random(3);
    return '<video id="'.$randomid.'" controls preload src="'.$url.'"></video>
            <script type="text/javascript">jqm("#'.$randomid.'").mediaelementplayer();</script>';
}

// e.g. https://www.youtube.com/watch?v=MD5bFCDfySc video page 
// e.g. https://m.youtube.com/watch?v=MD5bFCDfySc  video page
// e.g. https://youtu.be/MD5bFCDfySc video page
// e.g. https://www.youtube.com/embed/MD5bFCDfySc  video
// e.g. https://www.youtube.com/v/MD5bFCDfySc video
// $vid = MD5bFCDfySc
function url2html_youtube($url, $minorArea, $path, $query) {
    // https://www.youtube.com/embed/MD5bFCDfySc -- video url
    // https://www.youtube.com/v/MD5bFCDfySc -- video url
    if (preg_match("/^\/(embed|v)\/([a-z0-9\_\-]+)$/i", $path, $matches))
    {
        $vid = $matches[2];
    }
    // https://www.youtube.com/watch?v=MD5bFCDfySc  -- video page url
    elseif (preg_match("/^\/watch$/i", $path, $matches))
    {
        parse_str($query, $my_array_of_vars);
        $vid = $my_array_of_vars["v"];
    }
    // https://youtu.be/MD5bFCDfySc  -- video page url
    elseif (preg_match("/^\/([a-z0-9\_\-]+)$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.5625);
    $randomid = 'youtube_'.random(3);
    return '<video id="'.$randomid.'" width="'.$width.'" height="'.$height.'">
                <source src="https://www.youtube.com/watch?v='.$vid.'" type="video/youtube"></video>
                <script>jqm("#'.$randomid.'").mediaelementplayer();</script>';
}

// e.g. http://www.dailymotion.com/video/x4xvnz_the-funny-crash-compilation_fun video page
// e.g. http://dai.ly/x4xvnz video page
// e.g. http://www.dailymotion.com/embed/video/x4xvnz video
// $vid = x4xvnz
function url2html_dailymotion($url, $minorArea, $path, $query) {
    // http://www.dailymotion.com/embed/video/x4xvnz
    // http://www.dailymotion.com/video/x4xvnz_the-funny-crash-compilation_fun
    if (preg_match("/^\/embed\/video\/([a-z0-9]+)$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    elseif (preg_match("/^\/video\/([a-z0-9]+)/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    // http://dai.ly/x4xvnz
    elseif (preg_match("/^\/([a-z0-9]+)$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.5625);
    return '<iframe src="//www.dailymotion.com/embed/video/'.$vid.'"
            frameborder=0 allowfullscreen allowtransparency width='.$width.' height='.$height.'></iframe>';
}

// e.g. https://vimeo.com/110145104 video page
// e.g. https://player.vimeo.com/video/110145104 video
function url2html_vimeo($url, $minorArea, $path, $query) {
    // https://vimeo.com/110145104
    if (preg_match("/^\/(\d+)$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    // https://player.vimeo.com/video/110145104
    elseif (preg_match("/\/video\/(\d+)$/i", $path, $matches)) {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.5625);
    return '<iframe src="https://player.vimeo.com/video/'.$vid.'" width="'.$width.'" height="'.$height.'" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}

// e.g. http://www.56.com/u75/v_MTE3ODg5ODgw.html video page
// e.g. http://www.56.com/iframe/MTE3ODg5ODgw video
// http://www.56.com/w17/play_album-aid-14393442_vid-MTQwNTU5NDg2.html is not supported, since it is actually sohu video (56 now belongs to sohu)
function url2html_56($url, $minorArea, $path, $query) {
    // http://www.56.com/iframe/MTE3ODg5ODgw
    if (preg_match("/^\/iframe\/([a-z0-9\_\-\=]+)$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    // http://www.56.com/u75/v_MTE3ODg5ODgw.html
    elseif (preg_match("/\/v_([a-z0-9]+)\.html$/i", $path, $matches)) {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.625);
    return '<iframe src="http://www.56.com/iframe/'.$vid.'"
            frameborder=0 allowfullscreen allowtransparency width='.$width.' height='.$height.'></iframe>';
}

// e.g. http://www.bilibili.com/video/av1286985 video page
// e.g. http://www.bilibili.com/video/av1286985/index_2.html video page
// e.g. http://static.hdslb.com/miniloader.swf?aid=1286985&page=2 flash
// $aid = 1286985, $page = 2 (default 1)
function url2html_bilibili($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    $page = 1;
    if (preg_match("/^\/video\/av(\d+)\/index_(\d+)\.html$/i", $path, $matches))
    {
        $vid = $matches[1];
        $page = $matches[2];
    }
    elseif (preg_match("/^\/video\/av(\d+)/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.75);
    return '<embed width="'.$width.'" height="'.$height.'" quality="high" allowfullscreen="true" type="application/x-shockwave-flash" src="http://static.hdslb.com/miniloader.swf" flashvars="aid='.$aid.'&page='.$page.'" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
}

function url2html_bilibili_swf($url, $minorArea, $path, $query) {
    if ($path!='/miniloader.swf') return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.75);
    return url2html_swf($url, $width, $height);
}

// e.g. http://v.ifeng.com/documentary/figure/201605/0362ac12-fd5d-4a16-b836-378c61543ca0.shtml video page
// e.g. http://v.ifeng.com/include/exterior.swf?guid=0362ac12-fd5d-4a16-b836-378c61543ca0&AutoPlay=false video
function url2html_ifeng($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.8125);
    if (preg_match("/\.swf$/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }
    if (preg_match("/\/([a-z0-9\-]+)\.shtml$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    return url2html_swf('http://v.ifeng.com/include/exterior.swf?guid='.$vid.'&AutoPlay=false', $width, $height);
}

// e.g. http://player.video.qiyi.com/4b40896a102b47584f9573b76b093fa3/0/0/v_19rrlty944.swf-albumId=482534600-tvId=482534600-isPurchase=0-cnId=25
function url2html_iqiyi($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.73);
    if (preg_match("/\.swf/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }
    return null;
}

// e.g. http://v.ku6.com/show/Es8Bn14RwCZKOS7Lz4ts3Q...html?hpsrc=1_25_1_2_0
// e.g. http://player.ku6.com/refer/Es8Bn14RwCZKOS7Lz4ts3Q../v.swf
// e.g. http://v.ku6.com/special/show_2562886/SoGnlFVxjStPTJmQ.html
function url2html_ku6($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.73);
    if (preg_match("/\/v\.swf$/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }
    if (preg_match("/\/show\/([^\/]+)\.html$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    elseif (preg_match("/\/special\/show_\d+\/([^\/]+)\.html$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    return url2html_swf('http://player.ku6.com/refer/'.$vid.'/v.swf', $width, $height);
}

// e.g. http://v.qq.com/page/q/v/6/q0149bzkuv6.html video page
// e.g. http://v.qq.com/cover/4/44q2pgwf0tb6hsq.html?vid=y0016wcg0ot video page
// e.g. http://v.qq.com/iframe/player.html?vid=q0149bzkuv6 video
// $vid = q0149bzkuv6
// e.g. http://m.v.qq.com/play/play.html?coverid=&vid=z0197m8xrv3 video page
function url2html_qq($url, $minorArea, $path, $query) {
    // http://v.qq.com/page/q/v/6/q0149bzkuv6.html
    if (preg_match("/\/page\/.*\/([a-z0-9\_\-\=]+)\.html$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    // http://v.qq.com/cover/4/44q2pgwf0tb6hsq.html?vid=y0016wcg0ot
    // http://v.qq.com/iframe/player.html?vid=q0149bzkuv6
    // http://m.v.qq.com/play/play.html?coverid=&vid=z0197m8xrv3
    else
    {
        parse_str($query, $my_array_of_vars);
        $vid = $my_array_of_vars["vid"];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.625);
    return '<iframe src="http://v.qq.com/iframe/player.html?vid='.$vid.'&auto=0"
            frameborder=0 allowfullscreen allowtransparency width='.$width.' height='.$height.'></iframe>';       
}

// e.g. http://video.sina.com.cn/view/250587748.html video page
// e.g. http://video.sina.com.cn/share/video/250587748.swf video
// $vid = 250587748
function url2html_sina($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.77);

    if (preg_match("/\.swf$/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }

    if (preg_match("/^\/view\/(\d+)\.html$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    return url2html_swf('http://video.sina.com.cn/share/video/'.$vid.'.swf', $width, $height);
}

// e.g. http://share.vrs.sohu.com/3029619/v.swf&topBar=1&autoplay=false&plid=1000083&pub_catecode=0&from=page
function url2html_sohu($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.625);

    if (preg_match("/\/v\.swf/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }
    return null;
}

// e.g. http://www.tudou.com/programs/view/xGgArUMPw9I video page
// e.g. http://www.tudou.com/listplay/Q0MiBE2DPCs/NQjV-Amh1ac.html video page
// e.g. http://www.tudou.com/programs/view/html5embed.action?code=xGgArUMPw9I video
// $vid = xGgArUMPw9I
// e.g. http://www.tudou.com/l/Q0MiBE2DPCs/&iid=132813935&resourceId=0_04_05_99/v.swf video
function url2html_tudou($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.625);
    
    if (preg_match("/\/v\.swf$/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }

    // http://www.tudou.com/programs/view/xGgArUMPw9I
    // http://www.tudou.com/listplay/Q0MiBE2DPCs/NQjV-Amh1ac.html
    if (preg_match("/\/([a-z0-9\_\-\=]+)(\.html)?$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    // http://www.tudou.com/programs/view/html5embed.action?code=xGgArUMPw9I
    elseif ($path=='/programs/view/html5embed.action')
    {
        parse_str($query, $my_array_of_vars);
        $vid = $my_array_of_vars["code"];
    }
    if (!isset($vid)) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.625);
    $extra = $minorArea?'':'&quality=high';
    return '<iframe src="http://www.tudou.com/programs/view/html5embed.action?code='.$vid.$extra.'"
            frameborder=0 allowfullscreen allowtransparency width='.$width.' height='.$height.'></iframe>';       
}

// e.g. http://v.youku.com/v_show/id_XMTgyMzE0NzE2.html?from=y1.6-97.3.1.cbfbda86962411de83b1
// e.g. http://v.youku.com/v_show/id_XMTgyMzE0NzE2.html
// e.g. http://player.youku.com/embed/XMTgyMzE0NzE2
// e.g. http://player.youku.com/player.php/sid/XMTgyMzE0NzE2/v.swf
function url2html_youku($url, $minorArea, $path, $query) {
    if (_url2html_isNoFlashDevice()) return null;
    list($width, $height) = _url2html_get_display_dimension($minorArea, 0.625);

    // http://player.youku.com/player.php/sid/XMTgyMzE0NzE2/v.swf
    if (preg_match("/\/v\.swf$/i", $path, $matches))
    {
        return url2html_swf($url, $width, $height);
    }

    // http://player.youku.com/embed/XMTgyMzE0NzE2
    if (preg_match("/^\/embed\/([a-z0-9\_\-\=]+)$/i", $path, $matches))
    {
        $vid = $matches[1];
    }
    // http://v.youku.com/v_show/id_XMTgyMzE0NzE2.html
    elseif (preg_match("/^\/v_show\/id_([a-z0-9\_\-\=]+)\.html$/i", $path, $matches)) {
        $vid = $matches[1];
    }
    if (!isset($vid)) return null;
    return '<iframe src="http://player.youku.com/embed/'.$vid.'"
             frameborder=0 allowfullscreen allowtransparency width='.$width.' height='.$height.'></iframe>';       
}

//http://xlkezhan.com/wizard/posts/Iz1v9ib9wmrHmmxDiS4y.html
function url2html_xiaojingling($url) {
    return '<iframe src="'.$url.'" 
            frameborder=0 width=810 height=470 scrolling="no"></iframe>';       
}

function url2html_link($url) {
    $surl = addslashes($url);
    return "<a href=\"$surl\" target=\"_blank\">$url</a>";
}

function _url2html_get_display_dimension($minorArea, $ratio) {
    $width = $minorArea?400:640;
    $height = $width * $ratio;
    return array($width, $height);
}

// http://stackoverflow.com/questions/6322112/check-if-php-page-is-accessed-from-an-ios-device
function _url2html_isNoFlashDevice() {
    $iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
    $webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
    return $iPod || $iPhone || $iPad || $Android || $webOS;
}
?>