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.

74 lines
2.3 KiB

9 years ago
9 years ago
9 years ago
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class Controller {
  5. private function _is_logged_in(&$request, &$response) {
  6. session_start();
  7. if(!session('user_id')) {
  8. session_destroy();
  9. $response->setStatusCode(302);
  10. $response->headers->set('Location', '/login?return_to='.$request->getPathInfo());
  11. return false;
  12. } else {
  13. return true;
  14. }
  15. }
  16. public function index(Request $request, Response $response) {
  17. $response->setContent(view('index', [
  18. 'title' => 'Telegraph'
  19. ]));
  20. return $response;
  21. }
  22. public function dashboard(Request $request, Response $response) {
  23. if(!$this->_is_logged_in($request, $response)) {
  24. return $response;
  25. }
  26. // Default to load their first site, but let the query string override it
  27. $role = ORM::for_table('roles')->join('sites', 'roles.site_id = sites.id')
  28. ->where('user_id', session('user_id'))->order_by_asc('sites.created_at')->find_one();
  29. if($request->get('account')) {
  30. $role = ORM::for_table('roles')->where('user_id', session('user_id'))->where('site_id', $request->get('account'))->find_one();
  31. // Check that the user has permission to access this account
  32. if(!$role) {
  33. $response->setStatusCode(302);
  34. $response->headers->set('Location', '/dashboard');
  35. return $response;
  36. }
  37. }
  38. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  39. $webmentions = [];
  40. foreach(ORM::for_table('webmentions')->where('site_id', $site->id)->find_many() as $m) {
  41. $webmentions[] = [
  42. 'webmention' => $m,
  43. 'statuses' => ORM::for_table('webmention_status')->where('webmention_id', $m->id)->order_by_desc('created_at')->find_many()
  44. ];
  45. }
  46. $response->setContent(view('dashboard', [
  47. 'title' => 'Telegraph Dashboard',
  48. 'user' => $this->_user(),
  49. 'accounts' => $this->_accounts(),
  50. 'site' => $site,
  51. 'webmentions' => $webmentions
  52. ]));
  53. return $response;
  54. }
  55. private function _user() {
  56. return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
  57. }
  58. private function _accounts() {
  59. return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
  60. ->where('roles.user_id', session('user_id'))
  61. ->find_many();
  62. }
  63. }