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.

129 lines
3.8 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. $query = ORM::for_table('webmentions')->where('site_id', $site->id)
  40. ->order_by_desc('created_at')
  41. ->limit(20)
  42. ->find_many();
  43. $webmentions = [];
  44. foreach($query as $m) {
  45. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $m->id)->order_by_desc('created_at')->find_many();
  46. if(count($statuses) == 0) {
  47. $icon = 'wait';
  48. $status = 'pending';
  49. } else {
  50. $status = $statuses[0]->status;
  51. switch($status) {
  52. case 'success':
  53. case 'accepted':
  54. $icon = 'checkmark';
  55. break;
  56. case 'not_supported':
  57. case 'error':
  58. $icon = 'warning';
  59. break;
  60. default:
  61. $icon = '';
  62. }
  63. }
  64. $webmentions[] = [
  65. 'webmention' => $m,
  66. 'statuses' => $statuses,
  67. 'status' => $status,
  68. 'icon' => $icon
  69. ];
  70. }
  71. $response->setContent(view('dashboard', [
  72. 'title' => 'Telegraph Dashboard',
  73. 'user' => $this->_user(),
  74. 'accounts' => $this->_accounts(),
  75. 'site' => $site,
  76. 'webmentions' => $webmentions
  77. ]));
  78. return $response;
  79. }
  80. public function webmention_details(Request $request, Response $response, $args) {
  81. if(!$this->_is_logged_in($request, $response)) {
  82. return $response;
  83. }
  84. // Look up the webmention by its token
  85. $webmention = ORM::for_table('webmentions')->where('token', $args['code'])->find_one();
  86. if(!$webmention) {
  87. $response->setContent(view('not-found'));
  88. return $response;
  89. }
  90. $site = ORM::for_table('sites')->where_id_is($webmention->site_id)->find_one();
  91. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $webmention->id)->order_by_desc('created_at')->find_many();
  92. $response->setContent(view('webmention-details', [
  93. 'title' => 'Webmention Details',
  94. 'user' => $this->_user(),
  95. 'accounts' => $this->_accounts(),
  96. 'site' => $site,
  97. 'webmention' => $webmention,
  98. 'statuses' => $statuses
  99. ]));
  100. return $response;
  101. }
  102. private function _user() {
  103. return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
  104. }
  105. private function _accounts() {
  106. return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
  107. ->where('roles.user_id', session('user_id'))
  108. ->find_many();
  109. }
  110. }