osb/app/Models/Service/SSL.php

116 lines
2.0 KiB
PHP

<?php
namespace App\Models\Service;
use Carbon\Carbon;
use Illuminate\Support\Arr;
/**
* Class SSL (Service)
* Services that are provide an SSL Certificate
*/
class SSL extends Type
{
protected $table = 'service_ssl';
protected $public_key = NULL;
protected $crt_parse = NULL;
/* STATIC */
public static function boot()
{
parent::boot();
static::retrieved(function($model) {
if ($model->cert);
$model->crt_parse = collect(openssl_x509_parse($model->cert));
if ($model->csr) {
$model->public_key = collect(openssl_pkey_get_details(openssl_csr_get_public_key($model->csr)));
}
});
}
/* ABSTRACT */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return mixed
*/
public function scopeSearch($query,string $term)
{
// @todo
}
/* INTERFACES */
/**
* Return the Cert DN
*
* @return string
*/
public function getServiceDescriptionAttribute(): string
{
if ($this->cert)
return Arr::get($this->crt_parse,'name');
else {
$dn = '';
$dna = openssl_csr_get_subject($this->csr);
foreach ($dna as $k=>$v) {
if ($dn)
$dn .= ',';
$dn .= sprintf('%s=%s',$k,$v);
}
return $dn;
}
}
/**
* Return the Certificate Expiry Date
*/
public function getServiceExpireAttribute(): ?Carbon
{
return $this->cert ? Carbon::createFromTimestamp($this->crt_parse->get('validTo_time_t')) : NULL;
}
/**
* Return the Cert Name
*
* @return string
*/
public function getServiceNameAttribute(): string
{
return $this->cert
? Arr::get($this->crt_parse,'subject.CN')
: Arr::get(openssl_csr_get_subject($this->csr),'CN','');
}
/**
* Certificates have no contract and can be cancelled anytime.
*
* @return bool
*/
public function inContract(): bool
{
return FALSE;
}
/* METHODS */
/**
* @return Carbon|null
* @deprecated use getServiceExpireAttribute()
*/
public function getValidToAttribute()
{
abort(500,'use getServiceExpireAttribute');
}
}