2024-04-22 11:14:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-04-26 16:20:06 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-04-22 11:14:05 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-04-24 19:58:22 +00:00
|
|
|
use Illuminate\Support\Str;
|
2024-04-22 11:14:05 +00:00
|
|
|
|
|
|
|
class CronJob extends Model
|
|
|
|
{
|
|
|
|
protected $fillable = [
|
|
|
|
'schedule',
|
|
|
|
'command',
|
|
|
|
'user',
|
2024-04-26 16:22:55 +00:00
|
|
|
'hosting_subscription_id'
|
2024-04-22 11:14:05 +00:00
|
|
|
];
|
|
|
|
|
2024-04-26 16:20:06 +00:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
|
|
|
static::addGlobalScope('customer', function (Builder $query) {
|
|
|
|
if (auth()->check() && auth()->guard()->name == 'web_customer') {
|
|
|
|
$query->whereHas('hostingSubscription', function ($query) {
|
|
|
|
$query->where('customer_id', auth()->user()->id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hostingSubscription()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(HostingSubscription::class);
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
2024-04-24 20:16:26 +00:00
|
|
|
static::creating(function ($model) {
|
2024-04-26 16:22:55 +00:00
|
|
|
if ($model->hosting_subscription_id) {
|
|
|
|
$hostingSubscription = HostingSubscription::where('id', $model->hosting_subscription_id)->first();
|
|
|
|
if ($hostingSubscription) {
|
|
|
|
$model->user = $hostingSubscription->system_username;
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 20:16:26 +00:00
|
|
|
$allCronJobs = [];
|
|
|
|
$oldCronJobs = self::where('user', $model->user)->get();
|
|
|
|
foreach ($oldCronJobs as $oldCronJob) {
|
|
|
|
$allCronJobs[$oldCronJob->user][] = $oldCronJob->toArray();
|
|
|
|
}
|
|
|
|
$allCronJobs[$model->user][] = $model->toArray();
|
|
|
|
|
|
|
|
$model->configureCronJobs($allCronJobs);
|
2024-04-24 19:58:22 +00:00
|
|
|
});
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-04-24 20:16:26 +00:00
|
|
|
static::updating(function ($model) {
|
|
|
|
$allCronJobs = [];
|
|
|
|
$oldCronJobs = self::where('user', $model->user)->get();
|
|
|
|
foreach ($oldCronJobs as $oldCronJob) {
|
|
|
|
if ($oldCronJob->id == $model->id) {
|
|
|
|
$allCronJobs[$model->user][] = $model->toArray();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$allCronJobs[$oldCronJob->user][] = $oldCronJob->toArray();
|
|
|
|
}
|
|
|
|
$model->configureCronJobs($allCronJobs);
|
2024-04-24 19:58:22 +00:00
|
|
|
});
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-04-24 20:16:26 +00:00
|
|
|
static::deleting(function ($model) {
|
|
|
|
$allCronJobs = [];
|
|
|
|
$oldCronJobs = self::where('user', $model->user)->get();
|
|
|
|
foreach ($oldCronJobs as $oldCronJob) {
|
|
|
|
if ($oldCronJob->id == $model->id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$allCronJobs[$oldCronJob->user][] = $oldCronJob->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->configureCronJobs($allCronJobs);
|
2024-04-22 11:14:05 +00:00
|
|
|
});
|
2024-04-24 19:58:22 +00:00
|
|
|
}
|
2024-04-24 20:16:26 +00:00
|
|
|
public function configureCronJobs($cronJobs)
|
2024-04-24 19:58:22 +00:00
|
|
|
{
|
2024-04-24 20:16:26 +00:00
|
|
|
$now = now();
|
|
|
|
$user = $this->user;
|
|
|
|
$cronContent = <<<EOT
|
|
|
|
# PhyrePanel Cron Jobs
|
|
|
|
# User: $user
|
|
|
|
# Generated at: $now
|
|
|
|
# Do not edit this file manually, it is automaticly generated by PhyrePanel
|
|
|
|
EOT;
|
|
|
|
$cronContent .= PHP_EOL . PHP_EOL;
|
2024-04-22 11:14:05 +00:00
|
|
|
|
2024-04-24 20:16:26 +00:00
|
|
|
if (!empty($cronJobs)) {
|
|
|
|
foreach ($cronJobs as $user => $cronJobs) {
|
2024-04-24 19:58:22 +00:00
|
|
|
foreach ($cronJobs as $cronJob) {
|
|
|
|
$cronContent .= $cronJob['schedule'] . ' ' . $cronJob['command'] . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 20:16:26 +00:00
|
|
|
$cronContent .= PHP_EOL;
|
|
|
|
$cronFile = '/tmp/crontab-' . $user;
|
|
|
|
file_put_contents($cronFile, $cronContent);
|
|
|
|
|
|
|
|
$output = shell_exec('crontab -u ' . $user . ' ' . $cronFile);
|
|
|
|
unlink($cronFile);
|
|
|
|
|
2024-04-24 19:58:22 +00:00
|
|
|
return false;
|
2024-04-22 11:14:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 16:22:55 +00:00
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
}
|