<?php
class CXml
{
	var $pointer;
	var $index;
	
   function CXml($xml_data = false) {
   	if ($xml_data) $this->Set_xml_data( $xml_data );
	unset($this->pointer);
	unset($this->index);
   }
   
   function Set_xml_data( &$xml_data )
   {
       $this->index = 0;
       $this->pointer[] = &$this;
   
       $parser = xml_parser_create( "UTF-8" );
   
       xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
	   xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
       xml_set_object( $parser, $this );
       xml_set_element_handler( $parser, "_startElement", "_endElement");
       
       //xml_parse( $parser, $xml_data, true );
	   
	    if (!xml_parse( $parser, $xml_data, true )) {
    	    die(sprintf("XML error: %s at line %d pos %d",
                    xml_error_string(xml_get_error_code($parser)),
                    xml_get_current_line_number($parser), xml_get_current_column_number($parser)));
		}
       xml_parser_free( $parser );
   }

   
   function _startElement( $parser, $tag, $attributeList )   {
       foreach( $attributeList as $name => $value )
       {
           $value = $this->_cleanString( $value );
           $object->$name = $value;
       }
       //replaces the special characters with the underscore (_) in tag name
       $tag = preg_replace("/[:\-\. ]/", "_", $tag);
       eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );
       eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" );
       eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" );
       $this->index++;
   }

   function _endElement( $parser, $tag )   {
       array_pop( $this->pointer );
       $this->index--;
   }

   function _cleanString( $string )   {
       //return utf8_decode( trim( stripslashes( $string  ) ) );    
       //sreturn utf8_decode( trim( stripslashes( unhtmlspecialchars($string ) ) ) );
       //return utf8_decode( trim( unhtmlspecialchars( $string ) ) );
       return utf8_decode( trim( unhtmlspecialchars( $string ) ) );
   }
}

class xml extends CXml
{
		var $error_text;
		var $error_code;
        
        function xml($xml_call_url = false, $parameters = "", $nocache = false, $SSL = NULL) {
			global $xml_cache_path, $xml_use_ssl, $xml_use_compression, $xml_server_url, $xml_cache_path, $xml_cache_enabled,$xml_server_url_secure, $global_xml_debug,$scode;
			static $ch;
			
			if (is_null($SSL)) $SSL=$xml_use_ssl;
			if (!$xml_call_url) return false;
			
			if (!$xml_cache_enabled) $nocache = true;
			
			$debug_view = $_GET['debug_view'];
			if ($global_xml_debug) $debug_view=true;
			
			if ($xml_use_compression) $parameters .= "&compress=yes";
			//begin added Feb 13 2012 by Ajay to deal with sameserver nginx issue
			if ($scode!="") {
			   $tmpname="&scode=" . $scode;
			   $parameters .= $tmpname;
			}
			//end added Feb 13 2012 by Ajay to deal with sameserver nginx issue


			if ($parameters) {
				if (substr($parameters, 0, 1)=="&") $parameters = substr($parameters, 1);
				if ($parameters) {
					$parameters = "?".$parameters;
				}
			}
			$url = $xml_server_url.$xml_call_url.$parameters;
            if ($SSL) {
                $url =  $xml_server_url_secure.$xml_call_url.$parameters;
            }    
			$cache_file = "$xml_cache_path/" . md5($url) . ".xml";
			if ($debug_view) echo "<h2>Request:</h2><a href=\"$url\" target=\"_blank\">$url</a><BR>";
			//look for XML in cache
			if (!$nocache && ($xml_data = @file_get_contents($cache_file)) ) {
				if ($debug_view) echo "<b>Cached ($cache_file)</b><br>\n";
				$nocache = true;
			} else { //not cached XML
				if ($SSL) {
					if (!$ch) $ch = curl_init();
					curl_setopt($ch, CURLOPT_URL, $url);
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
					$xml_data=curl_exec($ch);
					//curl_close ($ch);
					//if ($xml_data) echo "<b>Successfully read " . strlen($xml_data) . " bytes from socket</b><BR>";
					//echo htmlspecialchars($contents);
				} else {
					$xml_data = @file_get_contents($url);//webportal
					//$xml_data = fetch_contents( $url ); //CURL
				}
				if (!$xml_data) {
					if ($debug_view) echo "file_get_contents failed! SSL=" . intval($SSL) . "<BR>";
					//if ($debug_view) echo "fetch_contents failed! SSL=" . intval($SSL) . "<BR>";
				}
				if ($xml_use_compression) $xml_data = gzuncompress ($xml_data);
			}
			
			if ($debug_view) echo "<h2>Reply:</h2><PRE>" . htmlspecialchars($xml_data) . "</PRE>";
			if (!$nocache) { //write to cache
				$file = @fopen($cache_file, "w");
				if ($file && $debug_view) echo "<b>Written to cache: $cache_file</b><BR>";
				@fwrite($file, $xml_data);
				@fclose($file);
			}
			
			$this->CXml($xml_data);
			
			if ($debug_view) echo "<h2>Object:</h2><PRE>" . print_r($this, true) . "</PRE>";
			
			//error handling
			if (isset($this->error[0]->row[0]->row[0]->Error)) {
				$this->error_text = $this->error[0]->row[0]->row[0]->Error;
				$this->error_code = $this->error[0]->row[0]->row[0]->ErrorCode;
			}
        } //function
		
		function extract_to_object(&$object) {
			if (is_object($this->root[0])) {
				foreach($this->root[0] as $name => $root) {
					if (is_object($root[0]->row[0])) {
						foreach($root[0]->row[0] as $var => $val) {
							$var = $name . "_" . $var;
							//echo "${var}=$val<BR>";
							$object->$var = $val;
						}
					}
				} //foreach
			}
		}
		
		function extract_namespaces_to_object(&$object) {
			if (is_object($this->root[0]->namespaces[0])) {
				foreach($this->root[0]->namespaces[0] as $name => $root) {
					if (is_array($root[0]->row)) {
						foreach($root[0]->row as $row) {
							if (is_object($row)) {
								foreach($row as $var => $val) {
									$var = $name . "_" . $var;
									//echo "\$${var}=$val<BR>";
									$object->$var = $val;
								}
								//echo "using pattern &lt;&lt;$name&gt;&gt;<BR><BR>";
								$object->$name .= $object->pattern($name);
							}
						} //foreach
					} //if
				} //foreach
			}
		}

} //class
?>