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.

209 lines
5.8 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('/add-to-home', function() use($app) {
  105. $params = $app->request()->params();
  106. if(array_key_exists('token', $params) && !session('add-to-home-started')) {
  107. // Verify the token and sign the user in
  108. try {
  109. $data = JWT::decode($params['token'], Config::$jwtSecret);
  110. $_SESSION['user_id'] = $data->user_id;
  111. $_SESSION['me'] = $data->me;
  112. $app->redirect('/new', 301);
  113. } catch(DomainException $e) {
  114. header('X-Error: DomainException');
  115. $app->redirect('/', 301);
  116. } catch(UnexpectedValueException $e) {
  117. header('X-Error: UnexpectedValueException');
  118. $app->redirect('/', 301);
  119. }
  120. } else {
  121. if($user=require_login($app)) {
  122. if(array_key_exists('start', $params)) {
  123. $_SESSION['add-to-home-started'] = true;
  124. $token = JWT::encode(array(
  125. 'user_id' => $_SESSION['user_id'],
  126. 'me' => $_SESSION['me'],
  127. 'created_at' => time()
  128. ), Config::$jwtSecret);
  129. $app->redirect('/add-to-home?token='.$token, 301);
  130. } else {
  131. unset($_SESSION['add-to-home-started']);
  132. $html = render('add-to-home', array('title' => 'Quill'));
  133. $app->response()->body($html);
  134. }
  135. }
  136. }
  137. });
  138. $app->get('/micropub/syndications', function() use($app) {
  139. if($user=require_login($app)) {
  140. $data = get_syndication_targets($user);
  141. $app->response()->body(json_encode(array(
  142. 'targets' => $data['targets'],
  143. 'response' => $data['response']
  144. )));
  145. }
  146. });
  147. $app->post('/micropub/post', function() use($app) {
  148. if($user=require_login($app)) {
  149. $params = $app->request()->params();
  150. // Remove any blank params
  151. $params = array_filter($params, function($v){
  152. return $v !== '';
  153. });
  154. // Now send to the micropub endpoint
  155. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  156. $request = $r['request'];
  157. $response = $r['response'];
  158. $user->last_micropub_response = json_encode($r);
  159. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  160. // Check the response and look for a "Location" header containing the URL
  161. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  162. $location = $match[1];
  163. $user->micropub_success = 1;
  164. } else {
  165. $location = false;
  166. }
  167. $user->save();
  168. $app->response()->body(json_encode(array(
  169. 'request' => htmlspecialchars($request),
  170. 'response' => htmlspecialchars($response),
  171. 'location' => $location,
  172. 'error' => $r['error'],
  173. 'curlinfo' => $r['curlinfo']
  174. )));
  175. }
  176. });