Skip to content

Instantly share code, notes, and snippets.

@touhonoob
Created April 14, 2012 16:10
Show Gist options
  • Save touhonoob/2385478 to your computer and use it in GitHub Desktop.
Save touhonoob/2385478 to your computer and use it in GitHub Desktop.
Pixmicat youtube預覽模組,需搭配cURL & mod_bbcode
<?php
class mod_videoattach {
var $myPage;
function mod_videoattach() {
global $PMS;
$PMS->hookModuleMethod('ModulePage', 'mod_videoattach');
$this->myPage = $PMS->getModulePageURL('mod_videoattach');
}
function getModuleName() {
return 'mod_videoattach: ';
}
function getModuleVersionInfo() {
return 'v 1.0';
}
function autoHookHead(&$txt, $isReply) {
$txt .= '
<style>
#youtube_url{
width: 340px;
}
#youtube_check{
width: 60px;
}
</style>
<script type="text/javascript">
<!--
var sendbtn;
var fcom_inputed = false, fsub_inputed = false;
$("body").ready(function(){
sendbtn = $("input[name=sendbtn]");
$("#fcom").parent().parent().before(\'<tr><td class="Form_bg"><b>Youtube網址<input id="youtube_attach" type="checkbox" /></b></td><td><input disabled="disabled" type="text" name="youtube_url" id="youtube_url" /><input id="youtube_check" onclick="check_youtube()" type="button" value="檢查" disabled="disabled" /><div id="youtube_msg" style="color:red;font-weight:800;"></div><div id="youtube_preview"></div></td></tr> \');
$("textarea#fcom").keydown(function(){
fcom_inputed = true;
});
$("input#fsub").keydown(function(){
fsub_inputed = true;
});
$("input#youtube_attach").click(function(){
youtube_toggle();
});
});
function youtube_toggle(){
var status = $("input#youtube_attach").attr("checked");
status = (status=="checked"||status==true ? true : false)
sendbtn.attr("disabled",status);
$("input#noimg").attr("checked",status);
$("input#youtube_check").attr("disabled", !status);
$("input#youtube_url").attr("disabled", !status);
}
function youtube_error(msg){
$("#youtube_msg").html(msg);
}
$("#youtube_msg").ajaxError(function(){
youtube_error("意外的錯誤");
$("input#youtube_check").attr("disabled", false);
sendbtn.attr("disabled",true);
});
function show_youtube_preview(vid, height, width){
var player = \'<object style="height: \'+height+\'px; width: \' + width + \'px"> \
<param name="movie" value="http://www.youtube.com/v/\'+vid+\'?version=3&fs=1"> \
<param name="allowFullScreen" value="true"> \
<param name="allowScriptAccess" value="always"> \
<embed src="http://www.youtube.com/v/\'+vid+\'?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="\'+width+\'" height="\'+height+\'"> \
</object>\';
$("#youtube_preview").html(player);
}
function check_youtube(){
$("input#youtube_check").attr("disabled", true);
$("#youtube_msg").html("");
$("#youtube_preview").html("");
var youtube = $("#youtube_url").val();
$.get("' . html_entity_decode($this->myPage) . '", {youtube_check:encodeURI(youtube)}, function(data){
if(data != "0"){
var oembed = $.parseJSON(data);
show_youtube_preview(oembed.code, 360, 480);
var fsub = $("input#fsub");
if(fsub.val()=="" || !fsub_inputed){
fsub.val(oembed.title);
}
var fcom = $("textarea#fcom");
if(fcom.val()=="" || !fcom_inputed){
fcom.val("[youtube]" + oembed.code + "[/youtube]");
}else{
fcom.val(fcom.val() + "\n[youtube]" + oembed.code + "[/youtube]");
}
sendbtn.attr("disabled",false);
}else{
youtube_error("網址錯誤");
sendbtn.attr("disabled",true);
}
$("input#youtube_check").attr("disabled", false);
});
}
-->
</script>';
}
function ModulePage() {
if (isset($_GET['youtube_check'])) {
$ch = curl_init('http://www.youtube.com/oembed?url=' . urlencode($_GET['youtube_check']) . '&format=json');
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 8,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_HTTPGET => 1
));
$result = curl_exec($ch);
if ($result != '404 Not Found') {
$youtube_check = urldecode($_GET['youtube_check']);
if (preg_match('%(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?.*?v=([-|~_0-9A-Za-z]+)&?.*?)%', $youtube_check, $regs) > 0) {
$code = $regs[3] ? $regs[3] : $regs[4];
} else if (preg_match('%http://youtu\.be/([-|~_0-9A-Za-z]+)%', $youtube_check, $regs) > 0) {
$code = $regs[1];
} else {
$code = null;
}
if ($code != null) {
$json = json_decode($result, true);
$json['code'] = $code;
echo json_encode($json);
}else{
echo '0';
}
} else {
echo '0';
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment