editjob.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $loader = new Twig_Loader_Filesystem('templates');
  42. $twig = new Twig_Environment($loader, array('cache' => 'cache', "debug" => true));
  43. $error = "";
  44. if (isset($_GET["error"])) {
  45. switch ($_GET["error"]) {
  46. case "emptyfields":
  47. $error = "Some fields were empty"; break;
  48. case "invalidurl":
  49. $error = "The URL is invalid"; break;
  50. case "invaliddelay":
  51. $error = "The delay is invalid"; break;
  52. }
  53. }
  54. echo $twig->render('editjob.html.twig', array("name" => $name, "url" => $url, "host" => $host, "delay" => $delay, "expected" => $expected, 'nextrun' => $nextrun, "jobID" => $jobID, "error" => $error));
  55. }
  56. elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
  57. if (empty($_POST['name']) || empty($_POST['url'] || empty($_POST['delay']))) {
  58. header("location:editjob.php?error=emptyfields");
  59. exit;
  60. }
  61. $url = $_POST['url'];
  62. $name = $_POST['name'];
  63. $delay = $_POST['delay'];
  64. $host = $_POST['host'];
  65. $expected = $_POST['expected'];
  66. $nextrunObj = DateTime::createFromFormat("d/m/Y H:i:s", $_POST['nextrun']);
  67. $nextrun = $nextrunObj->getTimestamp();
  68. if(!is_numeric($delay)) {
  69. header("location:editjob.php?jobID=" . $jobID . "&error=invaliddelay");
  70. exit;
  71. }
  72. if(!is_numeric($nextrun)) {
  73. header("location:editjob.php?jobID=" . $jobID . "&error=invalidnextrun");
  74. exit;
  75. }
  76. $stmt = $db->prepare("UPDATE jobs SET name = ?, url = ?, host = ?, delay = ?, nextrun = ?, expected = ? WHERE jobID = ?");
  77. $stmt->execute(array($name, $url, $host, $delay, $nextrun, $expected, $jobID));
  78. header("location:overview.php?message=edited");
  79. exit;
  80. }
  81. require_once 'include/finalize.inc.php';