CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
xtempl_base.php889 linesDownload Raw Back to classes
1<?php2// menuItem class3include_once(getabspath("include/menuitem.php"));4include(getabspath("include/testing.php"));5 6/**7  * Xlinesoft Template Engine8  */9class XTempl_Base10{11	var $xt_vars=array();12	var $xt_stack;13	var $xt_events=array();14	var $template;15	var $template_file;16	var $charsets=array();17	var $testingFlag=false;18	var $eventsObject;19	var $hiddenBricks = array();20	var $hiddenItems = array();21	/**22	 * array if arrays23	 */24	var $hiddenRecordItems = array();25	var $preparedContainers = array();26	var $layout;27	var $pageId = 1;28	var $pageObject = null;29 30	protected $messageParamStack = array();31 32	/**33	 * $cssFiles34	 * Array of css files for page styles and layouts35	 * @var {array}36	 * @intellisense37	 */38	var $cssFiles = array();39 40	var $jsonMode = false;41 42	/**43	  * Returns variable by name.44      * @intellisense45      */46	function getVar($name)47	{48		return xt_getvar($this,$name);49	}50 51	function recTesting(&$arr)52	{53		global $testingLinks;54		foreach($arr as $k=>$v)55			if(is_array($v))56				$this->recTesting($arr[$k]);57			else58				if(isset($testingLinks[$k]))59					$arr[$k].=" func=\"".$testingLinks[$k]."\"";60	}61 62	function Testing()63	{64		if(!$this->testingFlag)65			return;66		$this->recTesting($this->xt_vars);67	}68 69	function report_error($message)70	{71		echo $message;72		exit();73	}74 75	protected function assign_headers()76	{77		//check if headers are already assigned78		if( isset( $this->xt_vars['header'] ) )79			return;80 81		if ( !$this->mobileTemplateMode() )82		{83			$this->assign("header","header");84			$this->assign("footer","footer");85		}86		else87		{88			$this->assign("header","mheader");89			$this->assign("footer","mfooter");90		}91	}92 93 94	/**95	 * @param Boolean hideAddedCharts (optional) #9607 1.96	 */97	 function __construct( $hideAddedCharts = false )98	{99		global $mlang_charsets;100 101		$this->xt_vars=array();102		$this->xt_stack=array();103		$this->xt_stack[]=&$this->xt_vars;104 105		$this->assign_method("event",$this, "xt_event", array());106		$this->assign_function("label","xt_label",array());107		$this->assign_function("tooltip","xt_tooltip",array());108		$this->assign_function("tooltipAttr","xt_tooltipAttr",array());109		$this->assign_method("custom", $this, "customLabel",array());110		$this->assign_function("htmlcustom","xt_htmlcustom",array());111		$this->assign_function("cl_length","xt_cl_length",array());112		$this->assign_function("caption","xt_caption",array());113		$this->assign_function("pagetitlelabel", "xt_pagetitlelabel", array());114		$this->assign_function("logo","printProjectLogo",array());115		$this->assign_function("home_link","printHomeLink",array());116		$this->assign_function("file_url","getFileUrl",array());117		$this->assign_function("jscaption","xt_jscaption",array());118		$this->assign_function("jslabel","xt_jslabel",array());119		$this->assign_function("jspagetitlelabel","xt_jspagetitlelabel",array());120 121		$this->assign_function("pdf_image","getPdfImageObject",array());122		$this->assign_function("pdf_chart", "getPdfChartObject", array());123 124		$this->assign_method("map", $this, "xt_event_map", array());125 126		$this->assign( "projectPath", runner_htmlspecialchars( projectPath() ) );127 128		//	message parameters129		$this->assign_method("mlp_push", $this, "mlpPush", array());130		$this->assign_method("mlp_pop", $this, "mlpPop", array());131		$this->assign_method("mlparam", $this, "mlpParam", array());132		$this->assign_method("mltext", $this, "mlText", array());133 134		$this->assign_method("messagepart", $this, "messagePart", array());135 136 137		if( !$hideAddedCharts ) //#9607 1. Temporary fix138		{139		}140 141 142		$mlang_charsets=array();143		144$mlang_charsets["English"]="Windows-1252";;145		$this->charsets = &$mlang_charsets;146 147		$html_attrs = '';148		if(isRTL())149		{150			$this->assign("RTL_block",true);151			$this->assign("rtlCSS",true);152			$html_attrs .= 'dir="RTL" ';153		}154		else155		{156			$this->assign("LTR_block",true);157		}158		if(mlang_getcurrentlang() == 'English')159				$html_attrs .= 'lang="en"';160		$this->assign("html_attrs",$html_attrs);161		$this->assign("menu_block",true);162	}163 164 165	/**166	  * Assign value to name.167	  * @intellisense168	  */169	function assign($name,$val)170	{171		$this->xt_vars[$name]=$val;172	}173 174	/**175	  * Assign value to name by reference.176	  * @intellisense177	  */178	function assignbyref($name,&$var)179	{180		$this->xt_vars[$name]=&$var;181	}182 183	function bulk_assign( $arr )184	{185		foreach($arr as $key => $value)186		{187			$this->xt_vars[$key] = $value;188		}189	}190 191 192	function enable_section($name)193	{194		if(!isset($this->xt_vars[$name]))195		{196			$this->xt_vars[$name] = true;197		}198		elseif($this->xt_vars[$name] == false)199		{200			$this->xt_vars[$name] = true;201		}202	}203 204	function assign_section($name,$begin,$end)205	{206		$arr = array();207		$arr["begin"]=$begin;208		$arr["end"]=$end;209		$this->xt_vars[$name]=&$arr;210	}211 212	function assign_loopsection($name,&$data)213	{214		$arr = array();215		$arr["data"]=&$data;216		$this->xt_vars[$name]=&$arr;217	}218 219	function assign_array($name,$innername,$_arr)220	{221		$arr = array();222		foreach($_arr as $a)223			$arr[] = array($innername => $a);224		$this->xt_vars[$name]=array("data" => $arr);225	}226 227 228	function assign_loopsection_byValue($name, $data)229	{230		$arr = array();231		$arr["data"] = $data;232		$this->xt_vars[$name] = &$arr;233	}234 235	function assign_function( $name, $func, $params )236	{237		$this->xt_vars[$name] = XTempl::create_function_assignment( $func,$params );238	}239 240	static function create_function_assignment($func,$params)241	{242		return array( "func" => $func, "params" => $params );243	}244 245	function assign_method($name,&$object,$method,$params = null)246	{247		$this->xt_vars[$name] = XTempl::create_method_assignment( $method, $object, $params );248	}249 250	static function create_method_assignment( $method, &$object, $params = null )251	{252		return array( "method"=>$method,253			"params"=>$params,254			"object" => $object255		);256	}257 258	/**259	 * Remove assigned element260	 * @param string - name of assigned element261	 * @intellisense262	 */263	function unassign($name){264		unset($this->xt_vars[$name]);265	}266 267	function assign_event($name,&$object,$method,$params)268	{269		 $this->xt_events[$name]=array("method"=>$method,"params"=>$params);270		 $this->xt_events[$name]["object"]=&$object;271	}272 273	function xt_event($params)274	{275		global $projectLanguage;276		if( $this->jsonMode ) {277			if( $projectLanguage !== "aspx" ) {278				ob_start();279				$this->xt_doevent( $params );280				$out = jsreplace( ob_get_contents() );281				ob_end_clean();282				echo $out;283				return;284			} else {285				return jsreplace( $this->xt_doevent( $params ) );286			}287		}288		return $this->xt_doevent( $params );289	}290 291	function customLabel($params)292	{293		$ret = GetCustomLabel($params["custom1"]);294		$ret = $this->processMessageParams( $ret );295 296		if ( $this->jsonMode )  {297			$ret = jsreplace( $ret );298		}299 300		echo $ret;301	}302 303 304	function xt_event_map( $params )305	{306		if( !$this->jsonMode )307			return $this->xt_doevent( $params );308 309		ob_start();310		$this->xt_doevent( $params );311		$out = ob_get_contents() ;312		ob_end_clean();313 314		echo $out;315	}316 317	function xt_doevent($params)318	{319	}320 321	function fetchVar($varName)322	{323		ob_start();324		$varParams = array();325		$this->processVar($this->getVar($varName), $varParams);326		$out=ob_get_contents();327		ob_end_clean();328		return $out;329 330	}331 332	function fetch_loadedJSON( $filtertag="" ) {333		$this->jsonMode = true;334		return $this->fetch_loaded( $filtertag );335	}336 337	function fetch_loaded($filtertag="")338	{339		ob_start();340		$this->display_loaded($filtertag);341		$out=ob_get_contents();342		ob_end_clean();343		return $out;344	}345 346 347	function call_func($var)348	{349	}350 351	function set_template($template)352	{353		$this->template_file = basename($template,".htm");354		$this->set_layout();355 356 357		//	read template file358		$templatesPath = "templates/";359		if ( $this->mobileTemplateMode() )360			$templatesPath = "mobile/";361 362		if( $this->jsonMode ) {363			$templatesPath = "pdf/";364			$template = $this->template_file . ".json";365		}366 367		if( !$this->jsonMode && isOldCustomFile( $this->template_file ) ) {368			$template = getOldTemplateFilename( $this->template_file ).".htm";369		}370		if(file_exists(getabspath($templatesPath.$template)))371			$this->template = myfile_get_contents(getabspath($templatesPath.$template));372 373 374		if ( $this->mobileTemplateMode() && $this->template=='' )375		{376			$templatesPath = "templates/";377			$this->template = myfile_get_contents(getabspath($templatesPath.$template));378		}379		$this->assign_headers();380	}381 382	function set_layout()383	{384		$this->layout = getLayoutByFilename( $this->template_file );385	}386 387	function prepare_template($template)388	{389		$this->prepareContainers();390	}391 392	function load_templateJSON($template) {393		$this->jsonMode = true;394		$this->load_template( $template );395	}396 397	function display_loaded($filtertag = "")398	{399		$str = $this->template;400		if($filtertag)401		{402			if( !isset($this->xt_vars[ $filtertag ]) || $this->xt_vars[ $filtertag ] === false )403				return;404 405			$pos1 = strpos($this->template, "{BEGIN ".$filtertag."}");406			$pos2 = strpos($this->template, "{END ".$filtertag."}");407			if($pos1 === false || $pos2 === false)408				return;409 410			$pos2 += strlen("{END ".$filtertag."}");411			$str = substr($this->template,$pos1,$pos2-$pos1);412		}413		$this->Testing();414		$this->process_template( $str );415	}416 417	function load_template($template)418	{419		$this->set_template($template);420		$this->prepareContainers();421	}422 423	function display($template)424	{425	}426 427	function displayJSON($template)428	{429		$this->jsonMode = true;430		$this->display( $template );431	}432 433	function displayPartial($template)434	{435		$savedTemplate = $this->template;436		$this->display( $template );437		$this->template = $savedTemplate;438	}439 440	function processVar(&$var, &$varparams)441	{442	}443 444	function displayItemsHidden($items, $recordId = "")445	{446		if( !$items )447			return;448		foreach($items as $name)449		{450			$this->displayItemHidden( $name, $recordId );451		}452	}453 454	function showHiddenItems($items)455	{456		if( !$items )457			return;458		foreach($items as $name)459		{460			unset( $this->hiddenItems[$name] );461		}462	}463 464 465	function displayItemHidden($name, $recordId = "")466	{467		if( !$recordId )468			$this->hiddenItems[$name] = true;469		else {470			if( !isset( $this->hiddenRecordItems[ $name ] ))471				$this->hiddenRecordItems[ $name ] = array();472			$this->hiddenRecordItems[ $name ][ $recordId ] = true;473		}474 475	}476 477	function showHiddenItem($name, $recordId = "")478	{479		if( !$recordId )480			unset( $this->hiddenItems[$name] );481		else {482			if( isset( $this->hiddenRecordItems[ $name ] ))483				unset( $this->hiddenRecordItems[ $name ][ $recordId ] );484		}485 486	}487 488 489 490 491 492	/**493	 * DEPRECATED494	 * Hide All bricks on the page495	 * @param {array} of excepted bricks496	 * @intellisense497	 */498	function hideAllBricksExcept($arrExceptBricks){499	}500 501	/**502	 * Show brick on the page503	 * @param {string} name of brick504	 * @intellisense505	 */506	function showBrick($name)507	{508		if($this->layout->version == PD_BS_LAYOUT)509			return;510		foreach($this->layout->containers as $cname=>$container)511		{512			foreach($container as $brick)513			{514				if ($brick["name"]==$name){515					$this->assign($brick["block"],true);516				}517			}518		}519	}520 521	private function setContainerDisplayed($cname, $show, $firstContainerSubstyle, $lastContainerSubstyle)522	{523		if( $this->layout->version == BOOTSTRAP_LAYOUT )524		{525			$this->assign( "container_".$cname, true );526			if( !$show )527				$this->assign( $cname . "_chiddenattr", "data-hidden" );528			return;529		}530		$this->prepareContainerAttrs( $cname );531		if( $show )532		{533			$styleString = $this->preparedContainers[ $cname ]["showString"];534			$this->unassign("wrapperclass_".$cname);535		}536		else537		{538			$styleString = $this->preparedContainers[ $cname ]["hideString"];539			$this->assign("wrapperclass_".$cname,"rnr-hiddencontainer");540		}541		$this->assign_section("container_".$cname,"<div ".$styleString.">","</div>");542		$this->assign("cheaderclass_".$cname,$firstContainerSubstyle);543		$this->assign("cfooterclass_".$cname,$lastContainerSubstyle);544	}545 546	private function getPageStyle()547	{548		if(postvalue("pdf"))549		{550			return  $this->layout->pdfStyle();551		}552		return $this->layout->style;553	}554 555	private function prepareContainerAttrs( $cname )556	{557		$pageStyle = $this->getPageStyle();558		if( isset($this->preparedContainers[ $cname ]) )559			return;560		$this->preparedContainers[ $cname ] = array();561		$hiddenStyleString = "";562		$styleString = "";563		if(isset($this->layout->skins[$cname]))564		{565			$skin = @$this->layout->skins[$cname];566 567			$buttonsType = $this->layout->skinsparams[$skin]["button"];568			$buttonsClass = $buttonsType == "button2" ? " aslinks" : " asbuttons";569 570		// printing properties571			$printMode = $this->layout->container_properties[$cname]["print"];572			$printClass = "";573 574			if($printMode == "repeat")575				$printClass = " rp-repeat";576			else if($printMode == "none")577				$printClass = " rp-noprint";578 579			if($this->layout->version == 1) {580				$styleString = " class=\"rnr-cw-".$cname." runner-s-".$skin." ".$pageStyle;581			} else {582				$styleString = " class=\"rnr-cw-".$cname." rnr-s-".$skin.$buttonsClass." ".$pageStyle.$printClass;583			}584			$hiddenStyleString = $styleString . " rnr-hiddencontainer";585			$styleString .= "\"";586			$hiddenStyleString .= "\"";587			$this->preparedContainers[ $cname ] = array("showString" => $styleString, "hideString" => $hiddenStyleString );588		}589	}590	/**591	 * Prepare containers for show on page592	 * @intellisense593	 */594	function prepareContainers()595	{596		if(!$this->layout)597			return;598		$this->layout->prepareForms( $this, $this->hiddenItems, $this->hiddenRecordItems, $this->pageObject );599	}600 601	function hideField($fieldName)602	{603		if($this->layout->version == 1)604			$this->assign("fielddispclass_".GoodFieldName($fieldName), "runner-hiddenfield");605		else606			$this->assign("fielddispclass_".GoodFieldName($fieldName), "rnr-hiddenfield");607	}608 609	function showField($fieldName)610	{611		$this->assign("fielddispclass_".GoodFieldName($fieldName), "");612	}613 614 615	function mobileTemplateMode() {616		if($this->layout)617			return mobileDeviceDetected() && $this->layout->version < BOOTSTRAP_LAYOUT;618		else619			return false;620	}621 622	function setPage( $page ) {623		$this->pageObject = $page;624	}625 626	function transformFuncParams( $varparams, &$params ) {627		$key=1;628		foreach($varparams as $val)629		{630			if( strlen($val) )631				$params["custom".($key++)] = $val;632		}633		return $params;634	}635 636	/**637	 * Create new frame in the message stack638	 */639	function mlpPush($params = null) {640		$this->messageParamStack[] = array();641	}642 643	function mlpPop($params = null) {644		array_pop( $this->messageParamStack );645	}646 647	function & mlpHead() {648		if( $this->messageParamStack ) {649			return $this->messageParamStack[ count( $this->messageParamStack ) - 1 ];650		}651		return array();652	}653 654	/**655	 * Substitute %xxx% macros in the string with values from mlpHead656	 * @return String657	 */658	function processMessageParams( $str ) {659		$stackFrame =& $this->mlpHead();660		if( !$stackFrame ) {661			return $str;662		}663		$pattern = '/\%(\w)+\%/';664		$matches = findMatches( $pattern, $str );665		$out = "";666		$offset = 0;667		foreach( $matches as $m ) {668			$out .= substr( $str, $offset, $m["offset"] - $offset );669			$paramName = substr( $m["match"], 1, strlen( $m["match"] ) - 2 );670			$offset = $m["offset"] + strlen( $paramName ) + 2;671			$out .= $stackFrame[ $paramName ];672		}673		$out .= substr( $str, $offset );674		return $out;675	}676 677	function mlpParam( $params ) {678		$stackFrame =& $this->mlpHead();679		$type = $params["custom2"];680		$name = $params["custom1"];681		$value = "";682		if( $type == "text" ) {683			//	todo: support for spaces and curly braces684			$text = $params["custom3"];685			$stackFrame[ $name ] = XTempl_Base::unmaskText( $text );686		}687		if( $type == "custom" ) {688			$labelName = $params["custom3"];689			$stackFrame[ $name ] = GetCustomLabel( $labelName );690		}691		else if( $type == "var" ) {692			$varName = $params["custom3"];693			$stackFrame[ $name ] = $this->getVar( $varName );694		}695 696	}697 698	static function unmaskText( $str ) {699		return str_replace(700			array(  "\\_", "\\[", "\\]", "\\\\" ),701			array(  " ", "{", "}", "\\" ),702			$str703		);704	}705 706 707	function mlText( $params ) {708		$text = $params["custom1"];709		echo $this->processMessageParams( XTempl_Base::unmaskText( $text ) );710	}711 712	/**713	 * Show n-th part of a message714	 * A part is a substring adjacent to a '%value%'-like substring715	 */716	function messagePart( $params ) {717		$tag = $params["custom1"];718		$partN = $params["custom2"];719 720		$fullMessage = mlang_message( $tag );721		$pattern = "/%[^%\W]+%/i";722 723		$partOffset = 0;724		$parts = array();725		$matches = findMatches( $pattern, $fullMessage );726		for( $i = 0; $i < count( $matches ); ++$i ) {727			$m = $matches[ $i ];728			if ( $m["offset"] > 0 ) {729				$parts[] = substr( $fullMessage, $partOffset, $m["offset"] - $partOffset );730			}731			$partOffset = $m["offset"] +  strlen( $m["match"] );732		}733		if( $partOffset < strlen( $fullMessage ) ) {734			$parts[] = substr( $fullMessage, $partOffset );735		}736 737		if( $partN <= count( $parts ) )738			echo $parts[ $partN - 1 ];739 740		echo "";741	}742 743 744	function process_template( $str )745	{746	//	parse template file tag by tag747		$varparams=array();748		$start=0;749		$literal=false;750		$len = strlen($str);751		while(true)752		{753			$pos = strpos($str,"{",$start);754			if($pos===false)755			{756				echo substr($str,$start,$len-$start);757				break;758			}759			$section=false;760			$var=false;761			$message=false;762			if(substr($str,$pos+1,6)=="BEGIN ")763				$section=true;764			elseif(substr($str,$pos+1,1)=='$')765				$var=true;766			elseif(substr($str,$pos+1,14)=='mlang_message ')767			{768				$message=true;769			}770			else771			{772	//	no tag, just '{' char773				echo substr($str,$start,$pos-$start+1);774				$start=$pos+1;775				continue;776			}777			echo substr($str,$start,$pos-$start);778			if($section)779			{780	//	section781				$endpos=strpos($str,"}",$pos);782				if($endpos===false)783				{784					$this->report_error("Page is broken");785					return;786				}787				$section_name=trim(substr($str,$pos+7,$endpos-$pos-7));788				$endtag="{END ".$section_name."}";789				$endpos1=strpos($str,$endtag,$endpos);790				if($endpos1===false)791				{792					echo "End tag not found:".runner_htmlspecialchars($endtag);793					$this->report_error("Page is broken");794					return;795				}796				$section=substr($str,$endpos+1,$endpos1-$endpos-1);797				$start=$endpos1+strlen($endtag);798				$sectionVar = xt_getvar( $this, $section_name );799				if($sectionVar===false)800				{801					continue;802				}803				$begin="";804				$end="";805				if(is_array($sectionVar))806				{807					$begin=@$sectionVar["begin"];808					$end=@$sectionVar["end"];809 810					$var=@$sectionVar["data"];811				}812				else813				{814					$var = $sectionVar;815				}816				if(!is_array($var))817				{818	//	if section819					echo $begin;820					if(is_array($sectionVar))821					$this->xt_stack[]=&$sectionVar;822					$this->process_template( $section );823					if(is_array($sectionVar))824						array_pop($this->xt_stack);825						$this->processVar($end, $varparams);826				}827				else828				{829	//	foreach section830					echo $begin;831					$keys=array_keys($var);832					foreach($keys as $i)833					{834						$this->xt_stack[]=&$var[$i];835						if(is_array($var[$i]) && isset($var[$i]["begin"]))836							echo $var[$i]["begin"];837						$this->process_template( $section );838						array_pop($this->xt_stack);839						if(is_array($var[$i]) && isset($var[$i]["end"]))840							echo $var[$i]["end"];841					}842					$this->processVar($end, $varparams);843				}844			}845			elseif($var)846			{847	//	display a variable or call a function848				$endpos=strpos($str,"}",$pos);849				if($endpos===false)850				{851					$this->report_error("Page is broken");852					return;853				}854				$varparams=array();855				$var_name = trim(substr($str,$pos+2,$endpos-$pos-2));856				if(strpos($var_name," ")!==FALSE)857				{858					$varparams = explode(" ",$var_name);859					$var_name = $varparams[0];860					unset($varparams[0]);861				}862				$start=$endpos+1;863				$var = xt_getvar( $this, $var_name );864				if($var===false)865				{866					continue;867				}868				$this->processVar($var, $varparams);869			}870			elseif($message)871			{872				$endpos=strpos($str,"}",$pos);873				if($endpos===false)874				{875					$this->report_error("Page is broken");876					return;877				}878				$tag = trim(substr($str,$pos+15,$endpos-$pos-15));879				$start=$endpos+1;880				echo runner_htmlspecialchars(881					$this->processMessageParams(882						mlang_message($tag)883					));884			}885		}886	}887 888}889?>