'WS', 'USERREQDIR' => 'REQ', 'USERDATADIR' => 'DATA',
'USERWORKINGDIR' =>'RES', 'USERTTDIR' => 'TT', 'USERJOBDIR' => 'JOBS',
'USERTEMPDIR' => 'TEMP');
protected $userGrps;
protected $amdaClient; //client to dd webservice
public $isFirst = false;
public $isNewInfo = false;
public $isSpecialInfo = null;
function __construct($username = NULL, $password = NULL, $sessionID = NULL)
{
// if magic quotes is on, stripslashes
if(get_magic_quotes_gpc()) {
$in = array(&$_GET, &$_POST, &$_COOKIE);
while(list($k,$v) = each($in)) {
foreach($v as $key => $val) {
if(!is_array($val)) {
$in[$k][$key] = stripslashes($val);
continue;
}
$in[] =& $in[$k][$key];
}
}
unset($in);
}
if (isset($_POST['username'])) {
// Process Guest Login
if (strcasecmp(trim($_POST['username']),"guest") == 0) {
$this->processGuestLogin();
}
else {
$this->user = trim($_POST['username']);
}
}
else if (isset($username))
$this->user = trim($username);
//TODO crypt / decrypt
if (!isset($this->passwd)) {
if (isset($_POST['password']))
$this->passwd = $_POST['password'];
else if (isset($password))
$this->passwd = $password;
}
if (isset($_GET['sessionID']))
$this->user = $_GET['sessionID'];
else if (isset($sessionID))
$this->user = $sessionID;
//TODO if AmdaClient is needed ?
$this->amdaClient = new AmdaClient();
}
//TODO if needed?? set session ID
protected function setID() {}
public function getIPclient()
{
/*
REMOTE_ADDR is the only really reliable information,
as it is transmitted to you by your web server that
is handling the request. It can be theoretically
falsified as well, but that is much, much harder
than spoofing a header value, and an entirely different class of attack.
*/
if (getenv('REMOTE_ADDR'))
{
$realIP = getenv('REMOTE_ADDR');
if ($realIP == '10.10.131.1' || $realIP == '10.10.135.119') { // proxy amdatest et openam; amdadev
$allIPs = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$realIP = count($allIPs) > 1 ? trim($allIPs[0]) : $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
else
{
//get local IP
$command="hostname -i";
$realIP = exec($command);
}
return $realIP;
}
public function getUserInfo()
{
// array("success" => TRUE,
// "login" => $login,
// "name" => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("name") : "undefined",
// "first_name" => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("first_name") : "undefined",
// "group" => $this->getUserMemberGroups($login),
// "email" => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("email") : "undefined",
// "date" => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("date") : "undefined",
// "news" => ($theUser->length > 0) ? $theUser->item(0)->getAttribute("news") : "0");
$info = $this->amdaClient->getUserInfo($this->user);
$wsSize = intval($this->getWsSize()/1024./1024.);
$quota = intval($this->getSpecialSettings()/1024./1024.);
$info['total'] = $quota;
$info['available'] = $quota - $wsSize;
$info['used'] = $wsSize;
return $info;
}
public function createDir()
{
if (!mkdir($this->userdir, 0755, true)) return false;
foreach ($this->userDirs as $key => $val) {
if (!mkdir($this->userdir.$val.'/', 0755, true)) return false;
}
return true;
}
protected function getUserGrps()
{
$info = $this->amdaClient->getUserInfo($this->user);
if ($info['group'] == '')
return null;
else
return explode(',',$info['group']);
}
/*
* Check if special groups with settings exist and user is from these groups
* Take the first group from user list
*/
protected function isSpecialGroup()
{
$specialGrps = new DomDocument("1.0");
if (!($specialGrps->load(specialGrpsXml))) return null;
$specialGrpNode = null;
foreach ($this->userGrps as $grp) {
$specialGrpNode = $specialGrps->getElementById($grp);
if ($specialGrpNode) break;
}
return $specialGrpNode;
}
public function getSpecialSettings()
{
$userSettings = new DomDocument("1.0");
if (!file_exists(specialSettingsXml) || !$userSettings->load(specialSettingsXml)) {
return DISK_QUOTA_standard;
}
$theUser = $userSettings->getElementById($this->user);
if (!$theUser) {
return DISK_QUOTA_standard;
}
$settings = $theUser->getElementsByTagName("setting");
if ($settings->length == 0) {
return DISK_QUOTA_standard;
}
foreach ($settings as $setting) {
if ($setting->getAttribute("name") == 'DISK_QUOTA')
return $setting->getAttribute("value");
}
}
public function setSpecialSettings()
{
$userSettings = new DomDocument("1.0");
if (!file_exists(specialSettingsXml) || !$userSettings->load(specialSettingsXml)) {
define("DISK_QUOTA", DISK_QUOTA_standard);
return;
}
$theUser = $userSettings->getElementById($this->user);
if (!$theUser) {
define("DISK_QUOTA", DISK_QUOTA_standard);
return;
}
$settings = $theUser->getElementsByTagName("setting");
if ($settings->length == 0) {
define("DISK_QUOTA", DISK_QUOTA_standard);
return;
}
foreach ($settings as $setting) {
$key = $setting->getAttribute("name");
$value = $setting->getAttribute("value");
$isSetting = $setting->hasAttribute("isSetting");
if ($isSetting) {
ini_set("$key",$value);
}
else {
// Attention !!! CONSTANT cannot be redefined
define("$key",$value);
}
}
}
/*
* Totally replace IMPEX staff in user remote tree
*/
protected function updateImpex()
{
$myRemoteBases = new DomDocument("1.0");
if (!@$myRemoteBases->load(USERWSDIR.'RemoteParams.xml')) return false;
$myBases = $myRemoteBases->getElementsByTagName('dataCenter');
if ($myBases->length < 1) return false;
// Delete all impex staff at first
$i = $myBases->length - 1;
while ($i > -1)
{
$base = $myBases->item($i);
$id = $base->getAttribute('xml:id');
if ($id == 'FMI_GUMICS')
{
$simuRemoteBases = new DomDocument("1.0");
$simuRemoteBases->load(USERWSDIR.'RemoteParams.xml');
$gumicsNode = $simuRemoteBases->getElementById('FMI_GUMICS');
if ($gumicsNode != NULL)
$gumicsRuns = $gumicsNode->getElementsByTagName('runID');
}
if (($base->hasAttribute('isSimulation')) || $id == "CLWeb")
{
$base->parentNode->removeChild($base);
}
$i--;
}
$remoteBases = new DomDocument("1.0");
if (!$remoteBases->load(RemoteData.'Bases.xml')) return false;
$bases = $remoteBases->getElementsByTagName('dataCenter');
if ($bases->length < 1) return false;
$myRootElement = $myRemoteBases->documentElement;
// and add impex
foreach ($bases as $base)
{
//TODO && $id != 'FMI_GUMICS' or ! hasAttribute('isAddable')
if ($base->hasAttribute('isSimulation'))
{
$baseId = $base->getAttribute('xml:id');
$baseXml = new DomDocument("1.0");
// no data base description ; skip this data base
if (!file_exists(RemoteData.$baseId.'/base.xml')) continue;
// can't read base.xml ; skip this data base
if (!@$baseXml->load(RemoteData.$baseId.'/base.xml')) continue;
$myBase = $myRemoteBases->importNode($baseXml->getElementById($baseId), true);
$myBase->setAttribute('name', $base->getAttribute('name'));
$myBase->setAttribute('desc', $base->getAttribute('desc'));
if ($baseId == "FMI_GUMICS")
{
$gumicsSimuReg = $myRemoteBases->getElementById('FMI_GUMICS_Earth_Magnetosphere');
if (($gumicsSimuReg != NULL)&& ($gumicsRuns->length > 0))
{
for($c = 0; $c < $gumicsRuns->length; $c++)
{
$node = $gumicsRuns->item($c);
$node = $myRemoteBases->importNode($node, true);
$gumicsSimuReg->appendChild($node);
}
}
}
// Check if WebService is ON
$center = new $baseId();
$myBase->setAttribute('available',$center->monitor());
$myRootElement->appendChild($myBase);
}
}
return $myRemoteBases->save(USERWSDIR.'RemoteParams.xml');
}
/*
* make remote data tree from list of distant bases if it doezn't exist
*/
protected function makeRemoteTree()
{
$remoteBases = new DomDocument("1.0");
unlink(USERWSDIR.'RemoteParams.xml');
if (file_exists(USERWSDIR.'RemoteParams.xml'))
{
// replace IMPEX nodes
$status = $this->updateImpex();
if (!$status)
error_log('IMPEX Remote Base can not be updated',1,email);
// check/change access rights
$remoteBases->load(USERWSDIR.'RemoteParams.xml');
$bases = $remoteBases->getElementsByTagName('dataCenter');
$delete = new UserDeleteObsolete();
if ($bases->length > 0)
foreach ($bases as $base)
{
$baseId = $base->getAttribute('xml:id');
if ($base->getAttribute('isSimulation')) continue;
// $notAvailable = (array_search($baseId,$availableMis) === FALSE);
// $base->setAttribute('available',!$notAvailable);
// Update Info on External Data Sets in RemoteTree.xml for everything except IMPEX
$dataSets = $base->getElementsByTagName("dataset");
if ($dataSets->length > 0)
{
$baseExtDom = new DomDocument("1.0");
if (!$baseExtDom->load(RemoteData.$baseId.'/base.xml'))
{
$base->setAttribute("desc","ATTENTION!!! This Base set DOES NOT ANY MORE EXIST!!! Remove it from your tree");
$base->setAttribute('obsolete', true);
error_log('NO '.RemoteData.$baseId.'/base.xml',1,email);
continue;
}
foreach ($dataSets as $dataSet)
{
$dataSetID = $dataSet->getAttribute("xml:id");
$origDataSet = $baseExtDom->getElementById($dataSetID);
if ($origDataSet != null)
{
$desc = $origDataSet->getAttribute("desc");
if ($desc != null) $dataSet->setAttribute("desc", $desc);
}
else
{
$delete->setVI($dataSet->getAttribute('name'));
$res = $delete->deleteDerived();
$res = $delete->deleteConditions();
$res = $delete->deleteRequests();
$res = $delete->deleteAliases();
$dataSet->setAttribute("desc","ATTENTION!!! This data set DOES NOT EXIST ANY MORE !!! Remove it from your tree");
$dataSet->setAttribute('obsolete', true);
}
}
}
}
return $remoteBases->save(USERWSDIR.'RemoteParams.xml');
}
$remoteBases->load(RemoteData.'Bases.xml');
$bases = $remoteBases->getElementsByTagName('dataCenter');
if ($bases->length == 0) return 'NO REMOTE BASES';
$paramMgr = new RemoteParamManager();
$paramMgr->xmlDom = new DomDocument("1.0");
$paramMgr->xmlDom->formatOutput = true;
$paramMgr->xmlDom->preserveWhiteSpace = false;
$BASE = $paramMgr->xmlDom->createElement('dataRoot');
$BASE->setAttribute('xml:id','myRemoteData-treeRootNode');
$baseXml = new DomDocument("1.0");
foreach ($bases as $base)
{
$baseId = $base->getAttribute('xml:id');
$paramMgr->baseId = $baseId;
// no data base description ; skip this data base
if (!@file_exists(RemoteData.$baseId.'/base.xml')) continue;
// can't read base.xml ; skip this data base
if (!@$baseXml->load(RemoteData.$baseId.'/base.xml')) continue;
if ($base->hasAttribute('default'))
{
if ($base->hasAttribute('isSimulation'))
{
$centerNode = $paramMgr->xmlDom->importNode($baseXml->getElementById($baseId), true);
$datasets = $centerNode->getElementsByTagName('dataset');
foreach ($datasets as $dataset)
{
$infoFileName = $paramMgr->getInfoName($dataset->getAttribute('name'));
$paramMgr->localInfo = RemoteData.$baseId.'/'.$infoFileName;
if (!@file_exists($paramMgr->localInfo)) continue;
$params = $dataset->getElementsByTagName('parameter');
$paramMgr->remoteViId = $dataset->getAttribute('name');
foreach ($params as $param)
{
$paramMgr->paramId = $param->getAttribute('name');
$paramGlobalId = $param->getAttribute('xml:id');
$paramMgr->paramXML = RemoteData.'PARAMS/'.$paramGlobalId.'.xml';
if (!@file_exists($paramMgr->paramXML)) continue;
if (!$paramMgr->paramDom)
$paramMgr->paramDom = new DomDocument("1.0");
$paramMgr->paramDom->load($paramMgr->paramXML);
if (!$paramMgr->makeComponents($param)) continue;
}
}
}
/*
* Some small & well known data centers are included by default:
* Each VI structure and VI (dataset) description at DDBASE for them should exist
*/
else
{
$center = new $baseId();
$centerNode = $center->makeCenterNode($paramMgr->xmlDom);
}
}
else
{
$centerNode = $paramMgr->xmlDom->importNode($base, true);
}
$centerNode->setAttribute('name', $base->getAttribute('name'));
$centerNode->setAttribute('desc', $base->getAttribute('desc'));
$centerNode->setAttribute('available',!$notAvailable);
$BASE->appendChild($centerNode);
}
$paramMgr->xmlDom->appendChild($BASE);
return $paramMgr->xmlDom->save(USERWSDIR.'RemoteParams.xml');
}
protected function processGuestLogin()
{
if (!$this->check_email_address($_POST['password']))
{
die('Invalid e-mail address. Please, try once more.
');
}
$passwd = $_POST['password'];
$IP = $this->getIPclient();
$Guest = new Guest($IP,$passwd);
// email and IP in guests.login
$Guest->registerGuest();
$Guest->checkGuestTimes();
$user = $Guest->addGuest();
if ($user == "allGuestLoginsInUse")
{
die('Sorry, all guest accounts are currently in use. Please, try to login in 30 min.
');
}
$this->user = $user;
$this->passwd = "guest";
}
/*****************************************************************
* PUBLIC FUNCTIONS
*****************************************************************/
// // migration from old to new AMDA
// public function convertWS() {
//
// $convert = new UserWsTransfer($this->user);
//
// $res = $convert->checkWS();
// if (!$res['success']) return $res;
//
// $res = $convert->transferDerived();
// $msg = $res['msg'];
//
// $res = $convert->transferTimeTables();
// $msg .= $res['msg'];
//
// $res = $convert->transferConditions();
// $msg .= $res['msg'];
//
// $res = $convert->transferRequests();
// $msg .= $res['msg'];
//
// $res = $convert->transferMyData();
// $msg .= $res['msg'];
//
// error_log('Transfer workspace from old AMDA for '.$this->user, 1, 'amda@irap.omp.eu');
// return array('success' => true, 'msg' => $msg);
// }
public function setPath()
{
if (isset($_GET['sessionID'])) {
$this->user = $_GET['sessionID'];
$this->userdir = USERPATH."/".$this->user."/";
}
// for testing purposes
else if (defined('TRANSFERUSER')) {
$this->userdir = BASE_PATH."test/".TRANSFERUSER."/";
}
else if (isset($this->user)) {
$this->userdir = USERPATH."/".$this->user."/";
}
$usrdir = $this->userdir;
define ("USERDIR", "$usrdir/");
foreach ($this->userDirs as $key => $val) {
$dir = $usrdir.$val;
define("$key","$dir/");
}
}
public function ddCheckUser()
{
$this->IP = $this->getIPclient();
$cmdCheckUser = "DDCheckUser ".$this->IP." ".$this->user." 1> /dev/null 2> /dev/null";
system($cmdCheckUser, $res);
return $res;
}
public function ddLogin()
{
$this->IP = $this->getIPclient();
$loginCommd = "DDHtmlLogin ".$this->user." ".$this->passwd." ".$this->IP;
system($loginCommd, $res);
return $res;
}
public function init()
{
if ($this->ddLogin() != 0)
die('