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.

117 lines
3.7 KiB

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