<?php
/*
Plugin Name: embeded swf object Validator
Plugin URI: http://wppluginsj.sourceforge.jp/
Description: Embeded SWF objects are converted into the XHTML Valid tag. 
Version: 0.0.3
Author: wokamoto
Author URI: http://dogmap.jp/

 Released under the GPL license
  http://www.gnu.org/copyleft/gpl.html
*/

class embedSWFValidateController {
	var $video_param = array(
		'YouTube'     => array('match' => '/\.youtube\.com\//i',      'width' => 425, 'height' => 355),
		'metacafe'    => array('match' => '/\.metacafe\.com\//i',     'width' => 400, 'height' => 345),
		'liveleak'    => array('match' => '/\.liveleak\.com\//i',     'width' => 450, 'height' => 370),
		'googlevideo' => array('match' => '/video\.google\.com\//i',  'width' => 400, 'height' => 326),
		'dailymotion' => array('match' => '/\.dailymotion\.com\//i',  'width' => 420, 'height' => 330),
		'ifilm'       => array('match' => '/\.(ifilm|spike)\.com\//i','width' => 448, 'height' => 365),
	);

	function embedValidateController() {
	}

	function get_object_tag($param) {
		if (!is_array($param))
			return(false);

		if (!isset($param["vurl"]))
			$param["vurl"] = "";
		if (!isset($param["url"]))
			$param["url"] = "";
		if (!isset($param["title"]))
			$param["title"] = "";
		if (!isset($param["width"]))
			$param["width"] = 0;
		if (!isset($param["height"]))
			$param["height"] = 0;
		if (!isset($param["type"]))
			$param["type"] = "application/x-shockwave-flash";
		if (!isset($param["class"]))
			$param["class"] = "";
		if (!isset($param["style"]))
			$param["style"] = "";
		if (!isset($param["flashvars"]))
			$param["flashvars"] = "";

		$param_tags = array();

		foreach($this->video_param as $key => $val) {
			if (preg_match($this->video_param[$key]['match'], ($param["url"]!=''?$param["url"]:($param["vurl"]!=''?$param["vurl"]:$param["flashvars"])))) {
				if ($param["width"]  == 0)
					$this->video_param[$key]['width'];
				if ($param["height"] == 0)
					$this->video_param[$key]['height'];

				switch ($key) {
				case 'YouTube':
					if ($param["vurl"] != '') {
						$vid  = preg_replace("/^.*\.youtube\.com\/v\/(.*)$/i", "$1", $param["vurl"]);
						$param["url"]  = 'http://www.youtube.com/watch?v='.$vid;
					} else {
						$param["url"]  = preg_replace("/&.*$/i", "", $param["url"]);
						$vid  = preg_replace("/^.*\.youtube.*watch.*?v=(.*)$/i", "$1", $param["url"]);
						$param["vurl"] = 'http://www.youtube.com/v/'.$vid;
					}
					$param["flashvars"]='';
					break;

				case 'metacafe':
					if ($param["vurl"] != '' || $param["flashvars"] != '') {
						$vid  = preg_replace("/^.*\.metacafe.*fplayer\/(.*)\.swf$/i", "$1", $param["vurl"]);
						$param["url"]  = 'http://www.metacafe.com/watch/'.$vid.'/';
					} else {
						$vid  = preg_replace("/^.*\.metacafe.*watch\/(.*)$/i", "$1", preg_replace("/\/$/i", "", preg_replace("/\?.*$/", "", $param["url"])));
						$param["vurl"] = 'http://www.metacafe.com/fplayer/'.$vid.'.swf';
					}
					break;

				case 'liveleak':
					if ($param["vurl"] != '' || $param["flashvars"] != '') {
						$vid  = preg_replace("/^.*token=(.*)$/i", "$1", ($param["flashvars"]!=''?$param["flashvars"]:$param["vurl"]));
						$param["url"]  = 'http://www.liveleak.com/view?i='.$vid;
					} else {
						$vid  = preg_replace("/^.*\.liveleak.*view.*?i=(.*)$/i", "$1", $param["url"]);
						$param["vurl"] = 'http://www.liveleak.com/player.swf?autostart=false&amp;token='.$vid;
					}
					$param["flashvars"]='';
					$param_tags = array("quality"=>"high", "flashvars"=>$param["flashvars"]);
					break;

				case 'googlevideo':
					if ($param["vurl"] != '') {
						$vid  = preg_replace("/^.*\?docid=([^\&]*)[\&]?.*$/i", "$1", $param["vurl"]);
						$param["url"] = 'http://video.google.com/videoplay?docid='.$vid.'&amp;hl=en';
					} else {
						$vid  = preg_replace("/^.*video\.google\.com\/videoplay.*docid=(.*)$/i", "$1", $param["url"]);
						$param["vurl"] = 'http://video.google.com/googleplayer.swf?docId='.$vid.'&amp;hl=en';
					}
					$param["flashvars"]='';
					$param_tags = array("flashvars"=>$param["flashvars"]);
					break;

				case 'dailymotion':
					if ($param["vurl"] != '') {
						$param["vurl"] = preg_replace("/&.*$/i", "", $param["vurl"]);
						$vid  = preg_replace("/^.*\/swf\/(.*)$/i", "$1", $param["vurl"]);
						$param["url"]  = 'http://www.dailymotion.com/video/'.$vid;
					} else {
						$vid  = preg_replace("/^.*\.dailymotion\.com\/video\/([^_]*).*$/i", "$1", $param["url"]);
						$param["vurl"] = 'http://www.dailymotion.com/swf/'.$vid;
					}
					$param_tags = array("allowFullScreen"=>"true", "allowScriptAccess"=>"always");
					break;

				case 'ifilm':
					if ($param["flashvars"] != '') {
						$vid  = preg_replace("/^flvbaseclip=([^\&]*)\&$/i", "$1", $param["flashvars"]);
						$param["url"]  = 'http://www.spike.com/video/'.$vid;
					} else {
						$vid  = preg_replace("/^.*\.(ifilm|spike)\.com.*video\/(.*)$/i", "$2", preg_replace('/\/$/i', '', preg_replace('/\?.*$/', '', $param["url"])));
						$param["vurl"] = 'http://www.spike.com/efp';
					}
					$param["flashvars"] = "flvbaseclip=".$vid."&";
					$param_tags = array("flashvars"=>$param["flashvars"], "quality"=>"high", "bgcolor"=>"000000");
					break;

				default:
					break;

				}
				break;
			}
		}
		if ($param["title"] == '')
			$param["title"] = $param["url"];

		$param_tags = array_merge(array("movie"=>$param["vurl"], "wmode"=>"transparent"), $param_tags);
		$src   = '<object data="'.$param["vurl"].'" type="'.$param["type"].'" width="'.$param["width"].'" height="'.$param["height"].'"';
		if ($param["class"] != '')
			$src .= ' class="'.$param["class"].'"';
		if ($param["style"] != '')
			$src .= ' style="'.$param["style"].'"';
		$src .= '>';
		foreach($param_tags as $key => $val) {
			if ($val != '')
				$src .= '<param name="'.$key.'" value="'.$val.'" />';
		}
		if ($param["url"] != '')
			$src .= '<a href="'.$param["url"].'" title="'.$param["title"].'">'.$param["title"].'</a>';
		$src .= '</object>';
		unset($param_tags);

		return ($src);
	}

