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.

126 lines
4.0 KiB

  1. <?php
  2. $app->get('/micropub/syndications', function() use($app) {
  3. if($user=require_login($app)) {
  4. $data = get_micropub_config($user, ['q'=>'config']);
  5. $app->response()['Content-type'] = 'application/json';
  6. $app->response()->body(json_encode(array(
  7. 'targets' => $data['targets'],
  8. 'response' => $data['response']
  9. )));
  10. }
  11. });
  12. $app->get('/micropub/channels', function() use($app) {
  13. if($user=require_login($app)) {
  14. $data = get_micropub_config($user, ['q'=>'config']);
  15. $app->response()['Content-type'] = 'application/json';
  16. $app->response()->body(json_encode(array(
  17. 'channels' => $data['channels'],
  18. 'response' => $data['response']
  19. )));
  20. }
  21. });
  22. $app->post('/micropub/post', function() use($app) {
  23. if($user=require_login($app)) {
  24. $params = $app->request()->params();
  25. // Remove any blank params
  26. $params = array_filter($params, function($v){
  27. return $v !== '';
  28. });
  29. $r = micropub_post_for_user($user, $params);
  30. $app->response()['Content-type'] = 'application/json';
  31. $app->response()->body(json_encode(array(
  32. 'request' => htmlspecialchars($r['request']),
  33. 'response' => htmlspecialchars($r['response']),
  34. 'location' => (isset($r['location']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : null),
  35. 'error' => $r['error'],
  36. 'curlinfo' => $r['curlinfo']
  37. )));
  38. }
  39. });
  40. $app->post('/micropub/multipart', function() use($app) {
  41. if($user=require_login($app)) {
  42. // var_dump($app->request()->post());
  43. //
  44. // Since $app->request()->post() with multipart is always
  45. // empty (bug in Slim?) We're using the raw $_POST here.
  46. // PHP empties everything in $_POST if the file upload size exceeds
  47. // that is why we have to test if the variables exist first.
  48. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  49. if($file) {
  50. $error = validate_photo($file);
  51. unset($_POST['null']);
  52. if(!$error) {
  53. correct_photo_rotation($file['tmp_name']);
  54. $r = micropub_post_for_user($user, $_POST, $file);
  55. } else {
  56. $r = array('error' => $error);
  57. }
  58. } else {
  59. unset($_POST['null']);
  60. $r = micropub_post_for_user($user, $_POST);
  61. }
  62. // Populate the error if there was no location header.
  63. if(empty($r['location']) && empty($r['error'])) {
  64. $r['error'] = "No 'Location' header in response.";
  65. }
  66. $app->response()['Content-type'] = 'application/json';
  67. $app->response()->body(json_encode(array(
  68. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  69. 'location' => (isset($r['location']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : null),
  70. 'error' => (isset($r['error']) ? $r['error'] : null),
  71. )));
  72. }
  73. });
  74. $app->post('/micropub/media', function() use($app) {
  75. if($user=require_login($app)) {
  76. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  77. $error = validate_photo($file);
  78. unset($_POST['null']);
  79. if(!$error) {
  80. correct_photo_rotation($file['tmp_name']);
  81. $r = micropub_media_post_for_user($user, $file);
  82. } else {
  83. $r = array('error' => $error);
  84. }
  85. if(empty($r['location']) && empty($r['error'])) {
  86. $r['error'] = "No 'Location' header in response.";
  87. }
  88. $app->response()['Content-type'] = 'application/json';
  89. $app->response()->body(json_encode(array(
  90. 'location' => (isset($r['location']) ? $r['location'] : null),
  91. 'error' => (isset($r['error']) ? $r['error'] : null),
  92. )));
  93. }
  94. });
  95. $app->post('/micropub/postjson', function() use($app) {
  96. if($user=require_login($app)) {
  97. $params = $app->request()->params();
  98. $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true);
  99. $app->response()['Content-type'] = 'application/json';
  100. $app->response()->body(json_encode([
  101. 'location' => (isset($r['location']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : null),
  102. 'error' => $r['error'],
  103. 'response' => $r['response']
  104. ]));
  105. }
  106. });