Location: PHPKode > projects > AWS WALLY > tempxml.php
<?php
/*
 * AWS XMS - Online Web Development
 * 
 * Copyright (c) 2010 Cezar Lucan hide@address.com
 * Licensed under GPL license.
 * http://www.aws-dms.com
 *
 * Date: 2010-10-24
 */
//0.1-beta3: domiterator/eachnamedreference/referencenamenode - will execute the code if a eachnamedreference child with the same name will be found;
//0.1-beta3: domiterator/eachnamedreference/referencenamenode - similar with eachreference but executes code for the named reference only;
//0.1-beta1: exec($directiveParentNode) - like runhere
//0.1-beta1: appRootElement[output] - when attribute is set to a value XSL will output required document; default xml;
//0.1-beta1: $HTMLPARSER is now $XMLPARSER
//0.1-beta1: no LANG support

session_start();
include_once 'defaults.php';
include_once 'includes/awsxmsutils.php';

//VERSION
define("AWS_XMLTEMPLATE_ENGINE",'0.1-beta3');

//app Name for lang and config
$appName 					= "";

//user defined parsers
$userParsers = array();


//OUTPUT TRANSLATOR ARRAY
$GLOBALS["translator"] ="";

///////////////////////////////////
///////////////CONFIG//////////////
///////////////////////////////////
if($_GET["config"])
		$CONFIG 	= new awsXML(file_get_contents($_GET["config"]));
else
	if(file_exists("config.xml"))
		$CONFIG 	= new awsXML(file_get_contents("config.xml"));

///////////////////////////////////////
///////////////XML TEMPLATE////////////
///////////////////////////////////////
if($_GET["use"])
	$TEMPLATE 	= new awsXML(file_get_contents($_GET["use"]));
else
	$TEMPLATE 	= new awsXML(file_get_contents(AWS_HOME));
	
///////////////////////////////////
////////////USER PARSERS///////////
///////////////////////////////////

if(file_exists("xmlparsers.xml"))
			$FILTERS 	= new awsXML(file_get_contents("xmlparsers.xml"));

///////////////////////////////////////
/////////////////HTML//////////////////
///////////////////////////////////////

$XMLPARSER 		= new awsXML();

$outputType = $TEMPLATE->doc->documentElement->getAttribute("output");

/////////////////////////////////////////
/////////////////CALLBACKS///////////////
/////////////////////////////////////////


function addContent(&$el)
						{
						$newEl = $GLOBALS["XMLPARSER"]->doc->importNode($el,true);
						$GLOBALS["XMLPARSER"]->doc->appendChild($newEl);
						}
						
function processReplacements(&$el)
							{
							$functionText = "";
							if(strtolower($el->nodeName) == "replace" && $el->nodeType ==1)
								{
								if($el->hasChildNodes())
									foreach($el->childNodes as $child)
										if($child->nodeType == 3)
											$functionText = $child->wholeText;
											
								$toRun = create_function('$el',$functionText);
								
								$docFragment = $el->ownerDocument->createDocumentFragment();
										
													
								//adaugam sursa nodului la fragment
								$docFragment->appendXML($toRun($el->parentNode));
										
								//inlocuim elementul import cu fragmentul generat
								$el->parentNode->replaceChild($docFragment,$el);
								}
							}
						
