addjob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2017 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. if ($_SERVER["REQUEST_METHOD"] == "GET") {
  27. $loader = new Twig_Loader_Filesystem('templates');
  28. $twig = new Twig_Environment($loader, array('cache' => 'cache', "debug" => true));
  29. $error = "";
  30. if (isset($_GET["error"])) {
  31. switch ($_GET["error"]) {
  32. case "emptyfields":
  33. $error = "Some fields were empty"; break;
  34. case "invalidurl":
  35. $error = "The URL is invalid"; break;
  36. case "invaliddelay":
  37. $error = "The delay is invalid"; break;
  38. }
  39. }
  40. $message = "";
  41. if (isset($_GET["message"])) {
  42. switch ($_GET["message"]) {
  43. case "added":
  44. $message = "The cronjob has been added"; break;
  45. }
  46. }
  47. echo $twig->render('addjob.html.twig', array("message" => $message, "error" => $error));
  48. }
  49. elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
  50. if (empty($_POST['name']) || empty($_POST['url'] || empty($_POST['delay']))) {
  51. header("location:addjob.php?error=emptyfields");
  52. exit;
  53. }
  54. $url = $_POST['url'];
  55. $host = $_POST['host'];
  56. $name = $_POST['name'];
  57. $delay = $_POST['delay'];
  58. $expected = $_POST['expected'];
  59. $eternal = (isset($_POST['eternal']) && $_POST['eternal'] == true) ? true : false;
  60. $nextrunObj = DateTime::createFromFormat("d/m/Y H:i:s", $_POST['nextrun']);
  61. $nextrun = $nextrunObj->getTimestamp();
  62. if (!$eternal) {
  63. $lastrunObj = DateTime::createFromFormat("d/m/Y H:i:s", $_POST['lastrun']);
  64. $lastrun = $lastrunObj->getTimestamp();
  65. } else {
  66. $lastrun = -1;
  67. }
  68. if(!is_numeric($delay)) {
  69. header("location:addjob.php?error=invaliddelay");
  70. exit;
  71. }
  72. if(!is_numeric($nextrun)) {
  73. header("location:addjob.php?error=invalidnextrun");
  74. exit;
  75. }
  76. if(!is_numeric($lastrun)) {
  77. header("location:addjob.php?error=invalidlastrun");
  78. exit;
  79. }
  80. $stmt = $db->prepare("INSERT INTO jobs(user, name, url, host, delay, nextrun, expected) VALUES(?, ?, ?, ?, ?, ?, ?)");
  81. $stmt->execute(array($_SESSION["userID"], $name, $url, $host, $delay, $nextrun, $expected));
  82. header("location:addjob.php?message=added");
  83. exit;
  84. }
  85. require_once 'include/finalize.inc.php';