### ___ LIVELEAK ___
###
### Copy this code on the end of " swf-sites.php " file.
###
### To post a video in your pmwiki site, type...
###      (:liveleak video_id:)
###
###
### For example, for...
###      http://www.liveleak.com/view?i=807_1205822827
### type...
###      (:liveleak 807_1205822827:)
### end the result will be like this...
###      http://anarchy-domisi-ideon.awardspace.info/main/pmwiki.php?n=Main.ΚιθάραΜεΤοΚουτάλι
###
###

Markup('liveleak', '<img', "/\\(:liveleak\\s+([^\\s]+)\\s*(.*):\\)/e", "ShowLiveLeak('$1','$2')");
SDVA($LiveLeakDefaultParams, array(
	'width' => '450'
	,'height' => '370'
	,'plwidth' => '480' # slightly different default size for embedding playlists
	,'plheight' => '385' # slightly different default size for embedding playlists
	,'scale' => '1'
	,'fs' => '1'
	,'hd' => '1'
	,'nocookie' => '0'
));
SDV($LiveLeak_XHTMLcompliant, true);
SDV($LiveLeakROEenabled, true);

# ROEPatterns - LIVELEALIVELEAK EMBED CONVERSION 
# Converts pasted liveleak embed code into valid pmwiki (:liveleak:) code
if ($LiveLeakROEenabled) {
	$ROEPatterns['!<object.*http://www\.liveleak?\.com.*<\/object>!ie'] = "LiveleakROE(PSS('$0'))";
	SDV($LiveLeakROEFmt, '(:liveleak $1 scale=1:)');
	function LiveleakROE($videocode) {
		global $LiveLeakROEFmt;
		
		# add nocookie=1 if embed code is from http://liveleak-nocookie.com
		if(strpos($videocode,'-nocookie')) $LiveLeakROEFmt = str_replace('$1','$1 nocookie=1',$LiveLeakROEFmt);
		
		# gather other parameters - width, height, border, color1, color2, rel
		## gathering width and height here b/c liveleak now offers different embed dimensions
		preg_match('#<object width="(\d+)" height="(\d+)"#', $videocode, $matches);
		$params = 'width='.$matches[1].' height='.$matches[2];
		if(preg_match('#&(color1=[^&]+)&#', $videocode, $matches)) $params .= " $matches[1]";
		if(preg_match('#&(color2=[^&"]+)&?#', $videocode, $matches)) $params .= " $matches[1]";
		if(preg_match('#&(border=\d)#', $videocode, $matches)) $params .= " $matches[1]";
		if(preg_match('#&(rel=\d)#', $videocode, $matches)) $params .= " $matches[1]";

		# check for playlist
		if(strpos($videocode, '/p/')) $params .= " playlist=1";
	
		# append extra parameters after the videocode
		$LiveLeakROEFmt_local = str_replace('$1','$1 '.$params,$LiveLeakROEFmt);
		# must use _local version, or else multiple conversions on the same page keeping adding params to the ROEFmt array		
		$out = preg_replace('#^.*/[vp]/([^&]+)&.*$#', $LiveLeakROEFmt_local, $videocode);
		return $out;
	}
}


function ShowLiveLeak($id, $args='') {
	global $LiveLeakDefaultParams, $LiveLeak_XHTMLcompliant;
	
	# $args here is unlike the other recipes in that it waits to see if it's a playlist, and checks for the presence width/height params before combining default and current param-arrays. this is to make sure default values are implemented correctly
	$args = ParseArgs($args); # user input only - will check it against default values below
	
	# check to see if it's a regular video, or a playlist consisting of many videos
	if ($args['playlist']=="1" || $args['playlist']=="true") { $vidtype = 'p'; $playlist_flag = true; }
		else $vidtype = 'e';

	# define width & height & scale
	if ($args['width']) {
		# use user-supplied width if it's supplied
		$width = $args['width'];
	} else {
		# default vals: check to see if it's a regular video, or a playlist consisting of many videos
		if ($playlist_flag) $width = $LiveLeakDefaultParams['plwidth'];
		else $width = $LiveLeakDefaultParams['width'];
	}
	if ($args['height']) {
		# use user-supplied height if it's supplied
		$height = $args['height'];
	} else {
		# default vals: check to see if it's a regular video, or a playlist consisting of many videos
		if ($playlist_flag) $height = $LiveLeakDefaultParams['plheight'];
		else $height = $LiveLeakDefaultParams['height'];
	}
	## one can supply a 'scale' default different than '1', but it's not suggested, as it could be confusing to users
	if ($args['scale']) $scale = $args['scale'];
		else $scale = $LiveLeakDefaultParams['scale'];

	$width = $width * $scale;
	$height = $height * $scale;


	# combine presets with supplied args, replacing presets with supplied args
	$args = array_merge($LiveLeakDefaultParams, $args);

	# get cookie info before array is cleared from the api-array
	$nocookieArg = $args['nocookie'];
	
	# create api-url - need to clear out non-api params first
	if($args) {
		# remove width, height, scale, playlist and nocookie params if present - not necessary for parameter string
		$args = array_diff_key($args, array('width'=>'','height'=>'','scale'=>'','playlist'=>'','nocookie'=>''));

		# create parameter string, in the form ?arg1=val1&arg2=val2
		$params = "?";
		$i = 0;
		foreach($args as $key => $val) {
			if ($key!='#') {
				#echo "$key - $val<br />";
				if($i!=0) $params .= "&";
				$params .= "$key=$val";
				$i++;
			}
		}
	}

	# Privacy: NoCookie Option
	if ($LiveLeakDefaultParams['nocookie'] || $nocookieArg) 
		$url = "http://www.liveleak.com/$vidtype/$id$params";
		else $url = "http://www.liveleak.com/$vidtype/$id$params";
	
	# Output
	if ($LiveLeak_XHTMLcompliant) {
		# XHTML 1.0 COMPLIANT
		$out = "\n<object type='application/x-shockwave-flash' width='$width' height='$height' data='$url'>";
		$out .= "\n  <param name='movie' value='$url' />";
		$out .= "\n  <param name='wmode' value='transparent' />";
		$out .= "\n  <param name='allowFullScreen' value='true' />";
		$out .= "\n  <param name='allowscriptaccess' value='always' />";
		$out .= "\n</object>";
	} else {
		$out = "\n<object width='$width' height='$height'> ";
		$out .= "\n  <param name='movie' value='$url'></param>";
		$out .= "\n  <param name='wmode' value='transparent'></param>";
		$out .= "\n  <param name='allowFullScreen' value='true'></param>";
		$out .= "\n  <param name='allowscriptaccess' value='always'></param>";
		$out .= "\n  <embed src='$url' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' wmode='transparent' width='$width' height='$height'></embed>";
		$out .= "\n</object>";
	}

	return Keep($out);
}