<?php

// signup_lib.php
// -----------------------------------------------------------------------------
// This page contains libraries for a simple EDI Automation Hub client
// written in PHP.
// The simple_signup() function returns the EDI Automation Hub's
// results as a native PHP array.
//
// Project    > dmClubNewYear08:C37
// Copyright  > (C) Digital Mail Limited (2005-2008)
// Author     > RAK for DML
// -----------------------------------------------------------------------------

// INITIALISE VARIABLES

// The absolute URL of edi_automation_hub.php
$edi_hub_url = 'http://' . $_SERVER['HTTP_HOST'] . '/dmx/edi/edi_automation_hub.php';

// -----------------------------------------------------------------------------

function simple_signup($params) {
	// Execute the 'AutoNumSignup' EDI action, using the supplied array of parameters
	// First construct the request URL
	$q ='?edi_action=AutoNumSignup';
	reset($params);
	while (list($key, $value) = each($params)) $q .= '&' . $key . '=' . urlencode($value);
	// Now do the EDI request
	$response_xml = file_get_contents($GLOBALS['edi_hub_url'] . $q);
	// Decode the XML response
	$objXML         = new xml2Array();
	$response_array = $objXML->parse($response_xml);
	$inner_nodes = $response_array[0]['children'];
	for ($i=0; $i<count($inner_nodes); $i++) {
		$res_array[strtolower($inner_nodes[$i]['name'])] = utf8_decode($inner_nodes[$i]['tagData']);
	}
	// Return decoded response as a PHP array
	return $res_array;
}

// -----------------------------------------------------------------------------

class xml2Array {
	// Parse XML into a PHP array
  
	var $arrOutput = array();
	var $resParser;
	var $strXmlData;
  
	// -----------------------------------------------------------------------------
	
	function parse($strInputXML) {
  
		$this->resParser = xml_parser_create();
		xml_set_object($this->resParser,$this);
		xml_set_element_handler($this->resParser, 'tagOpen', 'tagClosed');
          
		xml_set_character_data_handler($this->resParser, 'tagData');
      
		$this->strXmlData = xml_parse($this->resParser,$strInputXML );
		if (!$this->strXmlData) {
			die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($this->resParser)),
			xml_get_current_line_number($this->resParser)) .
			'<hr>' .
			$strInputXML);
 		}
                          
		xml_parser_free($this->resParser);
          
		return $this->arrOutput;
	}
   
	function tagOpen($parser, $name, $attrs) {
		$tag = array('name'=>$name, 'attrs'=>$attrs);
		array_push($this->arrOutput,$tag);
	}
  
	function tagData($parser, $tagData) {
		if (trim($tagData)) {
			if (isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
				$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
			}
			else {
				$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
			}
		}
	}
  
	function tagClosed($parser, $name) {
		$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
		array_pop($this->arrOutput);
	}
}

// -----------------------------------------------------------------------------

?>