	function addContentFilter($content) {
		if (!preg_match('/(<object [^>]*>|<embed [^>]*>)/i', $content))
			return ($content);

		// object tag found
		if(preg_match("/<object [^>]*>/i", $content)) {
			preg_match_all("/<object([^>]*)>([^\[]*)<\/object>/i", $content, $matches, PREG_SET_ORDER);
			$search_strings  = array();
			$replace_strings = array();
			for ($i=0, $j=0; $i<count($matches); $i++) {
				$param = array();
				$matches[$i][1] = preg_replace('/[\r\n]/im', '', $matches[$i][1]);
				$matches[$i][2] = preg_replace('/[\r\n]/im', '', $matches[$i][2]);
				if(preg_match("/<embed [^>]*>/i", $matches[$i][2])) {
					if (preg_match_all("/[ \t]*([^\=]*)\=[\"']([^ ]*)[\"'][ \t]*/i", preg_replace("/^.*<embed ([^\]]*)>.*$/i", "$1", $matches[$i][2]), $options, PREG_SET_ORDER)) {
						for ($k=0; $k<count($options); $k++) {
							$param[strtolower($options[$k][1])] = $options[$k][2];
						}
						$param["vurl"] = $param["src"];
					}
				} else {
					if (preg_match_all("/[ \t]*([^\=]*)\=[\"']([^ ]*)[\"'][ \t]*/i", $matches[$i][1], $options, PREG_SET_ORDER)) {
						for ($k=0; $k<count($options); $k++) {
							$param[strtolower($options[$k][1])] = $options[$k][2];
						}
						$param["vurl"] = $param["data"];
					}
				}
				if ($param["type"] == 'application/x-shockwave-flash') {
					$search_strings[$j]  = "/".preg_quote($matches[$i][0],"/")."/i";
					$replace_strings[$j] = $this->get_object_tag($param);
					$j++;
				}
				unset($options);
				unset($param);
			}
			$content = preg_replace($search_strings, $replace_strings, $content);
			unset($search_strings);
			unset($replace_strings);
			unset($matches);
		}

		// embed tag found
		if(preg_match("/<embed [^>]*>/i", $content)) {
			preg_match_all("/<embed([^>]*)><\/embed>/i", $content, $matches, PREG_SET_ORDER);
			$search_strings  = array();
			$replace_strings = array();
			for ($i=0, $j=0; $i<count($matches); $i++) {
				$param = array();
				$matches[$i][1] = preg_replace('/[\r\n]/im', '', $matches[$i][1]);
				if (preg_match_all("/[ \t]*([^\=]*)\=[\"']([^ ]*)[\"'][ \t]*/i", $matches[$i][1], $options, PREG_SET_ORDER)) {
					for ($k=0; $k<count($options); $k++) {
						$param[strtolower($options[$k][1])] = $options[$k][2];
					}
					$param["vurl"] = $param["src"];
				}
				if ($param["type"] == 'application/x-shockwave-flash') {
					$search_strings[$j]  = "/".preg_quote($matches[$i][0],"/")."/i";
					$replace_strings[$j] = $this->get_object_tag($param);
					$j++;
				}
				unset($param);
			}
			$content = preg_replace($search_strings, $replace_strings, $content);
			unset($search_strings);
			unset($replace_strings);
			unset($matches);
		}

		return ($content);
	}
}

$embedSWFValid = new embedSWFValidateController();
add_action('the_content', array(&$embedSWFValid,'addContentFilter'));
unset($embedSWFValid);
?>

