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.

115 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'=>'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->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']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : 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. correct_photo_rotation($file['tmp_name']);
  44. $r = micropub_post_for_user($user, $_POST, $file);
  45. } else {
  46. $r = array('error' => $error);
  47. }
  48. } else {
  49. unset($_POST['null']);
  50. $r = micropub_post_for_user($user, $_POST);
  51. }
  52. // Populate the error if there was no location header.
  53. if(empty($r['location']) && empty($r['error'])) {
  54. $r['error'] = "No 'Location' header in response.";
  55. }
  56. $app->response()['Content-type'] = 'application/json';
  57. $app->response()->body(json_encode(array(
  58. 'response' => (isset($r['response']) ? htmlspecialchars($r['response']) : null),
  59. 'location' => (isset($r['location']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : null),
  60. 'error' => (isset($r['error']) ? $r['error'] : null),
  61. )));
  62. }
  63. });
  64. $app->post('/micropub/media', function() use($app) {
  65. if($user=require_login($app)) {
  66. $file = isset($_FILES['photo']) ? $_FILES['photo'] : null;
  67. $error = validate_photo($file);
  68. unset($_POST['null']);
  69. if(!$error) {
  70. correct_photo_rotation($file['tmp_name']);
  71. $r = micropub_media_post_for_user($user, $file);
  72. } else {
  73. $r = array('error' => $error);
  74. }
  75. if(empty($r['location']) && empty($r['error'])) {
  76. $r['error'] = "No 'Location' header in response.";
  77. }
  78. $app->response()['Content-type'] = 'application/json';
  79. $app->response()->body(json_encode(array(
  80. 'location' => (isset($r['location']) ? $r['location'] : null),
  81. 'error' => (isset($r['error']) ? $r['error'] : null),
  82. )));
  83. }
  84. });
  85. $app->post('/micropub/postjson', function() use($app) {
  86. if($user=require_login($app)) {
  87. $params = $app->request()->params();
  88. $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true);
  89. $app->response()['Content-type'] = 'application/json';
  90. $app->response()->body(json_encode([
  91. 'location' => (isset($r['location']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : null),
  92. 'error' => $r['error'],
  93. 'response' => $r['response']
  94. ]));
  95. }
  96. });