<?php 

if (isset($_GET['__xml'])){
	header("Content-type:text/xml");
	echo convert_to_EXCEL_XML($_GET['__xml']);
}
/**
 * convert_to_EXCEL_XML()
 * 
 * This function is use for producing Excel compatible XML feed from standard DHTMLX XML feed.
 * Main functions are adding a <columns> node for all <column> collection under <head> block and removing all <div> elements 
 * from header data. Secondly adding CDATA for all node values then echo the feed.
 *  
 * Usage :-  
 * 	1 convert_to_EXCEL_XML('https://www.ekwa.com/client_master/index.php/client_master/index/false/0/0/data_feed_template/XML/');
 * 
 * @param string $input_xml - Accepts XML feed url or strings.
 */
function convert_to_EXCEL_XML($input_xml){
	// load as simplexml object
	//libxml_use_internal_errors (true);
	//$xml_feed = simplexml_load_file($input_xml);
	/*//------------------------------------
	$opts = array('http' =>
			array(
					'method'  => 'GET',
					'header'  => "Content-Type: text/xml\r\n",
					'header' =>'Connection: keep-alive',
					'timeout' => 60
			)
	);
	$context  = stream_context_create($opts);
	$xml_feed = file_get_contents($input_xml,false,$context);
	//--------------------------------------*/
	//Both of the above methods failed due to timeout on getting xml content
	//Hence the following method used. (WCD on 22/05/2015)
	//=======================================
	// Timeout in seconds
	$timeout = 120;// Increase this if the log reports a xml feed timeout
	$temp = explode('://',$input_xml);//var_dump($temp);exit();
	if($temp[0] == 'http' || $temp[0] == 'https'){
		$url = explode('/',$temp[1]);
		$host = $url[0];
		array_shift($url);
		$url = "/". implode('/',$url);
	}else{
		$url = explode('/',$temp[0]);
		$host = $url[0];
		array_shift($url);
		$url = "/". implode('/',$url);
	}
	//var_dump($host,$url);exit();
	$fp = fsockopen($host, 80, $errno, $errstr, $timeout);
	
	if ($fp) {
		fwrite($fp, "GET $url HTTP/1.1\r\n");
		fwrite($fp, "Host: $host\r\n");
		fwrite($fp, "Connection: Close\r\n\r\n");
	
		stream_set_blocking($fp, TRUE);
		stream_set_timeout($fp,$timeout);
		$info = stream_get_meta_data($fp);
		$data ='';
		while ((!feof($fp)) && (!$info['timed_out'])) {
			$data .= fgets($fp, 4096);
			$info = stream_get_meta_data($fp);
			//ob_flush();
			flush();
		}
	
		if ($info['timed_out']) {
			error_log("XML FEED Connection Timed Out!",0);
		} else {
			//Trim off the HTML Headers and trailing 0 & CRLFs
			$xml_feed = rtrim(stristr($data,"<?xml ")," 0\n\r\0\t");
		}
	}
	//=======================================
	//error_log('SIZE OF XML FEED: '.strlen($xml_feed).'; LIBXML_PARSEHUGE: '.LIBXML_PARSEHUGE."\nXML URL IS: ".$input_xml."\nDATADEED IS:\n".$data."\nFILTERED XML IS:\n".$xml_feed,0);
	$xml_feed = simplexml_load_string($xml_feed);
	
	//do xpath to get tags
	$rows = $xml_feed->xpath('//rows');
	$head = $xml_feed->xpath('//head');
	$column = $xml_feed->xpath('//column');
	$row_node = $xml_feed->xpath('//row');
	
	//create a new doc
	$doc = new DOMDocument('1.0', 'UTF-8');
	
	//create root element
	$rows_element = $doc->createElement("rows");
		
	foreach($rows as $row){
		//if loaded object has attributes on its root element create them in the new doc
		foreach($row->attributes() as $a => $v) {
			//echo $a,'="',$v,"\"<br>";
			$valid_attr = $doc->createAttribute($a);
			$valid_attr->value = $v;
			$rows_element->appendChild($valid_attr);
		}	
	}//end if	

	//create head element
	$head_element = $doc->createElement("head");
	
	//create column element
	$columns_element = $doc->createElement("columns");
		
	//now add column from loaded simple obj's column list
	foreach($column as $col){
		//create column node	
		$column_element = $doc->createElement("column");
		if($col->attributes()){
			//add attributes
			foreach($col->attributes() as $a => $v) {
				//echo "$a => $v<br>";
				$valid_attr1 = $doc->createAttribute($a);
				$valid_attr1->value = $v;
				$column_element->appendChild($valid_attr1);			
			}
		}
		//remove <div> from column headders
		if(count(explode('>',$col))>0){
			$colValue = explode('>',$col);
			if(($colValue[1]) != ''){
				$colValue = explode("<", $colValue[1]);
			}else{$colValue[0] = $col;}				
		}else{
			$colValue[0] = $col;
		}
		$column_data = $doc->createCDATASection( $colValue[0] );
		$column_element->appendChild($column_data);
		$columns_element->appendChild($column_element);
	}//end foreach		
		
	$rows_element->appendChild($head_element);
	$head_element->appendChild($columns_element);
	
	//for row
	foreach($row_node as $ro){
			
		$row_element = $doc->createElement("row");
		if($ro->attributes()){
			foreach($ro->attributes() as $a => $v) {
				//echo "$a => $v<br>";
				$valid_attr2 = $doc->createAttribute($a);
				$valid_attr2->value = $v;
				$row_element->appendChild($valid_attr2);
			}//end foreach
		}//end if 			
		$rows_element->appendChild($row_element);

		//now create cell node and values
		$cell = $ro->cell;
				
		//for cell
		foreach($cell as $cel){
		
			$cell_element = $doc->createElement("cell");
			if($cel->attributes()){
				foreach($cel->attributes() as $a => $v) {
					//echo "$a => $v<br>";
					$valid_attr3 = $doc->createAttribute($a);
					$valid_attr3->value = $v;
					$cell_element->appendChild($valid_attr3);
				}//end foreach
			}//end if
			
			$cell_data = $doc->createCDATASection($cel );
			$cell_element->appendChild($cell_data);
			$row_element->appendChild($cell_element);
		}//end foreach cell
				
	}//end foreach row
	
	//finally attache root element to the dom then output as xml feed
	$doc->appendChild($rows_element);
	//header("Content-type:text/xml");
	return $doc->saveXML();
	
		
}//end of function 
 
 ?>