Here a new version under a New BSD License, a lot more has been done. =)
<?php
/**
* Autoembed MyCode Plugin for MyBB
* Allows multiple video services to be embedded by just one tag [vid][/vid]
*
* Copyright (c) 2009, Christopher John Jackson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the cj-jackson.com nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$plugins->add_hook('toolbar_container_collect', 'autoembed_collect');
$plugins->add_hook("parse_message", "autoembed_mycode");
$plugins->add_hook("pre_output_page", "autoembed_javascript");
function autoembed_info()
{
return array(
"name" => "Autoembed MyCode Plugin for MyBB",
"description" => "Allows multiple videos services to be embedded by just one tag [vid][/vid]'",
"website" => "http://www.autoembed.com/",
"author" => "Christopher John Jackson",
"authorsite" => "http://cj-jackson.com/",
"version" => "1.3.0",
"guid" => "a1f7fb491448e5b0300944ab4f6371f6",
"compatibility" => "*"
);
}
function autoembed_activate()
{
//Do nothing
}
function autoembed_deactivate()
{
//Do nothing
}
class AutoEmbedMyBB
{
private $_width;
private $_height;
private $_url;
private $_data;
private $_AE;
public function __construct()
{
if(file_exists(MYBB_ROOT."AutoEmbed.class.php"))
{
include_once MYBB_ROOT."AutoEmbed.class.php";
$this->_AE = new AutoEmbed();
} else {
$this->_AE = false;
}
}
public function setData($data){
$this->_data = $data;
}
public function mycodeReplace()
{
$data = $this->_data;
$data = preg_replace_callback("#\[vid\](.+?)\[/vid\]#i", array(&$this,"_embedVideo"), $data);
$data = preg_replace_callback("#\[vid ([0-9]{1,3}?)x([0-9]{1,3}?)\](.+?)\[/vid\]#i", array(&$this,"_embedVideoBySize"), $data);
$this->_data = $data;
return $this->_data;
}
private function _embedVideo($matches)
{
$this->_width = -1;
$this->_height = -1;
$this->_url = $matches[1];
return $this->_getHTMLcode();
}
private function _embedVideoBySize($matches)
{
$this->_width = $matches[1];
$this->_height = $matches[2];
$this->_url = $matches[3];
$this->_checkSize();
return $this->_getHTMLcode();
}
private function _checkSize()
{
if($this->_width < 240 || $this->_height < 240 || $this->_width > 860 || $this->_height > 600){
$this->_width = -1;
$this->_height = -1;
}
}
private function _getHTMLcode()
{
if(!preg_match("#(^http://|^https://)#i",$this->_url))
{
return $this->_errorHandler("Not a valid URL");
}
if($this->_jwplayerCompatibilityCheck())
{
$data = $this->_getJWPlayerCode();
} elseif($this->_windowsMediaCompatibilityCheck()) {
$data = $this->_getWindowsMediaCode();
} else {
$data = $this->_getAutoEmbedCode();
}
return '<div class="autoembed">'.$data.'</div>';
}
private function _jwplayerCompatibilityCheck()
{
$swfPlayerExists = file_exists(MYBB_ROOT."jwplayer.swf");
$fileinfo = parse_url($this->_url);
$fileinfo = pathinfo($fileinfo['path']);
$extention = $fileinfo['extension'];
if(preg_match("#^(flv|f4v|mp4|m4a|m4v|aac|mp3)$#i",$extention)){
$fileCompatible = true;
if(preg_match("#^(m4a|aac|mp3)$#i",$extention)){
$this->_width = 300;
$this->_height = 24;
}
} else {
$fileCompatible = false;
}
return $swfPlayerExists && $fileCompatible;
}
private function _windowsMediaCompatibilityCheck()
{
$wmPlayerExists = file_exists(MYBB_ROOT."wmvplayer.xaml");
$fileinfo = parse_url($this->_url);
$fileinfo = pathinfo($fileinfo['path']);
$extention = $fileinfo['extension'];
if(preg_match("#^(wmv|wma|asf)$#i",$extention)){
$fileCompatible = true;
if(preg_match("#^(wma)$#i",$extention)){
$this->_width = 300;
$this->_height = 20;
}
} else {
$fileCompatible = false;
}
return $wmPlayerExists && $fileCompatible;
}
private function _errorHandler($string)
{
$uri = $_SERVER['REQUEST_URI'];
if(preg_match("#(processed=1$)#i",$uri)){
return $string;
} else {
return "...";
}
}
private function _getJWPlayerCode()
{
if($this->_width == -1){
$this->_width = 425;
$this->_height = 344 + 24;
} else {
$this->_height = $this->_height + 24;
}
$image = $this->_url.".jpg";
$image = preg_replace("#^http://#","http/",$image);
$image = preg_replace("#^https://#","https/",$image);
$data = '<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="'.$this->_width.'" height="'.$this->_height.'">';
$data .= '<param name="movie" value="jwplayer.swf" />';
$data .= '<param name="allowfullscreen" value="true" />';
$data .= '<param name="flashvars" value="file='.urlencode($this->_url).'&image=imagepreview.php/'.$image.'" />';
$data .= '<embed ';
$data .= 'type="application/x-shockwave-flash" ';
$data .= 'id="player2" ';
$data .= 'name="player2" ';
$data .= 'src="jwplayer.swf" ';
$data .= 'width="'.$this->_width.'" ';
$data .= 'height="'.$this->_height.'" ';
$data .= 'allowfullscreen="true" ';
$data .= 'flashvars="file='.urlencode($this->_url).'&image=imagepreview.php/'.$image.'" ';
$data .= '/>';
$data .= '</object>';
return $data;
}
private function _getWindowsMediaCode()
{
if($this->_width == -1){
$this->_width = 425;
$this->_height = 344 + 20;
} else {
$this->_height = $this->_height + 20;
}
$image = $this->_url.".jpg";
$image = preg_replace("#^http://#","http/",$image);
$image = preg_replace("#^https://#","https/",$image);
$hash = hash('ripemd160',microtime());
$data = '<div name="'.$hash.'" id="'.$hash.'"></div>';
$data .= '<script type="text/javascript">';
$data .= 'var cnt = document.getElementById("'.$hash.'");';
$data .= 'var src = \'wmvplayer.xaml\';';
$data .= 'var cfg = {';
$data .= ' file:\''.$this->_url.'\',';
$data .= ' image:\'imagepreview.php/'.$image.'\',';
$data .= ' height:\''.$this->_height.'\',';
$data .= ' width:\''.$this->_width.'\'';
$data .= '};';
$data .= 'var ply = new jeroenwijering.Player(cnt,src,cfg);';
$data .= '</script> ';
return $data;
}
private function _getAutoEmbedCode()
{
if($this->_AE){
if($this->_AE->parseUrl($this->_url)){
if($this->_width != -1){
$this->_AE->setWidth($this->_width);
$this->_AE->setHeight($this->_height);
}
$thestub = $this->_AE->getStub();
if($thestub['title'] != 'Last.fm (Audio)'){
$this->_AE->setParam('wmode','window');
$this->_AE->setObjectParam('wmode','window');
}
$data = $this->_AE->getEmbedCode();
$data .= "<!-- Autoembed Class -->";
} else {
$data = "";
}
} else {
$data = file_get_contents("http://autoembed.com/api/index.php?url=".urlencode($this->_url));
if($data != "")
{
if(!preg_match("#last\.fm/webclient#",$data)){
$data = str_replace("transparent","window",$data);
}
if($this->_width != -1){
$data = preg_replace("#width=\"([0-9]+)\"#","width=\"".$this->_width."\"",$data);
$data = preg_replace("#height=\"([0-9]+)\"#","height=\"".$this->_height."\"",$data);
}
$data .= "<!-- Autoembed API -->";
}
}
if($data == "")
{
$data = '<a href="'.urlencode($this->url).'">'.urlencode($this->url).'</a>';
}
return $data;
}
}
function autoembed_mycode($message)
{
$autoembedmycode = new AutoEmbedMyBB();
$autoembedmycode->setData($message);
return $autoembedmycode->mycodeReplace();
}
function autoembed_javascript($page)
{
if(file_exists(MYBB_ROOT."wmvplayer.xaml"))
{
$page = str_replace("</head>","<script type='text/javascript' src=\"jscripts/silverlight.js\"></script>\n<script type='text/javascript' src=\"jscripts/wmvplayer.js\"></script>\n</head>",$page);
}
return $page;
}
function autoembed_collect(&$ic) {
$callback = "
var ask_loc = prompt('Enter a url from video sharing site such as youtube or directly to a video file URL\\ne.g http://www.youtube.com/watch?v=xxxxxx or http://www.example.com/video.mp4', '');
if (ask_loc == null) return;
var ask_resolution = prompt('Enter resolution eg: 800x600, if you do wish to specify then click cancel or leave blank!', '');
var if_resolution = ask_resolution.match(/(^[0-9]{1,3})x([0-9]{1,3}$)/);
if ((ask_resolution != null || ask_resolution != '') && if_resolution != null){
clickableEditor.performInsert('[vid ' + ask_resolution + ']' + ask_loc + '[/vid]', '', true, false);
} else {
clickableEditor.performInsert('[vid]' + ask_loc + '[/vid]', '', true, false);
}
";
$toolbar = array(
'id' => 'autoembed',
'name' => 'autoembed',
'image' => 'autoembed.gif',
'callback' => $callback,
'title' => 'Embed video from sharing site from URL'
);
$ic->collect_item($toolbar);
}
?>