diff --git a/php/classes/UserMgr.php b/php/classes/UserMgr.php index 3524f1d..44565e3 100644 --- a/php/classes/UserMgr.php +++ b/php/classes/UserMgr.php @@ -21,7 +21,7 @@ class UserMgr protected $userDirs = array('USERWSDIR' => 'WS', 'USERREQDIR' => 'REQ', 'USERDATADIR' => 'DATA', 'USERWORKINGDIR' =>'RES', 'USERTTDIR' => 'TT', 'USERJOBDIR' => 'JOBS', 'USERTEMPDIR' => 'TEMP'); - protected $userMissions; + protected $userGrps; protected $amdaClient; //client to dd webservice public $isFirst = false; @@ -31,15 +31,11 @@ class UserMgr function __construct($username = NULL, $password = NULL, $sessionID = NULL) { // if magic quotes is on, stripslashes - if(get_magic_quotes_gpc()) - { + 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)) - { + while(list($k,$v) = each($in)) { + foreach($v as $key => $val) { + if(!is_array($val)) { $in[$k][$key] = stripslashes($val); continue; } @@ -49,15 +45,12 @@ class UserMgr unset($in); } - if (isset($_POST['username'])) - { + if (isset($_POST['username'])) { // Process Guest Login - if (strcasecmp(trim($_POST['username']),"guest") == 0) - { + if (strcasecmp(trim($_POST['username']),"guest") == 0) { $this->processGuestLogin(); } - else - { + else { $this->user = trim($_POST['username']); } } @@ -65,8 +58,7 @@ class UserMgr $this->user = trim($username); //TODO crypt / decrypt - if (!isset($this->passwd)) - { + if (!isset($this->passwd)) { if (isset($_POST['password'])) $this->passwd = $_POST['password']; else if (isset($password)) @@ -77,7 +69,8 @@ class UserMgr $this->user = $_GET['sessionID']; else if (isset($sessionID)) $this->user = $sessionID; - // $this->amdaClient = new AmdaClient(); + //TODO if AmdaClient is needed ? + $this->amdaClient = new AmdaClient(); } //TODO if needed?? set session ID @@ -94,8 +87,9 @@ class UserMgr */ if (getenv('REMOTE_ADDR')) { - $realIP = getenv('REMOTE_ADDR'); - if ($realIP == '10.10.131.1') { // proxy amdatest et openam + $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']; } @@ -111,52 +105,57 @@ class UserMgr public function getUserInfo() { - return $this->amdaClient->getUserInfo($this->user); - } - -/* -* Get Available Missions for a User (groups restriction) -*/ - protected function getAvailableMissionsByUser() - { - $res = $this->amdaClient->getUserAvailableMissions($this->user); - - if (!$res['success']) - return array('local' => array(), 'external' => array()); - - return array('local' => $res['local'], 'external' => $res['external']); + // 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) - { + 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 -*/ + /* + * 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; - $info = $this->amdaClient->getUserInfo($this->user); - - $userGrps = explode(',',$info['group']); - $specialGrpNode = null; - foreach ($userGrps as $grp) - { + foreach ($this->userGrps as $grp) { $specialGrpNode = $specialGrps->getElementById($grp); if ($specialGrpNode) break; } @@ -164,6 +163,31 @@ class UserMgr 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"); @@ -185,22 +209,21 @@ class UserMgr } 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); - } - } + $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 - */ +/* +* Totally replace IMPEX staff in user remote tree +*/ protected function updateImpex() { $myRemoteBases = new DomDocument("1.0"); @@ -289,8 +312,9 @@ class UserMgr /* * make remote data tree from list of distant bases if it doezn't exist -*/ - protected function makeRemoteTree() +*/ + +protected function makeRemoteTree() { $remoteBases = new DomDocument("1.0"); @@ -493,18 +517,15 @@ unlink(USERWSDIR.'RemoteParams.xml'); public function setPath() { - if (isset($_GET['sessionID'])) - { + if (isset($_GET['sessionID'])) { $this->user = $_GET['sessionID']; $this->userdir = USERPATH."/".$this->user."/"; } // for testing purposes - else if (defined('TRANSFERUSER')) - { + else if (defined('TRANSFERUSER')) { $this->userdir = BASE_PATH."test/".TRANSFERUSER."/"; } - else if (isset($this->user)) - { + else if (isset($this->user)) { $this->userdir = USERPATH."/".$this->user."/"; } @@ -512,8 +533,7 @@ unlink(USERWSDIR.'RemoteParams.xml'); define ("USERDIR", "$usrdir/"); - foreach ($this->userDirs as $key => $val) - { + foreach ($this->userDirs as $key => $val) { $dir = $usrdir.$val; define("$key","$dir/"); } @@ -546,17 +566,14 @@ unlink(USERWSDIR.'RemoteParams.xml'); $this->userdir = USERPATH."/".$this->user."/"; - if (!is_dir($this->userdir)) - { - if (!$this->createDir()) - { + if (!is_dir($this->userdir)) { + if (!$this->createDir()) { die("Login for ".$this->user." failed: Can't create WS dirs"); } $this->isFirst = true; } - if (file_exists($this->userdir.'newLogin')) - { + if (file_exists($this->userdir.'newLogin')) { copy($this->userdir.'newLogin',$this->userdir.'lastLogin'); $dt = filemtime($this->userdir.'newLogin'); touch($this->userdir.'lastLogin', $dt); @@ -582,18 +599,17 @@ unlink(USERWSDIR.'RemoteParams.xml'); touch($this->userdir.'newLogin'); $this->setPath(); - // $this->userMissions = $this->getAvailableMissionsByUser(); + + $this->userGrps = $this->getUserGrps(); + + if (!$this->makeLocalTree()) + die("Login for ".$this->user." failed: Can't make LocalParams.xml");; - if (file_exists(USERWSDIR.'LocalParams.xml')) - unlink(USERWSDIR.'LocalParams.xml'); - // if (!file_exists(USERWSDIR.'LocalParams.xml')) - symlink(DATAPATH.'/LocalData/LocalParams.xml', USERWSDIR.'LocalParams.xml'); - $ok = $this->makeRemoteTree(); if (!file_exists(USERWSDIR.'Request.xml')) $reqMgr = new RequestMgr(); if (!file_exists(USERWSDIR.'Tt.xml')) $ttMgr = new TimeTableMgr(); - if (!file_exists(USERWSDIR.'Alias.xml'))$ttMgr = new AliasMgr(); + if (!file_exists(USERWSDIR.'Alias.xml')) $ttMgr = new AliasMgr(); //TODO sessionID = user + WSname $sessionID = $this->user; @@ -606,53 +622,85 @@ unlink(USERWSDIR.'RemoteParams.xml'); /* * Special groups are defined in the generic_data/SpecialSettings/Groups.xml */ - // $specialGroup = $this->isSpecialGroup(); - $specialGroup = false; - // Special Info for special groups - if ($specialGroup) - { - // Special Settings for special groups - first visit just copying - if ($this->isFirst) - { - $grp = $specialGroup->getAttribute('xml:id'); - $tags = $specialGroup->getElementsByTagName('folder'); - - foreach ($tags as $tag) - { - $folder = $tag->getAttribute('name'); - foreach (glob(SpecialSettingsDir.$grp."/".$folder."/*") as $file) - { - copy($file, $this->userdir.$folder."/".basename($file)); - } + if ($this->userGrps) { + // $specialGroup = $this->isSpecialGroup(); + $specialGroup = false; + // Special Info for special groups + if ($specialGroup) { + // Special Settings for special groups - first visit just copying + if ($this->isFirst) { + $grp = $specialGroup->getAttribute('xml:id'); + $tags = $specialGroup->getElementsByTagName('folder'); + + foreach ($tags as $tag) { + $folder = $tag->getAttribute('name'); + + foreach (glob(SpecialSettingsDir.$grp."/".$folder."/*") as $file) { + copy($file, $this->userdir.$folder."/".basename($file)); + } + } + // mark to show help information + touch($this->userdir."$grp"."Help"); } - // mark to show help information - touch($this->userdir."$grp"."Help"); + // add requests + else { } + $grpName = $specialGroup->getAttribute('xml:id'); + $helpName = "$grpName"."Help"; + + if (file_exists(HELPPATH.$helpName) && file_exists($this->userdir.$helpName)) + $this->isSpecialInfo = $helpName; } - // add requests - else { } - $grpName = $specialGroup->getAttribute('xml:id'); - $helpName = "$grpName"."Help"; - - if (file_exists(HELPPATH.$helpName) && file_exists($this->userdir.$helpName)) - $this->isSpecialInfo = $helpName; - } return $sessionID; } + protected function makeLocalTree() + { + if (file_exists(USERWSDIR.'LocalParams.xml')) + unlink(USERWSDIR.'LocalParams.xml'); + + if (!copy(DATAPATH.'/LocalData/LocalParams.xml', USERWSDIR.'LocalParams.xml')) + die("Login for ".$this->user." failed: Can't copy LocalParams.xml"); + + if ($this->userGrps) + return $this->updateTreeForGrps(USERWSDIR.'LocalParams.xml'); + + return true; + } + + protected function updateTreeForGrps($file) + { + $xml = new DomDocument("1.0"); + + if(!$xml->load($file)) + die("Login for ".$this->user." failed: Can't load LocalParams.xml"); + + $xp = new domxpath($xml); + + foreach ($this->userGrps as $grp) { + $nodes = $xp->query("//*[@group='".$grp."']"); + + if ($nodes->length > 0) + foreach ($nodes as $node) { + $node->removeAttribute('group'); + if ($node->hasAttribute('restriction')) + $node->removeAttribute('restriction'); + } + } + + return $xml->save($file); + } + public function dirSize($dir) { $handle = opendir($dir); $mas = 0; - while ($file = readdir($handle)) - { - if ($file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) - { + while ($file = readdir($handle)) { + if ($file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) { $mas += filesize($dir.'/'.$file); } - else if (is_dir($dir.'/'.$file) && $file != '..' && $file != '.') - { + else if (is_dir($dir.'/'.$file) && $file != '..' && $file != '.') { $mas += $this->dirSize($dir.'/'.$file); } } @@ -669,7 +717,7 @@ unlink(USERWSDIR.'RemoteParams.xml'); return $wsSize; } -// http://www.ilovejackdaniels.com/php/email-address-validation/ + // http://www.ilovejackdaniels.com/php/email-address-validation/ public function check_email_address($email) { // First, we check that there's one @ symbol, and that the lengths are right -- libgit2 0.21.2