function doFilterImport(&$el)
						{
						$hasCheck = false;
						//file_put_contents("imp",file_get_contents(imp).$el->nodeName."\n");
						//daca nu avem sursa  folosim drept sursa default template-ul
						if($el->hasAttribute("source"))
							$documentFrom = new awsXML(file_get_contents($el->getAttribute("source")));
							
							//doar in cazul in care se da xpath facem modificarile
							if($el->hasAttribute("xpath"))
								{
								$xpath = $el->getAttribute("xpath");
	
										//creez fragmentul de document in HTML-ul nostru
										$docFragment = $el->ownerDocument->createDocumentFragment();
										
										$documentFrom->q($xpath);
										
										foreach($documentFrom->results as $result)
											{
											$newnode = $el->ownerDocument->importNode($result, TRUE);
											$docFragment->appendChild($newnode);
											}
											
										if($el->hasChildNodes())
											foreach($el->childNodes as $cn)
												if($cn->nodeType==1)
													switch($cn->nodeName)
													{
													case "check":
																if($cn->hasChildNodes())
																	foreach($cn->childNodes as $child)
																		if($child->nodeType == 3)
																				$functionText = $child->wholeText;
			
																$toRun = create_function('$doc',$functionText);
																$checkedFunctionReturnValue = $toRun($documentFrom);
																
																$hasCheck = true;
													break;
													}
													
										if(!$hasCheck || $hasCheck && $checkedFunctionReturnValue)
											{
											//inlocuim elementul import cu fragmentul generat
											if($el->hasAttribute("where"))
												{
												$last = false;
												$pathMembers = explode("/",$el->getAttribute("where"));
												if(sizeof($pathMembers)>0)
													{
													foreach($pathMembers as $k=>$member)
														{
														if($member)
															if(!$last)
																$last = $el->ownerDocument->createElement($member," ");
																else
																	{
																	$newChild = $el->ownerDocument->createElement($member," ");
																	$last->appendChild($newChild);
																	$last = $newChild;
																	}
														}
													$last->appendChild($docFragment);
													$el->parentNode->replaceChild($last,$el);
													}
												}
											else
												$el->parentNode->replaceChild($docFragment,$el);
											}
								
								}
							
						
						}
						
function doImport(&$el)
						{
						if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "runfirst":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													$toRun = create_function('$el',$functionText);
													$toRun($el);
										break;
										}
						//daca nu avem sursa  folosim drept sursa default template-ul
						if($el->hasAttribute("source"))
							$documentFrom = new awsHTML(file_get_contents($el->getAttribute("source")));
							else
								$documentFrom = $GLOBALS["TEMPLATE"];
						
						
						//doar in cazul in care se da xpath facem modificarile
						if($el->hasAttribute("xpath"))
							{
							$xpath = $el->getAttribute("xpath");
							
							/////////////////
							//import/filter//
							/////////////////
							if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "filter":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													$toRun = create_function('$doc,$el',$functionText);
													$toRun($documentFrom,$el);
										break;
										case "check":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;

													$toRun = create_function('$doc,$el',$functionText);
													$checkedFunctionReturnValue = $toRun($documentFrom,$el);
													
													$hasCheck = true;
										break;
										case "eachnode":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;

													$withResults = create_function('$el',$functionText);
										break;
										}

							if($hasCheck)
								{
								//daca functia returneaza o alta valoare in afara de false
								if($checkedFunctionReturnValue)
									//execut acelasi cod ca atunci cand nu avem un check pe acest element
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$documentFrom->q($xpath);

									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									$el->parentNode->replaceChild($docFragment,$el);					
									}
									else
										//in acest caz inlocuiesc element(import aici) cu nimic :D
										{
										$docFragment = $el->ownerDocument->createDocumentFragment();
										//adaugam sursa nodului la fragment
										$docFragment->appendXML(" ");
										
										//inlocuim elementul import cu fragmentul generat
										$el->parentNode->replaceChild($docFragment,$el);										
										}
								}
								else
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									//iau sursele nodului returnat de interogare
									$interogationSource = "";
									
									$res = "";
									$documentFrom->q($xpath);
									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);
									}
							
							}
						}
						
