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.

322 lines
9.1 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 new_site(Request $request, Response $response) {
  114. if(!$this->_is_logged_in($request, $response)) {
  115. return $response;
  116. }
  117. if(!$role=$this->_get_role($request, $response)) {
  118. return $response;
  119. }
  120. if($request->get('account')) {
  121. // permissions are checked already by _get_role
  122. $site = ORM::for_table('sites')->where_id_is($request->get('account'))->find_one();
  123. } else {
  124. $site = null;
  125. }
  126. $response->setContent(view('new-site', [
  127. 'title' => 'Create New Site',
  128. 'user' => $this->_user(),
  129. 'accounts' => $this->_accounts(),
  130. 'role' => $role,
  131. 'site' => $site
  132. ]));
  133. return $response;
  134. }
  135. public function save_site(Request $request, Response $response) {
  136. if(!$this->_is_logged_in($request, $response)) {
  137. return $response;
  138. }
  139. if(!$role=$this->_get_role($request, $response)) {
  140. return $response;
  141. }
  142. if($request->get('account')) {
  143. // permissions are checked already by _get_role
  144. $site = ORM::for_table('sites')->where_id_is($request->get('account'))->find_one();
  145. $site->name = $request->get('name');
  146. $site->url = $request->get('url');
  147. $site->save();
  148. } else {
  149. $site = ORM::for_table('sites')->create();
  150. $site->created_by = session('user_id');
  151. $site->created_at = date('Y-m-d H:i:s');
  152. $site->name = $request->get('name');
  153. $site->url = $request->get('url');
  154. $site->save();
  155. $role = ORM::for_table('roles')->create();
  156. $role->site_id = $site->id;
  157. $role->user_id = session('user_id');
  158. $role->role = 'owner';
  159. $role->token = random_string(32);
  160. $role->save();
  161. }
  162. $response->setStatusCode(302);
  163. $response->headers->set('Location', '/dashboard?account='.$site->id);
  164. return $response;
  165. }
  166. public function webmention_details(Request $request, Response $response, $args) {
  167. session_start();
  168. // Look up the webmention by its token
  169. $webmention = ORM::for_table('webmentions')->where('token', $args['code'])->find_one();
  170. if(!$webmention) {
  171. $response->setContent(view('not-found'));
  172. return $response;
  173. }
  174. $site = ORM::for_table('sites')->where_id_is($webmention->site_id)->find_one();
  175. $statuses = ORM::for_table('webmention_status')->where('webmention_id', $webmention->id)->order_by_desc('created_at')->find_many();
  176. if(count($statuses) == 0) {
  177. $status = 'pending';
  178. } else {
  179. $status = $statuses[0]->status;
  180. }
  181. $icon = self::_icon_for_status($status);
  182. $response->setContent(view('webmention-details', [
  183. 'title' => 'Webmention Details',
  184. 'user' => $this->_user(),
  185. 'accounts' => $this->_accounts(),
  186. 'site' => $site,
  187. 'webmention' => $webmention,
  188. 'statuses' => $statuses,
  189. 'icon' => $icon,
  190. 'status' => $status
  191. ]));
  192. return $response;
  193. }
  194. public function dashboard_send(Request $request, Response $response) {
  195. if(!$this->_is_logged_in($request, $response)) {
  196. return $response;
  197. }
  198. if(!$role=$this->_get_role($request, $response)) {
  199. return $response;
  200. }
  201. $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
  202. $response->setContent(view('webmention-send', [
  203. 'title' => 'Webmention Details',
  204. 'user' => $this->_user(),
  205. 'accounts' => $this->_accounts(),
  206. 'site' => $site,
  207. 'role' => $role,
  208. 'url' => $request->get('url')
  209. ]));
  210. return $response;
  211. }
  212. public function get_outgoing_links(Request $request, Response $response) {
  213. if(!$this->_is_logged_in($request, $response)) {
  214. return $response;
  215. }
  216. $sourceURL = $request->get('url');
  217. $client = new IndieWeb\MentionClient();
  218. $source = $this->http->get($sourceURL);
  219. $parsed = \Mf2\parse($source['body'], $sourceURL);
  220. $links = array_values($client->findOutgoingLinks($parsed));
  221. $response->headers->set('Content-Type', 'application/json');
  222. $response->setContent(json_encode([
  223. 'links' => $links
  224. ]));
  225. return $response;
  226. }
  227. public function discover_endpoint(Request $request, Response $response) {
  228. if(!$this->_is_logged_in($request, $response)) {
  229. return $response;
  230. }
  231. $targetURL = $request->get('target');
  232. if(!Telegraph\Webmention::isProbablySupported($targetURL)) {
  233. $status = 'none';
  234. $cached = -1;
  235. } else {
  236. // Cache the discovered result
  237. $cacheKey = 'telegraph:discover_endpoint:'.$targetURL;
  238. if($request->get('ignore_cache') == 'true' || (!$status = redis()->get($cacheKey))) {
  239. $client = new IndieWeb\MentionClient();
  240. $endpoint = $client->discoverWebmentionEndpoint($targetURL);
  241. if($endpoint) {
  242. $status = 'webmention';
  243. } else {
  244. $endpoint = $client->discoverPingbackEndpoint($targetURL);
  245. if($endpoint) {
  246. $status = 'pingback';
  247. } else {
  248. $status = 'none';
  249. }
  250. }
  251. $cached = false;
  252. redis()->setex($cacheKey, 600, $status);
  253. } else {
  254. $cached = true;
  255. }
  256. }
  257. $response->headers->set('Content-Type', 'application/json');
  258. $response->setContent(json_encode([
  259. 'status' => $status,
  260. 'cached' => $cached
  261. ]));
  262. return $response;
  263. }
  264. private function _user() {
  265. if(!session('user_id')) return null;
  266. return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
  267. }
  268. private function _accounts() {
  269. return ORM::for_table('sites')->join('roles', 'roles.site_id = sites.id')
  270. ->where('roles.user_id', session('user_id'))
  271. ->find_many();
  272. }
  273. }