You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6 KiB

8 years ago
8 years ago
8 years ago
  1. <?php
  2. error_reporting(0);
  3. use \Psr\Http\Message\ServerRequestInterface as Request;
  4. use \Psr\Http\Message\ResponseInterface as Response;
  5. require '../vendor/autoload.php';
  6. $app = new \Slim\App;
  7. $container = $app->getContainer();
  8. $container['db'] = function ($c) {
  9. $db = $c['settings']['db'];
  10. $pdo = new PDO('sqlite:db.sqlite3');
  11. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  13. $pdo-> exec("CREATE TABLE IF NOT EXISTS `simulations` (
  14. `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  15. `timestamp` INTEGER,
  16. `status` VARCHAR NOT NULL DEFAULT 'scheduled',
  17. `token` VARCHAR NOT NULL,
  18. `username` VARCHAR NOT NULL,
  19. `topo_xml` TEXT NOT NULL DEFAULT '',
  20. `sessionname` VARCHAR,
  21. `repeat_interval` INTEGER,
  22. `repeat_count` INTEGER NOT NULL DEFAULT 0
  23. );");
  24. return $pdo;
  25. };
  26. $container['errorHandler'] = function ($container) {
  27. return function ($request, $response, $exception) use ($container) {
  28. return $container['response']->withStatus(500)
  29. ->withHeader('Content-Type', 'text/html')
  30. ->withJson(["statusText"=>$exception->getMessage()]);
  31. };
  32. };
  33. $app->get('/plannedsims/{id}', function (Request $request, Response $response) {
  34. $id = $request->getAttribute('id');
  35. $query = $this->db->prepare('SELECT * FROM simulations WHERE id = ? LIMIT 1;');
  36. $array = array($id);
  37. $query->execute($array);
  38. $result = $query->fetch();
  39. if ($result)
  40. return $response->withJson();
  41. });
  42. $app->delete('/plannedsims/{id}', function (Request $request, Response $response) {
  43. $id = $request->getAttribute('id');
  44. $query = $this->db->prepare('DELETE FROM simulations WHERE id = ?;');
  45. $array = array($id);
  46. $query->execute($array);
  47. return $response->withJson(array("statusText"=>"Successfully deleted simulation with id ".$id."."));
  48. });
  49. $app->get('/plannedsims', function (Request $request, Response $response ) {
  50. $data=$this->db->query("SELECT * FROM simulations;")->fetchAll();
  51. $newResponse = $response->withJson($data);
  52. return $newResponse;
  53. });
  54. $app->post('/plannedsims/new', function (Request $request, Response $response) {
  55. $params = $request->getQueryParams();
  56. $sessionName = $params["session"];
  57. $timestamp = $params["timestamp"];
  58. $token = $params["token"];
  59. $username = $params["username"];
  60. $repeat = $params["repeat"];
  61. switch ($repeat){
  62. case "daily":
  63. $repeat = strtotime('+1 day',0);
  64. break;
  65. case "weekly":
  66. $repeat = strtotime('1 week',0);
  67. break;
  68. default:
  69. $repeat = 0;
  70. break;
  71. }
  72. if (!is_numeric($repeat) && $repeat <=0)
  73. $repeat = null;
  74. $XML = $request->getBody()->getContents();
  75. if ($repeat != 0)
  76. $sth = $this->db->prepare('INSERT INTO simulations (timestamp, topo_xml,sessionname,token,username,repeat_interval,repeat_count,status) VALUES (?, ?, ?,?,?,?,0,"repeating");');
  77. else
  78. $sth = $this->db->prepare('INSERT INTO simulations (timestamp, topo_xml,sessionname,token,username,repeat_interval,repeat_count) VALUES (?, ?, ?,?,?,?,0);');
  79. $sth->execute([intval($timestamp),$XML,$sessionName,$token,$username,$repeat]);
  80. return $response->withJson(array("statusText"=>"Successfully added simulation startup."));
  81. });
  82. $app->run();