Example #1
0
 function OnOperation($FieldName, &$Value, &$PrmLst, &$Source, &$PosBeg, &$PosEnd, &$Loc)
 {
     if ($PrmLst['ope'] !== 'html') {
         return;
     }
     if (isset($PrmLst['select'])) {
         $Source = substr_replace($Source, '', $PosBeg, $PosEnd - $PosBeg + 1);
         // We delete the current TBS tag
         tbs_Html_MergeItems($Source, $Value, $PrmLst, $PosBeg);
         return false;
         // Return false to avoid TBS merging the current field
     } elseif (isset($PrmLst['look'])) {
         if (tbs_Html_IsHtml($Value)) {
             $PrmLst['look'] = '1';
             $Loc->ConvMode = false;
             // no conversion
         } else {
             $PrmLst['look'] = '0';
             $Loc->ConvMode = 1;
             // conversion to HTML
         }
     }
 }
Example #2
0
function meth_Locator_Replace(&$Txt,&$Loc,&$Value,$CheckSub) {
// This function enables to merge a locator with a text and returns the position just after the replaced block
// This position can be useful because we don't know in advance how $Value will be replaced.

	// Found the value if there is a subname
	if ($CheckSub and $Loc->SubOk) {
		$SubId = 0;
		while ($SubId<$Loc->SubNbr) {
			$x = $Loc->SubLst[$SubId]; // &$Loc... brings an error with Event Example, I don't know why.
			if (is_array($Value)) {
				if (isset($Value[$x])) {
					$Value =& $Value[$x];
				} elseif (array_key_exists($x,$Value)) {// can happens when value is NULL
					$Value =& $Value[$x];
				} else {
					$Value =& tbs_Misc_UnlinkVar('');
					if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert('Array value','Can\'t merge '.$this->ChrOpen.$Loc->FullName.$this->ChrClose.' because sub-item \''.$x.'\' is not an existing key in the array.',true);
				}
				$SubId++;
			} elseif (is_object($Value)) {
				if (method_exists($Value,$x)) {
					$x = call_user_func(array(&$Value,$x));
				} elseif (isset($Value->$x)) {
					$x = $Value->$x;
				} else {
					if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert('Object value','Can\'t merge '.$this->ChrOpen.$Loc->FullName.$this->ChrClose.' because \''.$x.'\' is neither a method nor a property in the class \''.get_class($Value).'\'.',true);
					$x =& tbs_Misc_UnlinkVar('');
				}
				$Value =& $x;
				$x =& tbs_Misc_UnlinkVar('');
				$SubId++;
			} else {
				if (isset($Loc->PrmLst['selected'])) {
					$SelArray =& $Value;
				} else {
					if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert('Object or Array value expected','Can\'t merge '.$this->ChrOpen.$Loc->FullName.$this->ChrClose.' because the item before \''.$x.'\' is neither an object nor an array. Its type is '.gettype($Value).'.',true);
				}
				$Value =& tbs_Misc_UnlinkVar('');
				$SubId = $Loc->SubNbr;
			}
		}
	}

	$CurrVal = $Value;

	// File inclusion
	if (isset($Loc->PrmLst['file'])) {
		$File = str_replace($this->ChrVal,$CurrVal,$Loc->PrmLst['file']);
		$this->meth_Merge_PhpVar($File,false);
		$OnlyBody = !(isset($Loc->PrmLst['htmlconv']) and (strtolower($Loc->PrmLst['htmlconv'])==='no')); // It's a text file, we don't get the BODY part
		if (tbs_Misc_GetFile($CurrVal,$File)) {
			if ($OnlyBody) $CurrVal = tbs_Html_GetPart($CurrVal,'BODY',false,true);
		} else {
			$CurrVal = '';
			if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert('Parameter \'file\'','Field '.$this->ChrOpen.$Loc->FullName.$this->ChrClose.' : unable to read the file \''.$File.'\'.',true);
		}
		$Loc->ConvHtml = false;
		$Loc->ConvProtect = false;
	}

	// OnFormat event
	if (isset($Loc->PrmLst['onformat'])) {
		if ($Loc->FirstMerge) {
			$Loc->OnFrmInfo = $Loc->PrmLst['onformat'];
			$Loc->OnFrmPrm = array(&$Loc->FullName,'',&$Loc->PrmLst,&$this);
			$ErrMsg = false;
			if (!$this->meth_Misc_UserFctCheck($Loc->OnFrmInfo,$ErrMsg)) {
				unset($Loc->PrmLst['onformat']);
				if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert('Parameter \'onformat\'',$ErrMsg);
				$Loc->OnFrmInfo = 'pi'; // Execute the function pi() just to avoid extra error messages 
			}
		}
		$Loc->OnFrmPrm[1] =& $CurrVal;
		if (isset($Loc->PrmLst['subtpl'])) {
			$this->meth_Misc_ChangeMode(true,$Loc,$CurrVal,true,true);
			call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmPrm);
			$this->meth_Misc_ChangeMode(false,$Loc,$CurrVal,true,true);
		} else {
			call_user_func_array($Loc->OnFrmInfo,$Loc->OnFrmPrm);
		}
	}

	// Select a value in a HTML option list
	$Select = isset($Loc->PrmLst['selected']);
	if ($Select) {
		if (is_array($CurrVal)) {
			$SelArray =& $CurrVal;
			$CurrVal =& tbs_Misc_UnlinkVar(' ');
		} else {
			$SelArray = false;
		}
	}

	// Convert the value to a string, use format if specified
	if (isset($Loc->PrmLst['frm'])) {
		$CurrVal = tbs_Misc_Format($Loc,$CurrVal);
		$Loc->ConvHtml = false;
	} else {
		if (!is_string($CurrVal)) $CurrVal = @strval($CurrVal);
	}

	// case of an 'if' 'then' 'else' options
	$OVal =& $CurrVal; // Must be assigner after $SelArray, if any
	$Script = isset($Loc->PrmLst['script']);
	if (isset($Loc->PrmLst['if'])) {
		if ($Loc->FirstMerge) {
			$this->meth_Merge_PhpVar($Loc->PrmLst['if'],false);
			if (isset($Loc->PrmLst['then'])) $this->meth_Merge_PhpVar($Loc->PrmLst['then'],true);
			if (isset($Loc->PrmLst['else'])) $this->meth_Merge_PhpVar($Loc->PrmLst['else'],true);
		}
		$x = str_replace($this->ChrVal,$CurrVal,$Loc->PrmLst['if']);
		if (tbs_Misc_CheckCondition($x)) {
			if (isset($Loc->PrmLst['then'])) $CurrVal =& tbs_Misc_UnlinkVar(''.$Loc->PrmLst['then']); // Now $CurrVal and $OVal are different
		} else {
			$Script = false;
			if (isset($Loc->PrmLst['else'])) {
				$CurrVal =& tbs_Misc_UnlinkVar(''.$Loc->PrmLst['else']); // Now $CurrVal and $OVal are different
			} else {
				$CurrVal = '';
			}
		}
	}

	if ($Script) {// Include external PHP script
		$File = str_replace($this->ChrVal,$CurrVal,$Loc->PrmLst['script']);
		$this->meth_Merge_PhpVar($File,false);
		$Switch = isset($Loc->PrmLst['subtpl']);
		$GetOb = ($Switch or isset($Loc->PrmLst['getob']));
		$CurrPrm =& $Loc->PrmLst; // Local var for users
		$this->meth_Misc_ChangeMode(true,$Loc,$CurrVal,$Switch,$GetOb);
		if (isset($Loc->PrmLst['once'])) {$x = @include_once($File);} else {$x = @include($File);}
		if ($x===false) {
			if (!isset($Loc->PrmLst['noerr'])) $this->meth_Misc_Alert('Parameter \'script\'','Field '.$this->ChrOpen.$Loc->FullName.$this->ChrClose.' cannot be merged because file \''.$File.'\' is not found or not readable.',true);
		}
		$this->meth_Misc_ChangeMode(false,$Loc,$CurrVal,$Switch,$GetOb);
	}

	if ($Loc->FirstMerge) {
		$Loc->FirstMerge = false;
		// Check HtmlConv parameter
		if (isset($Loc->PrmLst['htmlconv'])) {
			$x = strtolower($Loc->PrmLst['htmlconv']);
			$x = '+'.str_replace(' ','',$x).'+';
			if (strpos($x,'+esc+')!==false)  {tbs_Misc_ConvSpe($Loc); $Loc->ConvHtml = false; $Loc->ConvEsc = true; }
			if (strpos($x,'+wsp+')!==false)  {tbs_Misc_ConvSpe($Loc); $Loc->ConvWS = true; }
			if (strpos($x,'+js+')!==false)   {tbs_Misc_ConvSpe($Loc); $Loc->ConvHtml = false; $Loc->ConvJS = true; }
			if (strpos($x,'+no+')!==false)   $Loc->ConvHtml = false;
			if (strpos($x,'+yes+')!==false)  $Loc->ConvHtml = true;
			if (strpos($x,'+nobr+')!==false) {$Loc->ConvHtml = true; $Loc->ConvBr = false; }
			if (strpos($x,'+look+')!==false) {tbs_Misc_ConvSpe($Loc); $Loc->ConvLook = true; }
		} else {
			if ($this->HtmlCharSet===false) $Loc->ConvHtml = false; // No HTML
		}
		// We protect the data that does not come from the source of the template
		if (isset($Loc->PrmLst['protect'])) {
			$x = strtolower($Loc->PrmLst['protect']);
			switch ($x) {
			case 'no' : $Loc->ConvProtect = false; break;
			case 'yes': $Loc->ConvProtect = true; break;
			}
		} else {
			if ($this->Protect===false) $Loc->ConvProtect = false;
		}
	}

	// MaxLength
	if (isset($Loc->PrmLst['max'])) {
		$x = intval($Loc->PrmLst['max']);
		if (strlen($CurrVal)>$x) {
			if ($Loc->ConvHtml or ($this->HtmlCharSet===false)) {
				$CurrVal = substr($CurrVal,0,$x-1).$this->MaxEnd;
			} else {
				tbs_Html_Max($CurrVal,$x);
			}
		}
	}

	// HTML conversion, and TBS protection
	if ($Loc->ConvSpe) { // Using special parameters
		if ($Loc->ConvLook) {
			$Loc->ConvHtml = !tbs_Html_IsHtml($OVal);
			if ($Loc->ConvHtml===false) $OVal = tbs_Html_GetPart($OVal,'BODY',false,true);
		}
		if ($Loc->ConvHtml) {
			$this->meth_Conv_Html($OVal);
			if ($Loc->ConvBr) $OVal = nl2br($OVal);
		}
		if ($Loc->ConvEsc) $OVal = str_replace('\'','\'\'',$OVal);
		if ($Loc->ConvWS) {
			$check = '  ';
			$nbsp = '&nbsp;';
			do {
				$pos = strpos($OVal,$check);
				if ($pos!==false) $OVal = substr_replace($OVal,$nbsp,$pos,1);
			} while ($pos!==false);
		}
		if ($Loc->ConvJS) {
			$OVal = addslashes($OVal); // apply to ('), ("), (\) and (null)
			$OVal = str_replace("\n",'\n',$OVal);
			$OVal = str_replace("\r",'\r',$OVal);
			$OVal = str_replace("\t",'\t',$OVal);
		}
	}	elseif ($Loc->ConvHtml) {
		$this->meth_Conv_Html($OVal);
		if ($Loc->ConvBr) $OVal = nl2br($OVal);
	}
	if ($Loc->ConvProtect) $OVal = str_replace($this->ChrOpen,$this->ChrProtect,$OVal);
	if ($CurrVal!==$OVal) $CurrVal = str_replace($this->ChrVal,$OVal,$CurrVal);

	// Case when it's an empty string
	if ($CurrVal==='') {

		if ($Loc->MagnetId===false) {
			if (isset($Loc->PrmLst['.'])) {
				$Loc->MagnetId = -1;
			} elseif (isset($Loc->PrmLst['ifempty'])) {
				$Loc->MagnetId = -2;
			} elseif (isset($Loc->PrmLst['magnet'])) {
				$Loc->MagnetId = 1;
				$Loc->PosBeg0 = $Loc->PosBeg;
				$Loc->PosEnd0 = $Loc->PosEnd;
				if (isset($Loc->PrmLst['mtype'])) {
					switch ($Loc->PrmLst['mtype']) {
					case 'm+m': $Loc->MagnetId = 2; break;
					case 'm*': $Loc->MagnetId = 3; break;
					case '*m': $Loc->MagnetId = 4; break;
					}
				}
			} else {
				$Loc->MagnetId = 0;
			}
		}

		switch ($Loc->MagnetId) {
		case 0: break;
		case -1: $CurrVal = '&nbsp;'; break; // Enables to avoid blanks in HTML tables
		case -2: $CurrVal = $Loc->PrmLst['ifempty']; break;
		case 1:
			$Loc->Enlarged = true;
			tbs_Locator_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],false,false);
			break;
		case 2:
			$Loc->Enlarged = true;
			$CurrVal = tbs_Locator_EnlargeToTag($Txt,$Loc,$Loc->PrmLst['magnet'],false,true);
			break;
		case 3:
			$Loc->Enlarged = true;
			$Loc2 = tbs_Html_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,false,1,false);
			if ($Loc2!==false) {
				$Loc->PosBeg = $Loc2->PosBeg;
				if ($Loc->PosEnd<$Loc2->PosEnd) $Loc->PosEnd = $Loc2->PosEnd;
			}
			break;
		case 4:
			$Loc->Enlarged = true;
			$Loc2 = tbs_Html_FindTag($Txt,$Loc->PrmLst['magnet'],true,$Loc->PosBeg,true,1,false);
			if ($Loc2!==false) $Loc->PosEnd = $Loc2->PosEnd;
			break;
		}
		$NewEnd = $Loc->PosBeg; // Useful when mtype='m+m'
	} else {
		$NewEnd = $Loc->PosBeg + strlen($CurrVal);
	}

	$Txt = substr_replace($Txt,$CurrVal,$Loc->PosBeg,$Loc->PosEnd-$Loc->PosBeg+1);

	if ($Select) tbs_Html_MergeItems($Txt,$Loc,$CurrVal,$SelArray,$NewEnd);

	return $NewEnd; // Returns the new end position of the field

}