dedetag.class.php

来自「这是matlab的一个小程序」· PHP 代码 · 共 1,023 行 · 第 1/2 页

PHP
1,023
字号
				$ResultString .= substr($this->SourceString,$nextTagEnd,$this->CTags[$i]->StartPos-$nextTagEnd);
				$ResultString .= $this->CTags[$i]->GetValue();
				$nextTagEnd = $this->CTags[$i]->EndPos;
			}
		}
		$slen = strlen($this->SourceString);
		if($slen>$nextTagEnd)
		{
			$ResultString .= substr($this->SourceString,$nextTagEnd,$slen-$nextTagEnd);
		}
		return $ResultString;
	}

	//把分析模板输出到一个字符串中,并返回
	function GetResult()
	{
		$ResultString = '';
		if($this->Count==-1)
		{
			return $this->SourceString;
		}
		$this->AssignSysTag();
		$nextTagEnd = 0;
		$strok = "";
		for($i=0;$i<=$this->Count;$i++)
		{
			$ResultString .= substr($this->SourceString,$nextTagEnd,$this->CTags[$i]->StartPos-$nextTagEnd);
			$ResultString .= $this->CTags[$i]->GetValue();
			$nextTagEnd = $this->CTags[$i]->EndPos;
		}
		$slen = strlen($this->SourceString);
		if($slen>$nextTagEnd)
		{
			$ResultString .= substr($this->SourceString,$nextTagEnd,$slen-$nextTagEnd);
		}
		return $ResultString;
	}

	//直接输出分析模板
	function Display()
	{
		echo $this->GetResult();
	}

	//把分析模板输出为文件
	function SaveTo($filename)
	{
		$fp = @fopen($filename,"w") or die("DedeTag Engine Create File False");
		fwrite($fp,$this->GetResult());
		fclose($fp);
	}

	//解析模板
	function ParseTemplet()
	{
		$TagStartWord = $this->TagStartWord;
		$TagEndWord = $this->TagEndWord;
		$sPos = 0; $ePos = 0;
		$FullTagStartWord =  $TagStartWord.$this->NameSpace.":";
		$sTagEndWord =  $TagStartWord."/".$this->NameSpace.":";
		$eTagEndWord = "/".$TagEndWord;
		$tsLen = strlen($FullTagStartWord);
		$sourceLen=strlen($this->SourceString);
		
		if( $sourceLen <= ($tsLen + 3) )
		{
			return;
		}
		$cAtt = new DedeAttributeParse();
		$cAtt->charToLow = $this->CharToLow;

		//遍历模板字符串,请取标记及其属性信息
		for($i=0; $i < $sourceLen; $i++)
		{
			$tTagName = '';

			//如果不进行此判断,将无法识别相连的两个标记
			if($i-1 >= 0)
			{
				$ss = $i-1;
			}
			else
			{
				$ss = 0;
			}
			$sPos = strpos($this->SourceString,$FullTagStartWord,$ss);
			$isTag = $sPos;
			if($i==0)
			{
				$headerTag = substr($this->SourceString,0,strlen($FullTagStartWord));
				if($headerTag==$FullTagStartWord)
				{
					$isTag=true; $sPos=0;
				}
			}
			if($isTag===FALSE)
			{
				break;
			}
			//判断是否已经到倒数第三个字符(可能性几率极小,取消此逻辑)
			/*
			if($sPos > ($sourceLen-$tsLen-3) )
			{
				break;
			}
			*/
			for($j=($sPos+$tsLen);$j<($sPos+$tsLen+$this->TagMaxLen);$j++)
			{
				if($j>($sourceLen-1))
				{
					break;
				}
				else if( ereg("[/ \t\r\n]",$this->SourceString[$j]) || $this->SourceString[$j] == $this->TagEndWord )
				{
					break;
				}
				else
				{
					$tTagName .= $this->SourceString[$j];
				}
			}
			if($tTagName!='')
			{
				$i = $sPos+$tsLen;
				$endPos = -1;
				$fullTagEndWordThis = $sTagEndWord.$tTagName.$TagEndWord;
				
				$e1 = strpos($this->SourceString,$eTagEndWord, $i);
				$e2 = strpos($this->SourceString,$FullTagStartWord, $i);
				$e3 = strpos($this->SourceString,$fullTagEndWordThis,$i);
				
				//$eTagEndWord = /} $FullTagStartWord = {tag: $fullTagEndWordThis = {/tag:xxx]
				
				$e1 = trim($e1); $e2 = trim($e2); $e3 = trim($e3);
				$e1 = ($e1=='' ? '-1' : $e1);
				$e2 = ($e2=='' ? '-1' : $e2);
				$e3 = ($e3=='' ? '-1' : $e3);
				//not found '{/tag:'
				if($e3==-1) 
				{
					$endPos = $e1;
					$elen = $endPos + strlen($eTagEndWord);
				}
				//not found '/}'
				else if($e1==-1) 
				{
					$endPos = $e3;
					$elen = $endPos + strlen($fullTagEndWordThis);
				}
				//found '/}' and found '{/dede:'
				else
				{
					//if '/}' more near '{dede:'、'{/dede:' , end tag is '/}', else is '{/dede:'
					if($e1 < $e2 &&  $e1 < $e3 )
					{
						$endPos = $e1;
						$elen = $endPos + strlen($eTagEndWord);
					}
					else
					{
						$endPos = $e3;
						$elen = $endPos + strlen($fullTagEndWordThis);
					}
				}

				//not found end tag , error
				if($endPos==-1)
				{
					echo "Character postion $sPos, '$tTagName' Error!<br />\r\n";
					break;
				}
				$i = $elen;
				$ePos = $endPos;

				//分析所找到的标记位置等信息
				$attStr = '';
				$innerText = '';
				$startInner = 0;
				for($j=($sPos+$tsLen);$j < $ePos;$j++)
				{
					if($startInner==0 && ($this->SourceString[$j]==$TagEndWord && $this->SourceString[$j-1]!="\\") )
					{
						$startInner=1;
						continue;
					}
					if($startInner==0)
					{
						$attStr .= $this->SourceString[$j];
					}
					else
					{
						$innerText .= $this->SourceString[$j];
					}
				}
				//echo "<xmp>$attStr</xmp>\r\n";
				$cAtt->SetSource($attStr);
				if($cAtt->cAttributes->GetTagName()!='')
				{
					$this->Count++;
					$CDTag = new DedeTag();
					$CDTag->TagName = $cAtt->cAttributes->GetTagName();
					$CDTag->StartPos = $sPos;
					$CDTag->EndPos = $i;
					$CDTag->CAttribute = $cAtt->cAttributes;
					$CDTag->IsReplace = FALSE;
					$CDTag->TagID = $this->Count;
					$CDTag->InnerText = $innerText;
					$this->CTags[$this->Count] = $CDTag;
				}
			}
			else
			{
				$i = $sPos+$tsLen;
				break;
			}
		}//结束遍历模板字符串

		if($this->IsCache)
		{
			$this->SaveCache();
		}
	}

	//处理某字段的函数
	function EvalFunc($fieldvalue,$functionname,&$refObj)
	{
		$DedeFieldValue = $fieldvalue;
		$functionname = str_replace("{\"","[\"",$functionname);
		$functionname = str_replace("\"}","\"]",$functionname);
		$functionname = eregi_replace("'@me'|\"@me\"|@me",'$DedeFieldValue',$functionname);
		$functionname = "\$DedeFieldValue = ".$functionname;
		@eval($functionname.";"); //or die("<xmp>$functionname</xmp>");
		if(empty($DedeFieldValue))
		{
			return '';
		}
		else
		{
			return $DedeFieldValue;
		}
	}

	//获得一个外部变量
	function GetGlobals($varname)
	{
		$varname = trim($varname);

		//禁止在模板文件读取数据库密码
		if($varname=="dbuserpwd"||$varname=="cfg_dbpwd")
		{
			return "";
		}

		//正常情况
		if(isset($GLOBALS[$varname]))
		{
			return $GLOBALS[$varname];
		}
		else
		{
			return "";
		}
	}

	//引入文件
	function IncludeFile($filename,$ismake='no')
	{
		global $cfg_df_style;
		$restr = '';
		if($filename=='')
		{
			return '';
		}
		if( file_exists(DEDEROOT."/templets/".$filename) )
		{
			$okfile = DEDEROOT."/templets/".$filename;
		}
		else if(file_exists(DEDEROOT.'/templets/'.$cfg_df_style.'/'.$filename) )
		{
			$okfile = DEDEROOT.'/templets/'.$cfg_df_style.'/'.$filename;
		}
		else
		{
			return "无法在这个位置找到: $filename";
		}

		//编译
		if($ismake!="no")
		{
			require_once(DEDEINC."/channelunit.func.php");
			$dtp = new DedeTagParse();
			$dtp->LoadTemplet($okfile);
			MakeOneTag($dtp,$this->refObj);
			$restr = $dtp->GetResult();
		}
		else
		{
			$fp = @fopen($okfile,"r");
			while($line=fgets($fp,1024)) $restr.=$line;
			fclose($fp);
		}
		return $restr;
	}
}

