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.

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