mivita/app/Models/UserShop.php
2022-04-14 13:14:36 +02:00

230 lines
No EOL
8.6 KiB
PHP

<?php
namespace App\Models;
use App\Http\Controllers\Api\KasController;
use App\Http\Controllers\Api\KasSLLController;
use Carbon\Carbon;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\UserShop
*
* @property int $id
* @property int $user_id
* @property string $name
* @property string $slug
* @property int $active
* @property int|null $set_defaults
* @property string|null $active_date
* @property string|null $title
* @property string|null $contact
* @property string|null $accessibility
* @property string|null $about
* @property array|null $featured
* @property string|null $filename
* @property string|null $originalname
* @property string|null $ext
* @property string|null $mine
* @property int|null $size
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserShopOnSite[] $on_sites
* @property-read int|null $on_sites_count
* @property-read \App\User $user
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop findSimilarSlugs($attribute, $config, $slug)
* @method static bool|null forceDelete()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop newQuery()
* @method static \Illuminate\Database\Query\Builder|\App\Models\UserShop onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop query()
* @method static bool|null restore()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereAbout($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereAccessibility($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereActiveDate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereContact($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereExt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereFeatured($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereFilename($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereMine($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereOriginalname($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereSetDefaults($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereSize($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserShop whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\UserShop withTrashed()
* @method static \Illuminate\Database\Query\Builder|\App\Models\UserShop withoutTrashed()
* @mixin \Eloquent
* @method static \Illuminate\Database\Eloquent\Builder|UserShop withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug)
*/
class UserShop extends Model
{
protected $table = 'user_shops';
private $is_online = NULL;
private $ssl_certificate_sni = NULL;
private $ssl_certificate_sni_is_active = NULL;
protected $casts = [
'featured' => 'array',
];
protected $fillable = [
'user_id', 'name', 'active', 'active_date',
];
use SoftDeletes;
protected $dates = ['deleted_at'];
use Sluggable;
public function sluggable()
{
return [
'slug' => [
'source' => 'name'
]
];
}
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function on_sites(){
return $this->hasMany('App\Models\UserShopOnSite', 'user_shop_id', 'id');
}
public function getActiveDateFormat(){
if(!$this->attributes['active_date']){ return ""; }
return Carbon::parse($this->attributes['active_date'])->format(\Util::formatDateTimeDB());
}
public function getActiveDateFormatSmall(){
if(!$this->attributes['active_date']){ return ""; }
return Carbon::parse($this->attributes['active_date'])->format("d.m.Y");
}
public function getSubdomain($session=true)
{
if($session && \Session::has('user_shop_domain')){
return \Session::get('user_shop_domain');
}
return config('app.protocol').$this->attributes['slug'].".".config('app.domain').config('app.tld_care');
}
public function getSubdomainStatus()
{
if($this->is_online !== NULL){
return $this->is_online;
}
$kas = new KasController();
$domain = 'mivita.care';
$pra = array(
'subdomain_name' => $this->attributes['slug'].".".$domain,
'domain_name' => $domain,
);
//check if exisist
$subdomain = $kas->action('get_subdomains', $pra);
if(is_soap_fault($subdomain)){
return false;
}
if(!empty($subdomain[0]['is_active']) && $subdomain[0]['is_active'] == 'Y'){
$this->is_online = true;
}else{
$this->is_online = false;
}
if(!empty($subdomain[0]['ssl_certificate_sni']) && $subdomain[0]['ssl_certificate_sni'] == 'Y'){
$this->ssl_certificate_sni = true;
}else{
$this->ssl_certificate_sni = false;
$this->userShopAddSSL($subdomain[0]['subdomain_name']);
}
if(!empty($subdomain[0]['ssl_certificate_sni_is_active']) && $subdomain[0]['ssl_certificate_sni_is_active'] == 'j'){
$this->ssl_certificate_sni_is_active = true;
}else{
$this->ssl_certificate_sni_is_active = false;
}
return $this->is_online;
}
public function userShopAddSSL($full_subdomain_name)
{
$kas = new KasController();
$ssl = KasSLLController::getApiSSLParameter();
$pra = array(
'hostname' => $full_subdomain_name
);
$pra = array_merge($pra, $ssl);
$value = $kas->action('update_ssl', $pra);
return $value;
}
public function getSubdomainSslSin ()
{
return $this->ssl_certificate_sni;
}
public function getSubdomainSslSinActive ()
{
return $this->ssl_certificate_sni_is_active;
}
public function getSubdomainAvailable ()
{
$rCurlHandle = curl_init ( $this->attributes['slug'].".mivita.care" );
curl_setopt ( $rCurlHandle, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt ( $rCurlHandle, CURLOPT_HEADER, TRUE );
curl_setopt ( $rCurlHandle, CURLOPT_NOBODY, TRUE );
curl_setopt ( $rCurlHandle, CURLOPT_RETURNTRANSFER, TRUE );
$strResponse = curl_exec ( $rCurlHandle );
curl_close ( $rCurlHandle );
if ( !$strResponse )
{
return FALSE;
}
$url = "http://".$this->attributes['slug'].".mivita.care/domain/check";
if(@file_get_contents($url) != 'checked'){
return FALSE;
}
return TRUE;
}
public function isImage(){
if(empty($this->attributes['filename']) || @$this->attributes['filename'] == null || @$this->attributes['filename'] == ""){
return false;
}
if(!\Storage::disk('public')->has('images/shop/'.$this->filename)){
return false;
}
return true;
}
public function getImage(){
if($this->isImage()){
$link = 'images/shop/'.$this->filename;
return '/storage/'.$link.'?=lm='.\Storage::disk('public')->lastModified($link);
}
return false;
}
}