function doXmlImport(&$el)
						{
						if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "runfirst":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													$toRun = create_function('$el',$functionText);
													$toRun($el);
										break;
										}
						//daca nu avem sursa  folosim drept sursa default template-ul
						if($el->hasAttribute("source"))
							$documentFrom = new awsXML(file_get_contents($el->getAttribute("source")));
							else
								$documentFrom = $GLOBALS["TEMPLATE"];
						
						
						//doar in cazul in care se da xpath facem modificarile
						if($el->hasAttribute("xpath"))
							{
							$xpath = $el->getAttribute("xpath");
							
							/////////////////
							//import/filter//
							/////////////////
							if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "filter":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													
													$toRun = create_function('$doc,$el',$functionText);
													$toRun($documentFrom,$el);
										break;
										case "check":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													
													$toRun = create_function('$doc,$el',$functionText);
													$checkedFunctionReturnValue = $toRun($documentFrom,$el);
													
													$hasCheck = true;
										break;
										case "eachnode":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;

													$withResults = create_function('$el',$functionText);
										break;
										}

							if($hasCheck)
								{
								//daca functia returneaza o alta valoare in afara de false
								if($checkedFunctionReturnValue)
									//execut acelasi cod ca atunci cand nu avem un check pe acest element
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$res = "";
									$documentFrom->q($xpath);
									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
										
												
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);					
									}
									else
										//in acest caz inlocuiesc element(import aici) cu nimic :D
										{
										$docFragment = $el->ownerDocument->createDocumentFragment();
										//adaugam sursa nodului la fragment
										$docFragment->appendXML(" ");
										
										//inlocuim elementul import cu fragmentul generat
										$el->parentNode->replaceChild($docFragment,$el);										
										}
								}
								else
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$documentFrom->q($xpath);
									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);
									}
							}
						}
						
function doAdvancedImport(&$el)
						{
						if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "runfirst":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													$toRun = create_function('$el',$functionText);
													$toRun($el);
										break;
										}
						
						$data = "";
						$options = "";
						//
						
							if($el->hasChildNodes())
								foreach($el->childNodes as $ch)
									if($ch->nodeType==1)
										switch($ch->nodeName)
										{
										case "data":
											//POST and GET vars
											if($ch->hasChildNodes())
												foreach($ch->childNodes as $dataNode)
													if($dataNode->nodeType == 1)
														$data[$dataNode->nodeName] = $dataNode->textContent;
										break;
										
										default:
										//toate celelalte http, ftp sau socket
											if($ch->hasAttributes())
												foreach($ch->attributes as $attr)
													$options[$ch->nodeName][$attr->name] = $attr->value;
										break;
										}
										
						//creez http query
						if(sizeof($data)>0)
							$postdata = http_build_query($data);
							
						//caut in optiuni http sau ftp sau orice in afara de socket
						//si adaug postdata ca si key content
						foreach($options as $k=>$v)
							if(strtolower($k) != "socket")
								$options[$k]["content"] =  $postdata;
							
						//creez context doar daca am optiuni
						if(sizeof($options)>0)
							$context  = stream_context_create($options);
							
							
						//daca nu avem sursa  folosim drept sursa default template-ul
						if($el->hasAttribute("source"))
							$documentFrom = new awsHTML(file_get_contents($el->getAttribute("source"), false, $context));
						
						
						//doar in cazul in care se da xpath facem modificarile
						if($el->hasAttribute("xpath"))
							{
							$xpath = $el->getAttribute("xpath");
							
							/////////////////
							//import/filter//
							/////////////////
							if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "filter":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													
													$toRun = create_function('$doc,$el',$functionText);
													$toRun($documentFrom,$el);
										break;
										case "check":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													
													$toRun = create_function('$doc,$el',$functionText);
													$checkedFunctionReturnValue = $toRun($documentFrom,$el);
													
													$hasCheck = true;
										break;
										case "eachnode":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;

													$withResults = create_function('$el',$functionText);
										break;
										}

							if($hasCheck)
								{
								//daca functia returneaza o alta valoare in afara de false
								if($checkedFunctionReturnValue)
									//execut acelasi cod ca atunci cand nu avem un check pe acest element
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$documentFrom->q($xpath);
									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);					
									}
									else
										//in acest caz inlocuiesc element(import aici) cu nimic :D
										{
										$docFragment = $el->ownerDocument->createDocumentFragment();
										//adaugam sursa nodului la fragment
										$docFragment->appendXML(" ");
										
										//inlocuim elementul import cu fragmentul generat
										$el->parentNode->replaceChild($docFragment,$el);										
										}
								}
								else
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$documentFrom->q($xpath);

									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);
									}
							
							}
						}
						
