2021-04-08 12:54:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace JeroenED\Webcron\Repository;
|
|
|
|
|
|
|
|
|
2021-04-13 14:07:11 +02:00
|
|
|
use DateTime;
|
2021-05-24 18:36:16 +02:00
|
|
|
use GuzzleHttp\Client;
|
2021-05-27 11:46:30 +02:00
|
|
|
use JeroenED\Framework\Repository;
|
2021-05-24 18:36:16 +02:00
|
|
|
use phpseclib3\Crypt\PublicKeyLoader;
|
|
|
|
use phpseclib3\Net\SSH2;
|
2021-04-08 12:54:49 +02:00
|
|
|
|
2021-05-27 11:46:30 +02:00
|
|
|
class Job extends Repository
|
2021-04-08 12:54:49 +02:00
|
|
|
{
|
|
|
|
public function getAllJobs()
|
|
|
|
{
|
|
|
|
$jobsSql = "SELECT * FROM job";
|
|
|
|
$jobsStmt = $this->dbcon->prepare($jobsSql);
|
2021-05-24 18:36:16 +02:00
|
|
|
$jobsRslt = $jobsStmt->executeQuery();
|
2021-04-08 12:54:49 +02:00
|
|
|
$jobs = $jobsRslt->fetchAllAssociative();
|
|
|
|
foreach ($jobs as $key=>&$job) {
|
|
|
|
$job['data'] = json_decode($job['data'], true);
|
|
|
|
}
|
|
|
|
return $jobs;
|
|
|
|
}
|
2021-04-13 14:07:11 +02:00
|
|
|
|
2021-05-24 18:36:16 +02:00
|
|
|
|
|
|
|
public function getJobsDue()
|
|
|
|
{
|
2021-05-27 21:17:10 +02:00
|
|
|
$jobsSql = "SELECT id FROM job WHERE nextrun <= :timestamp AND running != 1";
|
2021-05-24 18:36:16 +02:00
|
|
|
$jobsStmt = $this->dbcon->prepare($jobsSql);
|
|
|
|
$jobsRslt = $jobsStmt->executeQuery([':timestamp' => time()]);
|
|
|
|
$jobs = $jobsRslt->fetchAllAssociative();
|
|
|
|
$return = [];
|
|
|
|
foreach ($jobs as $job) {
|
|
|
|
$return[] = $job['id'];
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setJobRunning(int $job, bool $status): void
|
|
|
|
{
|
2021-05-27 21:17:10 +02:00
|
|
|
$jobsSql = "UPDATE job SET running = :status WHERE id = :id AND running in (0,1)";
|
2021-05-24 18:36:16 +02:00
|
|
|
$jobsStmt = $this->dbcon->prepare($jobsSql);
|
|
|
|
$jobsStmt->executeQuery([':id' => $job, ':status' => $status ? 1 : 0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
private function runHttpJob(array $job): array
|
2021-05-24 18:36:16 +02:00
|
|
|
{
|
2021-05-28 09:59:55 +02:00
|
|
|
$client = new Client();
|
2021-05-24 18:36:16 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
if(!empty($job['data']['vars'])) {
|
|
|
|
foreach($job['data']['vars'] as $key => $var) {
|
|
|
|
$job['data']['basicauth-username'] = str_replace('{' . $key . '}', $var['value'], $job['data']['basicauth-username']);
|
|
|
|
$job['data']['url'] = str_replace('{' . $key . '}', $var['value'], $job['data']['url']);
|
2021-05-24 18:36:16 +02:00
|
|
|
}
|
2021-05-28 09:59:55 +02:00
|
|
|
}
|
2021-05-24 18:36:16 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
$url = $job['data']['url'];
|
|
|
|
$options['http_errors'] = false;
|
|
|
|
$options['auth'] = [$job['data']['basicauth-username'], $job['data']['basicauth-password']];
|
|
|
|
$res = $client->request('GET', $url, $options);
|
2021-05-24 18:36:16 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
$return['exitcode'] = $res->getStatusCode();
|
|
|
|
$return['output'] = $res->getBody();
|
|
|
|
return $return;
|
|
|
|
}
|
2021-05-27 20:24:03 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
private function runCommandJob(array $job): array
|
|
|
|
{
|
|
|
|
if(!empty($job['data']['vars'])) {
|
|
|
|
foreach ($job['data']['vars'] as $key => $var) {
|
|
|
|
$job['data']['command'] = str_replace('{' . $key . '}', $var['value'], $job['data']['command']);
|
2021-05-27 20:24:03 +02:00
|
|
|
}
|
2021-05-28 09:59:55 +02:00
|
|
|
}
|
2021-05-24 18:36:16 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
$command = $job['data']['command'];
|
|
|
|
if ($job['data']['containertype'] == 'docker') {
|
|
|
|
$command = $this->prepareDockerCommand($command, $job['data']['service'], $job['data']['container-user']);
|
|
|
|
}
|
|
|
|
if($job['data']['hosttype'] == 'local') {
|
|
|
|
return $this->runLocalCommand($command);
|
|
|
|
} elseif($job['data']['hosttype'] == 'ssh') {
|
|
|
|
return $this->runSshCommand($command, $job['data']['host'], $job['data']['user'], $job['data']['ssh-privkey'], $job['data']['privkey-password']);
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
private function runLocalCommand(string $command): array
|
|
|
|
{
|
|
|
|
pcntl_signal(SIGCHLD, SIG_DFL);
|
|
|
|
$return['exitcode'] = NULL;
|
|
|
|
$return['output'] = NULL;
|
|
|
|
exec($command . ' 2>&1', $return['output'], $return['exitcode']);
|
|
|
|
pcntl_signal(SIGCHLD, SIG_IGN);
|
|
|
|
$return['output'] = implode("\n", $return['output']);
|
|
|
|
return $return;
|
|
|
|
}
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
private function runSshCommand(string $command, string $host, string $user, string $privkey, string $password): array
|
|
|
|
{
|
|
|
|
$ssh = new SSH2($host);
|
|
|
|
$key = null;
|
|
|
|
if(!empty($privkey)) {
|
|
|
|
if(!empty($password)) {
|
|
|
|
$key = PublicKeyLoader::load(base64_decode($privkey), $password);
|
|
|
|
} else {
|
|
|
|
$key = PublicKeyLoader::load(base64_decode($privkey));
|
|
|
|
}
|
|
|
|
} elseif (!empty($password)) {
|
|
|
|
$key = $privkey;
|
|
|
|
}
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
if (!$ssh->login($user, $key)) {
|
|
|
|
throw new \Exception('Login failed');
|
|
|
|
}
|
|
|
|
$return['output'] = $ssh->exec($command);
|
|
|
|
$return['exitcode'] = $ssh->getExitStatus();
|
|
|
|
return $return;
|
|
|
|
}
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
private function runRebootJob(array $job): array
|
|
|
|
{
|
|
|
|
if($job['running'] == 1) {
|
|
|
|
$job['data']['reboot-command'] = str_replace('{reboot-delay}', $job['data']['reboot-delay'], $job['data']['reboot-command']);
|
|
|
|
$job['data']['reboot-command'] = str_replace('{reboot-delay-secs}', $job['data']['reboot-delay-secs'], $job['data']['reboot-command']);
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
if (!empty($job['data']['vars'])) {
|
|
|
|
foreach ($job['data']['vars'] as $key => $var) {
|
|
|
|
$job['data']['reboot-command'] = str_replace('{' . $key . '}', $var['value'], $job['data']['reboot-command']);
|
2021-05-27 21:17:10 +02:00
|
|
|
}
|
2021-05-28 09:59:55 +02:00
|
|
|
}
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
$jobsSql = "UPDATE job SET running = :status WHERE id = :id";
|
|
|
|
$jobsStmt = $this->dbcon->prepare($jobsSql);
|
|
|
|
$jobsStmt->executeQuery([':id' => $job['id'], ':status' => time() + $job['data']['reboot-delay-secs'] + ($job['reboot-duration'] * 60)]);
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
if($job['data']['hosttype'] == 'ssh') {
|
|
|
|
$this->runSshCommand($job['data']['reboot-command'], $job['data']['host'], $job['data']['user'], $job['data']['ssh-privkey'], $job['data']['privkey-password']);
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
} elseif($job['data']['hosttype'] == 'local') {
|
|
|
|
$this->runLocalCommand($job['data']['reboot-command']);
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
|
|
|
|
} elseif($job['running'] != 0) {
|
|
|
|
if($job['running'] > time()) {
|
|
|
|
exit;
|
|
|
|
}
|
2021-05-27 21:17:10 +02:00
|
|
|
|
2021-05-28 09:59:55 +02:00
|
|
|
if (!empty($job['data']['vars'])) {
|
|
|
|
foreach ($job['data']['vars'] as $key => $var) {
|
|
|
|
$job['data']['getservices-command'] = str_replace('{' . $key . '}', $var['value'], $job['data']['getservices-command']);
|
2021-05-27 21:17:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-28 09:59:55 +02:00
|
|
|
if($job['data']['hosttype'] == 'ssh') {
|
|
|
|
return $this->runSshCommand($job['data']['getservices-command'], $job['data']['host'], $job['data']['user'], $job['data']['ssh-privkey'], $job['data']['privkey-password']);
|
|
|
|
} elseif($job['data']['hosttype'] == 'local') {
|
|
|
|
return $this->runLocalCommand($job['data']['getservices-command']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private function prepareDockerCommand(string $command, string $service, string|NULL $user): string
|
|
|
|
{
|
|
|
|
$prepend = 'docker exec ';
|
|
|
|
$prepend .= $service . ' ';
|
|
|
|
$prepend .= (!empty($user)) ? ' --user=' . $user . ' ' : '';
|
|
|
|
return $prepend . $command;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runJob(int $job): void
|
|
|
|
{
|
|
|
|
$job = $this->getJob($job, true);
|
|
|
|
if($job['data']['crontype'] == 'http') {
|
|
|
|
$result = $this->runHttpJob($job);
|
|
|
|
} elseif($job['data']['crontype'] == 'command') {
|
|
|
|
$result = $this->runCommandJob($job);
|
|
|
|
} elseif($job['data']['crontype'] == 'reboot') {
|
|
|
|
$result = $this->runRebootJob($job);
|
2021-05-24 18:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handling of response
|
2021-05-25 23:54:24 +02:00
|
|
|
$addRunSql = 'INSERT INTO run(job_id, exitcode, output, timestamp) VALUES (:job_id, :exitcode, :output, :timestamp)';
|
2021-05-24 18:36:16 +02:00
|
|
|
$addRunStmt = $this->dbcon->prepare($addRunSql);
|
2021-05-28 09:59:55 +02:00
|
|
|
$addRunStmt->executeQuery([':job_id' => $job['id'], ':exitcode' => $result['exitcode'], ':output' => $result['output'], ':timestamp' => time()]);
|
2021-05-24 18:36:16 +02:00
|
|
|
|
|
|
|
// setting nextrun to next run
|
|
|
|
$nextrun = $job['nextrun'];
|
|
|
|
do {
|
|
|
|
$nextrun = $nextrun + $job['interval'];
|
|
|
|
} while ($nextrun < time());
|
|
|
|
|
|
|
|
|
2021-05-25 23:54:24 +02:00
|
|
|
$addRunSql = 'UPDATE job SET nextrun = :nextrun WHERE id = :id';
|
2021-05-24 18:36:16 +02:00
|
|
|
$addRunStmt = $this->dbcon->prepare($addRunSql);
|
2021-05-25 23:54:24 +02:00
|
|
|
$addRunStmt->executeQuery([':id' => $job['id'], ':nextrun' => $nextrun]);
|
2021-05-24 18:36:16 +02:00
|
|
|
}
|
2021-04-13 14:07:11 +02:00
|
|
|
public function addJob(array $values)
|
|
|
|
{
|
2021-05-19 13:24:38 +02:00
|
|
|
if(empty($values['crontype']) ||
|
2021-04-13 14:07:11 +02:00
|
|
|
empty($values['name']) ||
|
2021-05-06 15:53:21 +02:00
|
|
|
empty($values['interval']) ||
|
2021-04-13 14:07:11 +02:00
|
|
|
empty($values['nextrun'])
|
|
|
|
) {
|
2021-05-21 13:09:48 +02:00
|
|
|
throw new \InvalidArgumentException('Some fields are empty');
|
2021-04-13 14:07:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:09:48 +02:00
|
|
|
$data = $this->prepareJob($values);
|
|
|
|
$data['data'] = json_encode($data['data']);
|
2021-05-24 18:36:16 +02:00
|
|
|
$addJobSql = "INSERT INTO job(name, data, interval, nextrun, lastrun, running) VALUES (:name, :data, :interval, :nextrun, :lastrun, :running)";
|
2021-05-21 13:09:48 +02:00
|
|
|
|
|
|
|
$addJobStmt = $this->dbcon->prepare($addJobSql);
|
2021-05-24 18:36:16 +02:00
|
|
|
$addJobStmt->executeQuery([':name' => $data['name'], ':data' => $data['data'], ':interval' => $data['interval'], ':nextrun' => $data['nextrun'], ':lastrun' => $data['lastrun'], ':running' => 0]);
|
2021-05-21 13:09:48 +02:00
|
|
|
|
|
|
|
return ['success' => true, 'message' => 'Cronjob succesfully added'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function editJob(int $id, array $values)
|
|
|
|
{
|
|
|
|
if(empty($values['crontype']) ||
|
|
|
|
empty($values['name']) ||
|
|
|
|
empty($values['interval']) ||
|
|
|
|
empty($values['nextrun'])
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException('Some fields are empty');
|
|
|
|
}
|
|
|
|
$data = $this->prepareJob($values);
|
|
|
|
$data['data'] = json_encode($data['data']);
|
|
|
|
$editJobSql = "UPDATE job set name = :name, data = :data, interval = :interval, nextrun = :nextrun, lastrun = :lastrun WHERE id = :id";
|
|
|
|
|
|
|
|
$editJobStmt = $this->dbcon->prepare($editJobSql);
|
|
|
|
$editJobStmt->executeQuery([':name' => $data['name'], ':data' => $data['data'], ':interval' => $data['interval'], ':nextrun' => $data['nextrun'], ':lastrun' => $data['lastrun'],':id' => $id ]);
|
|
|
|
|
|
|
|
return ['success' => true, 'message' => 'Cronjob succesfully edited'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function prepareJob(array $values): array
|
|
|
|
{
|
2021-05-26 13:34:19 +02:00
|
|
|
if(empty($values['lastrun']) || (isset($values['lastrun-eternal']) && $values['lastrun-eternal'] == 'true')) {
|
2021-04-13 14:07:11 +02:00
|
|
|
$values['lastrun'] = NULL;
|
|
|
|
} else {
|
2021-05-24 12:28:47 +02:00
|
|
|
$values['lastrun'] = DateTime::createFromFormat('d/m/Y H:i:s',$values['lastrun'])->getTimestamp();
|
2021-04-13 14:07:11 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 12:28:47 +02:00
|
|
|
$values['nextrun'] = DateTime::createFromFormat('d/m/Y H:i:s', $values['nextrun'])->getTimestamp();
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['crontype'] = $values['crontype'];
|
|
|
|
$values['data']['hosttype'] = $values['hosttype'];
|
|
|
|
$values['data']['containertype'] = $values['containertype'];
|
2021-04-13 14:07:11 +02:00
|
|
|
|
2021-05-24 12:28:47 +02:00
|
|
|
if(empty($values['data']['crontype'])) {
|
|
|
|
throw new \InvalidArgumentException("Crontype cannot be empty");
|
|
|
|
}
|
2021-05-21 13:09:48 +02:00
|
|
|
switch($values['data']['crontype'])
|
2021-04-13 14:07:11 +02:00
|
|
|
{
|
2021-05-19 13:24:38 +02:00
|
|
|
case 'command':
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['command'] = $values['command'];
|
|
|
|
$values['data']['response'] = $values['response'];
|
2021-04-15 13:52:27 +02:00
|
|
|
break;
|
2021-05-06 14:30:35 +02:00
|
|
|
case 'reboot':
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['reboot-command'] = $values['reboot-command'];
|
|
|
|
$values['data']['getservices-command'] = $values['getservices-command'];
|
|
|
|
$values['data']['reboot-duration'] = $values['reboot-duration'];
|
2021-05-06 14:30:35 +02:00
|
|
|
if(!empty($values['reboot-delay'])) {
|
|
|
|
$newsecretkey = count($values['var-value']);
|
|
|
|
$values['var-id'][$newsecretkey] = 'reboot-delay';
|
|
|
|
$values['var-issecret'][$newsecretkey] = false;
|
|
|
|
$values['var-value'][$newsecretkey] = (int)$values['reboot-delay'];
|
|
|
|
|
|
|
|
$newsecretkey = count($values['var-value']);
|
|
|
|
$values['var-id'][$newsecretkey] = 'reboot-delay-secs';
|
|
|
|
$values['var-issecret'][$newsecretkey] = false;
|
|
|
|
$values['var-value'][$newsecretkey] = (int)$values['reboot-delay'] * 60;
|
|
|
|
}
|
|
|
|
break;
|
2021-04-13 14:07:11 +02:00
|
|
|
case 'http':
|
|
|
|
$parsedUrl = parse_url($values['url']);
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['url'] = $values['url'];
|
|
|
|
$values['data']['response'] = $values['response'];
|
|
|
|
$values['data']['basicauth-username'] = $values['basicauth-username'];
|
2021-04-13 14:07:11 +02:00
|
|
|
if(empty($parsedUrl['host'])) {
|
2021-05-21 13:09:48 +02:00
|
|
|
throw new \InvalidArgumentException('Some data was invalid');
|
2021-04-13 14:07:11 +02:00
|
|
|
}
|
2021-05-06 13:30:12 +02:00
|
|
|
if(!empty($values['basicauth-password'])) {
|
|
|
|
$newsecretkey = count($values['var-value']);
|
|
|
|
$values['var-id'][$newsecretkey] = 'basicauth-password';
|
|
|
|
$values['var-issecret'][$newsecretkey] = true;
|
|
|
|
$values['var-value'][$newsecretkey] = $values['basicauth-password'];
|
|
|
|
}
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['host'] = $parsedUrl['host'];
|
2021-04-13 14:07:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:09:48 +02:00
|
|
|
switch($values['data']['hosttype']) {
|
2021-05-24 12:28:47 +02:00
|
|
|
default:
|
2021-05-25 19:44:57 +02:00
|
|
|
if($values['data']['crontype'] == 'http') break;
|
|
|
|
$values['data']['hosttype'] = 'local';
|
2021-05-19 13:24:38 +02:00
|
|
|
case 'local':
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['host'] = 'localhost';
|
2021-05-19 13:24:38 +02:00
|
|
|
break;
|
|
|
|
case 'ssh':
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['host'] = $values['host'];
|
|
|
|
$values['data']['user'] = $values['user'];
|
2021-05-19 13:24:38 +02:00
|
|
|
if(!empty($values['privkey-password'])) {
|
2021-05-21 13:09:48 +02:00
|
|
|
$newsecretkey = count($values['var-value']);
|
|
|
|
$values['var-id'][$newsecretkey] = 'privkey-password';
|
|
|
|
$values['var-issecret'][$newsecretkey] = true;
|
|
|
|
$values['var-value'][$newsecretkey] = $values['privkey-password'];
|
2021-05-19 13:24:38 +02:00
|
|
|
}
|
2021-05-24 12:28:47 +02:00
|
|
|
$privkeyid = NULL;
|
2021-05-19 13:24:38 +02:00
|
|
|
if(!empty($_FILES['privkey']['tmp_name'])) {
|
|
|
|
$newsecretkey = count($values['var-value']);
|
2021-05-24 12:28:47 +02:00
|
|
|
$privkeyid = $newsecretkey;
|
2021-05-19 13:24:38 +02:00
|
|
|
$values['var-id'][$newsecretkey] = 'ssh-privkey';
|
|
|
|
$values['var-issecret'][$newsecretkey] = true;
|
|
|
|
$values['var-value'][$newsecretkey] = base64_encode(file_get_contents($_FILES['privkey']['tmp_name']));
|
|
|
|
}
|
2021-05-24 12:28:47 +02:00
|
|
|
if($values['privkey-keep'] == true) {
|
|
|
|
$privkeyid = ($privkeyid === NULL) ? count($values['var-value']) : $privkeyid ;
|
|
|
|
$values['var-id'][$privkeyid] = 'ssh-privkey';
|
|
|
|
$values['var-issecret'][$privkeyid] = true;
|
|
|
|
$values['var-value'][$privkeyid] = $values['privkey-orig'];
|
|
|
|
|
|
|
|
}
|
2021-05-19 13:24:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-20 13:06:53 +02:00
|
|
|
|
2021-05-21 13:09:48 +02:00
|
|
|
switch($values['data']['containertype']) {
|
2021-05-24 12:28:47 +02:00
|
|
|
default:
|
2021-05-25 19:44:57 +02:00
|
|
|
if($values['data']['crontype'] == 'http') break;
|
|
|
|
$values['data']['containertype'] = 'none';
|
2021-05-24 12:28:47 +02:00
|
|
|
case 'none':
|
|
|
|
// No options for no container
|
|
|
|
break;
|
2021-05-20 13:06:53 +02:00
|
|
|
case 'docker':
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['service'] = $values['service'];
|
2021-05-24 12:28:47 +02:00
|
|
|
$values['data']['container-user'] = $values['container-user'];
|
2021-05-20 13:06:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:30:12 +02:00
|
|
|
if(!empty($values['var-value'])) {
|
|
|
|
foreach($values['var-value'] as $key => $name) {
|
|
|
|
if(!empty($name)) {
|
2021-05-06 14:30:35 +02:00
|
|
|
if(isset($values['var-issecret'][$key]) && $values['var-issecret'][$key] != false) {
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['vars'][$values['var-id'][$key]]['issecret'] = true;
|
|
|
|
$values['data']['vars'][$values['var-id'][$key]]['value'] = base64_encode(Secret::encrypt($values['var-value'][$key]));
|
2021-05-06 13:30:12 +02:00
|
|
|
} else {
|
2021-05-21 13:09:48 +02:00
|
|
|
$values['data']['vars'][$values['var-id'][$key]]['issecret'] = false;
|
|
|
|
$values['data']['vars'][$values['var-id'][$key]]['value'] = $values['var-value'][$key];
|
2021-05-06 13:30:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 14:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-21 13:09:48 +02:00
|
|
|
return $values;
|
2021-04-13 14:07:11 +02:00
|
|
|
}
|
2021-04-13 14:44:58 +02:00
|
|
|
|
|
|
|
public function getJob(int $id, bool $withSecrets = false) {
|
|
|
|
$jobSql = "SELECT * FROM job WHERE id = :id";
|
|
|
|
$jobStmt = $this->dbcon->prepare($jobSql);
|
|
|
|
$jobRslt = $jobStmt->execute([':id' => $id])->fetchAssociative();
|
|
|
|
|
|
|
|
$jobRslt['data'] = json_decode($jobRslt['data'], true);
|
2021-05-21 13:09:48 +02:00
|
|
|
|
2021-05-06 13:30:12 +02:00
|
|
|
if(!empty($jobRslt['data']['vars'])) {
|
|
|
|
foreach ($jobRslt['data']['vars'] as $key => &$value) {
|
|
|
|
if ($value['issecret']) {
|
|
|
|
$value['value'] = ($withSecrets) ? Secret::decrypt(base64_decode($value['value'])) : '';
|
|
|
|
}
|
2021-04-13 14:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:09:48 +02:00
|
|
|
switch($jobRslt['data']['crontype']) {
|
|
|
|
case 'http':
|
|
|
|
if(isset($jobRslt['data']['vars']['basicauth-password']['value'])) {
|
|
|
|
$jobRslt['data']['basicauth-password'] = $jobRslt['data']['vars']['basicauth-password']['value'];
|
|
|
|
unset($jobRslt['data']['vars']['basicauth-password']);
|
|
|
|
}
|
|
|
|
break;
|
2021-05-24 12:28:47 +02:00
|
|
|
case 'reboot':
|
|
|
|
$jobRslt['data']['reboot-delay'] = $jobRslt['data']['vars']['reboot-delay']['value'];
|
2021-05-27 21:17:10 +02:00
|
|
|
$jobRslt['data']['reboot-delay-secs'] = $jobRslt['data']['vars']['reboot-delay-secs']['value'];
|
2021-05-24 12:28:47 +02:00
|
|
|
unset($jobRslt['data']['vars']['reboot-delay']);
|
|
|
|
unset($jobRslt['data']['vars']['reboot-delay-secs']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($jobRslt['data']['hosttype']) {
|
|
|
|
case 'ssh':
|
|
|
|
if(isset($jobRslt['data']['vars']['ssh-privkey']['value'])) {
|
|
|
|
$jobRslt['data']['ssh-privkey'] = $jobRslt['data']['vars']['ssh-privkey']['value'];
|
|
|
|
unset($jobRslt['data']['vars']['ssh-privkey']);
|
|
|
|
}
|
|
|
|
if(isset($jobRslt['data']['vars']['privkey-password']['value'])) {
|
|
|
|
$jobRslt['data']['privkey-password'] = $jobRslt['data']['vars']['privkey-password']['value'];
|
|
|
|
unset($jobRslt['data']['vars']['privkey-password']);
|
|
|
|
}
|
|
|
|
break;
|
2021-05-21 13:09:48 +02:00
|
|
|
}
|
|
|
|
if($jobRslt['data']['crontype'] == 'http') {
|
|
|
|
}
|
2021-04-13 14:44:58 +02:00
|
|
|
return $jobRslt;
|
|
|
|
}
|
2021-05-24 14:08:30 +02:00
|
|
|
|
|
|
|
public function deleteJob(int $id)
|
|
|
|
{
|
|
|
|
$addJobSql = "DELETE FROM job WHERE id = :id";
|
|
|
|
|
|
|
|
$addJobStmt = $this->dbcon->prepare($addJobSql);
|
|
|
|
$addJobStmt->executeQuery([':id' => $id]);
|
|
|
|
|
|
|
|
return ['success' => true, 'message' => 'Cronjob succesfully deleted'];
|
|
|
|
}
|
2021-04-08 12:54:49 +02:00
|
|
|
}
|