<?php
function LIB_getContents($str,$html)
{
	global $d;
	// 20120709 타이니님 제공
	if ($html == 'HTML')
	{
		$patterns = array();
		$pattern = array 
		(
			$d['admin']['secu_iframe'] ? '' : "'<iframe[^>]*?>'si",
			$d['admin']['secu_script'] ? '' : "'<script[^>]*?>'si",
			$d['admin']['secu_style'] ? '' : "'<style[^>]*?>'si",
			"'<meta[^>]*?>'si",
		);
		foreach ($pattern as $val) if ($val) $patterns[] = $val;
		
		$iframes = getIframes($str);
		$secuDomain = explode(',',$d['admin']['secu_domain']);
		foreach ($iframes as $im)
		{
			foreach ($secuDomain as $dm) 
			{
				if(stripos($im,$dm))
				{
					$str = str_replace($im,str_ireplace('iframe','@IFRAME@',$im),$str);
					break;
				}
			}
		}

		$str = str_replace('<A href=','<a target="_blank" href=',$str);
		$str = str_replace('<a href=','<a target="_blank" href=',$str);
		$str = str_replace('<a target="_blank" href="#','<a href="#',$str);
		$str = str_replace(' target="_blank">','>',$str);
		$str = str_replace('< param','<param',$str);
		$str = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',$str);
		if ($patterns) $str = preg_replace($patterns,' ',$str);
		$str = str_replace('@IFRAME@','iframe',$str);

		$onAttributes = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');        
		$eventPattern = '/\\s(?:'.implode('|', $onAttributes).')\\s*=\\s*(?:"[^"]*"|\'[^\']*\'|[^\\s>]+)/i';
		$urlPattern = '/\\s(?:href|src|xlink:href)\\s*=\\s*(?:"\\s*(?:javascript|vbscript|data:text\\/html)[^"]*"|\'\\s*(?:javascript|vbscript|data:text\\/html)[^\']*\'|(?:javascript|vbscript|data:text\\/html)[^\\s>]*)/i';
		$stylePattern = '/\\sstyle\\s*=\\s*(?:"[^"]*(?:expression|javascript:|-moz-binding)[^"]*"|\'[^\']*(?:expression|javascript:|-moz-binding)[^\']*\'|(?=[^\\s>]*(?:expression|javascript:|-moz-binding))[^\\s>]+)/i';
		$str = preg_replace_callback('/<[^>]*>/s', function ($matches) use ($eventPattern, $urlPattern, $stylePattern) {
			$tag = stripslashes($matches[0]);
			$tag = preg_replace(array($urlPattern, $eventPattern, $stylePattern, '/\\s+/'), array('', '', '', ' '), $tag);
			return $tag;
		}, $str);
		$str = str_replace('imgOrignWin(this.src)=""','onclick="imgOrignWin(this.src);"',$str);
		$str = str_replace('imgorignwin(this.src)=""','onclick="imgOrignWin(this.src);"',$str);
		if ($GLOBALS['my']['admin']&&!$d['admin']['secu_flash'])
		{
			$mat = '<div class="sysMsgBox"><img src="'.$GLOBALS['g']['img_core'].'/_public/ico_notice.gif" alt="" />'.$GLOBALS['lang']['sys']['sysmsg'].'</div>';
			$str = preg_replace("#(\<(embed|object)[^\>]*)\>(\<\/(embed|object)\>)?#i",$mat,$str);
		}
	}
	else {
		$str = str_replace('<','&lt;',$str);
		$str = str_replace('>','&gt;',$str);
		$str = str_replace('&nbsp;','&amp;nbsp;',$str);
		$str = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',$str);
		$str = nl2br($str);
	}
	return $str;
}
function getIframes($str) 
{  
	preg_match_all("/<iframe[^>]*?>/si", $str, $mat);
	return $mat[0];
}
?>