function doAdvancedXmlImport(&$el)
						{
						if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "runfirst":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													$toRun = create_function('$el',$functionText);
													$toRun($el);
										break;
										}
						$data = "";
						$options = "";
						//
						
							if($el->hasChildNodes())
								foreach($el->childNodes as $ch)
									if($ch->nodeType==1)
										switch($ch->nodeName)
										{
										case "data":
											//POST and GET vars
											if($ch->hasChildNodes())
												foreach($ch->childNodes as $dataNode)
													if($dataNode->nodeType == 1)
														$data[$dataNode->nodeName] = $dataNode->textContent;
										break;
										
										default:
										//toate celelalte http, ftp sau socket
											if($ch->hasAttributes())
												foreach($ch->attributes as $attr)
													$options[$ch->nodeName][$attr->name] = $attr->value;
										break;
										}
										
						//creez http query
						if(sizeof($data)>0)
							$postdata = http_build_query($data);
							
						//caut in optiuni http sau ftp sau orice in afara de socket
						//si adaug postdata ca si key content
						foreach($options as $k=>$v)
							if(strtolower($k) != "socket")
								$options[$k]["content"] =  $postdata;
							
						//creez context doar daca am optiuni
						if(sizeof($options)>0)
							$context  = stream_context_create($options);
							
							
						//daca nu avem sursa  folosim drept sursa default template-ul
						if($el->hasAttribute("source"))
							$documentFrom = new awsXML(file_get_contents($el->getAttribute("source"), false, $context));
						
						
						//doar in cazul in care se da xpath facem modificarile
						if($el->hasAttribute("xpath"))
							{
							$xpath = $el->getAttribute("xpath");
							
							/////////////////
							//import/filter//
							/////////////////
							if($el->hasChildNodes())
								foreach($el->childNodes as $cn)
									if($cn->nodeType==1)
										switch($cn->nodeName)
										{
										case "filter":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													
													$toRun = create_function('$doc,$el',$functionText);
													$toRun($documentFrom,$el);
										break;
										case "check":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;
													
													$toRun = create_function('$doc,$el',$functionText);
													$checkedFunctionReturnValue = $toRun($documentFrom,$el);
													
													$hasCheck = true;
										break;
										case "eachnode":
													if($cn->hasChildNodes())
														foreach($cn->childNodes as $child)
															if($child->nodeType == 3)
																	$functionText = $child->wholeText;

													$withResults = create_function('$el',$functionText);
										break;
										}

							if($hasCheck)
								{
								//daca functia returneaza o alta valoare in afara de false
								if($checkedFunctionReturnValue)
									//execut acelasi cod ca atunci cand nu avem un check pe acest element
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$documentFrom->q($xpath);
									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);					
									}
									else
										//in acest caz inlocuiesc element(import aici) cu nimic :D
										{
										$docFragment = $el->ownerDocument->createDocumentFragment();
										//adaugam sursa nodului la fragment
										$docFragment->appendXML(" ");
										
										//inlocuim elementul import cu fragmentul generat
										$el->parentNode->replaceChild($docFragment,$el);										
										}
								}
								else
									{
									//creez fragmentul de document in HTML-ul nostru
									$docFragment = $el->ownerDocument->createDocumentFragment();
									
									$documentFrom->q($xpath);
									
									foreach($documentFrom->results as $result)
										{
										$newnode = $el->ownerDocument->importNode($result, TRUE);
										if(function_exists($withResults))
											$withResults($newnode);
										$docFragment->appendChild($newnode);
										}
									
									//inlocuim elementul import cu fragmentul generat
									$el->parentNode->replaceChild($docFragment,$el);
									}
							
							}
						}
						
