index.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require_once '../vendor/autoload.php';
  3. require_once __DIR__ . '/../src/controllers/user.php';
  4. require_once __DIR__ . '/../src/controllers/ssh.php';
  5. function get_view($view_name) {
  6. try {
  7. $loader = new Twig\Loader\FilesystemLoader('../templates');
  8. $twig = new Twig\Environment($loader);
  9. $twig->addGlobal('session', $_SESSION);
  10. return $twig->load($view_name . '.html.twig');
  11. } catch (Exception $e) {
  12. die ('ERROR: ' . $e->getMessage());
  13. }
  14. }
  15. $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
  16. $r->addRoute('GET', '/', 'signin');
  17. $r->addRoute('POST', '/auth', 'auth');
  18. $r->addRoute('GET', '/home', 'home');
  19. $r->addRoute('GET', '/changepassword', 'form_password');
  20. $r->addRoute('POST', '/changepassword', 'change_password');
  21. $r->addRoute('GET', '/signout', 'signout');
  22. $r->addRoute('GET', '/ssh', 'form_ssh');
  23. $r->addRoute('POST', '/addsshkey', 'add_ssh_key');
  24. $r->addRoute('POST', '/delsshkey', 'del_ssh_key');
  25. });
  26. // Fetch method and URI from somewhere
  27. $httpMethod = $_SERVER['REQUEST_METHOD'];
  28. $uri = $_SERVER['REQUEST_URI'];
  29. // Strip query string (?foo=bar) and decode URI
  30. if (false !== $pos = strpos($uri, '?')) {
  31. $uri = substr($uri, 0, $pos);
  32. }
  33. $uri = rawurldecode($uri);
  34. $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
  35. switch ($routeInfo[0]) {
  36. case FastRoute\Dispatcher::NOT_FOUND:
  37. // ... 404 Not Found
  38. break;
  39. case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
  40. $allowedMethods = $routeInfo[1];
  41. // ... 405 Method Not Allowed
  42. break;
  43. case FastRoute\Dispatcher::FOUND:
  44. $handler = $routeInfo[1];
  45. $handler();
  46. break;
  47. }