Newer
Older
<?php
require 'vendor/autoload.php';
require_once('language.php');
private string $ecloudUrl;
private string $ecloudAccountsApiUrl;
protected int $quota = 1024; # Quota in MB
public function __construct(string $ecloudUrl)
$this->ecloudUrl = endsWith($ecloudUrl, "/") ? $ecloudUrl : $ecloudUrl . "/";
$this->ecloudAccountsApiUrl = $this->ecloudUrl . 'apps/ecloud-accounts/api/';
$this->commonApiUrl = getenv('COMMON_SERVICES_URL');
$this->commonApiUrl = endsWith($this->commonApiUrl, '/') ? $this->commonApiUrl : $this->commonApiUrl . '/';
$quota = getenv('CLOUD_QUOTA_IN_MB');
if ($quota !== false) {
$this->quota = intval($quota);
}
}
public function tryToCreate(object $userData)
{
global $strings;
$userData->quota = $this->quota;
$answer = $this->createAccount($userData);
if ($answer->success === false) {
sendAPIResponse(400, createAPIResponse("general", $strings[$answer->type]));
public function validateData(object $userData): ValidatedData
{
$id = "e_cloud_account_data";
try {
// We check if account with uid set to email or username exists
if ($this->isUsernameTaken($userData->username) || $this->isAliasTaken($userData->username)) {
return new \ValidatedData($id, "error_account_taken");
return new \ValidatedData($id, "error_server_side");
}
return new \ValidatedData($id, null);
}
protected function postCreationActions(object $userData, string $commonApiVersion = '')
$aliasDomain = getenv('ALIAS_DOMAIN');
// Create HME Alias
$hmeAlias = $this->createHMEAlias($userData->email, $this->commonApiUrl, $commonApiVersion, $aliasDomain);
// $this->createNewDomainAlias($username, $email, $commonApiUrl, $commonApiVersion, $aliasDomain);
// Create alias with same name as email pointing to email to block this alias
$domain = getMailDomain();
$this->createNewDomainAlias($userData->username, $userData->email, $this->commonApiUrl, $commonApiVersion, $domain);
$userData->hmeAlias = $hmeAlias;
error_log('Error during alias creation for user: ' . $userData->username . ' with email: ' . $userData->email . ' : ' . $e->getMessage());
$userData->quota = strval($userData->quota) . ' MB';
$answer = $this->setAccountDataAtNextcloud($userData);
private function createHMEAlias(string $resultmail, string $commonApiUrl, string $commonApiVersion, string $domain) : string
$endpoint = $commonApiVersion . '/aliases/hide-my-email/';
$url = $commonApiUrl . $endpoint . $resultmail;
$headers = [
"Authorization: Bearer $token"
];
$result = curlRequest('POST', $url, $headers, $data);
$output = $result->output;
if ($result->statusCode != 200) {
$err = $output->message;
throw new Error($err);
}
$alias = isset($output->emailAlias) ? $output->emailAlias : '';
return $alias;
}
private function createNewDomainAlias(string $alias, string $resultmail, string $commonApiUrl, string $commonApiVersion, string $domain)
$endpoint = $commonApiVersion . '/aliases/';
$url = $commonApiUrl . $endpoint . $resultmail;
$headers = [
"Authorization: Bearer $token"
];
$result = curlRequest('POST', $url, $headers, $data);
$output = $result->output;
if ($result->statusCode != 200) {
$err = $output->message;
throw new Error($err);
}
}
private function setAccountDataAtNextcloud(object $userData)
$token = getenv('ECLOUD_ACCOUNTS_SECRET');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
"email" => $userData->email,
"quota" => $userData->quota,
"recoveryEmail" => $userData->authmail,
"hmeAlias" => $userData->hmeAlias,
"tosAccepted" => $userData->tosAccepted
$data['uid'] = $this->usernameIsEmail ? $userData->email : $userData->username;
$data['userLanguage'] = strtolower(getCurrentRequestLanguage());
curl_setopt($ch, CURLOPT_URL, $this->ecloudAccountsApiUrl . 'set_account_data');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$output = curl_exec($ch);
$output = json_decode($output, false);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$answer = new \stdClass;
$answer->success = true;
$errorNotEmpty = !empty($output->error);
$isRecoveryEmailError = $errorNotEmpty && $output->error === 'error_setting_recovery';
$isHmeError = $errorNotEmpty && $output->error === 'error_adding_hme_alias';
if ($isRecoveryEmailError) {
$message = 'Setting recovery email of user ' . $userData->email . ' failed with status code: ' . $statusCode . '(recovery email: ' . $userData->authmail . ')' . PHP_EOL;
$message = 'Setting HME alias of user ' . $userData->email . ' failed with status code: ' . $statusCode . '(HME alias: ' . $userData->hmeAlias . ')' . PHP_EOL;
// Don't fail if recovery email or hide-my-email alias not set correctly
$answer->success = $isRecoveryEmailError || $isHmeError;
$answer->type = $errorNotEmpty ? $output->error : 'error_creating_account';
}
return $answer;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$data = array(
"uid" => $uid,
"token" => $token,
);
curl_setopt($ch, CURLOPT_URL, $this->ecloudAccountsApiUrl . 'user_exists');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$output = curl_exec($ch);
$output = json_decode($output);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($statusCode !== 200) {
$err = curl_error($ch);
throw new Error($err);
private function isAliasTaken(string $alias): bool
{
$token = getenv('COMMON_SERVICES_TOKEN');
$endpoint = "v2/aliases/hide-my-email?alias=$alias";
$url = $this->commonApiUrl . $endpoint;
$headers = [
"Authorization: Bearer $token"
];
$result = curlRequest('GET', $url, $headers);
$statusCode = $result->statusCode;
if ($statusCode !== 200 && $statusCode !== 404) {
throw new Error("Error with request to check if alias exists, status code : " . $statusCode);
}
return $statusCode === 200;
}