Files
2025-07-24 16:18:27 +07:00

363 lines
10 KiB
PHP

<?php
namespace App\Http\Services;
use App\Models\ModuleHosting;
use App\Support\Eloquent\Sluggable;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class Hosting
{
use Sluggable;
public string|null $apiKey = null;
public string|null $url = null;
public string|null $username = null;
public string|null $password = null;
/*
* get All Domains in hosting server
*/
public function getAllDomains()
{
$apiCheck = $this->apiCheck();
if ($apiCheck) {
return Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->get("https://" . $this->url . '/api/v2/domains')->json();
}
return "Login_Again";
}
/*
* Check exits apikey
*/
protected function apiCheck(): bool
{
if (is_null($this->apiKey)) {
Log::info('Không tồn tại apiKey');
return false;
}
$response = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])->get("https://$this->url/api/v2/clients");
if ($response->status() === 200) {
return true;
}
return false;
}
/*
* check user exits from hosting server with email client
*/
public function LoginServer(): array
{
$response = Http::withoutVerifying()
->withBasicAuth($this->username, $this->password)
->acceptJson()
->post("https://$this->url/api/v2/auth/keys",
[
"login" => "admin"
]
);
if ($response->status() === 201) {
return [
'error' => false,
'data' => $response->json()
];
}
return [
'error' => true,
'data' => $response->status()
];
}
/*
* get All Client in hosting server
*/
public function CheckUserClient($customer = []): array|string
{
$apiCheck = $this->apiCheck();
if ($apiCheck) {
$allClient = $this->getAllClients();
$clientCheck = array_filter($allClient, function ($element) use ($customer) {
if ($element["email"] == $customer->email) {
return $element;
}
});
if (count($clientCheck)) {
$Service = ModuleHosting::where('customer_id', $customer->id)->first();
return [
'error' => false,
'data' => array_merge($clientCheck, ['password' => SignRSA::DecryptFunc($Service->password)])
];
}
return [
'error' => true,
'data' => $clientCheck
];
}
return "Login_Again";
}
/*
* Login to server
*/
public function getAllClients()
{
return Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])->get("https://$this->url/api/v2/clients")->json();
}
public function CreateNewClient($product_data = [], $password = ''): array
{
$response = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->acceptJson()
->post("https://$this->url/api/v2/clients", [
'name' => $product_data->Customer->firstname . " " . $product_data->Customer->lastname,
'company' => $product_data->Customer->company,
'login' => $product_data->Customer->email,
'email' => $product_data->Customer->email,
'password' => $password,
'type' => 'customer'
]);
if ($response->status() === 201) {
return [
'error' => false,
'data' => array_merge($response->json(), ['password' => $password])
];
}
return [
'error' => true,
'data' => $response->json()
];
}
public function CheckDomainExit($customer_id = 0, $domain = ''): array|string
{
$apiCheck = $this->apiCheck();
if ($apiCheck) {
$ClientDomain = $this->GetAllDomainForCustomer($customer_id);
if ($ClientDomain['error'] === false) {
$Result = array_filter($ClientDomain['data'], function ($data) use ($domain) {
if ($data['name'] === $domain) {
return $data;
}
});
if (count($Result)) {
return [
'error' => true,
'data' => 'Domain already exists'
];
}
return [
'error' => false,
'data' => "Can't find domain for customer"
];
}
return [
'error' => false,
'data' => $ClientDomain['data']
];
}
return "Login_Again";
}
public function GetAllDomainForCustomer($customer_id = 0): array|string
{
$apiCheck = $this->apiCheck();
if ($apiCheck) {
$allDomain = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->get("https://$this->url/api/v2/clients/$customer_id/domains");
if ($allDomain->status() === 20) {
return [
'error' => false,
'data' => $allDomain->json()
];
}
return [
'error' => true,
'data' => $allDomain->json()
];
}
return "Login_Again";
}
/*
* create new plan user domain
*/
public function CreateNewDomain($product_data = [], $domain = '', $customer = [], $password = '')
{
$planHosting = $product_data->GetInvoiceItem()->GetItemProduct()->template;
$apiCheck = $this->apiCheck();
if ($apiCheck) {
$CreateDomain = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->post("https://$this->url/api/v2/domains", [
"name" => $domain,
"hosting_type" => "virtual",
"hosting_settings" => [
"ftp_login" => $domain,
"ftp_password" => $password
],
"owner_client" => [
"id" => $customer['id'],
"login" => "owner",
"guid" => $customer['guid']
],
"plan" => [
"name" => $planHosting
]
]);
if ($CreateDomain->status() === 201) {
return [
'error' => false,
'data' => $CreateDomain->json()
];
}
return [
'error' => true,
'data' => $CreateDomain->json()
];
}
return "Login_Again";
}
public function ChangeStatusHosting($domain_id = '', $status = 'active')
{
}
public function CreateExpirationForDomain($domain = '', $date_exp = '')
{
$response = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->post("https://$this->url/api/v2/cli/domain_pref/call", [
"params" => [
"--update",
"$domain",
"-expiration",
"$date_exp"
]
])->json();
if ($response['code'] === 0) {
return [
'error' => false,
'data' => $response['stdout']
];
}
return [
'error' => true,
'data' => $response['stdout']
];
}
public function SuspendAccount($account_id = 0): array
{
$response = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->put("https://$this->url/api/v2/clients/$account_id/suspend")->json();
if ($response['status'] === "success") {
return [
'error' => false
];
}
return [
'error' => true,
'data' => $response['message']
];
}
public function ActivateAccount($account_id = 0): array
{
$response = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->put("https://$this->url/api/v2/clients/$account_id/activate")->json();
if ($response['status'] === "success") {
return [
'error' => false
];
}
return [
'error' => true,
'data' => $response['message']
];
}
public function ResetPassword($account_id = 0, $password = ''): array
{
$response = Http::withoutVerifying()
->withHeaders([
'X-API-Key' => $this->apiKey,
'accept' => 'application/json'
])
->put("https://$this->url/api/v2/clients/$account_id", [
'password' => $password
])->json();
if (!isset($response['code'])) {
return [
'error' => false
];
}
return [
'error' => true,
'data' => $response['errors']['message']
];
}
}