Difference between revisions of "Extension:Flashlets.php"

From Organic Design wiki
m
(embedding and args)
Line 30: Line 30:
  
 
# convert args to command-line switches
 
# convert args to command-line switches
 +
$width  = isset($argv['width'])  ? intval($argv['width'])  : 300;
 +
$height  = isset($argv['height'])  ? intval($argv['height']) : 200;
 +
$fps    = isset($argv['fps'])    ? intval($argv['fps'])    : 25;
 +
$bgcolor = isset($argv['bgcolor']) ? $argv['bgcolor']        : '#ffffff';
  
 
# delete swf's older than cache-expiry
 
# delete swf's older than cache-expiry
Line 54: Line 58:
 
fwrite($handle,$actionscript);
 
fwrite($handle,$actionscript);
 
fclose($handle);
 
fclose($handle);
$sh = "$wgFlashletsMTASC -cp $wgFlashletsCP -swf /usr/local/mtasc/test.swf -out $file.swf -main $switches -header 300:200:20 $file.as 2>&1";
+
$sh = "$wgFlashletsMTASC -cp $wgFlashletsCP -swf /usr/local/mtasc/test.swf -out $file.swf -main $switches -header $width:$height:$fps $file.as 2>&1";
 
$html = shell_exec($sh);
 
$html = shell_exec($sh);
 
unlink("$file.as");
 
unlink("$file.as");
Line 62: Line 66:
  
 
# If the SWF exists, return the HTMLcode to request and embed the SWF otherwise return error information
 
# If the SWF exists, return the HTMLcode to request and embed the SWF otherwise return error information
if (file_exists("$file.swf")) $html = "<a href='$wgFlashletsPath/$swf.swf'>Download SWF</a>";
+
if (file_exists("$file.swf")) {
 +
$html = "\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>\n";
 +
$html .= "\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
 +
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=$version,0,0,0\"
 +
width=\"$width\" height=\"$height\" id=\"$swf\"
 +
type=\"application/x-shockwave-flash\" data=\"$file.swf\">
 +
<param name=\"flashvars\" value=\"$flashvars\">
 +
<param name=\"movie\" value=\"$file.swf\">
 +
<param name=\"quality\" value=\"$quality\">
 +
<param name=\"bgcolor\" value=\"$background\">
 +
<embed src=\"$file.swf\" quality=\"$quality\" bgcolor=\"$background\"
 +
width=\"$width\" height=\"$height\" flashvars=\"$flashvars\"
 +
name=\"$swf\" type=\"application/x-shockwave-flash\"
 +
pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
 +
</object>\n";
 +
if ($caption) $html .= "<tr><td align=\"center\"><a href='$wgFlashletsPath/$swf.swf'>$caption</a></td></tr>\n";
 +
$html .= "</table>";
 +
}
 
else $html = "\n<br><b>Could not compile SWF!</b><pre>$html</pre>\n";
 
else $html = "\n<br><b>Could not compile SWF!</b><pre>$html</pre>\n";
  

Revision as of 04:26, 1 April 2007

<?

  1. Flashlets Extension
  2. - Allows ActionScript code to render in the page a SWF using the MTASC (http://www.mtasc.org) SWF compiler
  3. - Version 1.00 (2007-03-30)
  4. - See http://www.mediawiki.org/wiki/Extension:Flashlets for installation and usage details
  5. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  6. - Author: http://www.organicdesign.co.nz/nad

$wgFlashletsMagic = "swf"; # the tag name for embedding a SWF $wgFlashletsMTASC = "mtasc"; # Command used to launch the MTASC executable $wgFlashletsCP = "/usr/local/mtasc/std"; # Directory containing the MTASC class headers $wgFlashletsPath = "/wiki/swf"; # Writable directory to store files $wgFlashletsDirectory = "$IP/swf"; # Writable directory to store files $wgExtensionFunctions[] = 'wfSetupFlashlets';

class Flashlets {

var $version = '1.00, 2007-03-30'; var $expire = 100; # Number of minutes a generated SWF lives for without being accessed

# Constructor function Flashlets() { global $wgParser,$wgFlashletsMagic; $wgParser->setHook($wgFlashletsMagic,array($this,'tagHook')); }

# Convert the $magic tags to client-side javascript request code from its stored parameters function tagHook($actionscript,$argv,&$parser) { global $wgFlashletsMTASC,$wgFlashletsCP,$wgFlashletsDirectory,$wgFlashletsPath;

# convert args to command-line switches $width = isset($argv['width'])  ? intval($argv['width'])  : 300; $height = isset($argv['height'])  ? intval($argv['height']) : 200; $fps = isset($argv['fps'])  ? intval($argv['fps'])  : 25; $bgcolor = isset($argv['bgcolor']) ? $argv['bgcolor']  : '#ffffff';

# delete swf's older than cache-expiry $expiry = $now - $this->expire; if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $stat = stat($file); #if ($stat[8] < $expire) unlink($file); } } } closedir($handle);

# create swf filename unique to the actionscript content and parameters $swf = md5($actionscript.$switches); $file = "$wgFlashletsDirectory/$swf";

# Compile the SWF if it doesn't already exist if (!file_exists("$file.swf")) {

# Write the actionscript to a file for MTASC to compile from if ($handle = fopen("$file.as", 'w+')) { fwrite($handle,$actionscript); fclose($handle); $sh = "$wgFlashletsMTASC -cp $wgFlashletsCP -swf /usr/local/mtasc/test.swf -out $file.swf -main $switches -header $width:$height:$fps $file.as 2>&1"; $html = shell_exec($sh); unlink("$file.as"); } else $html = "Could not open '$file.as' for writing!";

}

# If the SWF exists, return the HTMLcode to request and embed the SWF otherwise return error information if (file_exists("$file.swf")) {

$html = "\n

\n"; $html .= "
\n";

$html .= "\n<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=$version,0,0,0\" width=\"$width\" height=\"$height\" id=\"$swf\" type=\"application/x-shockwave-flash\" data=\"$file.swf\"> <param name=\"flashvars\" value=\"$flashvars\"> <param name=\"movie\" value=\"$file.swf\"> <param name=\"quality\" value=\"$quality\"> <param name=\"bgcolor\" value=\"$background\"> <embed src=\"$file.swf\" quality=\"$quality\" bgcolor=\"$background\" width=\"$width\" height=\"$height\" flashvars=\"$flashvars\" name=\"$swf\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /> </object>\n";

if ($caption) $html .= "
<a href='$wgFlashletsPath/$swf.swf'>$caption</a>

";

}

else $html = "\n
Could not compile SWF!

$html

\n";

return $html; }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupFlashlets() { global $wgFlashlets; $wgFlashlets = new Flashlets(); }

?>