function processMarkers(&$el)
						{
						$queryText = "";
						$filterText = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								if($child->nodeType == 3)
									$queryText = $child->wholeText;

						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
									$filterText = $child->wholeText;
									
									
						$GLOBALS["translator"][$queryText] = $filterText;
						}
											
function processEvals(&$el)
						{
						$queryText = "";
						$filterText = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								if($child->nodeType == 3)
									$queryText = $child->wholeText;

						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
									$filterText = $child->wholeText;
									
									
						$toEval = create_function("",$filterText);
						$GLOBALS["translator"][$queryText] = $toEval();
						}
						
function appInitDirective(&$el)
						{
						$functionText = "";
						$toReturnVarName = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								{
								if($child->nodeType == 3)
									$functionText = $child->wholeText;
								else
									if($child->nodeType == 1)
										switch(strtolower($child->nodeName))
											{
											case "return":
												$toReturnVarName = $child->textContent;
											break;
											}
										
								}
						
						
						$toRun = create_function("",$functionText);

						if($toReturnVarName)
								$GLOBALS["APPINIT"][$toReturnVarName] = $toRun();
								else
								$toRun();							

							
						$el->parentNode->removeChild($el);
						}

function userPHPCodeExecute(&$el)
						{
						$functionText = "";
						$toReturnVarName = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								{
								if($child->nodeType == 3)
									$functionText = $child->wholeText;
								else
									if($child->nodeType == 1)
										switch(strtolower($child->nodeName))
											{
											case "return":
												$toReturnVarName = $child->textContent;
											break;
											}
										
								}
						
						$toRun = create_function('&$el',$functionText);
						if($toRun)
							{
							if($toReturnVarName)
								$GLOBALS[$toReturnVarName] = $toRun($el->parentNode);
								else
								$toRun($el->parentNode);							
							}

							
						$el->parentNode->removeChild($el);
						}
						
function runCodeHere(&$el)
						{
						$functionText = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								if($child->nodeType == 3)
									$functionText = $child->wholeText;
										
						
						
						$toRun = create_function('$el',$functionText);
						$toRun($el->parentNode);
						
						$el->parentNode->removeChild($el);
						}
						
//executa functia pentru cu parametru fiecare  element in parte
//accesul la parametru se face: $el = func_get_arg(0); sau $el 
function processXpath(&$el)
						{
						$functionText = "";
						$queryText = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								if($child->nodeType == 3)
									$queryText = $child->wholeText;

						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
										$functionText = $child->wholeText;
						
						$GLOBALS["XMLPARSER"]->q($queryText)->each(create_function('$el',$functionText));
						}
						
//executa functia pentru fiecare intregul nodelist apeland=o cu cu parametru de tip $GLOBALS["XMLPARSER"];
//merge cu append, replace ... toate din clasa HTMLDoc
//accesul la parametru se face: $el = func_get_arg(0); sau $el
function processXpathNodeList(&$el)
						{
						$functionText = "";
						$queryText = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								if($child->nodeType == 3)
									$queryText = $child->wholeText;

						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
										$functionText = $child->wholeText;
						
						$runMe = create_function('$el',$functionText);
						$runMe($GLOBALS["XMLPARSER"]->q($queryText));
						}

/*
 * array(eticheta=>valoare, ....)
 * */
