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.

95 lines
2.6 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->post('/micropub/post', function() use($app) {
  58. if($user=require_login($app)) {
  59. $params = $app->request()->params();
  60. // Now send to the micropub endpoint
  61. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  62. $response = $r['response'];
  63. $user->last_micropub_response = json_encode($r);
  64. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  65. // Check the response and look for a "Location" header containing the URL
  66. if($response && preg_match('/Location: (.+)/', $response, $match)) {
  67. $location = $match[1];
  68. $user->micropub_success = 1;
  69. } else {
  70. $location = false;
  71. }
  72. $user->save();
  73. $app->response()->body(json_encode(array(
  74. 'response' => htmlspecialchars($response),
  75. 'location' => $location,
  76. 'error' => $r['error'],
  77. 'curlinfo' => $r['curlinfo']
  78. )));
  79. }
  80. });