webcron/src/Repository/Job.php

161 lines
6.4 KiB
PHP
Raw Normal View History

2021-04-08 12:54:49 +02:00
<?php
namespace JeroenED\Webcron\Repository;
2021-04-13 14:07:11 +02:00
use DateTime;
2021-04-08 12:54:49 +02:00
use Doctrine\DBAL\Connection;
class Job
{
private Connection $dbcon;
public function __construct(Connection $dbcon)
{
$this->dbcon = $dbcon;
}
public function getAllJobs()
{
$jobsSql = "SELECT * FROM job";
$jobsStmt = $this->dbcon->prepare($jobsSql);
$jobsRslt = $jobsStmt->execute();
$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
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'])
) {
return ['success' => false, 'message' => 'Some fields are empty'];
}
if(empty($values['lastrun'])) {
$values['lastrun'] = NULL;
} else {
$values['lastrun'] = DateTime::createFromFormat('m/d/Y g:i:s A',$values['lastrun'])->getTimestamp();
}
$values['nextrun'] = DateTime::createFromFormat('m/d/Y g:i:s A', $values['nextrun'])->getTimestamp();
2021-05-19 13:24:38 +02:00
$data['crontype'] = $values['crontype'];
$data['hosttype'] = $values['hosttype'];
2021-05-20 13:06:53 +02:00
$data['containertype'] = $values['containertype'];
2021-04-13 14:07:11 +02:00
2021-05-19 13:24:38 +02:00
switch($data['crontype'])
2021-04-13 14:07:11 +02:00
{
2021-05-19 13:24:38 +02:00
case 'command':
2021-04-15 13:52:27 +02:00
$data['command'] = $values['command'];
2021-05-19 20:09:09 +02:00
$data['response'] = $values['response'];
2021-04-15 13:52:27 +02:00
break;
2021-05-06 14:30:35 +02:00
case 'reboot':
$data['reboot-command'] = $values['reboot-command'];
$data['getservices-command'] = $values['getservices-command'];
$data['reboot-duration'] = $values['reboot-duration'];
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']);
$data['url'] = $values['url'];
2021-05-19 20:09:09 +02:00
$data['response'] = $values['response'];
2021-04-13 14:44:58 +02:00
$data['basicauth-username'] = $values['basicauth-username'];
2021-04-13 14:07:11 +02:00
if(empty($parsedUrl['host'])) {
return ['success' => false, 'message' => 'Some data was invalid'];
}
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-04-13 14:07:11 +02:00
$data['host'] = $parsedUrl['host'];
break;
}
2021-05-19 13:24:38 +02:00
switch($data['hosttype']) {
case 'local':
$data['host'] = 'localhost';
break;
case 'ssh':
$data['host'] = $values['host'];
$data['user'] = $values['user'];
if(!empty($values['privkey-password'])) {
$newsecretkey = count($values['var-value']);
$values['var-id'][$newsecretkey] = 'privkey-password';
$values['var-issecret'][$newsecretkey] = true;
$values['var-value'][$newsecretkey] = $values['privkey-password'];
}
if(!empty($_FILES['privkey']['tmp_name'])) {
$newsecretkey = count($values['var-value']);
$values['var-id'][$newsecretkey] = 'ssh-privkey';
$values['var-issecret'][$newsecretkey] = true;
$values['var-value'][$newsecretkey] = base64_encode(file_get_contents($_FILES['privkey']['tmp_name']));
}
break;
}
2021-05-20 13:06:53 +02:00
switch($data['containertype']) {
case 'docker':
$data['service'] = $values['service'];
$data['user'] = $values['user'];
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-06 13:30:12 +02:00
$data['vars'][$values['var-id'][$key]]['issecret'] = true;
$data['vars'][$values['var-id'][$key]]['value'] = base64_encode(Secret::encrypt($values['var-value'][$key]));
} else {
$data['vars'][$values['var-id'][$key]]['issecret'] = false;
$data['vars'][$values['var-id'][$key]]['value'] = $values['var-value'][$key];
}
}
2021-04-13 14:44:58 +02:00
}
}
2021-04-13 14:07:11 +02:00
$data = json_encode($data);
2021-05-06 15:53:21 +02:00
$addJobSql = "INSERT INTO job(name, data, interval, nextrun, lastrun) VALUES (:name, :data, :interval, :nextrun, :lastrun)";
2021-04-13 14:07:11 +02:00
$addJobStmt = $this->dbcon->prepare($addJobSql);
2021-05-19 13:24:38 +02:00
$addJobStmt->executeQuery([':name' => $values['name'], ':data' => $data, ':interval' => $values['interval'], ':nextrun' => $values['nextrun'], ':lastrun' => $values['lastrun'], ]);
2021-04-13 14:07:11 +02:00
return ['success' => true, 'message' => 'Cronjob succesfully added'];
}
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-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
}
}
return $jobRslt;
}
2021-04-08 12:54:49 +02:00
}