Mirror of Quill
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.

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