<?php
// plugin functions
$_pluginList = [];
$_pluginKeys = [];
$_pluginjQuery = '';


function _pluginLoad($list)
{
	global $_pluginList;
	global $_pluginKeys;	
	if($list)
	foreach($list as $each)
	{
		if(!@is_array($each))
		$_pluginList[] = $each;
		else
		{
			$_pluginList[] = $each[0];
			$tmpKey = $each[0];
			unset($each[0]);
			$_pluginKeys[$tmpKey] = $each;
		}	
	}
}

function _loadPluginFile($path,$type)
{
	global $_pluginList;
	global $setting_pluginPath;
	global $setting_publicPath;
	global $_pluginScript;
	global $_pluginjQuery;
	global $_pluginKeys;
	global $pluginFiles;
	global $fullpath;

	if($_pluginList)
	foreach($_pluginList as $plg)
	{
		include($path."../$setting_publicPath/$setting_pluginPath/$plg/plugin.php");
		
		$attr = "";
		if(@$pluginFiles[$type])
		{
			foreach($pluginFiles[$type] as $each)
			{
				if(isset($each['load']))
				{
					?>

<script type="text/javascript">
<?php		

						$tmpScript = file_get_contents($path."../$setting_publicPath/$setting_pluginPath/$plg/".$each['load']);

						$subject = $tmpScript;
						preg_match_all('/\{[a-zA-Z\-]+\}/', $subject, $matches);

						if(sizeof($matches[0]))
							foreach($matches[0] as $ech)
								$tmpScript = str_replace($ech, $_pluginKeys[$plg][substr($ech,1,-1)], $tmpScript);	
								
						echo $tmpScript;

					?>

</script>			<?php
				}
				else
				{
					$attr = "";
					foreach($each as $key=>$val)
					{
						$subject = $val;
						preg_match_all('/\{[a-zA-Z\-]+\}/', $subject, $matches);
						if(sizeof($matches[0]))
						foreach($matches[0] as $ech)
						$val = str_replace($ech, $_pluginKeys[$plg][substr($ech,1,-1)], $val);					
											
						if((($type=='js' && $key=="src") || ($type=='css' && $key=="href")) && !@$each['fullPath'])
						$val = $path.$setting_pluginPath."/$plg/".$val;

						if($key!="fullPath" && $key!="async" && $key!="defer")
						{
							if(!@is_numeric($key))
							$attr.= " $key=\"$val\"";
							else
							$attr.= " $val";
						}				
					}

					if($type=='css')
					{
						?>
						
<link rel="stylesheet" <?php echo $attr?>><?php
					}
					
					if($type=='js')
					{
						if(@$each["async"])
						$attr.= " async";
					
						if(@$each["defer"])
						$attr.= " defer";					
						?>

<script type="text/javascript" <?php echo $attr?>></script><?php
					}
				}				
			}
		}
		
		if(@$pluginFiles['jquery']!='' && $type=='js') {
			
			$tmpScript = file_get_contents($path."../$setting_publicPath/$setting_pluginPath/$plg/".$pluginFiles['jquery']);

			$subject = $tmpScript;
			preg_match_all('/\{[a-zA-Z\-]+\}/', $subject, $matches);

			if(sizeof($matches[0]))
				foreach($matches[0] as $ech)
					$tmpScript = str_replace($ech, $_pluginKeys[$plg][substr($ech,1,-1)], $tmpScript);

			$_pluginjQuery.= $tmpScript;				
		}
	}	
}

function _pluginCSS($path='')
{
	global $_pluginjQuery;		
	_loadPluginFile($path,"css");
}

function _pluginScript($path='')
{
	global $_pluginScript;
	global $_pluginjQuery;	
	_loadPluginFile($path,"js");
	?>

<script type="text/javascript">
<?php
echo $_pluginScript;
?>

$(function () {
<?php
echo $_pluginjQuery;
?>
});
</script>
<?php
}

function _pluginPHP($plugins,$path='./')
{
	global $setting_pluginPath;
	if(@is_array($plugins) && sizeof($plugins))	
	foreach($plugins as $plg)	
	include($path.$setting_pluginPath.'/'.$plg['plugin'].'/'.$plg['library'].'.php');
}
?>
