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.

136 lines
3.8 KiB

  1. <?php
  2. function require_login(&$app) {
  3. if(!array_key_exists('user_id', $_SESSION)) {
  4. $app->redirect('/');
  5. return false;
  6. } else {
  7. return ORM::for_table('users')->find_one($_SESSION['user_id']);
  8. }
  9. }
  10. $app->get('/new', function() use($app) {
  11. if($user=require_login($app)) {
  12. $entry = false;
  13. $photo_url = false;
  14. $test_response = '';
  15. if($user->last_micropub_response) {
  16. try {
  17. if(@json_decode($user->last_micropub_response)) {
  18. $d = json_decode($user->last_micropub_response);
  19. $test_response = $d->response;
  20. }
  21. } catch(Exception $e) {
  22. }
  23. }
  24. $html = render('dashboard', array(
  25. 'title' => 'New Post',
  26. 'micropub_endpoint' => $user->micropub_endpoint,
  27. 'micropub_scope' => $user->micropub_scope,
  28. 'micropub_access_token' => $user->micropub_access_token,
  29. 'response_date' => $user->last_micropub_response_date,
  30. 'test_response' => $test_response,
  31. 'location_enabled' => $user->location_enabled
  32. ));
  33. $app->response()->body($html);
  34. }
  35. });
  36. $app->post('/prefs', function() use($app) {
  37. if($user=require_login($app)) {
  38. $params = $app->request()->params();
  39. $user->location_enabled = $params['enabled'];
  40. $user->save();
  41. }
  42. $app->response()->body(json_encode(array(
  43. 'result' => 'ok'
  44. )));
  45. });
  46. $app->get('/creating-a-token-endpoint', function() use($app) {
  47. $app->redirect('http://indiewebcamp.com/token-endpoint', 301);
  48. });
  49. $app->get('/creating-a-micropub-endpoint', function() use($app) {
  50. $html = render('creating-a-micropub-endpoint', array('title' => 'Creating a Micropub Endpoint'));
  51. $app->response()->body($html);
  52. });
  53. $app->get('/docs', function() use($app) {
  54. $html = render('docs', array('title' => 'Documentation'));
  55. $app->response()->body($html);
  56. });
  57. $app->get('/add-to-home', function() use($app) {
  58. $params = $app->request()->params();
  59. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  60. // Verify the token and sign the user in
  61. try {
  62. $data = JWT::decode($params['token'], Config::$jwtSecret);
  63. $_SESSION['user_id'] = $data->user_id;
  64. $_SESSION['me'] = $data->me;
  65. $app->redirect('/new', 301);
  66. } catch(DomainException $e) {
  67. header('X-Error: DomainException');
  68. $app->redirect('/', 301);
  69. } catch(UnexpectedValueException $e) {
  70. header('X-Error: UnexpectedValueException');
  71. $app->redirect('/', 301);
  72. }
  73. } else {
  74. if($user=require_login($app)) {
  75. if(array_key_exists('start', $params)) {
  76. $_SESSION['add-to-home-started'] = true;
  77. $token = JWT::encode(array(
  78. 'user_id' => $_SESSION['user_id'],
  79. 'me' => $_SESSION['me'],
  80. 'created_at' => time()
  81. ), Config::$jwtSecret);
  82. $app->redirect('/add-to-home?token='.$token, 301);
  83. } else {
  84. unset($_SESSION['add-to-home-started']);
  85. $html = render('add-to-home', array('title' => 'Quill'));
  86. $app->response()->body($html);
  87. }
  88. }
  89. }
  90. });
  91. $app->post('/micropub/post', function() use($app) {
  92. if($user=require_login($app)) {
  93. $params = $app->request()->params();
  94. // Now send to the micropub endpoint
  95. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  96. $response = $r['response'];
  97. $user->last_micropub_response = json_encode($r);
  98. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  99. // Check the response and look for a "Location" header containing the URL
  100. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  101. $location = $match[1];
  102. $user->micropub_success = 1;
  103. } else {
  104. $location = false;
  105. }
  106. $user->save();
  107. $app->response()->body(json_encode(array(
  108. 'response' => htmlspecialchars($response),
  109. 'location' => $location,
  110. 'error' => $r['error'],
  111. 'curlinfo' => $r['curlinfo']
  112. )));
  113. }
  114. });