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.

170 lines
4.7 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. 'token_scope' => $user->token_scope,
  49. 'access_token' => $user->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->post('/prefs', function() use($app) {
  59. if($user=require_login($app)) {
  60. $params = $app->request()->params();
  61. $user->location_enabled = $params['enabled'];
  62. $user->save();
  63. }
  64. $app->response()->body(json_encode(array(
  65. 'result' => 'ok'
  66. )));
  67. });
  68. $app->get('/creating-a-token-endpoint', function() use($app) {
  69. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  70. });
  71. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  72. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint'));
  73. $app->response()->body($html);
  74. });
  75. $app->get('/docs', function() use($app) {
  76. $html = render('docs', array('title' => 'Documentation'));
  77. $app->response()->body($html);
  78. });
  79. $app->get('/add-to-home', function() use($app) {
  80. $params = $app->request()->params();
  81. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  82. // Verify the token and sign the user in
  83. try {
  84. $data = JWT::decode($params['token'], Config::$jwtSecret);
  85. $_SESSION['user_id'] = $data->user_id;
  86. $_SESSION['me'] = $data->me;
  87. $app->redirect('/new', 301);
  88. } catch(DomainException $e) {
  89. header('X-Error: DomainException');
  90. $app->redirect('/', 301);
  91. } catch(UnexpectedValueException $e) {
  92. header('X-Error: UnexpectedValueException');
  93. $app->redirect('/', 301);
  94. }
  95. } else {
  96. if($user=require_login($app)) {
  97. if(array_key_exists('start', $params)) {
  98. $_SESSION['add-to-home-started'] = true;
  99. $token = JWT::encode(array(
  100. 'user_id' => $_SESSION['user_id'],
  101. 'me' => $_SESSION['me'],
  102. 'created_at' => time()
  103. ), Config::$jwtSecret);
  104. $app->redirect('/add-to-home?token='.$token, 301);
  105. } else {
  106. unset($_SESSION['add-to-home-started']);
  107. $html = render('add-to-home', array('title' => 'Teacup'));
  108. $app->response()->body($html);
  109. }
  110. }
  111. }
  112. });
  113. $app->post('/micropub/post', function() use($app) {
  114. if($user=require_login($app)) {
  115. $params = $app->request()->params();
  116. // Remove any blank params
  117. $params = array_filter($params, function($v){
  118. return $v !== '';
  119. });
  120. // Now send to the micropub endpoint
  121. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  122. $request = $r['request'];
  123. $response = $r['response'];
  124. $user->last_micropub_response = json_encode($r);
  125. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  126. // Check the response and look for a "Location" header containing the URL
  127. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  128. $location = $match[1];
  129. $user->micropub_success = 1;
  130. } else {
  131. $location = false;
  132. }
  133. $user->save();
  134. $app->response()->body(json_encode(array(
  135. 'request' => htmlspecialchars($request),
  136. 'response' => htmlspecialchars($response),
  137. 'location' => $location,
  138. 'error' => $r['error'],
  139. 'curlinfo' => $r['curlinfo']
  140. )));
  141. }
  142. });