<?
/*
	XML szerkezete mindig: <list> benne az <item>-ekkel
*/

class xml {
	var $path='xml/';
	var $text; // true kell, ha az <item>-ek textNode-ok (pl: <item>dsfdsfds</item>)

function xml($name='',$path_type=false) {
	/* $path_type:
		1: ajax;
		2: teljes �tvonal
	*/
	if($path_type==2) $this->file=$name;
	else{
		if($path_type==1) $this->path='../../'.$this->path;
		if($name) $this->path($name);
	}
	if($this->file){
		if(!strstr($this->file,'.php')){
			if(substr(($this->file),-4)!='.xml') $this->file.='.xml';
		}
	}
}

function path($name) {
	$this->file=$this->path.$name;
}

function load() {

	$doc=new DOMDocument;

	$doc->load($this->file);

	$list_set = $doc->getElementsByTagName('list');
	$list = $list_set->item(0);
	$item_set = $list->childNodes;

	$key=0;
	for($i=0;$i<$item_set->length;$i++) {
		$item = $item_set->item($i);
		if($item->nodeType == XML_ELEMENT_NODE) {
			$element_set = $item->childNodes;
			for($j=0;$j<$element_set->length;$j++) {
				$element = $element_set->item($j);
				if($element->nodeType == ($this->text ? XML_TEXT_NODE : XML_ELEMENT_NODE)) {
					$value=iconv('utf-8','iso-8859-2',$element->nodeValue);
					if($this->text)
						$data[$key]=$value;
					else
						$data[$key][$element->nodeName]=$value;
				}
			}
			$key++;
		}
	}
	if($this->reverse_list && $data) krsort($data);

	return $data;
}

function save($list,$turnInsideOut=false) {
	if($turnInsideOut) $list=$this->array_turnInsideOut($list); //t�mb kiford�t�sa (ha POST-b�l j�n)
	$str_list='<?xml version="1.0" encoding="ISO-8859-2"?>'.
		"\n<list>\n";

	foreach($list as $index => $item) {
		$str_item="\t<item>";
		$has_value=false;
		if(is_array($item)){
			$str_item.="\n\t";
			foreach($item as $k=>$v) {
				if($v) {
					$has_value=true;
					$v=$this->cleanText($v);
				}
				$str_item.="\t<$k>$v</$k>\n\t";
			}
		}elseif($this->text) {
			$has_value=true;
			$item=$this->cleanText($item);
			$str_item.=$item;
		}
		if($has_value) $str_list.=$str_item."</item>\n"; //�res sorokat ne vegye f�l
	}

	$str_list.="</list>";

	if(!$this->file) return $str_list;

	$fp=fopen($this->file,'w') or die("Could not open $file for writing");
	fputs($fp,$str_list);
	fclose($fp);
}

function array_turnInsideOut($a) {
	//t�mb kiford�t�sa
	foreach($a as $k1=>$group)
		foreach($group as $k2=>$value)
			$b[$k2][$k1]=$value;
	return $b;
}

function cleanText($s) {
	//cser�k
	$s=str_replace('\\','',$s);
	$s=str_replace('"',"''",$s);
	//cdata
	foreach(array('<','>','&') as $ch) if(strstr($s,$ch)) {$cdata=true;break;}
	if($cdata) $s="<![CDATA[$s]]>";
	return $s;
}

function save_message($list,$turnInsideOut=false) {

	if($turnInsideOut) $list=$this->array_turnInsideOut($list);


	$fp=fopen($this->file,'r') or die("Could not open $this->file for writing");
	$str_item = fread($fp, filesize($this->file)-7);
	fclose($fp);

	$str_item.="\t<item>";
	$str_item.="\n\t";

	foreach($list as $k=>$v) {
			$str_item.="\t<$k>$v</$k>\n\t";
	}


	$str_item.="</item></list>";

	$fp=fopen($this->file,'w') or die("Could not open $file for writing");
	fputs($fp,$str_item);
	fclose($fp);

}

}
?>