function processMatches(&$el)
						{
						$elementXPATH = "";
						$element = "";
						$elementC14n = "";
						$functionText = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								if($child->nodeType == 3)
									$elementXPATH = $child->wholeText;

						//continutul functiei care trebuie sa returneze array pt strtr
						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
									$functionText = $child->wholeText;
						
						$runMe = create_function('$el',$functionText);
						$pattern = $runMe();
						
						//caut element din XPATH si il pun in var element
						$GLOBALS["XMLPARSER"]->q($elementXPATH)->to($element);
						
						//luam sursa elem ca text
						foreach($element as $domel)
							$elementC14n.=$domel->C14N();
						
						
						foreach($pattern as $k=>$v)
								$elementC14n=preg_replace(AWS_ITERATOR_MATCH_PREFIX.$k.AWS_ITERATOR_MATCH_SUFFIX,$v,$elementC14n);
						
						//inlocuiesc sursa element din doc cu noua sursa
						$GLOBALS["XMLPARSER"]->q($elementXPATH)->replace($elementC14n);
						}
						
						
/*
 * mai ales pentru interogare baze de date
 * array(0=> array(eticheta=>valoare, ....), 1=>=> array(eticheta=>valoare, ....),...)
 * */
function processMatchesIterator(&$el)
						{
						$elementXPATH = "";
						$element = "";
						$unit = "";
						$elementC14n = "";
						$functionText = "";
						$translator = "";
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								{
									if($child->nodeType == 3)
										$elementXPATH = $child->wholeText;

									if($child->nodeName == "norecords")
										foreach($child->childNodes as $cnn)
											{
											$noRecordsFunctionText = $cnn->wholeText;
															
											$toRun = create_function("",$noRecordsFunctionText);
												
											$noRecords = $GLOBALS["XMLPARSER"]->doc->documentElement->ownerDocument->createDocumentFragment();
											//adaugam sursa nodului la fragment
											$noRecords->appendXML($toRun());
											}
								}


						//continutul functiei care trebuie sa returneze array pt strtr
						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
									$functionText = $child->wholeText;
						
						$runMe = create_function('$el',$functionText);
						$translator = $runMe();						
						
						//caut element din XPATH si il pun in var element
						$q4u = new awsXML($GLOBALS["XMLPARSER"]->q($elementXPATH)->results->item(0));
						$q4u->q("descendant-or-self::*[@unit]")->to($unit);
						
						//luam sursa elem unit ca text
						$elementC14n=$unit->item(0)->C14N();
						
						$collection = "";
						
						if(sizeof($translator)>0)
							foreach($translator as $pattern)
								{
								$subj = $elementC14n;
								foreach($pattern as $k=>$v)
									$subj=preg_replace(AWS_ITERATOR_MATCH_PREFIX.$k.AWS_ITERATOR_MATCH_SUFFIX,$v,$subj);
									
								$collection.=$subj;
								}
								else
									if($noRecords instanceof DOMDocumentFragment)
										$unit->item(0)->parentNode->insertBefore($noRecords,$unit->item(0));
							
						//inlociuesc unit cu toata colectia de mai sus
						$q4u->replace($collection);	
						}
						
