array(
'password' => $pass,
'username' => $name
),
/* These will be used as default values, if they are not
* specified in the XML file */
'asset' => array(
'template' => array(
'targetPath' => "/common/HTML",
'xml' => '
')));
/* Pass the data from the XML file to our update function */
update_templates($template_xml);
}
/* This function will be called by the XML parser when it
* finds an opening tag */
function start_element($parser, $name, $attrs) {
global $tagstack;
global $template_xml;
/* Push the current tag name onto our tag stack */
array_unshift($tagstack, $name);
/* If we have just entered the context of the path tag,
* append a new path to our path array */
/** Use this when using a n-sized array */
if ($name == "path")
$template_xml['path'][] = "";
}
/* This function will be called by the XML parser when it
* finds a closing tag */
function end_element($parser, $name) {
global $tagstack;
/* We are exiting a tag, so we want to pop it off of our
* tag stack */
array_shift($tagstack);
}
/* This function will be called by the XML parser when it
* finds character data between the opening and the closing
* of a tag */
function character_data($parser, $data) {
global $tagstack;
global $template_xml;
/* If we aren't deep enough to be in "template don't do anything */
if (count($tagstack) < 2)
return;
/* The tag we are currently looking at will be the first element
* on the tag stack. */
$current_tag = $tagstack[0];
/** here's where we traverse everything */
switch ($tagstack[0]) {
case "server":
echo ".";
global $host;
$host = $data;
break;
case "username":
global $name;
$name = $data;
break;
case "password":
global $pass;
$pass = $data;
break;
/* If we are inside of a path, we want to add to the path array */
case "path":
$last = count($template_xml['path']) - 1;
$template_xml['path'][$last] .= $data;
break;
default:
/* If we are in the context of the template tag and
* are inside of a child tag (notice we check the second element
* of the stack */
if ($tagstack[1] == "system-data-structure") {
/* If we haven't seen this tag before, then initialize it */
if (!isset($template_xml[$current_tag]))
$template_xml[$current_tag] = "";
/* Append the character data to the current tag */
$template_xml[$current_tag] .= $data;
}
}
}
function parse_xml($file) {
global $template_xml;
global $tagstack;
/* Initialize our template data structure */
$template_xml = array();
$template_xml['path'] = array();
/* Initialize the tag stack. This will be a stack of the tag
* structure we are currently in */
$tagstack = array();
/* Create an XML Parser object */
$xml_parser = xml_parser_create();
/* Turn off case folding on the XML Parser object, this
* will ensure that the parser will be case sensitive */
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
/* Set the callback functions for the XML Parser object. The object
* will call these functions when various actions happen */
xml_set_element_handler($xml_parser, "start_element", "end_element");
xml_set_character_data_handler($xml_parser, "character_data");
/* Try to open the XML file */
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
/* Read the data a bit at a a time */
while ($data = fread($fp, 4096)) {
/* Try to parse that bit */
if (!xml_parse($xml_parser, $data, feof($fp))) {
/* Give and error and quit if the parsing fails */
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
/* Free the parser data */
xml_parser_free($xml_parser);
}
function update_templates($templates) {
global $template_params;
global $host;
/* if the XML specified an XML string, use that */
if (isset($templates["xml"]))
//grab the xml, and unescape everything. As long as & is unescaped last, it should be fine
$xml = str_replace("&", "&", str_replace("<", "<", str_replace(">", ">", str_replace("'", "\'", str_replace(""", "\"", $templates["xml"])))));
$template_params["asset"]["template"]["xml"] = $xml;
if (isset($templates["server"])) {
echo "here";
}
/* if the XML specified a target path, use that */
if (isset($templates["target"]))
$template_params["asset"]["template"]["targetPath"] = $templates["target"];
/* Construct a SOAP client connecting to Cascade instance */
$client =
new SoapClient($host . "/ws/services/AssetOperationService?wsdl",
array('trace' => 1));
/* Iterate through each quote */
foreach ($templates["path"] as $path) {
$system_name = basename($path);
$parentFolder = dirname($path);
/* Insert our new parameters into the global request */
$template_params["asset"]["template"]["name"] = $system_name;
$template_params["asset"]["template"]["parentFolderPath"] = $parentFolder;
$template_params["asset"]["template"]["path"] = $path;
print("Updating template: " . $path . "\n");
try {
/* Pass this request to Web Services */
$response = $client->edit($template_params);
/* If something goes wrong check the value of
* __getLastResponse() */
// print("\n". $client->__getLastResponse() . "\n");
} catch (Exception $e) {
/* The request will throw an exception when parameters do
* not conform to the WSDL. This will *not* catch errors
* in the parameters themselves */
print("Template update request failed to conform to the WSDL.\n");
}
if (substr_count($client->__getLastResponse(), "Content is not allowed") > 0
| substr_count($client->__getLastResponse(), "The markup") > 0) {
echo "Fail: invalid XML. Please put everything in a global element tag";
}
if (substr_count($client->__getLastResponse(), "Login failed") > 0
| substr_count($client->__getLastResponse(), "User not found") > 0) {
echo "Invalid username or password";
}
}
}
?>