2022-03-05 16:02:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-05-14 17:21:51 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-10-04 05:35:46 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-03-05 16:02:12 +01:00
|
|
|
|
|
|
|
class Server extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
2022-07-22 15:49:18 +02:00
|
|
|
protected $table = 'servers';
|
|
|
|
|
|
|
|
protected $keyType = 'string';
|
|
|
|
|
2022-03-05 16:02:12 +01:00
|
|
|
public $incrementing = false;
|
|
|
|
|
2022-06-26 09:47:18 +02:00
|
|
|
protected $fillable = ['id', 'hostname', 'ipv4', 'ipv6', 'server_type', 'os_id', 'location_id', 'provider_id',
|
|
|
|
'ram', 'disk', 'ram_type', 'disk_type', 'ns1', 'ns2', 'label', 'bandwidth', 'ram_as_mb', 'disk_as_gb',
|
|
|
|
'has_yabs', 'was_promo', 'owned_since', 'ssh', 'active', 'show_public', 'cpu'];
|
2022-03-05 16:02:12 +01:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
2022-10-04 05:35:46 +02:00
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::addGlobalScope('order', function (Builder $builder) {
|
2022-10-31 01:14:26 +01:00
|
|
|
$array = Settings::orderByProcess(Session::get('sort_on') ?? 2);//created_at desc if not set
|
2022-10-04 05:35:46 +02:00
|
|
|
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
|
|
|
|
$builder->orderBy($array[0], $array[1]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-19 05:12:51 +02:00
|
|
|
public static function allServers()
|
|
|
|
{//All servers and relationships (no using joins)
|
|
|
|
return Cache::remember("all_servers", now()->addMonth(1), function () {
|
2022-10-20 04:57:04 +02:00
|
|
|
$query = Server::with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels']);
|
|
|
|
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
|
|
|
|
$options = Settings::orderByProcess(Session::get('sort_on'));
|
|
|
|
$query->orderBy(Pricing::select("pricings.$options[0]")->whereColumn("pricings.service_id", "servers.id"), $options[1]);
|
|
|
|
}
|
|
|
|
return $query->get();
|
2022-07-19 05:12:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public static function server(string $server_id): Server
|
2022-07-19 05:12:51 +02:00
|
|
|
{//Single server and relationships (no using joins)
|
|
|
|
return Cache::remember("server.$server_id", now()->addMonth(1), function () use ($server_id) {
|
|
|
|
return Server::where('id', $server_id)
|
2022-10-13 04:43:09 +02:00
|
|
|
->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels'])->first();
|
2022-07-19 05:12:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function allActiveServers()
|
|
|
|
{//All ACTIVE servers and relationships replaces activeServersDataIndexPage()
|
|
|
|
return Cache::remember("all_active_servers", now()->addMonth(1), function () {
|
2023-08-18 11:33:26 +02:00
|
|
|
$query = Server::where('active', 1)
|
2022-10-20 04:57:04 +02:00
|
|
|
->with(['location', 'provider', 'os', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'price']);
|
|
|
|
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
|
|
|
|
$options = Settings::orderByProcess(Session::get('sort_on'));
|
|
|
|
$query->orderBy(Pricing::select("pricings.$options[0]")->whereColumn("pricings.service_id", "servers.id"), $options[1]);
|
|
|
|
}
|
|
|
|
return $query->get();
|
2022-07-19 05:12:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function allNonActiveServers()
|
|
|
|
{//All NON ACTIVE servers and relationships replaces nonActiveServersDataIndexPage()
|
|
|
|
return Cache::remember("non_active_servers", now()->addMonth(1), function () {
|
2023-08-18 11:33:26 +02:00
|
|
|
return Server::where('active', 0)
|
2022-10-13 04:43:09 +02:00
|
|
|
->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels'])
|
2022-07-19 05:12:51 +02:00
|
|
|
->get();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function allPublicServers()
|
|
|
|
{//server data that will be publicly viewable (values in settings)
|
|
|
|
return Cache::remember("public_server_data", now()->addMonth(1), function () {
|
2023-08-18 11:33:26 +02:00
|
|
|
return Server::where('show_public', 1)
|
2022-10-13 04:43:09 +02:00
|
|
|
->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels'])
|
2022-07-19 05:12:51 +02:00
|
|
|
->get();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-04 02:13:57 +02:00
|
|
|
public static function serviceServerType(int $type, bool $short = true): string
|
2022-03-05 16:02:12 +01:00
|
|
|
{
|
|
|
|
if ($type === 1) {
|
|
|
|
return "KVM";
|
|
|
|
} elseif ($type === 2) {
|
|
|
|
return "OVZ";
|
|
|
|
} elseif ($type === 3) {
|
2022-10-04 02:13:57 +02:00
|
|
|
if (!$short) {
|
|
|
|
return "Dedicated";
|
|
|
|
}
|
2022-03-05 16:02:12 +01:00
|
|
|
return "DEDI";
|
|
|
|
} elseif ($type === 4) {
|
|
|
|
return "LXC";
|
2022-05-14 15:39:31 +02:00
|
|
|
} elseif ($type === 6) {
|
|
|
|
return "VMware";
|
2022-10-04 02:13:57 +02:00
|
|
|
} elseif ($type === 7) {
|
|
|
|
return "NAT";
|
2024-06-08 08:04:08 +02:00
|
|
|
} elseif ($type === 8) {
|
|
|
|
if (!$short) {
|
|
|
|
return "Colocated";
|
|
|
|
}
|
|
|
|
return "COLO";
|
2022-03-05 16:02:12 +01:00
|
|
|
} else {
|
2022-10-04 02:13:57 +02:00
|
|
|
if (!$short) {
|
|
|
|
return "Semi-dedicated";
|
|
|
|
}
|
2022-03-05 16:02:12 +01:00
|
|
|
return "SEMI-DEDI";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 02:58:19 +02:00
|
|
|
public static function osIntToIcon(int $os, string $os_name): string
|
2022-03-05 16:02:12 +01:00
|
|
|
{
|
|
|
|
if ($os === 1) {//None
|
|
|
|
return "<i class='fas fa-expand' title='{$os_name}'></i>";
|
|
|
|
} else if ($os <= 3) {//Centos
|
|
|
|
return "<i class='fab fa-centos os-icon' title='{$os_name}'></i>";
|
2022-09-22 04:25:19 +02:00
|
|
|
} elseif ($os > 7 && $os <= 11) {//Debain
|
2022-03-05 16:02:12 +01:00
|
|
|
return "<i class='fab fa-linux os-icon' title='{$os_name}'></i>";
|
2022-09-22 04:25:19 +02:00
|
|
|
} elseif ($os > 12 && $os < 15) {//Fedora
|
2022-03-05 16:02:12 +01:00
|
|
|
return "<i class='fab fa-fedora os-icon' title='{$os_name}'></i>";
|
2022-09-22 04:25:19 +02:00
|
|
|
} elseif ($os > 14 && $os < 18) {//FreeBSD
|
2022-03-05 16:02:12 +01:00
|
|
|
return "<i class='fab fa-linux os-icon' title='{$os_name}'></i>";
|
2022-09-22 04:25:19 +02:00
|
|
|
} elseif ($os > 17 && $os < 21) {//OpenBSD
|
2022-03-05 16:02:12 +01:00
|
|
|
return "<i class='fab fa-linux os-icon' title='{$os_name}'></i>";
|
2022-09-22 04:25:19 +02:00
|
|
|
} elseif ($os > 25 && $os < 32) {//Ubuntu
|
2022-03-05 16:02:12 +01:00
|
|
|
return "<i class='fab fa-ubuntu os-icon' title='{$os_name}'></i>";
|
2022-09-22 04:25:19 +02:00
|
|
|
} elseif ($os > 32 && $os < 38) {//Windows
|
2022-03-05 16:02:12 +01:00
|
|
|
return "<i class='fab fa-windows os-icon' title='{$os_name}'></i>";
|
|
|
|
} else {//OTHER ISO CUSTOM etc
|
|
|
|
return "<i class='fas fa-compact-disc os-icon' title='{$os_name}'></i>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 02:58:19 +02:00
|
|
|
public static function tableRowCompare(string $val1, string $val2, string $value_type = '', bool $is_int = true): string
|
2022-03-05 16:02:12 +01:00
|
|
|
{
|
|
|
|
//<td class="td-nowrap plus-td">+303<span class="data-type">MBps</span></td>
|
|
|
|
$str = '<td class="td-nowrap ';
|
|
|
|
$value_append = '<span class="data-type">' . $value_type . '</span>';
|
|
|
|
if ($is_int) {
|
|
|
|
$val1 = (int)$val1;
|
|
|
|
$val2 = (int)$val2;
|
|
|
|
}
|
|
|
|
if ($val1 > $val2) {//val1 is greater than val2
|
|
|
|
$result = '+' . ($val1 - $val2);
|
|
|
|
if (!empty($value_type)) {
|
|
|
|
$result = '+' . ($val1 - $val2) . $value_append;
|
|
|
|
|
|
|
|
}
|
|
|
|
$str .= 'plus-td">' . $result . '</td>';
|
|
|
|
} elseif ($val1 < $val2) {//val1 is less than val2
|
|
|
|
$result = '-' . ($val2 - $val1);
|
|
|
|
if (!empty($value_type)) {
|
|
|
|
$result = '-' . ($val2 - $val1) . $value_append;
|
|
|
|
}
|
|
|
|
$str .= 'neg-td">' . $result . '</td>';
|
|
|
|
} else {//Equal
|
|
|
|
$result = 0;
|
|
|
|
if (!empty($value_type)) {
|
|
|
|
$result = '0' . $value_append;
|
|
|
|
}
|
|
|
|
$str .= 'equal-td">' . $result . '</td>';
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2022-05-14 17:21:51 +02:00
|
|
|
public static function serverRelatedCacheForget(): void
|
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
Cache::forget('all_servers');//All servers
|
2022-05-14 17:21:51 +02:00
|
|
|
Cache::forget('services_count');//Main page services_count cache
|
|
|
|
Cache::forget('due_soon');//Main page due_soon cache
|
|
|
|
Cache::forget('recently_added');//Main page recently_added cache
|
2022-07-19 05:12:51 +02:00
|
|
|
Cache::forget('all_active_servers');//all active servers cache
|
|
|
|
Cache::forget('non_active_servers');//all non active servers cache
|
2022-05-14 17:21:51 +02:00
|
|
|
Cache::forget('servers_summary');//servers summary cache
|
|
|
|
Cache::forget('public_server_data');//public servers
|
2022-05-16 06:45:22 +02:00
|
|
|
Cache::forget('all_pricing');//All pricing
|
2022-05-29 09:02:49 +02:00
|
|
|
Cache::forget('services_count_all');
|
|
|
|
Cache::forget('pricing_breakdown');
|
2023-09-14 15:38:46 +02:00
|
|
|
Cache::forget('all_active_pricing');
|
2022-05-14 17:21:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function serverSpecificCacheForget(string $server_id): void
|
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
Cache::forget("server.$server_id");//Will replace one below
|
2022-05-14 17:21:51 +02:00
|
|
|
Cache::forget("service_pricing.$server_id");//Pricing
|
|
|
|
}
|
|
|
|
|
2022-07-19 05:12:51 +02:00
|
|
|
public static function serverYabsAmount(string $server_id): int
|
2022-11-02 12:02:12 +01:00
|
|
|
{//Returns amount of YABS a server has
|
2023-08-18 11:31:21 +02:00
|
|
|
return Yabs::where('server_id', $server_id)->count();
|
2022-07-19 05:12:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function yabs(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-05-14 17:21:51 +02:00
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
return $this->hasMany(Yabs::class, 'server_id', 'id');
|
2022-05-14 17:21:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function ips(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-05-14 17:21:51 +02:00
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
return $this->hasMany(IPs::class, 'service_id', 'id');
|
2022-05-14 17:21:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function location(): \Illuminate\Database\Eloquent\Relations\HasOne
|
2022-05-14 17:21:51 +02:00
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
return $this->hasOne(Locations::class, 'id', 'location_id');
|
2022-05-14 17:21:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function provider(): \Illuminate\Database\Eloquent\Relations\HasOne
|
2022-05-14 17:21:51 +02:00
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
return $this->hasOne(Providers::class, 'id', 'provider_id');
|
2022-05-14 17:21:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function os(): \Illuminate\Database\Eloquent\Relations\HasOne
|
2022-05-14 17:21:51 +02:00
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
return $this->hasOne(OS::class, 'id', 'os_id');
|
2022-05-14 17:21:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function price(): \Illuminate\Database\Eloquent\Relations\HasOne
|
2022-07-18 06:27:11 +02:00
|
|
|
{
|
2022-07-19 05:12:51 +02:00
|
|
|
return $this->hasOne(Pricing::class, 'service_id', 'id');
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:35:53 +02:00
|
|
|
public function labels(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-07-19 05:12:51 +02:00
|
|
|
{
|
|
|
|
return $this->hasMany(LabelsAssigned::class, 'service_id', 'id');
|
2022-07-18 06:27:11 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 03:57:28 +01:00
|
|
|
public function note(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
{
|
|
|
|
return $this->hasOne(Note::class, 'service_id', 'id');
|
|
|
|
}
|
|
|
|
|
2022-03-05 16:02:12 +01:00
|
|
|
}
|