// array(0=> array(eticheta=>valoare, ....), 1=>=> array(eticheta=>valoare, ....),...)
function processDomIterator(&$el)
						{
						$elementXPATH = "";
						$unit = "";
						$functionText = "";
						$translator = "";
						$eachnamedreference = false;
						
						if($el->hasChildNodes())
							foreach($el->childNodes as $child)
								{
								if($child->nodeType == 3)
									$elementXPATH = $child->wholeText;
								
								if($child->nodeName == "eachreference")
									foreach($child->childNodes as $cne)
										if($cne->nodeType == 3)
											$foreachreferenceFunction = create_function('&$el,$label,$value,$recordset',$cne->wholeText);
											
								if($child->nodeName == "eachnamedreference")
									$eachnamedreference = $child;
											
								if($child->nodeName == "norecords")
									foreach($child->childNodes as $cnn)
										{
										$noRecordsFunctionText = $cnn->wholeText;
														
										$toRun = create_function("",$noRecordsFunctionText);
											
										$noRecords = $GLOBALS["XMLPARSER"]->doc->documentElement->ownerDocument->createDocumentFragment();
										//adaugam sursa nodului la fragment
										$noRecords->appendXML($toRun());
										}
								}

						//continutul functiei care trebuie sa returneze array pt strtr
						if($el->parentNode->hasChildNodes())
							foreach($el->parentNode->childNodes as $child)
								if($child->nodeType == 3)
									$functionText = $child->wholeText;
						
						$runMe = create_function('$el',$functionText);
						$translator = $runMe();						
						
						//caut element din XPATH si il pun in var element
						$q4u = new awsXML($GLOBALS["XMLPARSER"]->q($elementXPATH)->results->item(0));
						$q4u->q("descendant-or-self::*[@unit]")->to($unit);

						
						if(sizeof($translator)>0)
							foreach($translator as $pattern)
								{
								$df = $unit->item(0)->ownerDocument->createDocumentFragment();
								$df->appendXML($unit->item(0)->C14N());
								$unitClone =$df->firstChild;
								
								$unitClone->removeAttribute("unit");
								$unitClone->setAttribute("clone","TRUE");
								$unit->item(0)->parentNode->insertBefore($unitClone,$unit->item(0));

								$unitQ = new awsXML($unitClone);
								
								foreach($pattern as $k=>$v)
									{
									$check = create_function('&$el','if($el->hasAttribute("skip")) return false; else return true;');
									
									if(!function_exists($foreachreferenceFunction))
											$unitQ->q('descendant-or-self::*[@reference="'.$k.'"]')->check($check)->replaceContent($v);
										else
											$unitQ->q('descendant-or-self::*[@reference="'.$k.'"]')->check($check)->replaceContent($v)->each($foreachreferenceFunction,$k,$v,$pattern);
											
									}
								
								foreach($pattern as $k=>$v)
									{
									//$eachnamedreference
									if($eachnamedreference)
										foreach($eachnamedreference->childNodes as $namedreferencenode)
											if($namedreferencenode->nodeName == $k)
												foreach($namedreferencenode->childNodes as $nrt)
													if($nrt->nodeType == 3)
														{
														$namedReferenceFunction = create_function('&$el,$label,$value,$recordset',$nrt->wholeText);
														$unitQ->q('descendant-or-self::*[@reference="'.$k.'"]')->each($namedReferenceFunction,$k,$v,$pattern);
														}
									}
								}
								else
									if($noRecords instanceof DOMDocumentFragment)
										$unit->item(0)->parentNode->insertBefore($noRecords,$unit->item(0));
								

						$q4u->q('descendant::*[@clone="TRUE"]')->removeAttr("clone");
						
						//unit remove
						$unit->item(0)->parentNode->removeChild($unit->item(0));
						}

					
function getFilter(&$el)
			{
			$tmpFilter = array();
			
			foreach($el->childNodes as $child)
				{
					if($child->nodeType == 1)
						{
						
						switch(strtolower($child->nodeName))
							{
							case "alias":
									foreach($child->childNodes as $textNode)
										if($textNode->nodeType == 3)
											$tmpFilter[strtolower($child->nodeName)] = $textNode->textContent;
							break;
							case "check":
									foreach($child->childNodes as $textNode)
										if($textNode->nodeType == 3)
											$tmpFilter[strtolower($child->nodeName)] = create_function('&$el',$textNode->textContent);
							break;
							case "xpath":
									foreach($child->childNodes as $textNode)
										if($textNode->nodeType == 3)
											$tmpFilter[strtolower($child->nodeName)] = $textNode->textContent;
							break;
							case "callback":
									foreach($child->childNodes as $textNode)
										if($textNode->nodeType == 3)
											$tmpFilter[strtolower($child->nodeName)] = create_function('&$el',$textNode->textContent);
							break;
							}
						}
				}
				
			$GLOBALS["userParsers"][] = $tmpFilter;
			}
						
						
