/** * implementation of abstract parse() operation from RestXMLParser. * * @xml SimpleXMLElement the root element of the application XML representation * * @return Default_Model_Researcher * @access public */ public function parse($xml) { if (!is_null($this->_user)) { $person = new Default_Model_Researcher(); try { $xml = new SimpleXMLElement($xml); } catch (Exception $e) { $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION; $this->_extError = $e->getMessage(); return $person; } $xmli = $xml->xpath('//person:person'); if (count($xmli) === 0) { $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION; return $person; } $xml = $xmli[0]; if ($this->_parent->getMethod() === RestMethodEnum::RM_POST) { if ($xml->attributes()->id) { $person->id = strval($xml->attributes()->id); } else { $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION; $this->_extError = 'Resource ID missing'; return $person; } } if ($xml->attributes()->nodissemination) { $person->noDissemination = strval($xml->attributes()->nodissemination) === "true" ? true : false; } if ($xml->attributes()->cname) { $person->cname = strval($xml->attributes()->cname); } $firstname = $this->el($xml, "person:firstname"); if (!is_null($firstname) && trim(strval($firstname)) !== "") { $person->firstName = trim(strval($firstname)); } $lastname = $this->el($xml, "person:lastname"); if (!is_null($lastname) && trim(strval($lastname)) !== "") { $person->lastName = trim(strval($lastname)); } $gender = $this->el($xml, "person:gender"); if (!is_null($gender)) { if (trim(strval($gender->attributes(RestAPIHelper::XMLNS_XSI())->nil)) === "true") { $person->gender = 'n/a'; } elseif (trim(strval($gender)) !== "") { if (trim(strtolower(strval($gender))) === "male") { $person->gender = "male"; } elseif (trim(strtolower(strval($gender))) === "female") { $person->gender = "female"; } } } if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT) { $person->dateInclusion = date("Y-m-d"); $person->addedByID = $this->_parent->getUser()->id; } $person->lastUpdated = date('Y-m-d'); $institute = trim(strval($this->el($xml, "person:institute"))); if (!is_null($institute)) { $person->institution = trim(strval($institute)); } $country = $this->el($xml, "regional:country"); if (!is_null($country) && trim(strval($country->attributes()->id)) !== "") { $person->countryID = trim(strval($country->attributes()->id)); } $role = $this->el($xml, "person:role"); if (!is_null($role) && trim(strval($role->attributes()->id)) !== "") { $person->positionTypeID = trim(strval($role->attributes()->id)); } $image = $this->el($xml, "person:image"); $removeImageCache = false; if (!is_null($image)) { if (trim(strval($image->attributes(RestAPIHelper::XMLNS_XSI())->nil)) === "true") { $person->clearImage(); $removeImageCache = true; } else { if (!is_null($image->attributes()->type) && trim(strval($image->attributes()->type)) === "base64") { // image is given as byte64 encoded string if (trim(strval($image)) != '') { $person->image = pg_escape_bytea(trim(strval($image))); $removeImageCache = true; } } else { // image is given as URL if (trim(parse_url(strval($image), PHP_URL_SCHEME)) == '') { // no URL scheme present; assume uploaded file though // portal's uploadimage action in AppsController if (trim(strval($image)) != '') { try { $person->image = pg_escape_bytea(base64_encode(file_get_contents(APPLICATION_PATH . "/../public/" . trim(strval($image))))); $removeImageCache = true; } catch (Exception $e) { $this->_error = RestErrorEnum::RE_BACKEND_ERROR; $this->_extError = $e->getMessage(); return $person; } } } else { // URL scheme present; assume remote file if (trim(strval($image)) != '') { try { $person->image = pg_escape_bytea(base64_encode(file_get_contents(trim(strval($image))))); $removeImageCache = true; } catch (Exception $e) { $this->_error = RestErrorEnum::RE_BACKEND_ERROR; $this->_extError = $e->getMessage(); return $person; } } } } } } if ($removeImageCache === true) { if ($person->id != '' && file_exists(APPLICATION_PATH . "/../cache/ppl-image-" . $person->id . ".png")) { unlink(APPLICATION_PATH . "/../cache/ppl-image-" . $person->id . ".png"); } } $person->save(); if ($this->_parent->getMethod() === RestMethodEnum::RM_POST) { //remove existing contact info $conts = new Default_Model_Contacts(); $conts->filter->researcherid->equals($person->id); $conts->refresh(); for ($i = count($conts->items) - 1; $i >= 0; $i--) { $conts->remove($conts->items[$i]); } } //add new contact info $cts = new Default_Model_ContactTypes(); $cts->refresh(); $xmli = $xml->xpath("//person:contact"); $conts2 = new Default_Model_Contacts(); foreach ($xmli as $x) { if (trim(strval($x)) !== '') { $cont = new Default_Model_Contact(); $cont->researcherID = $person->id; $ct = trim(strval($x->attributes()->type)); $ctid = null; for ($i = 0; $i < count($cts->items); $i++) { if (strtolower($ct) == strtolower($cts->items[$i]->description)) { $ctid = $cts->items[$i]->id; break; } } if (!is_null($ctid)) { $cont->contactTypeID = $ctid; } else { $cont->contactTypeID = 7; //e-mail by default } $cont->data = trim(strval($x)); if (strval($x->attributes()->primary) === "true") { $cont->isPrimary = true; } $conts2->filter->data->equals($cont->data)->and($conts2->filter->contacttypeid->equals(7))->and($conts2->filter->researcherid->notequals($person->id)); $conts2->refresh("xml"); if (count($conts2->items) == 0) { $cont->save(); } else { $this->_error = RestErrorEnum::RE_BACKEND_ERROR; $this->_extError = "e-mail address `" . $cont->data . "' already exists"; return $person; } } } if ($this->_parent->getMethod() === RestMethodEnum::RM_POST || $this->_parent->getMethod() === RestMethodEnum::RM_PUT) { $xrels = $xml->xpath("person:relation"); $ps = new Default_Model_Researchers(); $ps->filter->id->equals($person->id); $p = null; if (count($ps->items) > 0) { $p = $ps->items[0]; } if ($p !== null) { $rels = array(); if (count($xml->xpath('person:relation[@xsi:nil="true"]')) === 0) { foreach ($xrels as $x) { $targuid = trim(strval($x->attributes()->targetguid)); $subguid = trim(strval($x->attributes()->subjectguid)); $rel = array("id" => trim(strval($x->attributes()->id)), "parentid" => trim(strval($x->attributes()->parentid))); if ($targuid === "") { $rel["subjectguid"] = $subguid; } else { if ($subguid === "") { $rel["targetguid"] = $targuid; } } if ($rel["parentid"] === "") { $rel["parentid"] = null; } $rels[] = $rel; } } try { $res = PersonRelations::syncRelations($p->guid, $this->_user->id, $rels); } catch (Exception $ex) { $res = $ex->getMessage(); } if (is_string($res)) { $this->_error = RestErrorEnum::RE_BACKEND_ERROR; $this->_extError = $res; return $p; } } } } $this->_error = RestErrorEnum::RE_OK; return $person; }
public function loginAction() { /* Check whether the user is already logged in */ if ($this->session->userid !== null) { if (!$this->handle_actions()) { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(); header('Location: https://' . $_SERVER['HTTP_HOST'] . '/'); } return; } $this->view->session = $this->session; $ldap = ApplicationConfiguration::service('egi.ldap.host'); if (array_key_exists('username', $_POST)) { $this->view->username = $_POST['username']; $username = "******" . $_POST['username'] . ",ou=People,dc=egi,dc=eu"; $password = $_POST['password']; $ds = $this->initLDAP(true, $username, $password); if (is_resource($ds)) { @ldap_close($ds); $username = $_POST['username']; $this->session->username = $username; $users = new Default_Model_Researchers(); $users->viewModerated = true; $users->filter->username->equals($username); if ($users->count() == 1) { if (!$users->items[0]->deleted) { $this->session->userid = $users->items[0]->Id; $this->session->userRole = $users->items[0]->positionTypeID; $this->session->userCountryID = $users->items[0]->countryID; $this->session->userCountryName = $users->items[0]->country->name; $this->session->user = $users->items[0]; $this->session->fullName = $users->items[0]->firstName . " " . $users->items[0]->lastName; $this->session->cname = $users->items[0]->cname; setAuthCookies($username, md5($password)); $user = $users->items[0]; $user->lastLogin = time(); $user->password = md5($password); // TODO: remove this when SAML has been implemented $user->save(); $this->saveUserCredentials($user->id, md5($password)); // TODO: replace with SAML Auth token $this->handle_actions(); } else { $this->view->accountDeleted = true; error_log(var_export($users->items[0]->delInfo, true)); $this->view->accountDeleter = $users->items[0]->delInfo->deleter->name; $this->view->accountDeleterID = $users->items[0]->delInfo->deleter->id; $this->view->accountDeletedOn = $users->items[0]->delInfo->deletedOn; } } else { $this->session->claimPassword = md5($password); $ds = $this->initLDAP(true); // no rdn/pwd: root connection if ($ds !== false) { $users2 = new Default_Model_Contacts(); $sr = ldap_search($ds, "ou=people,dc=egi,dc=eu", "(uid=" . $_POST['username'] . ")"); $info = ldap_get_entries($ds, $sr); ldap_close($ds); if ($info["count"] > 0) { $this->session->fullName = $info[0]['cn'][0]; $users2->filter->data->ilike($info[0]['mail'][0]); if ($users2->refresh()->count() != 0) { // Found existing profiles $ids = array(); for ($i = 0; $i < $users2->count(); $i++) { $ids[] = $users2->items[$i]->researcherid; } $users->viewModerated = false; $users->filter->id->in($ids); $users->refresh(); $entries = array(); $canClaim = array(); for ($i = 0; $i < $users->count(); $i++) { $entries[] = $users->items[$i]; $canClaim[] = $users->items[$i]->id; } $this->view->entries = $entries; $this->session->canClaim = $canClaim; } else { // Create a new profile $roleTypeID = 3; if (array_key_exists('usercertificatesubject', $info[0])) { $sub = explode('/', $info[0]['usercertificatesubject'][0]); foreach ($sub as $s) { if (substr($s, 0, 2) == 'C=') { $country = substr($s, 2); $cs = new Default_Model_Countries(); $cs->filter->isocode->equals($country); if ($cs->count() > 0) { $this->view->country = $cs->items[0]->name; } break; } } } if (array_key_exists('sn', $info[0])) { $this->view->lastname = $info[0]['sn'][0]; $this->view->firstname = str_replace($info[0]['sn'][0], "", $info[0]['cn'][0]); } if (array_key_exists('destinationindicator', $info[0])) { $this->view->gender = $info[0]['destinationindicator'][0]; } if (array_key_exists('ou', $info[0])) { $this->view->institute = $info[0]['ou']['0']; } if (array_key_exists('employeetype', $info[0])) { if ($info[0]['employeetype'][0] == "Experienced researcher" || $info[0]['employeetype'][0] == "Management") { $roleTypeID = 2; } else { if ($info[0]['employeetype'][0] == "Technical Support" || $info[0]['employeetype'][0] == "PhD Student") { $roleTypeID = 3; } else { $roleTypeID = 4; } } } if (array_key_exists('mail', $info[0])) { $this->view->SSOmail = $info[0]['mail'][0]; } $pts = new Default_Model_PositionTypes(); $pts->filter->id->equals($roleTypeID); $this->view->roleType = $pts->items[0]->description; $contactTypes = new Default_Model_ContactTypes(); $this->view->contactTypes = $contactTypes->refresh(); } } } else { error_log($this->view->ldapError); } } } else { error_log($this->view->ldapError); } } }
public function getContactType() { if ($this->_contactType === null) { $ContactTypes = new Default_Model_ContactTypes(); $ContactTypes->filter->id->equals($this->getContactTypeID()); if ($ContactTypes->count() > 0) { $this->_contactType = $ContactTypes->items[0]; } } return $this->_contactType; }