<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 * @version 0.3
 */
/*
    usage:
    {swfobject swf="/path/to/movie.swf"}
    {swfobject swf="/path/to/movie.swf" wmode="transparent"}
    {swfobject swf="/path/to/movie.swf" flashvars=$flashvars_assoc_array}
    {swfobject swf="/path/to/movie.swf" flashvars="param=value"}
    {swfobject swf="/path/to/movie.swf" version="9.0.0.0"}
    {swfobject swf="/path/to/movie.swf" alt="alternate <b>html text</b> download flash player 9!!!" version="9.0.0.0"}
    etc....
*/

function smarty_function_swfobject($params, &$smarty)
{
    
$key md5(serialize($params));
    
$result '';
    if (isset(
$params['swf']) && is_file($_SERVER['DOCUMENT_ROOT'].$params['swf']) && false !== ($size getimagesize($_SERVER['DOCUMENT_ROOT'].$params['swf']))) {
        
$width = (isset($params['width'])) ? $params['width'] : $size[0];
        
$height = (isset($params['height'])) ? $params['height'] : $size[1];
        
$version = (isset($params['version'])) ? $params['version'] : '8.0.0.0';
        
$alt = (isset($params['alt'])) ? $params['alt'] : 'need flash plugin version '.$version.' or never';
        
$bgcolor = (isset($params['bgcolor'])) ? $params['bgcolor'] : '';
        
$wmode = (isset($params['wmode'])) ? $params['wmode'] : 'opaque';
        
$salign = (isset($params['salign'])) ? $params['salign'] : '';
        
$align = (isset($params['align'])) ? $params['align'] : '';
        
$scale = (isset($params['scale'])) ? $params['scale'] : '';
        
$result '<div id="div_'.$key.'">'.$alt.'</div>'.PHP_EOL.'<script type="text/javascript">'.PHP_EOL;
        
$result .= ' var swf_'.$key.' = new SWFObject("'.str_replace('.swf','.v'.filemtime($_SERVER['DOCUMENT_ROOT'].$params['swf']).'.swf',$params['swf']).'", "name_'.$key.'", "'.$width.'", "'.$height.'", "'.$version.'"';
        if (
$bgcolor$result .=',"'.$bgcolor.'"';
        
$result .= ');'.PHP_EOL;
        
$result .= ' swf_'.$key.'.addParam("wmode","'.$wmode.'");'.PHP_EOL;
        if (
strlen($salign))
            
$result .= ' swf_'.$key.'.addParam("salign","'.$salign.'");'.PHP_EOL;        
        if (
strlen($scale)) {
            
$result .= ' swf_'.$key.'.addParam("scale","'.$scale.'");'.PHP_EOL;                    
        }
        if (
strlen($align)) $result .= ' swf_'.$key.'.addParam("align","'.$align.'");'.PHP_EOL;                    
        if (isset(
$params['flashvars'])) {
            if (
is_array($params['flashvars'])) {
                foreach (
$params['flashvars'] as $_key => $_value) {
                    
$result .= 'swf_'.$key.'.addVariable("'.$_key.'","'.$_value.'");'.PHP_EOL;
                }
            } else {
                
$_vars explode('&',$params['flashvars']);
                foreach (
$_vars as $_var) {
                    
$_params explode('=',$_var);
                    if (
count($_params) == 2) {
                        
$result .= 'swf_'.$key.'.addVariable("'.$_params[0].'","'.$_params[1].'");'.PHP_EOL;                    
                    }
                }
            }
        }
        if (isset(
$params['redirectUrl'])) {
            
$result .= 'swf_'.$key.'.setAttribute("redirectUrl","'.$params['redirectUrl'].'")'.PHP_EOL;
        }
        
$result .= 'swf_'.$key.'.write("div_'.$key.'");'.PHP_EOL;
        
$result .= '</script>'.PHP_EOL;
        return 
$result;
    } else {
        if (!isset(
$params['swf'])) {
            
$smarty->trigger_error("swfobject: not set required parameter 'swf' ");
        } else {
            
$smarty->trigger_error("swfobject: '".$params['swf']."' is not a valid swf file");
        }
    }
}
?>
1