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.

185 lines
5.2 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. public $http;
  6. public function __construct() {
  7. $this->http = new Telegraph\HTTP();
  8. }
  9. private function _is_logged_in(Request $request, Response $response) {
  10. session_start();
  11. if(!session('user_id')) {
  12. session_destroy();
  13. $response->setStatusCode(302);
  14. $response->headers->set('Location', '/login?return_to='.$request->getRequestURI());
  15. return false;
  16. } else {
  17. return true;
  18. }
  19. }
  20. private function _get_role(Request $request) {
  21. // Default to load their first site, but let the query string override it
  22. $role = ORM::for_table('roles')->join('sites', 'roles.site_id = sites.id')
  23. ->where('user_id', session('user_id'))->order_by_asc('sites.created_at')->find_one();
  24. if($request->get('account')) {
  25. $role = ORM::for_table('roles')->where('user_id', session('user_id'))->where('site_id', $request->get('account'))->find_one();
  26. // Check that the user has permission to access this account
  27. if(!$role) {
  28. $response->setStatusCode(302);
  29. $response->headers->set('Location', '/dashboard');
  30. return false;
  31. }
  32. }
  33. return $role;
  34. }
  35. public function index(Request $request, Response $response) {
  36. $response->setContent(view('index', [
  37. 'title' => 'Telegraph'
  38. ]));
  39. return $response;
  40. }
  41. public function dashboard(Request $request, Response $response) {
  42. if(!$this->_is_logged_in($request, $response)) {
  43. return $response;
  44. }
  45. if(!$role=$this->_get_role($request, $response)) {
  46. return $response;
  47. }
  48. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  49. $query = ORM::for_table('webmentions')->where('site_id', $site->id)
  50. ->order_by_desc('created_at')
  51. ->limit(20)
  52. ->find_many();
  53. $webmentions = [];
  54. foreach($query as $m) {
  55. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $m->id)->order_by_desc('created_at')->find_many();
  56. if(count($statuses) == 0) {
  57. $icon = 'wait';
  58. $status = 'pending';
  59. } else {
  60. $status = $statuses[0]->status;
  61. switch($status) {
  62. case 'success':
  63. case 'accepted':
  64. $icon = 'checkmark';
  65. break;
  66. case 'not_supported':
  67. case 'error':
  68. $icon = 'warning';
  69. break;
  70. default:
  71. $icon = '';
  72. }
  73. }
  74. $webmentions[] = [
  75. 'webmention' => $m,
  76. 'statuses' => $statuses,
  77. 'status' => $status,
  78. 'icon' => $icon
  79. ];
  80. }
  81. $response->setContent(view('dashboard', [
  82. 'title' => 'Telegraph Dashboard',
  83. 'user' => $this->_user(),
  84. 'accounts' => $this->_accounts(),
  85. 'site' => $site,
  86. 'webmentions' => $webmentions
  87. ]));
  88. return $response;
  89. }
  90. public function webmention_details(Request $request, Response $response, $args) {
  91. if(!$this->_is_logged_in($request, $response)) {
  92. return $response;
  93. }
  94. // Look up the webmention by its token
  95. $webmention = ORM::for_table('webmentions')->where('token', $args['code'])->find_one();
  96. if(!$webmention) {
  97. $response->setContent(view('not-found'));
  98. return $response;
  99. }
  100. $site = ORM::for_table('sites')->where_id_is($webmention->site_id)->find_one();
  101. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $webmention->id)->order_by_desc('created_at')->find_many();
  102. $response->setContent(view('webmention-details', [
  103. 'title' => 'Webmention Details',
  104. 'user' => $this->_user(),
  105. 'accounts' => $this->_accounts(),
  106. 'site' => $site,
  107. 'webmention' => $webmention,
  108. 'statuses' => $statuses
  109. ]));
  110. return $response;
  111. }
  112. public function dashboard_send(Request $request, Response $response) {
  113. if(!$this->_is_logged_in($request, $response)) {
  114. return $response;
  115. }
  116. if(!$role=$this->_get_role($request, $response)) {
  117. return $response;
  118. }
  119. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  120. $response->setContent(view('webmention-send', [
  121. 'title' => 'Webmention Details',
  122. 'user' => $this->_user(),
  123. 'accounts' => $this->_accounts(),
  124. 'site' => $site,
  125. 'role' => $role,
  126. 'url' => $request->get('url')
  127. ]));
  128. return $response;
  129. }
  130. public function get_outgoing_links(Request $request, Response $response) {
  131. if(!$this->_is_logged_in($request, $response)) {
  132. return $response;
  133. }
  134. $sourceURL = $request->get('url');
  135. $client = new IndieWeb\MentionClient();
  136. $source = $this->http->get($sourceURL);
  137. $parsed = \Mf2\parse($source['body'], $sourceURL);
  138. $links = $client->findOutgoingLinks($parsed);
  139. $response->headers->set('Content-Type', 'application/json');
  140. $response->setContent(json_encode([
  141. 'links' => array_values($links)
  142. ]));
  143. return $response;
  144. }
  145. private function _user() {
  146. return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
  147. }
  148. private function _accounts() {
  149. return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
  150. ->where('roles.user_id', session('user_id'))
  151. ->find_many();
  152. }
  153. }