2024-04-22 11:14:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Actions\CreateLinuxWebUser;
|
|
|
|
use App\Actions\GetLinuxUser;
|
2024-04-26 16:04:01 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-04-22 11:14:05 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class HostingSubscription extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'phyre_server_id',
|
|
|
|
'external_id',
|
|
|
|
'domain',
|
|
|
|
'customer_id',
|
|
|
|
'hosting_plan_id',
|
|
|
|
'system_username',
|
|
|
|
'system_password',
|
|
|
|
'description',
|
|
|
|
'setup_date',
|
|
|
|
'expiry_date',
|
|
|
|
'renewal_date',
|
|
|
|
];
|
|
|
|
|
2024-04-26 16:04:01 +00:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
|
|
|
static::addGlobalScope('customer', function (Builder $query) {
|
|
|
|
if (auth()->check() && auth()->guard()->name == 'web_customer') {
|
|
|
|
$query->where('customer_id', auth()->user()->id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::creating(function ($model) {
|
2024-05-12 18:26:16 +00:00
|
|
|
$findDomain = Domain::where('domain', $model->domain)->first();
|
|
|
|
if ($findDomain) {
|
|
|
|
throw new \Exception('Domain already exists');
|
|
|
|
}
|
2024-04-22 11:14:05 +00:00
|
|
|
$create = $model->_createLinuxWebUser($model);
|
2024-05-12 18:42:24 +00:00
|
|
|
if (isset($create['error'])) {
|
|
|
|
throw new \Exception($create['message']);
|
|
|
|
}
|
2024-04-22 11:14:05 +00:00
|
|
|
if (isset($create['system_username']) && isset($create['system_password'])) {
|
|
|
|
$model->system_username = $create['system_username'];
|
|
|
|
$model->system_password = $create['system_password'];
|
|
|
|
} else {
|
2024-08-05 11:40:58 +00:00
|
|
|
throw new \Exception('System username or password not created');
|
2024-04-22 11:14:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
static::created(function ($model) {
|
|
|
|
$makeMainDomain = new Domain();
|
|
|
|
$makeMainDomain->hosting_subscription_id = $model->id;
|
|
|
|
$makeMainDomain->domain = $model->domain;
|
|
|
|
$makeMainDomain->is_main = 1;
|
|
|
|
$makeMainDomain->status = Domain::STATUS_ACTIVE;
|
|
|
|
$makeMainDomain->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
static::deleting(function ($model) {
|
|
|
|
|
2024-04-30 23:16:24 +00:00
|
|
|
if (empty($model->system_username)) {
|
|
|
|
throw new \Exception('System username is empty');
|
|
|
|
}
|
2024-05-09 08:42:27 +00:00
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
$getLinuxUser = new GetLinuxUser();
|
|
|
|
$getLinuxUser->setUsername($model->system_username);
|
|
|
|
$getLinuxUserStatus = $getLinuxUser->handle();
|
|
|
|
|
|
|
|
if (! empty($getLinuxUserStatus)) {
|
|
|
|
shell_exec('userdel '.$model->system_username);
|
|
|
|
shell_exec('rm -rf /home/'.$model->system_username);
|
|
|
|
}
|
|
|
|
$findRelatedDomains = Domain::where('hosting_subscription_id', $model->id)->get();
|
|
|
|
if ($findRelatedDomains->count() > 0) {
|
|
|
|
foreach ($findRelatedDomains as $domain) {
|
|
|
|
$domain->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function customer()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Customer::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hostingPlan()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(HostingPlan::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function databases()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Database::class);
|
|
|
|
}
|
|
|
|
|
2024-04-25 14:12:03 +00:00
|
|
|
public function backups()
|
|
|
|
{
|
|
|
|
return $this->hasMany(HostingSubscriptionBackup::class);
|
|
|
|
}
|
|
|
|
|
2024-04-30 22:06:11 +00:00
|
|
|
public function domain()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Domain::class);
|
|
|
|
}
|
|
|
|
|
2024-05-09 08:42:27 +00:00
|
|
|
public function ftpAccounts()
|
|
|
|
{
|
|
|
|
return $this->hasMany(HostingSubscriptionFtpAccount::class);
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
private function _createLinuxWebUser($model): array
|
|
|
|
{
|
|
|
|
$findCustomer = Customer::where('id', $model->customer_id)->first();
|
|
|
|
if (! $findCustomer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:42:24 +00:00
|
|
|
if (!empty($model->system_username)) {
|
|
|
|
$getLinuxUser = new GetLinuxUser();
|
|
|
|
$getLinuxUser->setUsername($model->system_username);
|
|
|
|
$linuxUser = $getLinuxUser->handle();
|
|
|
|
if (!empty($linuxUser)) {
|
|
|
|
return [
|
|
|
|
'error' => true,
|
|
|
|
'message' => 'System username already exists.'
|
|
|
|
];
|
|
|
|
}
|
2024-04-22 11:14:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 18:42:24 +00:00
|
|
|
if (empty($model->system_username)) {
|
|
|
|
$systemUsername = $this->_generateUsername($model->domain . $findCustomer->id);
|
|
|
|
if ($this->_startsWithNumber($systemUsername)) {
|
|
|
|
$systemUsername = $this->_generateUsername(Str::random(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
$getLinuxUser = new GetLinuxUser();
|
|
|
|
$getLinuxUser->setUsername($systemUsername);
|
|
|
|
$linuxUser = $getLinuxUser->handle();
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-05-12 18:42:24 +00:00
|
|
|
if (!empty($linuxUser)) {
|
|
|
|
$systemUsername = $this->_generateUsername($systemUsername . $findCustomer->id . Str::random(4));
|
|
|
|
}
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-05-12 18:42:24 +00:00
|
|
|
$systemPassword = Str::random(14);
|
|
|
|
} else {
|
|
|
|
$systemUsername = $model->system_username;
|
|
|
|
$systemPassword = $model->system_password;
|
|
|
|
}
|
2024-04-22 11:14:05 +00:00
|
|
|
|
|
|
|
$createLinuxWebUser = new CreateLinuxWebUser();
|
|
|
|
$createLinuxWebUser->setUsername($systemUsername);
|
|
|
|
$createLinuxWebUser->setPassword($systemPassword);
|
2024-04-23 14:44:55 +00:00
|
|
|
$createLinuxWebUserOutput = $createLinuxWebUser->handle();
|
2024-04-22 11:14:05 +00:00
|
|
|
|
|
|
|
if (strpos($createLinuxWebUserOutput, 'Creating home directory') !== false) {
|
|
|
|
|
2024-08-05 11:40:58 +00:00
|
|
|
// Check user is created
|
|
|
|
$getLinuxUser = new GetLinuxUser();
|
|
|
|
$getLinuxUser->setUsername($systemUsername);
|
|
|
|
$linuxUser = $getLinuxUser->handle();
|
|
|
|
if (empty($linuxUser)) {
|
|
|
|
return [
|
|
|
|
'error' => true,
|
|
|
|
'message' => 'System username not created.'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
return [
|
|
|
|
'system_username' => $systemUsername,
|
|
|
|
'system_password' => $systemPassword
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
|
|
|
}
|
|
|
|
private static function _generateUsername($string)
|
|
|
|
{
|
|
|
|
$removedMultispace = preg_replace('/\s+/', ' ', $string);
|
|
|
|
$sanitized = preg_replace('/[^A-Za-z0-9\ ]/', '', $removedMultispace);
|
|
|
|
$lowercased = strtolower($sanitized);
|
|
|
|
$lowercased = str_replace(' ', '', $lowercased);
|
|
|
|
$lowercased = trim($lowercased);
|
|
|
|
if (strlen($lowercased) > 10) {
|
|
|
|
$lowercased = substr($lowercased, 0, 4);
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:49:13 +00:00
|
|
|
$username = $lowercased.rand(1111, 9999).Str::random(4);
|
2024-04-22 11:14:05 +00:00
|
|
|
$username = strtolower($username);
|
|
|
|
|
|
|
|
return $username;
|
|
|
|
}
|
|
|
|
private function _startsWithNumber($string) {
|
|
|
|
return strlen($string) > 0 && ctype_digit(substr($string, 0, 1));
|
|
|
|
}
|
|
|
|
}
|