runnow.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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(!isset($_GET['jobID'])) {
  27. header("location:/overview.php");
  28. exit;
  29. }
  30. $jobID = $_GET['jobID'];
  31. $jobnameqry = $db->prepare("SELECT * FROM jobs WHERE jobID = ?");
  32. $jobnameqry->execute(array($_GET['jobID']));
  33. $jobnameResult = $jobnameqry->fetchAll(PDO::FETCH_ASSOC);
  34. if ($jobnameResult[0]["user"] != $_SESSION["userID"]) {
  35. die(json_encode(array("error" => "You dirty hacker!")));
  36. }
  37. $nosave = false;
  38. if (filter_var($jobnameResult[0]["url"], FILTER_VALIDATE_URL)) {
  39. $client = new \GuzzleHttp\Client();
  40. $res = $client->request('GET', $jobnameResult[0]['url'], ['http_errors' => false]);
  41. $statuscode = $res->getStatusCode();
  42. $body = $res->getBody();
  43. $timestamp = time();
  44. } else {
  45. if(strpos($jobnameResult[0]["url"],"reboot") !== 0) {
  46. $body = '';
  47. $statuscode = 0;
  48. $url = "ssh " . $jobnameResult[0]['host'] . " '" . $jobnameResult[0]['url'] . "' 2>&1";
  49. exec($url, $body, $statuscode);
  50. $body = implode("\n", $body);
  51. $timestamp = time();
  52. } else {
  53. $rebootjobs = array();
  54. if (file_exists('cache/get-services.trigger')) {
  55. $rebootjobs = unserialize(file_get_contents('cache/get-services.trigger'));
  56. }
  57. if (!job_in_array($jobnameResult[0]['jobID'], $rebootjobs)) {
  58. $rebootjobs[] = $jobnameResult[0];
  59. touch("cache/reboot.trigger");
  60. $nosave = true;
  61. }
  62. }
  63. }
  64. if($nosave !== true) {
  65. $stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
  66. $stmt->execute(array($jobID, $statuscode, $body, $timestamp));
  67. }
  68. if(file_exists("cache/reboot.trigger")) {
  69. $rebootser = serialize($rebootjobs);
  70. file_put_contents("cache/get-services.trigger", $rebootser);
  71. echo json_encode(array("message" => "Reboot is scheduled. Programmer's fuel is awaiting"));
  72. } else {
  73. echo json_encode(array("message" => "Cronjob succesfully ran"));
  74. }
  75. require_once 'include/finalize.inc.php';