/**********************************************
//class DedeAttribute Dede模板标记属性集合
function c____DedeAttribute();
**********************************************/
//属性的数据描述
class DedeAttribute
{
	var $Count = -1;
	var $Items = ""; //属性元素的集合
	//获得某个属性
	function GetAtt($str)
	{
		if($str=="")
		{
			return "";
		}
		if(isset($this->Items[$str]))
		{
			return $this->Items[$str];
		}
		else
		{
			return "";
		}
	}

	//同上
	function GetAttribute($str)
	{
		return $this->GetAtt($str);
	}

	//判断属性是否存在
	function IsAttribute($str)
	{
		if(isset($this->Items[$str])) return true;
		else return false;
	}

	//获得标记名称
	function GetTagName()
	{
		return $this->GetAtt("tagname");
	}

	// 获得属性个数
	function GetCount()
	{
		return $this->Count+1;
	}
}

/*******************************
//属性解析器(本版本中已经支持使用\'这种语法,和用.间隔表示name属性,如 field.body)
function c____DedeAttributeParse();
********************************/
class DedeAttributeParse
{
	var $sourceString = "";
	var $sourceMaxSize = 1024;
	var $cAttributes = "";
	var $charToLow = true;
	function SetSource($str='')
	{
		$this->cAttributes = new DedeAttribute();
		$strLen = 0;
		$this->sourceString = trim(preg_replace("/[ \r\n\t]{1,}/"," ",$str));
		
		//为了在function内能使用数组,这里允许对[ ]进行转义使用
		$this->sourceString = str_replace('\]',']',$this->sourceString);
		$this->sourceString = str_replace('[','[',$this->sourceString);
		/*
		$this->sourceString = str_replace('\>','>',$this->sourceString);
		$this->sourceString = str_replace('<','>',$this->sourceString);
		$this->sourceString = str_replace('{','{',$this->sourceString);
		$this->sourceString = str_replace('\}','}',$this->sourceString);
		*/
		
		$strLen = strlen($this->sourceString);
		if($strLen>0 && $strLen <= $this->sourceMaxSize)
		{
			$this->ParseAttribute();
		}
	}

