getRemoteDDBaseProxies.php 2.68 KB
<?php
/*
*  Executable to get Remote Bases description from DDBase 
*  and Merge with AMDA side Bases
*/

	if (!function_exists('__autoload')) {
		function __autoload($class_name) {
					require_once $class_name . '.php';
		}
	}
	
	$AMDA_IHM = getenv('AMDA_IHM');
     
	require_once $AMDA_IHM."/php/config.php"; 
	
	// only "DDBASE" Bases.xml
	if (!file_exists(DATAPATH."/RemoteData/Bases.xml")) {
			if (!is_dir(DATAPATH."/RemoteData")) mkdir(DATAPATH."/RemoteData", 0775, true);
			chmod(DATAPATH."/RemoteData",0775);
			echo 'No Bases.xml file : Will use Bases.xml from DDBase'.PHP_EOL;
			$basesDom = new DomDocument("1.0");
			
			if (!@$basesDom->load(DDSERVICE."/BASE/INFO/Bases.xml"))
					exit("No Bases.xml file at DDBase/INFO ? no Remote bases at all".PHP_EOL);
					
			$bases = $basesDom->getElementsByTagName("dataCenter");		
			foreach ($bases as $base) {
				$baseId = $base->getAttribute("xml:id");
				
				$baseDir = DATAPATH."/RemoteData/$baseId";
				$baseDirRemote = DDSERVICE."/BASE/INFO/bases/$baseId";
				if (!is_dir($baseDir)) mkdir($baseDir, 0775, true);
				chmod($baseDir,0775);
				if (!copy("$baseDirRemote/base.xml", "$baseDir/base.xml"))
					echo "Cannot copy base.xml for $baseId".PHP_EOL;
			}
	}
	// Merge with existing Bases.xml
	else { 
		$basesDom = new DomDocument("1.0");
		$basesDom->load(DATAPATH."/RemoteData/Bases.xml");
		
		$basesDomRemote =  new DomDocument("1.0");
		if (!@$basesDomRemote->load(DDSERVICE."/BASE/INFO/Bases.xml"))
					exit("No Bases.xml file at DDBase/INFO ? IMPEX only".PHP_EOL);
					
		$basesRemote = $basesDomRemote->getElementsByTagName("dataCenter");	
		foreach ($basesRemote as $baseRemote) {
		
			$baseId = $baseRemote->getAttribute("xml:id");
			$baseDir = DATAPATH."/RemoteData/$baseId";
			$baseDirRemote = DDSERVICE."/BASE/INFO/bases/$baseId";
			
			// add remote base if it doesn't exist
			if (!$basesDom->getElementById($baseId)) {
				echo $baseDir;
				if (!is_dir($baseDir)) mkdir($baseDir, 0775, true);
				chmod($baseDir,0775);
				if (!copy("$baseDirRemote/base.xml", "$baseDir/base.xml")) {
						echo "Cannot copy base.xml for $baseId".PHP_EOL;
				}
				else {
					$baseClone = $basesDom->importNode($baseRemote);
					$basesDom->documentElement->appendChild($baseClone);
					echo "Added $baseId".PHP_EOL;
					if ($baseId == "THEMIS"  || $baseId == "SPEASY_CDAWEB") {
						$center = new $baseId();
						$center->makeAllParams();
					}
				}
			}// just copy base xml
			else {
				if (!copy("$baseDirRemote/base.xml", "$baseDir/base.xml")) {
						echo "Cannot copy base.xml for $baseId".PHP_EOL;
				}
				else {
						echo "Copied base.xml for $baseId".PHP_EOL;
				}
			}
		}
	}
	
	$basesDom->save(DATAPATH."/RemoteData/Bases.xml");
	
?>