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.

260 lines
7.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. 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, Response $response) {
  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 api(Request $request, Response $response) {
  42. session_start();
  43. if(session('user_id')) {
  44. $role = $this->_get_role($request, $response);
  45. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  46. } else {
  47. $role = false;
  48. $site = false;
  49. }
  50. $response->setContent(view('api', [
  51. 'title' => 'Telegraph API Documentation',
  52. 'user' => $this->_user(),
  53. 'accounts' => $this->_accounts(),
  54. 'site' => $site,
  55. 'role' => $role,
  56. 'return_to' => $request->getRequestURI()
  57. ]));
  58. return $response;
  59. }
  60. private static function _icon_for_status($status) {
  61. switch($status) {
  62. case 'success':
  63. case 'accepted':
  64. return 'green checkmark';
  65. case 'not_supported':
  66. return 'yellow x';
  67. case 'error':
  68. return 'red x';
  69. case 'pending':
  70. return 'orange wait';
  71. default:
  72. return '';
  73. }
  74. }
  75. public function dashboard(Request $request, Response $response) {
  76. if(!$this->_is_logged_in($request, $response)) {
  77. return $response;
  78. }
  79. if(!$role=$this->_get_role($request, $response)) {
  80. return $response;
  81. }
  82. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  83. $query = ORM::for_table('webmentions')->where('site_id', $site->id)
  84. ->order_by_desc('created_at')
  85. ->limit(20)
  86. ->find_many();
  87. $webmentions = [];
  88. foreach($query as $m) {
  89. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $m->id)->order_by_desc('created_at')->find_many();
  90. if(count($statuses) == 0) {
  91. $status = 'pending';
  92. } else {
  93. $status = $statuses[0]->status;
  94. }
  95. $icon = self::_icon_for_status($status);
  96. $webmentions[] = [
  97. 'webmention' => $m,
  98. 'statuses' => $statuses,
  99. 'status' => $status,
  100. 'icon' => $icon
  101. ];
  102. }
  103. $response->setContent(view('dashboard', [
  104. 'title' => 'Telegraph Dashboard',
  105. 'user' => $this->_user(),
  106. 'accounts' => $this->_accounts(),
  107. 'site' => $site,
  108. 'role' => $role,
  109. 'webmentions' => $webmentions
  110. ]));
  111. return $response;
  112. }
  113. public function webmention_details(Request $request, Response $response, $args) {
  114. session_start();
  115. // Look up the webmention by its token
  116. $webmention = ORM::for_table('webmentions')->where('token', $args['code'])->find_one();
  117. if(!$webmention) {
  118. $response->setContent(view('not-found'));
  119. return $response;
  120. }
  121. $site = ORM::for_table('sites')->where_id_is($webmention->site_id)->find_one();
  122. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $webmention->id)->order_by_desc('created_at')->find_many();
  123. if(count($statuses) == 0) {
  124. $status = 'pending';
  125. } else {
  126. $status = $statuses[0]->status;
  127. }
  128. $icon = self::_icon_for_status($status);
  129. $response->setContent(view('webmention-details', [
  130. 'title' => 'Webmention Details',
  131. 'user' => $this->_user(),
  132. 'accounts' => $this->_accounts(),
  133. 'site' => $site,
  134. 'webmention' => $webmention,
  135. 'statuses' => $statuses,
  136. 'icon' => $icon,
  137. 'status' => $status
  138. ]));
  139. return $response;
  140. }
  141. public function dashboard_send(Request $request, Response $response) {
  142. if(!$this->_is_logged_in($request, $response)) {
  143. return $response;
  144. }
  145. if(!$role=$this->_get_role($request, $response)) {
  146. return $response;
  147. }
  148. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  149. $response->setContent(view('webmention-send', [
  150. 'title' => 'Webmention Details',
  151. 'user' => $this->_user(),
  152. 'accounts' => $this->_accounts(),
  153. 'site' => $site,
  154. 'role' => $role,
  155. 'url' => $request->get('url')
  156. ]));
  157. return $response;
  158. }
  159. public function get_outgoing_links(Request $request, Response $response) {
  160. if(!$this->_is_logged_in($request, $response)) {
  161. return $response;
  162. }
  163. $sourceURL = $request->get('url');
  164. $client = new IndieWeb\MentionClient();
  165. $source = $this->http->get($sourceURL);
  166. $parsed = \Mf2\parse($source['body'], $sourceURL);
  167. $links = array_values($client->findOutgoingLinks($parsed));
  168. $response->headers->set('Content-Type', 'application/json');
  169. $response->setContent(json_encode([
  170. 'links' => $links
  171. ]));
  172. return $response;
  173. }
  174. public function discover_endpoint(Request $request, Response $response) {
  175. if(!$this->_is_logged_in($request, $response)) {
  176. return $response;
  177. }
  178. $targetURL = $request->get('target');
  179. if(!Telegraph\Webmention::isProbablySupported($targetURL)) {
  180. $status = 'none';
  181. $cached = -1;
  182. } else {
  183. // Cache the discovered result
  184. $cacheKey = 'telegraph:discover_endpoint:'.$targetURL;
  185. if($request->get('ignore_cache') == 'true' || (!$status = redis()->get($cacheKey))) {
  186. $client = new IndieWeb\MentionClient();
  187. $endpoint = $client->discoverWebmentionEndpoint($targetURL);
  188. if($endpoint) {
  189. $status = 'webmention';
  190. } else {
  191. $endpoint = $client->discoverPingbackEndpoint($targetURL);
  192. if($endpoint) {
  193. $status = 'pingback';
  194. } else {
  195. $status = 'none';
  196. }
  197. }
  198. $cached = false;
  199. redis()->setex($cacheKey, 600, $status);
  200. } else {
  201. $cached = true;
  202. }
  203. }
  204. $response->headers->set('Content-Type', 'application/json');
  205. $response->setContent(json_encode([
  206. 'status' => $status,
  207. 'cached' => $cached
  208. ]));
  209. return $response;
  210. }
  211. private function _user() {
  212. if(!session('user_id')) return null;
  213. return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
  214. }
  215. private function _accounts() {
  216. return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
  217. ->where('roles.user_id', session('user_id'))
  218. ->find_many();
  219. }
  220. }