	//解析属性
	function ParseAttribute()
	{
		$d = '';
		$tmpatt = '';
		$tmpvalue = '';
		$startdd = -1;
		$ddtag = '';
		$hasAttribute=false;
		$strLen = strlen($this->sourceString);

		// 获得Tag的名称,解析到 cAtt->GetAtt('tagname') 中
		for($i=0; $i<$strLen; $i++)
		{
			if($this->sourceString[$i]==' ')
			{
				$this->cAttributes->Count++;
				$tmpvalues = explode('.', $tmpvalue);
				$this->cAttributes->Items['tagname'] = ($this->charToLow ? strtolower($tmpvalues[0]) : $tmpvalues[0]);
				if(isset($tmpvalues[1]) && $tmpvalues[1]!='')
				{
					$this->cAttributes->Items['name'] = $tmpvalues[1];
				}
				$tmpvalue = '';
				$hasAttribute = true;
				break;
			}
			else
			{
				$tmpvalue .= $this->sourceString[$i];
			}
		}

		//不存在属性列表的情况
		if(!$hasAttribute)
		{
			$this->cAttributes->Count++;
			$tmpvalues = explode('.', $tmpvalue);
			$this->cAttributes->Items['tagname'] = ($this->charToLow ? strtolower($tmpvalues[0]) : $tmpvalues[0]);
			if(isset($tmpvalues[1]) && $tmpvalues[1]!='')
			{
				$this->cAttributes->Items['name'] = $tmpvalues[1];
			}
			return ;
		}
		$tmpvalue = '';

		//如果字符串含有属性值,遍历源字符串,并获得各属性
		for($i; $i<$strLen; $i++)
		{
			$d = $this->sourceString[$i];
			//查找属性名称
			if($startdd==-1)
			{
				if($d != '=')
				{
					$tmpatt .= $d;
				}
				else
				{
					if($this->charToLow)
					{
						$tmpatt = strtolower(trim($tmpatt));
					}
					else
					{
						$tmpatt = trim($tmpatt);
					}
					$startdd=0;
				}
			}

			//查找属性的限定标志
			else if($startdd==0)
			{
				switch($d)
				{
					case ' ':
						break;
					case '"':
						$ddtag = '"';
						$startdd = 1;
						break;
					case '\'':
						$ddtag = '\'';
						$startdd = 1;
						break;
					default:
						$tmpvalue .= $d;
						$ddtag = ' ';
						$startdd = 1;
						break;
				}
			}
			else if($startdd==1)
			{
				if($d==$ddtag && ( isset($this->sourceString[$i-1]) && $this->sourceString[$i-1]!="\\") )
				{
					$this->cAttributes->Count++;
					$this->cAttributes->Items[$tmpatt] = trim($tmpvalue);
					$tmpatt = '';
					$tmpvalue = '';
					$startdd = -1;
				}
				else
				{
					$tmpvalue .= $d;
				}
			}
		}//for

		//最后一个属性的给值
		if($tmpatt != '')
		{
			$this->cAttributes->Count++;
			$this->cAttributes->Items[$tmpatt] = trim($tmpvalue);
		}
		//print_r($this->cAttributes->Items);
	}// end func
}
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?