Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Skip to content
ecloud_account_creator.php 5.27 KiB
Newer Older
<?php
require 'vendor/autoload.php';
require_once('language.php');
require_once('account_creator.php');

use phpseclib3\Net\SSH2;

class ECloudAccountCreator implements AccountCreator
{
    private string $eCloudUrl;
    private string $eCloudUrlUsers;
    private string $eCloudCredentials;
    private int $quotaInMB = 1024;

    public function __construct(string $eCloudUrl, string $USERNAME_ADM, string $PASSWORD_ADM)
    {
        $this->eCloudUrl = endsWith($eCloudUrl, "/") ? $eCloudUrl : $eCloudUrl . "/";
        $this->eCloudUrlUsers = $this->eCloudUrl . "ocs/v2.php/cloud/users/";
        $this->eCloudCredentials = base64_encode($USERNAME_ADM . ":" . $PASSWORD_ADM);
    }
    public function validateData(object $userData): ValidatedData
    {
        $id = "e_cloud_account_data";
        try {
            if ($this->isUsernameTaken($userData->username)) 
                return new \ValidatedData($id, "error_account_taken");
        } catch(\Error $_) {
            return new \ValidatedData($id, "error_server_side");
        }
        return new \ValidatedData($id, null);
    }
    private function isUsernameTaken(string $username): bool
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $this->eCloudUrlUsers . $username,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application/json",
                "OCS-APIRequest: true",
                "Accept: application/json",
                "Authorization: Basic " . $this->eCloudCredentials
            ),
        ));
        curl_exec($curl);
        $statusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
        $err = curl_error($curl);
        curl_close($curl);
        if (!empty($err)) {
            throw new Error($err);
        }
        $userFound = $statusCode !== 404;
        return $userFound;
    }
    
    private function createMailAccount($resultmail, $pw, $pw2, $name, $quota, $authmail)
    {
        global $strings;
        $PF_HOSTNAME = "postfixadmin";
        $PF_USER = "pfexec";
        $PF_PWD = getenv("POSTFIXADMIN_SSH_PASSWORD");

        $ssh = new SSH2($PF_HOSTNAME);
        if (!$ssh->login($PF_USER, $PF_PWD)) {
            $error_string = $strings["error_server_side"];
            sendAPIResponse(500, createAPIResponse("general", $error_string));
        }


        // 1 - create the account
Akhil's avatar
Akhil committed
        $creationFeedBack = explode("\n", $ssh->exec('/postfixadmin/scripts/postfixadmin-cli mailbox add ' . escapeshellarg($resultmail) . ' --password ' . escapeshellarg($pw) . ' --password2 ' . escapeshellarg($pw2) . ' --name ' . escapeshellarg($name) . ' --email_other ' . escapeshellarg($authmail) . ' --quota ' . $quota . ' --active 1 --welcome-mail 0 2>&1'));
        $isCreated = preg_grep('/added/', $creationFeedBack);
        $answer = new \stdClass();
        if (empty($isCreated)) {
            // There was an error during account creation on PFA side, return it
            $answer->success = false;
            $answer->type = "error_creating_account";
            return $answer;
        } else {
            // 2 - the account was created, set some settings

            //set user's Email setting on NC
            $resultSetMail = curlCallNextcloud($resultmail, "email", $resultmail);
            $detailSetMail = json_decode($resultSetMail);
            //set user's Quota setting on NC
            $resultSetQuota = curlCallNextcloud($resultmail, "quota", $quota . " MB");
            $detailSetQuota = json_decode($resultSetQuota);

            if (($detailSetMail->ocs->meta->status == "ok") && ($detailSetQuota->ocs->meta->status == "ok")) {
                // ALL GOOD, account correctly created
                $recoveryEmailStatusCode = setRecoveryEmail($resultmail, $authmail);
                if($recoveryEmailStatusCode !== 200) {
                    $message = 'Setting recovery email of user ' . $resultmail . ' failed with status code: ' . $recoveryEmailStatusCode . '(recovery email: ' . $authmail . ')' . PHP_EOL ;
                    error_log($message, 0);
                }
                $answer->success = true;
                return $answer;
            } else {
                if (($detailSetMail->ocs->meta->status != "ok")) {
                    // TODO give distinct error detail about mail or quota error
                    $answer->success = false;
                    $answer->type = "error_setting_mail";
                    return $answer;
                } elseif (($detailSetQuota->ocs->meta->status != "ok")) {
                    $answer->success = false;
                    $answer->type = "error_setting_quota";
                    return $answer;
                }
            }
        }
    }

    public function tryToCreate(object $userData)
    {
        global $strings;
        $pw = $userData->password;
        $answer = $this->createMailAccount($userData->email, $pw, $pw, $userData->name, $this->quotaInMB, $userData->authmail);
        if ($answer->success === false) {
            sendAPIResponse(400, createAPIResponse("general", $strings[$answer->type]));
        }
    }
}