editjob.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2017-2018 Jeroen De Meerleer <me@jeroened.be>.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. require_once "include/initialize.inc.php";
  26. $jobID = $_GET['jobID'];
  27. if ($_SERVER["REQUEST_METHOD"] == "GET") {
  28. $jobnameqry = $db->prepare("SELECT * FROM jobs WHERE jobID = ?");
  29. $jobnameqry->execute(array($_GET['jobID']));
  30. $jobnameResult = $jobnameqry->fetchAll(PDO::FETCH_ASSOC);
  31. if ($jobnameResult[0]["user"] != $_SESSION["userID"]) {
  32. header("location:/overview.php");
  33. exit;
  34. }
  35. $name = $jobnameResult[0]['name'];
  36. $url = $jobnameResult[0]['url'];
  37. $host = $jobnameResult[0]['host'];
  38. $delay = $jobnameResult[0]['delay'];
  39. $expected = $jobnameResult[0]['expected'];
  40. $nextrun = date("d/m/Y H:i:s", $jobnameResult[0]['nextrun']);
  41. $lastrun = ($jobnameResult[0]['lastrun'] == -1) ? -1 : date("d/m/Y H:i:s", $jobnameResult[0]['lastrun']);
  42. $loader = new Twig_Loader_Filesystem('templates');
  43. $twig = new Twig_Environment($loader, array('cache' => 'cache', "debug" => true));
  44. $error = "";
  45. if (isset($_GET["error"])) {
  46. switch ($_GET["error"]) {
  47. case "emptyfields":
  48. $error = "Some fields were empty"; break;
  49. case "invalidurl":
  50. $error = "The URL is invalid"; break;
  51. case "invaliddelay":
  52. $error = "The delay is invalid"; break;
  53. }
  54. }
  55. echo $twig->render('editjob.html.twig', array("name" => $name, "url" => $url, "host" => $host, "delay" => $delay, "expected" => $expected, 'nextrun' => $nextrun, 'lastrun' => $lastrun, "jobID" => $jobID, "error" => $error));
  56. }
  57. elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
  58. if (empty($_POST['name']) || empty($_POST['url'] || empty($_POST['delay']))) {
  59. header("location:editjob.php?error=emptyfields");
  60. exit;
  61. }
  62. $url = $_POST['url'];
  63. $name = $_POST['name'];
  64. $delay = $_POST['delay'];
  65. $host = $_POST['host'];
  66. $expected = $_POST['expected'];
  67. $eternal = (isset($_POST['eternal']) && $_POST['eternal'] == true) ? true : false;
  68. $nextrunObj = DateTime::createFromFormat("d/m/Y H:i:s", $_POST['nextrun']);
  69. $nextrun = $nextrunObj->getTimestamp();
  70. if (!$eternal) {
  71. $lastrunObj = DateTime::createFromFormat("d/m/Y H:i:s", $_POST['lastrun']);
  72. $lastrun = $lastrunObj->getTimestamp();
  73. } else {
  74. $lastrun = -1;
  75. }
  76. if(!is_numeric($delay)) {
  77. header("location:editjob.php?jobID=" . $jobID . "&error=invaliddelay");
  78. exit;
  79. }
  80. if(!is_numeric($nextrun)) {
  81. header("location:editjob.php?jobID=" . $jobID . "&error=invalidnextrun");
  82. exit;
  83. }
  84. if(!is_numeric($lastrun)) {
  85. header("location:editjob.php?jobID=" . $jobID . "&error=invalidlastrun");
  86. exit;
  87. }
  88. $stmt = $db->prepare("UPDATE jobs SET name = ?, url = ?, host = ?, delay = ?, nextrun = ?, expected = ?, lastrun = ? WHERE jobID = ?");
  89. $stmt->execute(array($name, $url, $host, $delay, $nextrun, $expected, $lastrun, $jobID));
  90. header("location:overview.php?message=edited");
  91. exit;
  92. }
  93. require_once 'include/finalize.inc.php';