<?php
/**
* @version		$Id: helper.php v 1.1 2010/04/19 $
* @author		Joomlets.net
* @package		EasyFlash
* @copyright	(C) 2010-2013 Joomlets.net (www.joomlets.net)
* @license		http://www.gnu.org/copyleft/gpl.html GNU/GPL
* Notice:		The EasyFlash module uses swfobject 
*			(http://code.google.com/p/swfobject/) to embed
*			flash movies into Joomla module positions.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class EasyFlashHelper {

	// required and special parameters
	private static $requireds = array('movie', 'width', 'height', 'id');
	private static $exceptions = array('movie', 'flashvars', 'css_class', 'version', 'width', 'height', 'id');
	private static $params;
	
	// include swfobject.js script into the HTML header if not exists
	public static function addJsFile() {
		$document = &JFactory::getDocument();
		$header = $document->getHeadData();
		$scripts = $header[scripts];
		if (preg_match('/swfobject.js/', implode("#", $scripts)) == 0)
		$document->addScript(JURI::base().'modules/mod_easyflash/tmpl/js/swfobject.js');
	}
	
	// prepare flash parameters according to backend settings of easyflash module 
	public static function prepareParams() {

		$param_str = "";
		// put all parameters to an array
		$pars = explode("\n", self::$params->_raw);

		$is_first = true;
		
		foreach ($pars as $rows) {
			$param = explode("=", $rows);
			if (!in_array($param[0], self::$exceptions) && $param[1] != "") {
				$param_str .= (($is_first)?'':', ').$param[0].': "'.$param[1].'"';
				if ($is_first) {
					$is_first = false;
				}
			}
		}
		
		return $param_str;
	}
	
	// find missing flash parameters in module settings
	public static function chkMissingParam(&$params) {
	
		self::$params =& $params;
		foreach (self::$requireds as $required) {
			if (!self::$params->get($required)) {
				return $required;
			}
		}
		return false;
	}
	
	// making flashvars string for javascript
	public static function prepareFlashVars() {
		
		if (self::$params->get('flashvars')) {
			$flashvars = explode("&", self::$params->get('flashvars'));
			$is_first = true;
			foreach ($flashvars as $flashvar) {
				$expr = explode("=", $flashvar);
		
				// checking $_VARIABLE regexp - for security reasons
				if (preg_match("/\\$\w+\[[\'\"]\w+[\'\"]\]/", stripslashes($expr[1]))) {
					eval("\$value = $expr[1];");
				} else {
					$value = $expr[1];
				}
				$flashvars_str .= (($is_first)?"":", ").$expr[0].": \"".$value."\"";
				$is_first = false;
			}
		}
		
		return $flashvars_str;
	}
	
	public static function debugParams() {
		$pars = explode("\n", self::$params->_raw);
		$debug_str = "";
		
		foreach ($pars as $rows) {
			$param = explode("=", $rows);
			$debug_str .= $param[0].': "'.$param[1].'"<br />';
		}

		return $debug_str;
	}
}