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.

214 lines
6.0 KiB

  1. <?php
  2. function require_login(&$app) {
  3. $params = $app->request()->params();
  4. if(array_key_exists('token', $params)) {
  5. try {
  6. $data = JWT::decode($params['token'], Config::$jwtSecret);
  7. $_SESSION['user_id'] = $data->user_id;
  8. $_SESSION['me'] = $data->me;
  9. } catch(DomainException $e) {
  10. header('X-Error: DomainException');
  11. $app->redirect('/', 301);
  12. } catch(UnexpectedValueException $e) {
  13. header('X-Error: UnexpectedValueException');
  14. $app->redirect('/', 301);
  15. }
  16. }
  17. if(!array_key_exists('user_id', $_SESSION)) {
  18. $app->redirect('/');
  19. return false;
  20. } else {
  21. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  22. }
  23. }
  24. function generate_login_token() {
  25. return JWT::encode(array(
  26. 'user_id' => $_SESSION['user_id'],
  27. 'me' => $_SESSION['me'],
  28. 'created_at' => time()
  29. ), Config::$jwtSecret);
  30. }
  31. $app->get('/new', function() use($app) {
  32. if($user=require_login($app)) {
  33. $entry = false;
  34. $photo_url = false;
  35. $test_response = '';
  36. if($user->last_micropub_response) {
  37. try {
  38. if(@json_decode($user->last_micropub_response)) {
  39. $d = json_decode($user->last_micropub_response);
  40. $test_response = $d->response;
  41. }
  42. } catch(Exception $e) {
  43. }
  44. }
  45. $html = render('new-post', array(
  46. 'title' => 'New Post',
  47. 'micropub_endpoint' => $user->micropub_endpoint,
  48. 'micropub_scope' => $user->micropub_scope,
  49. 'micropub_access_token' => $user->micropub_access_token,
  50. 'response_date' => $user->last_micropub_response_date,
  51. 'syndication_targets' => json_decode($user->syndication_targets, true),
  52. 'test_response' => $test_response,
  53. 'location_enabled' => $user->location_enabled
  54. ));
  55. $app->response()->body($html);
  56. }
  57. });
  58. $app->get('/bookmark', function() use($app) {
  59. if($user=require_login($app)) {
  60. $params = $app->request()->params();
  61. $url = '';
  62. $name = '';
  63. $content = '';
  64. $tags = '';
  65. if(array_key_exists('url', $params))
  66. $url = $params['url'];
  67. if(array_key_exists('name', $params))
  68. $name = $params['name'];
  69. if(array_key_exists('content', $params))
  70. $content = $params['content'];
  71. $html = render('new-bookmark', array(
  72. 'title' => 'New Bookmark',
  73. 'bookmark_url' => $url,
  74. 'bookmark_name' => $name,
  75. 'bookmark_content' => $content,
  76. 'bookmark_tags' => $tags,
  77. 'token' => generate_login_token(),
  78. 'syndication_targets' => json_decode($user->syndication_targets, true)
  79. ));
  80. $app->response()->body($html);
  81. }
  82. });
  83. $app->post('/prefs', function() use($app) {
  84. if($user=require_login($app)) {
  85. $params = $app->request()->params();
  86. $user->location_enabled = $params['enabled'];
  87. $user->save();
  88. }
  89. $app->response()->body(json_encode(array(
  90. 'result' => 'ok'
  91. )));
  92. });
  93. $app->get('/creating-a-token-endpoint', function() use($app) {
  94. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  95. });
  96. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  97. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint'));
  98. $app->response()->body($html);
  99. });
  100. $app->get('/docs', function() use($app) {
  101. $html = render('docs', array('title' => 'Documentation'));
  102. $app->response()->body($html);
  103. });
  104. $app->get('/privacy', function() use($app) {
  105. $html = render('privacy', array('title' => 'Quill Privacy Policy'));
  106. $app->response()->body($html);
  107. });
  108. $app->get('/add-to-home', function() use($app) {
  109. $params = $app->request()->params();
  110. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  111. // Verify the token and sign the user in
  112. try {
  113. $data = JWT::decode($params['token'], Config::$jwtSecret);
  114. $_SESSION['user_id'] = $data->user_id;
  115. $_SESSION['me'] = $data->me;
  116. $app->redirect('/new', 301);
  117. } catch(DomainException $e) {
  118. header('X-Error: DomainException');
  119. $app->redirect('/', 301);
  120. } catch(UnexpectedValueException $e) {
  121. header('X-Error: UnexpectedValueException');
  122. $app->redirect('/', 301);
  123. }
  124. } else {
  125. if($user=require_login($app)) {
  126. if(array_key_exists('start', $params)) {
  127. $_SESSION['add-to-home-started'] = true;
  128. $token = JWT::encode(array(
  129. 'user_id' => $_SESSION['user_id'],
  130. 'me' => $_SESSION['me'],
  131. 'created_at' => time()
  132. ), Config::$jwtSecret);
  133. $app->redirect('/add-to-home?token='.$token, 301);
  134. } else {
  135. unset($_SESSION['add-to-home-started']);
  136. $html = render('add-to-home', array('title' => 'Quill'));
  137. $app->response()->body($html);
  138. }
  139. }
  140. }
  141. });
  142. $app->get('/micropub/syndications', function() use($app) {
  143. if($user=require_login($app)) {
  144. $data = get_syndication_targets($user);
  145. $app->response()->body(json_encode(array(
  146. 'targets' => $data['targets'],
  147. 'response' => $data['response']
  148. )));
  149. }
  150. });
  151. $app->post('/micropub/post', function() use($app) {
  152. if($user=require_login($app)) {
  153. $params = $app->request()->params();
  154. // Remove any blank params
  155. $params = array_filter($params, function($v){
  156. return $v !== '';
  157. });
  158. // Now send to the micropub endpoint
  159. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  160. $request = $r['request'];
  161. $response = $r['response'];
  162. $user->last_micropub_response = json_encode($r);
  163. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  164. // Check the response and look for a "Location" header containing the URL
  165. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  166. $location = $match[1];
  167. $user->micropub_success = 1;
  168. } else {
  169. $location = false;
  170. }
  171. $user->save();
  172. $app->response()->body(json_encode(array(
  173. 'request' => htmlspecialchars($request),
  174. 'response' => htmlspecialchars($response),
  175. 'location' => $location,
  176. 'error' => $r['error'],
  177. 'curlinfo' => $r['curlinfo']
  178. )));
  179. }
  180. });