function obFilter($buffer)
					{
					if(gettype($GLOBALS["translator"]) == "array")
						$buffer = strtr($GLOBALS["XMLPARSER"]->contentAs($GLOBALS["outputType"]),$GLOBALS["translator"]);
						
					return $buffer;
					}

$parsers = array(
				array(
						"alias"		=> "use",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/*[local-name()='use']",
						"callback"	=> "doFilterImport"
					 ),
				array(
						"alias"		=> "init",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/init",
						"callback"	=> "appInitDirective"
					 ),
				array(
						"alias"		=> "name",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/name",
						"callback"	=> "getAppName"
					 ),
				array(
						"alias"		=> "content",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/client/content/*",
						"callback"	=> "addContent"
					 ),
				array(
						"alias"		=> "import",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='import']",
						"callback"	=> "doImport"
					 ),
				array(
						"alias"		=> "xmlimport",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='xmlimport']",
						"callback"	=> "doXmlImport"
					 ),
				array(
						"alias"		=> "advancedimport",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='advancedimport']",
						"callback"	=> "doAdvancedImport"
					 ),
				array(
						"alias"		=> "advancedxmlimport",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='advancedxmlimport']",
						"callback"	=> "doAdvancedXmlImport"
					 ),
				array(
						"alias"		=> "exec",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='exec']",
						"callback"	=> "userPHPCodeExecute"
					 ),
				array(
						"alias"		=> "xpath",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/dom/filter/xpath",
						"callback"	=> "processXpath"
					 ),
				array(
						"alias"		=> "nodelist",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/dom/filter/nodelist",
						"callback"	=> "processXpathNodeList"
					 ),
				array(
						"alias"		=> "domiterator",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/dom/filter/domiterator",
						"callback"	=> "processDomIterator"
					 ),
				array(
						"alias"		=> "match",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/ob/filter/match",
						"callback"	=> "processMatches"
					 ),
				array(
						"alias"		=> "matchiterator",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/ob/filter/matchiterator",
						"callback"	=> "processMatchesIterator"
					 ),
				array(
						"alias"		=> "marker",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/ob/filter/marker",
						"callback"	=> "processMarkers"
					 ),
				array(
						"alias"		=> "eval",
						"check"		=> "",
						"target"	=> "TEMPLATE",
						"xpath"		=> "//app/filters/ob/filter/eval",
						"callback"	=> "processEvals"
					 ),
				array(
						"alias"		=> "replace",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='replace']",
						"callback"	=> "processReplacements"
					 ),
				array(
						"alias"		=> "runhere",
						"check"		=> "",
						"target"	=> "XMLPARSER",
						"xpath"		=> "//*[local-name()='runhere']",
						"callback"	=> "runCodeHere"
					 )
				);
				
				

foreach($parsers as $parser)
	{
	$check = TRUE;
	
	if(function_exists($parser["check"]))
		{
		$check = $parser["check"]();
		};
		
	if($check)
		$$parser["target"]->q($parser["xpath"])->each($parser["callback"]);
	}
	

					//////////////////////////////////////
					////GET GLOBAL SCOPE USER DIRECTIVES//
					//////////////////////////////////////

if($FILTERS instanceOf awsXML)
	$FILTERS->q("//parsers/item")->each("getFilter");

					//////////////////////////////////////
					////GET APP RELATED USER DIRECTIVES///
					//////////////////////////////////////
	
$TEMPLATE->q("//app/parsers/item")->each("getFilter");

					//PARSE USER DIRECTIVES//
foreach($userParsers as $parser)
	{
	$check = TRUE;
	
	if(function_exists($parser["check"]))
		$check = $parser["check"]();
		
		
	if($check)
		$XMLPARSER->q($parser["xpath"])->each($parser["callback"]);
	}

					//////////////////////////////////////
					////////////OUTPUT BUFERING///////////
					//////////////////////////////////////

ob_start("obFilter");

echo $XMLPARSER->contentAs($outputType);

ob_end_flush();

?>
Return current item: AWS WALLY