<?php

/**
 * [Discuz!] (C)2001-2099 Discuz! Team
 * This is NOT a freeware, use is subject to license terms
 * https://license.discuz.vip
 */

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

class media_bilibili {

	public static $version = '1.0';
	public static $name = 'bilibili';
	public static $checkurl = ['bilibili.com/video/', 'bilibili.tv/video/', 'acg.tv', 'b23.tv'];

	public static function parse($url, $width, $height) {
		if(preg_match('/https?:\/\/(m.|www.|)bilibili.(com|tv)\/video\/(a|b)v([A-Za-z0-9]+)(\/?.*?&p=|\/?\?p=)?(\d+)?/i', $url, $matches)) {
			$vid = (is_numeric($matches[4]) ? 'aid='.$matches[4] : 'bvid='.$matches[4]).(empty($matches[6]) ? '' : '&page='.intval($matches[6]));
			$flv = '';
			$iframe = 'https://player.bilibili.com/player.html?'.$vid.'&autoplay=0';
			$imgurl = '';
		} else if(preg_match('/https?:\/\/(www.|)(acg|b23).tv\/(a|b)v([A-Za-z0-9]+)(\/?.*?&p=|\/?\?p=)?(\d+)?/i', $url, $matches)) {
			$vid = (is_numeric($matches[4]) ? 'aid='.$matches[4] : 'bvid='.$matches[4]).(empty($matches[6]) ? '' : '&page='.intval($matches[6]));
			$flv = '';
			$iframe = 'https://player.bilibili.com/player.html?'.$vid.'&autoplay=0';
			$imgurl = '';
		}
		return [$flv, $iframe, $url, $imgurl];
	}

}