updateOnly = $updateOnly; /* wsdl location depends on DDServer AMDA is connected to * DDServer is defined in DD.res */ $SERVER = file(DDRES_DIR."/DD.res"); if (!file_exists(DATAPATH."wsdl.location")) { $msg = "No WSDL location file -> Nothing done".PHP_EOL; error_log($msg, 1, email); exit($msg); } /* Get WSDL Location */ $WsdlSources = file(DATAPATH."wsdl.location"); $isHOST = false; for ($i = 0; $i < count($WsdlSources); $i++) { list($HOST,$WSDL) = sscanf($WsdlSources[$i],"%s %s"); if ($HOST.PHP_EOL == $SERVER[0]) { $isHOST = true; break; } } if (!$isHOST) { $msg = "No WSDL location for ".$SERVER[0].PHP_EOL; error_log($msg, 1, email); exit($msg); } try { $this->client = new SoapClient($WSDL); } catch (SoapFault $exception) { $msg = $exception->faultstring.PHP_EOL; error_log($msg, 1, email); exit($msg); } // reset log file if (file_exists(log)) unlink(log); date_default_timezone_set('UTC'); } /* Just copy URL to Info/Bases.xml */ public function getAvailableExternalBases() { try { $extBases = $this->client->getAvailableExternalBases(); } catch (SoapFault $exception) { $msg = $exception->faultstring.PHP_EOL; error_log($msg, 1, email); exit($msg); } if ($extBases === "NOEXTERNALBASES") { if (!$this->updateOnly) error_log($extBases.PHP_EOL, 1, email); return $extBases; } $extBasesDom = new DomDocument("1.0"); if (!$extBasesDom->load($extBases)) { $msg = "Can't load Bases.xml".PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } $External = $extBasesDom->getElementsByTagName("External")->item(0); if ($External == NULL) { $msg = "no External Tag found in Bases.xml".PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } $extBasesNames = $External->getElementsByTagName("CenterID"); if ($extBasesNames->length === 0) { $msg = "no external bases found in Bases.xml".PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } if (!$this->updateOnly) { if (!is_dir(RemoteData)) mkdir(RemoteData, 0775, true); chgrp(RemoteData, APACHE_USER); chmod(RemoteData, 0775); if (!is_dir(RemoteData.'PARAMS')) mkdir(RemoteData.'PARAMS', 0775, true); chgrp(RemoteData.'PARAMS', APACHE_USER); chmod(RemoteData.'PARAMS', 0775); $xsl = new DOMDocument("1.0"); $xsl->load(XMLPATH."basesRaw2bases.xsl"); $xslt = new XSLTProcessor(); $xslt->importStylesheet($xsl); if (!$xslt->transformToDoc($extBasesDom)->save(RemoteData."Bases.xml")){ $msg = "problem while transforming Bases.xml".PHP_EOL; error_log($msg, 1, email); exit($msg); } } return $extBasesNames; } /* Get list of available local missions for a given user*/ public function getUserAvailableMissions($user) { try { $localmissions = $this->client->getUserAvailableLocalMissions($user); $externalmissions = $this->client->getUserAvailableExternalMissions($user); } catch (SoapFault $exception) { /*$log = fopen(DATAPATH."mylog","w"); fprintf($log,$exception->faultstring."\n"); $functions = $this->client->__getFunctions (); foreach ($functions as $function) fprintf($log,$function."\n");*/ return array('success' => false,'message' => $exception->faultstring); } return array('success' => true,'local' => $localmissions,'external' => $externalmissions); } /* Just copy URL to LocalData/Amda_Users.xml */ public function getUserGroups() { try { $group_info = $this->client->getUserGroups(); } catch (SoapFault $exception) { exit("Problems with DD Web Services".PHP_EOL); } if ($group_info === "NOUSERGROUPSSPECIFIED") return false; if (!file_exists(LocalData)) mkdir(LocalData, 0755, true); if (!copy($group_info,LocalData."AMDA_Users.xml")) exit("problem with copying AMDA_Users info".PHP_EOL); return true; } /* Get Time Restriction from VI in DDBASE */ public function getTimeRestriction($dataSet) { try { $restrictions = $this->client->getTimeRestriction($dataSet); } catch (SoapFault $exception) { error_log($exception->faultstring.PHP_EOL, 1, email); return FALSE; } if ($restrictions['success'] === FALSE) { error_log('TimeRestriction: '.$dataSet.' '.$restrictions['days'], 1, email); return FALSE; } return $restrictions; } /* Just copy URL to LocalData/Missions.xml */ public function getAvailableMissions() { try { $localMissions = $this->client->getAvailableMissions(); } catch (SoapFault $exception) { $msg = $exception->faultstring.PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } if ($localMissions === "NOLOCALDATA") { if (!$this->updateOnly) error_log($localMissions.PHP_EOL, 1, email); return $localMissions; } $locBasesDom = new DomDocument("1.0"); $locBasesDom->load($localMissions); $local = $locBasesDom->getElementsByTagName("Local")->item(0); if (!$local) { $msg = "no local data found in Missions.xml".PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } $locBasesNames = $local->getElementsByTagName("MissionID"); if ($locBasesNames->length === 0) { $msg = "no missions found in Missions.xml".PHP_EOL; error_log($msg, 1, email); exit($msg); } if (!is_dir(LocalData)) mkdir(LocalData, 0775, true); if (!is_dir(LocalDataParam)) mkdir(LocalDataParam, 0775, true); if (!is_dir(LocalDataTemplate)) mkdir(LocalDataTemplate, 0775, true); chgrp(LocalData,APACHE_USER); chgrp(LocalDataParam,APACHE_USER); chgrp(LocalDataTemplate,APACHE_USER); chmod(LocalData, 0775); chmod(LocalDataParam, 0775); chmod(LocalDataTemplate, 0775); if (!copy($localMissions,LocalData."Missions.xml")) { $msg = "problem with copying AMDA Local Missions info".PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msgL); } return $locBasesNames; } public function isMulti($baseNode){ $multi = $baseNode->getAttribute("missions"); if ($multi == "") return false; $nameArr = explode(";",$multi); if (count($nameArr) < 2) return false; return $nameArr; } public function makeMulti($locbase, $multiNames){ $templatename = LocalDataTemplate."DD_".$locbase->nodeValue.".template"; $paramtemplate = LocalDataTemplate.$locbase->nodeValue.".template"; $suffix = array(); foreach ($multiNames as $item) { $temp = explode("-",$item); if (count($temp) > 1) { $suffix[] = $temp[1]; } else { $suffix[] = substr($item,-1); } } $template = new DomDocument("1.0"); $status = $template->load($templatename); if (!$status) { print_r("Cannot load ".$templatename.PHP_EOL); if (!$this->updateOnly) error_log("Cannot load ".$templatename.PHP_EOL,1,email); } $paramdom = new DomDocument("1.0"); $status = $paramdom->load($paramtemplate); if (!$status) { print_r("Cannot load ".$paramtemplate.PHP_EOL); if (!$this->updateOnly) error_log("Cannot load ".$paramtemplate.PHP_EOL,1,email); } // Create DD_Mission.xml $locbasename = LocalData."DD_".$locbase->nodeValue.".xml"; $locbasedom = new DomDocument("1.0"); $root = $locbasedom->importNode($template->documentElement); $amda = $locbasedom->importNode($template->getElementsByTagName("dataCenter")->item(0)); $missionMain = $locbasedom->importNode($template->getElementsByTagName("mission")->item(0)); $mission = $locbasedom->importNode($template->getElementsByTagName("mission")->item(0), true); $targets = $mission->getElementsByTagName("target"); $targetNames = array(); foreach ($targets as $target) if ($target->parentNode->tagName == 'mission') // mission only targets $targetNames[] = $target->nodeValue; $observatory = $mission->getElementsByTagName("observatory")->item(0); $instruments = $observatory->getElementsByTagName("instrument"); // create param descriptor Mission.xml $parambasename = LocalDataParam.$locbase->nodeValue.".xml"; $parambasedom = new DomDocument("1.0"); $paramroot = $parambasedom->importNode($paramdom->documentElement); $instrumentNon = array(); foreach ($instruments as $instrument) { // absent instriments processing if ($instrument->hasAttribute("absent")) { $absent = $instrument->getAttribute("absent"); $absentArray = explode(";",$absent); $tempArr = array(); if (count($absentArray) > 1) { for ($i = 0; $i < count($suffix); $i++) $tempArr[$i] = false; for ($j = 0; $j < count($absentArray); $j++) { $temp = array_search($absentArray[$j], $suffix, true); $tempArr[$temp] = true; } } else { for ($i = 0; $i < count($suffix); $i++) { $tempArr[$i] = $absent == $suffix[$i]; } } $instrumentNon[$instrument->getAttribute("name")] = $tempArr; } } foreach ($targetNames as $targetName) { $missionMain->appendChild($locbasedom->createElement("target",$targetName)); } for ($index = 0; $index < count($suffix); $index++) { $missionArr[$index] = $observatory->cloneNode(); $missionArr[$index]->setAttribute("name", str_replace("XXX", $suffix[$index], $missionArr[$index]->getAttribute("name"))); $missionArr[$index]->setAttribute("xml:id", str_replace("XXX", $suffix[$index], $missionArr[$index]->getAttribute("name"))); $instruments = $observatory->getElementsByTagName("instrument"); foreach ($instruments as $instrumentReal){ if (!$instrumentNon[$instrumentReal->getAttribute("name")] || !$instrumentNon[$instrumentReal->getAttribute("name")][$index]) { $instrument = $instrumentReal->cloneNode(true); $instrument->setAttribute("desc",str_replace("XXX", $suffix[$index], $instrument->getAttribute("desc"))); $instrument->setAttribute("xml:id",str_replace("XXX", $suffix[$index], $instrument->getAttribute("xml:id"))); $tags = array("dataset", "parameter", "component"); foreach ($tags as $tag) { $all = $instrument->getElementsByTagName($tag); foreach ($all as $one) { if ($tag == 'parameter') { $id = $one->getAttribute("xml:id"); // create param tag in param XML descriptor $param = $paramdom->getElementById($id); if ($param){ $paramAdd = $parambasedom->importNode($param, true); $paramAdd->setAttribute("xml:id",str_replace("XXX", strtolower($suffix[$index]), $id)); $paramAdd->setAttribute("vi",str_replace("XXX", strtolower($suffix[$index]), $paramAdd->getAttribute("vi"))); $yTitle = $paramAdd->getElementsByTagName("YTITLE"); if ($yTitle->length > 0) { $value = $yTitle->item(0)->nodeValue; $yTitle->item(0)->nodeValue = str_replace("XXX", $suffix[$index], $value); } $paramroot->appendChild($paramAdd); } } $one->setAttribute("xml:id",str_replace("XXX", strtolower($suffix[$index]), $one->getAttribute("xml:id"))); if ($tag == 'dataset' && $one->hasAttribute("vi")) $one->setAttribute("vi",str_replace("XXX", strtolower($suffix[$index]), $one->getAttribute("vi"))); } } //apply to new tags included for EPNResources $all = $instrument->getElementsByTagName("alternateName"); foreach ($all as $one) $one->nodeValue = str_replace("XXX", strtolower($suffix[$index]), $one->nodeValue); $datasets = $instrument->getElementsByTagName("dataset"); foreach ($datasets as $dataset) { $info = $dataset->getElementsByTagName("info"); if ($info->length > 0) { $info = $info->item(0); $title = $info->getElementsByTagName("title"); if ($title->length > 0) { $title = $title->item(0); $title->nodeValue = str_replace("XXX", strtolower($suffix[$index]), $title->nodeValue); } $desc = $info->getElementsByTagName("description"); if ($desc->length > 0) { $desc = $desc->item(0); $desc->nodeValue = str_replace("XXX", strtolower($suffix[$index]), $desc->nodeValue); } $targets = $info->getElementsByTagName("target"); foreach ($targets as $target) $target->nodeValue = str_replace("XXX", strtolower($suffix[$index]), $target->nodeValue); } } $missionArr[$index]->appendChild($instrument); } } $missionMain->appendChild($missionArr[$index]); } $amda->appendChild($missionMain); $root->appendChild($amda); $locbasedom->appendChild($root); $status = $locbasedom->save($locbasename); if (!$status) { print_r("Cannot save ".$locbasename.PHP_EOL); error_log("Cannot save ".$locbasename.PHP_EOL,3,log); } $parambasedom->appendChild($paramroot); $status = $parambasedom->save($parambasename); if (!$status) { print_r("Cannot save ".$parambasename.PHP_EOL); error_log("Cannot save ".$parambasename.PHP_EOL,3,log); } } public function getDDLocalParamsFiles($locBases) { if (!$this->updateOnly) { //remove old DD_*.xml files (install mode) foreach (glob(LocalData."DD_*.xml") as $file) unlink($file); //remove old local parameters files foreach (glob(LocalDataParam."*.xml") as $file) unlink($file); //remove old template (install mode) foreach (glob(LocalDataTemplate."*.template") as $file) unlink($file); if (file_exists(spectraXml)) unlink(spectraXml); //remove old targets file if (file_exists(targetsXml)) unlink(targetsXml); //if (!is_dir(LocalDataTemplate)) mkdir(LocalDataTemplate); } foreach ($locBases as $locbase) { $ddfilename = "DD_".$locbase->nodeValue.".xml"; $ddparamfilename = $locbase->nodeValue.".xml"; $ddtemplate = "DD_".$locbase->nodeValue.".template"; $ddparamtemplate = $locbase->nodeValue.".template"; //get DD_*.xml or DD_*template file $multiNames = $this->isMulti($locbase); if (!$this->updateOnly || !file_exists(LocalData.$ddfilename)) { if ($multiNames) system("svn export ".SVN_TEMPLATES.$ddtemplate." ".LocalDataTemplate.$ddtemplate); else system("svn export ".SVN_DDMISSIONSINFO.$ddfilename." ".LocalData.$ddfilename); } //get local params for this mission if (!$this->updateOnly || !file_exists(LocalDataParam.$ddparamfilename)) { if ($multiNames) system("svn export ".SVN_TEMPLATES.$ddparamtemplate." ".LocalDataTemplate.$ddparamtemplate); else system("svn export ".SVN_LOCALPARAMSINFO.$ddparamfilename." ".LocalDataParam.$ddparamfilename); } if ($multiNames && !$this->updateOnly) $this->makeMulti($locbase, $multiNames); } //get SpectraParamList.xml if (!$this->updateOnly) { system("svn export ".SVN_DDMISSIONSINFO."SpectraParamList.xml ".spectraXml); } //get targets.xml if (!$this->updateOnly) { system("svn export ".SVN_DDMISSIONSINFO."targets.xml ".targetsXml); } //merge local params for each missions in LocalParams.xml + generate LocalParamsList.xml if (!$this->updateOnly) { $paramsDom = new DomDocument("1.0"); $paramsDom->formatOutput = true; $paramsDom->preserveWhiteSpace = false; $paramsRoot = $paramsDom->createElement('LOCAL'); $paramsDom->appendChild($paramsRoot); $listDom = new DomDocument("1.0"); $listDom->formatOutput = true; $listDom->preserveWhiteSpace = false; $listRoot = $listDom->createElement('LOCAL'); $listDom->appendChild($listRoot); $dom = new DomDocument("1.0"); foreach ($locBases as $locbase) { $ddparamfilename = $locbase->nodeValue.".xml"; $dom->load(LocalDataParam.$ddparamfilename); $params = $dom->getElementsByTagName('PARAM'); foreach ($params as $param) { $node = $paramsDom->importNode($param,TRUE); $paramsRoot->appendChild($node); $id = $param->getAttribute('xml:id'); $node = $listDom->createElement('PARAM',$id); $listRoot->appendChild($node); } } $paramsDom->save(paramXml); $listDom->save(paramListXml); } } /* Just copy URL to DATA/$BaseID/base.xml */ public function getAvailableExternalData($BaseID) { try { $extData = $this->client->getAvailableExternalData($BaseID); } catch (SoapFault $exception) { $msg = $exception->faultstring.PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } if ($extData === "NOEXTERNALDATA") return false; //drwxrwxr-x if (!$this->updateOnly) { if (!file_exists(RemoteData.$BaseID)) mkdir(RemoteData.$BaseID, 0775, true); chgrp(RemoteData.$BaseID, APACHE_USER); chmod(RemoteData.$BaseID, 0775); } if (!copy($extData,RemoteData.$BaseID."/base.xml")) { $msg = "problem with copying base.xml for ".$BaseID.PHP_EOL; if (!$this->updateOnly) error_log($msg, 1, email); exit($msg); } return true; } function days2MonthDay($oldDate, $addDay) { list($year, $day, $hour, $min, $sec) = sscanf($oldDate, "%04d%03d%02d%02d%02d"); if ($addDay) $day++; $newDate = date("Y/m/d",strtotime("+$day days",strtotime("$year-01-01 $hour:$min:$sec"))); return $newDate; } /* Update Local Bases Start - Stop */ public function updateStartStop($groups){ for ($k = 0; $k < $groups->length; $k++) { if (strpos($groups->item($k)->nodeValue, 'Models') !== false ) continue; $domName = LocalData."DD_".$groups->item($k)->nodeValue.".xml"; $dom = new DomDocument(); if (!$dom->load($domName)) { $msg = "Cannot load file ".$domName.PHP_EOL; print_r($msg); if (!$this->updateOnly) error_log($msg,1,email); continue; } $vi = $dom -> getElementsByTagName("dataset"); if ($vi->length > 0) for ($i = 0; $i < $vi->length; $i++) { // PHP sur manunja $name = $vi->item($i)->getAttribute('id'); $name = $vi->item($i)->getAttribute('xml:id'); $hasVersion = $vi->item($i)->hasAttribute('version'); if ($vi->item($i)->getAttribute('vi') != '') $name = $vi->item($i)->getAttribute('vi'); if ($name){ try { // Call AMDA WebService $res = $this->client->getStartStop($name); $Time = explode("-",$res); $msg = $name." : ".$res.PHP_EOL; // New Start - New Stop $newStart = $this->days2MonthDay($Time[0]); $vi->item($i)->getElementsByTagName('dataStart')->item(0)->nodeValue = $newStart; $newStop = $this->days2MonthDay($Time[1]); if ($newStart == $newStop) $newStop = $this->days2MonthDay($Time[1], true); $vi->item($i)->getElementsByTagName('dataStop')->item(0)->nodeValue = $newStop; if ($hasVersion) { $res = $this->client->getVersion(str_replace(":", "_", $name)); if ($res) { $dataSource = explode("//", $vi->item($i)->getElementsByTagName('dataSource')->item(0)->nodeValue); $vi->item($i)->getElementsByTagName('dataSource')->item(0)->nodeValue = "$dataSource[0]//$res"; } } } catch (SoapFault $exception) { $msg = $exception->faultstring." : DataSet ".$name." Start-Stop can't be updated".PHP_EOL; print_r($msg); if (!$this->updateOnly) error_log($msg,1,email); } } else echo "Models SINP not data".PHP_EOL; } if (!$dom->save($domName)){ error_log("Cannot save LocalParams.xml".PHP_EOL,1,email); } } return true; } /* Update Remote Bases Start - Stop */ public function updateRemoteStartStop($extBaseID){ $domName = RemoteData.$extBaseID."/base.xml"; $dom = new DomDocument(); if (!$dom->load($domName)){ $msg = "Cannot load file ".$domName.PHP_EOL; print_r($msg); if (!$this->updateOnly) error_log($msg,1,email); return false;; } $VIs = $dom -> getElementsByTagName("dataset"); foreach ($VIs as $vi) { $name = $vi->getAttribute('xml:id'); // Call AMDA WebService try { $res = $this->client->getRemoteStartStop($extBaseID, $name); print_r($name.' : '.$res.PHP_EOL); error_log($name.' : '.$res.PHP_EOL,3,log); if ($res !== "NOEXTERNALDATA" && $res !== "NODATASET") $vi->setAttribute('desc', $res); } catch (SoapFault $exception) { $msg = $exception->faultstring." : DataSet ".$name." Start-Stop can't be updated".PHP_EOL; print_r($msg); if (!$this->updateOnly) error_log($msg,1,email); } } $dom->save($domName); return true; } /* Check Remote Bases Connection */ public function checkRemoteConnection($extBaseID){ try { $res = $this->client->checkRemoteConnection($extBaseID); } catch (SoapFault $exception) { print_r($exception->faultstring.PHP_EOL); error_log($exception->faultstring.PHP_EOL,1,email); return false; } return $res; } /* Get user info from login */ public function getUserInfo($login) { try { $info = $this->client->getUserInfo($login,md5(DDSERVICE_PRIVATEKEY.$login.'getUserInfo')); //error_log($info,1,email); } catch (SoapFault $exception) { return array('success' => false, 'message' => $exception->faultstring); } return $info; } /* Get data set info */ public function getInfo($viID, $infoID) { try { $info = $this->client->getInfo($viID, $infoID); } catch (SoapFault $exception) { return array('success' => false, 'message' => $exception->faultstring); } return $info; } /* Recursively modify rights & grp */ public function fsmodify($obj) { chgrp($obj, APACHE_USER); chmod($obj, is_dir($obj) ? 0775 : 0664); } public function fsmodifyr($dir) { if($objs = glob($dir."/*")) { foreach($objs as $obj) { $this->fsmodify($obj); if(is_dir($obj)) $this->fsmodifyr($obj); } } return $this->fsmodify($dir); } /* get Spice Kernels from DDBASE/INFO and copy into AMDALIB/spice */ /* public function updateSpiceKernels() { $kernels = array('general','stereo'); $spiceDir = DDHOME."AMDALIB/spice"; if (!is_dir($spiceDir)) { mkdir($spiceDir, 0775, true); chgrp($spiceDir, APACHE_USER); chmod($spiceDir, 0775); } chdir($spiceDir); foreach ($kernels as $kernel) { try { if (!copy($tarFileURL,$tarFile)){ // } else { exec('tar -xf '.$tarFile); } } catch (SoapFault $exception) { $msg = $exception->faultstring.PHP_EOL; error_log($msg,1 , email); } } } */ }