From 0dc33384ba5d4f91be72e284769c4f365cf0f242 Mon Sep 17 00:00:00 2001 From: Osahenrumwen Aigbogun Date: Thu, 17 Oct 2024 17:34:34 +0100 Subject: [PATCH] Limited Cron exposure to project scope --- src/Libs/Cron/LayCron.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Libs/Cron/LayCron.php b/src/Libs/Cron/LayCron.php index fd2b1ec..4264f1f 100644 --- a/src/Libs/Cron/LayCron.php +++ b/src/Libs/Cron/LayCron.php @@ -56,7 +56,7 @@ public static function php_bin(): string * @return string|bool * @throws \Exception */ - public static function dump_crontab(bool $suppress_win_exception = false) : string|bool + public static function dump_crontab(bool $project_scope = true, bool $suppress_win_exception = false) : string|bool { if(LayConfig::get_os() == "WINDOWS") { if($suppress_win_exception) @@ -69,7 +69,7 @@ public static function dump_crontab(bool $suppress_win_exception = false) : stri "); } - $out = self::new()->get_crontab(); + $out = self::new()->get_crontab($project_scope); if(!$out) return false; @@ -156,7 +156,7 @@ private function project_server_jobs(string $mailto, string $cron_jobs) : string { $all_jobs = ""; $app_id = LayConfig::app_id(); - $server_jobs = $this->get_crontab() ?? []; + $server_jobs = $this->get_crontab(false) ?? []; foreach ($server_jobs as $i => $job) { if(empty($job)) @@ -407,13 +407,31 @@ public function get_job(string|int $uid) : ?array { ]; } - public function get_crontab() : ?array + public function get_crontab(bool $project_scope = true) : ?array { - exec("crontab -l 2>&1", $out); + exec("crontab -l 2>&1", $jobs); - if(str_contains($out[0], "no crontab for")) + if(str_contains($jobs[0], "no crontab for")) return null; + if(!$project_scope) + return $jobs; + + $out = [$jobs[0]]; + + $app_id = LayConfig::app_id(); + + foreach ($jobs as $i => $job) { + if($i == 0 && str_starts_with($job, "MAILTO")) + continue; + + $job_app_id = LayFn::extract_cli_tag(self::APP_ID_KEY, true, $job); + $job_app_id = $job_app_id ? trim($job_app_id, "'") : $job_app_id; + + if($app_id == $job_app_id) + $out[] = $job; + } + return $out; }