对接时间戳防盗链功能

EFV开启时间戳防盗链后,苹果CMS默认是用不了,这里提供对接好的文件,可直接使用。

开启EFV时间戳功能

先前往EFV后台防盗链/图片加密设置,开启时间戳+播放次数防盗链,并设置时间戳防盗链key值,然后再看下该功能对接的一些参数介绍 ⇒ 传送门

修改苹果CMS相关文件

首先编辑苹果CMS源码的application/common/controller/All.php文件,在开头根据下面示例,在指定位置添加相关代码,如:

class All extends Controller
{
    var $_ref;
    var $_cl;
    var $_ac;
    var $_tsp;
    var $_url;

    public function __construct()
    {
        parent::__construct();
        $this->_ref = mac_get_refer();
        $this->_cl = request()->controller();
        $this->_ac = request()->action();
        $this->_tsp = date('Ymd');
    }
    
    // 在后面添加下面这一段代码
    protected function getKey($url, $from)
    {   $efvtokenKey = 'efvtoken'; //设置EFV时间戳防盗链KEY值,修改efvtoken值即可
        if (strpos($url, "/movie") !== false) {
            $removedProtocolAndDomain = preg_replace('#^(https?:)?(//)?#', '', $url);
            $removedDomain = substr($removedProtocolAndDomain, strpos($removedProtocolAndDomain, '/'));
            $removedMovieAndM3U8 = str_replace(['/movie', '.m3u8'], '', $removedDomain);
            $currentTimestamp = time() * 1000;
            $fiveMinutesLaterTimestamp = $currentTimestamp + (60 * 1000); //链接过期时间,默认60秒,如果你想缩短时间,修改60即可
            $combinedString = $removedMovieAndM3U8 . '&counts=2&timestamp=' . $fiveMinutesLaterTimestamp . $efvtokenKey;
            $encryptedString = md5($combinedString);
            $outurl = $url . '?counts=2&timestamp=' . $fiveMinutesLaterTimestamp . '&key=' . $encryptedString;
        } 
        else if (strpos($url, "/videos") !== false) {
            $removedProtocolAndDomain = preg_replace('#^(https?:)?(//)?#', '', $url);
            $removedDomain = substr($removedProtocolAndDomain, strpos($removedProtocolAndDomain, '/'));
            $currentTimestamp = time() * 1000;
            $fiveMinutesLaterTimestamp = $currentTimestamp + (60 * 1000); //链接过期时间,默认60秒,如果你想缩短时间,修改60即可
            $combinedString = $removedDomain . '&counts=2&timestamp=' . $fiveMinutesLaterTimestamp . $efvtokenKey;
            $encryptedString = md5($combinedString);
            $outurl = $url . '?counts=2&timestamp=' . $fiveMinutesLaterTimestamp . '&key=' . $encryptedString;
        }
        else {
            $outurl = $url;
        }
        if ($from == 'dplayer') {
            $outurl = urlencode($outurl);
        }
        return $outurl;
    }
    // 添加到这里结束

    protected function load_page_cache($tpl,$type='html')
    {
        if(defined('ENTRANCE') && ENTRANCE == 'index' && $GLOBALS['config']['app']['cache_page'] ==1  && $GLOBALS['config']['app']['cache_time_page'] ) {
          // 代码省略
        }
    }

&counts=2为链接读取多少次后过期,默认2次就过期,可自行修改,一起有4个地方,且最少设置为2,或者3次,有些手机浏览器会替换播放器,导致需要多加载1次,甚至2次才能播放。

接下来还是在All.php文件修改,修改如下:

$player_info['url'] = (string)$info[$listfun][$param['sid']]['urls'][$param['nid']]['url'];

// 可通过搜索上面的代码关键词定位到指定位置,再添加下面2行代码即可
$url = $this->getKey($player_info['url'], $player_info['from']);
$player_info['url'] = $url;
// 添加到这里结束

$player_info['url_next'] = (string)$info[$listfun][$param['sid']]['urls'][$param['nid']+1]['url'];

修改后基本算是对接完成,后面如果你关闭时间戳防盗链功能,需恢复源文件,以免播放出现问题。