index.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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('GET', '/getsshkeys', 'get_ssh_keys');
  24. $r->addRoute('POST', '/addsshkey', 'add_ssh_key');
  25. $r->addRoute('POST', '/delsshkey', 'del_ssh_key');
  26. });
  27. // Fetch method and URI from somewhere
  28. $httpMethod = $_SERVER['REQUEST_METHOD'];
  29. $uri = $_SERVER['REQUEST_URI'];
  30. // Strip query string (?foo=bar) and decode URI
  31. if (false !== $pos = strpos($uri, '?')) {
  32. $uri = substr($uri, 0, $pos);
  33. }
  34. $uri = rawurldecode($uri);
  35. $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
  36. switch ($routeInfo[0]) {
  37. case FastRoute\Dispatcher::NOT_FOUND:
  38. // ... 404 Not Found
  39. break;
  40. case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
  41. $allowedMethods = $routeInfo[1];
  42. // ... 405 Method Not Allowed
  43. break;
  44. case FastRoute\Dispatcher::FOUND:
  45. $handler = $routeInfo[1];
  46. $handler();
  47. break;
  48. }