get_var_all("synced-files.file"); foreach($files as $file) { $filename = $file['path']; $cms_path = $file['cms_path']; create_or_update($filename, $cms_path); } } function create_or_update($filename, $cms_path) { global $client; $client = new SoapClient("http://martin:8080/ws/services/AssetOperationService?wsdl", array('trace' => 1)); if (is_readable($filename)) $contents = file_get_contents($filename); else return FALSE; if (exists_in_cms($cms_path)) { edit_in_cms($contents, $cms_path); } else { create_in_cms($contents, $cms_path); } } function exists_in_cms($cms_path) { global $client; $name = basename($cms_path); $dir = dirname($cms_path); /* remove leading slash */ if (substr($cms_path, 0, 1) == "/") $cms_path = substr($cms_path, 1); $read_params = array( 'authentication' => array( 'password' => "admin", 'username' => "admin" ), 'identifier' => array( 'id' => "", 'path' => $dir, 'type' => "folder" ) ); $client->readFolder($read_params); $result = $client->__getLastResponse(); if (!strstr($result, "$cms_path<")) return FALSE; else return TRUE; } function edit_in_cms($contents, $cms_path) { global $client; $name = basename($cms_path); $dir = dirname($cms_path); $create_params = array( 'authentication' => array( 'password' => "admin", 'username' => "admin" ), 'asset' => array( 'xmlBlock' => array( 'name' => $name, 'parentFolderPath' => $dir, 'path' => $cms_path, 'metadataSetPath' => "/Default", 'xml' => $contents ) ) ); $result = $client->edit($create_params); } function create_in_cms($contents, $cms_path) { global $client; $name = basename($cms_path); $dir = dirname($cms_path); $create_params = array( 'authentication' => array( 'password' => "admin", 'username' => "admin" ), 'asset' => array( 'xmlBlock' => array( 'name' => $name, 'parentFolderPath' => $dir, 'path' => "", 'metadataSetPath' => "/Default", 'xml' => $contents ) ) ); $response = $client->create($create_params); } $filename = Args::get("file"); $config = Resource::connect($filename); sync_files($config); /* XXX Resource.php XXX */ abstract class Resource { abstract function sync(); abstract function is_synced(); public $config; function __construct() { $this->config = array(); } public function get_var($key) { if (!isset($this->config)) return null; $k = explode(".", $key); // split on dots return $this->do_get($this->config, $k, false); } public function get_var_all($key) { if (!isset($this->config)) return null; $k = explode(".", $key); // split on dots return $this->do_get($this->config, $k, true); } public function set_var($key, $value) { if (!isset($this->config)) // error recovery return FALSE; $k = explode(".", $key); // split on dots $this->config = $this->do_set($this->config, $k, $value); // do the set return TRUE; } private function do_set($target, $path, $value) { if (count($path) <= 0) { // we've reached our destination return $value; // return the value } $element = array_shift($path); // get the next element if (!isset($target[$element])) $target[$element] = array(); $target[$element] = $this->do_set($target[$element], $path, $value); return $target; } private function do_get($target, $path, $return_all) { if (count($path) <= 0) { // we've reached our destination $to_ret = $target; /* check if we want to return everything or just the first element */ if (is_array($target) && $return_all == false) $to_ret = array_shift($target); if ($return_all == true && (!is_array($target) || $this->associative($to_ret))) $to_ret = array($to_ret); return $to_ret; // return the value } $element = array_shift($path); // get the next element if (!isset($target[$element])) return null; return $this->do_get($target[$element], $path, $return_all); } /* here we want to detect whether this is a file config or otherwise */ function connect($file) { $new_config = FileResource::connect($file); return $new_config; } protected function associative($array) { foreach($array as $key => $value) { if (!is_int($key)) return TRUE; } return FALSE; } } abstract class FileResource extends Resource { abstract function serialize(); protected $contents; protected $filename; function __construct($filename) { parent::__construct(); $this->filename = $filename; } function connect($file) { /* get the file extension */ $ext = str_replace('.','',strstr($file, '.')); /* check if the file is an XML file */ if (strtolower($ext) == "xml") $config_obj = new XMLResource($file); /* otherwise use a plain text output */ // else // $new_config = new TextResource($file); return $config_obj; } function read_file() { $temp = file_get_contents($this->filename); if ($temp == FALSE) // file read error return $temp; $this->contents = $temp; return true; } function write_file() { $result = file_put_contents($this->filename, $this->contents); if ($result <= 0 && count($contents) > 0) // file write error return FALSE; return TRUE; // return to child class } function sync() { /* check to make sure we can sync */ // if (is_writeable($this->filename) == FALSE) { // return FALSE; // } /* make a serialization of this config */ $serial = $this->serialize(); if ($serial == FALSE) return FALSE; $old_contents = $this->contents; /* record old contents */ $this->contents = $serial; /* record this serialization */ $result = $this->write_file(); if ($result == FALSE) { // the write failed $this->contents = $old_contents; return FALSE; } return TRUE; } function is_synced() { /* make a serialization of this config */ $serial = $this->serialize(); /* grab the file contents */ $temp = file_get_contents($this->filename); if ($temp == $serial && $serial != FALSE) // if this didn't work out return TRUE; // success else // something went wrong return FALSE; } } class XMLResource extends FileResource { function serialize() { $dom = new DOMDocument('1.0'); // create a dom /* create the root config node */ $root = $dom->createElement('config'); $root = $dom->appendChild($root); $this->arrayToDOM($this->config, $root, $dom); // turn config to dom $dom->formatOutput = true; // format output return $dom->saveXML(); // serialize the dom } function __construct($filename) { parent::__construct($filename); $this->initialize(); } function initialize() { /* we must be able to either read or write this file */ if (is_readable($this->filename) == FALSE && is_writeable($this->filename) == FALSE) { return FALSE; } $this->dom = new DOMDocument(); // create a DOM /* if we can read the DOM */ if (is_readable($this->filename)) { $this->read_file(); // read its contents $this->config = $this->parse_xml($this->contents); } } private function arrayToDOM($array, $node, $doc) { if (!is_array($array)) { $text = $doc->createTextnode($array); $node->appendChild($text); return; } foreach ($array as $key => $value) { if (is_array($value) && !$this->associative($value)) { foreach ($value as $vvalue) { $newNode = $doc->createElement($key); $newNode = $node->appendChild($newNode); $this->arrayToDOM($vvalue, $newNode, $doc); } } else { $newNode = $doc->createElement($key); $newNode = $node->appendChild($newNode); $this->arrayToDOM($value, $newNode, $doc); } } } private function parse_xml($xml) { $simplexml = simplexml_load_string($xml); return $this->xml_to_array($simplexml); } private function xml_to_array($xml) { if ($xml instanceof SimpleXMLElement) { $children = $xml->children(); $return = null; } $arrayed = FALSE; foreach ($children as $element => $value) { if ($value instanceof SimpleXMLElement) { $values = (array)$value->children(); if (count($values) > 0) { if (!isset($return[$element])) { $return[$element] = $this->xml_to_array($value); } else { if ($arrayed == FALSE) { $arrayed = TRUE; $return[$element] = array($return[$element], $this->xml_to_array($value)); } else { $return[$element][] = $this->xml_to_array($value); } } } else { if (!isset($return[$element])) { $return[$element] = (string) $value; } else { if (!is_array($return[$element])) $return[$element] = array($return[$element], (string)$value); else $return[$element][] = (string)$value; } } } } if (is_array($return)) { return $return; } else { return $false; } } } /* XXX Args.php XXX */ class Args { public function get($name) { global $argv; global $argc; if (isset($_POST[$name])) { return $_POST[$name]; } else if (isset($_GET[$name])) { return $_GET[$name]; } else { for ($i = 1; $i < $argc; $i++) { $arg = $argv[$i]; $splitted = explode("=", $arg); if ($splitted[0] == $name && count($splitted) > 1) return $splitted[1]; } } return null; } } ?>