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.

151 lines
4.5 KiB

  1. <?php
  2. $app->get('/editor', function() use($app) {
  3. $user = require_login($app, false);
  4. $html = $app->render('editor.php', [
  5. 'user' => $user
  6. ]);
  7. $app->response()->body($html);
  8. });
  9. $app->post('/editor/publish', function() use($app) {
  10. if($user=require_login($app)) {
  11. $params = $app->request()->params();
  12. $content = $params['body'];
  13. // Clean up the HTML from the editor
  14. $content = sanitize_editor_html($content);
  15. if($user->micropub_optin_html_content) {
  16. $content = ['html' => $content];
  17. $micropub_request = array(
  18. 'name' => [$params['name']],
  19. 'description' => [$params['description']],
  20. 'image' => [$params['image']],
  21. 'content' => [$content]
  22. );
  23. $json = true;
  24. } else {
  25. $json = false;
  26. $micropub_request = array(
  27. 'h' => 'entry',
  28. 'name' => [$params['name']],
  29. 'description' => [$params['description']],
  30. 'image' => [$params['image']],
  31. 'content' => [$content]
  32. );
  33. }
  34. if(array_key_exists('category', $params) && $params['category'])
  35. $micropub_request['category'] = $params['category'];
  36. if(array_key_exists('slug', $params) && $params['slug'])
  37. $micropub_request[$user->micropub_slug_field] = $params['slug'];
  38. if(array_key_exists('status', $params) && $params['status']) {
  39. if($params['status'] == 'draft')
  40. $micropub_request['post-status'] = $params['status'];
  41. }
  42. if(array_key_exists('publish', $params) && $params['publish'] != 'now') {
  43. $micropub_request['published'] = $params['publish'];
  44. }
  45. if($json) {
  46. $micropub_request = [
  47. 'type' => ['h-entry'],
  48. 'properties' => $micropub_request
  49. ];
  50. // Convert all values to arrays
  51. foreach($micropub_request['properties'] as $k=>$v) {
  52. if(!is_array($v))
  53. $micropub_request['properties'][$k] = [$v];
  54. }
  55. }
  56. $r = micropub_post_for_user($user, $micropub_request, null, $json);
  57. $app->response()['Content-type'] = 'application/json';
  58. $app->response()->body(json_encode([
  59. 'location' => $r['location'],
  60. 'response' => trim(htmlspecialchars($r['response']))
  61. ]));
  62. }
  63. });
  64. $app->post('/editor/upload', function() use($app) {
  65. if($user=require_login($app)) {
  66. $fn = $_FILES['files']['tmp_name'][0];
  67. $imageURL = false;
  68. if($user->micropub_media_endpoint) {
  69. // If the user has a media endpoint, upload to that and return that URL
  70. correct_photo_rotation($fn);
  71. $r = micropub_media_post_for_user($user, $fn);
  72. if(!empty($r['location'])) {
  73. $imageURL = $r['location'];
  74. }
  75. }
  76. if(!$imageURL) {
  77. // Otherwise, fake a file uploader by echo'ing back the data URI
  78. $imageData = base64_encode(file_get_contents($fn));
  79. $imageURL = 'data:'.mime_content_type($fn).';base64,'.$imageData;
  80. }
  81. $app->response()['Content-type'] = 'application/json';
  82. $app->response()->body(json_encode([
  83. 'files' => [
  84. ['url'=>$imageURL]
  85. ]
  86. ]));
  87. }
  88. });
  89. $app->post('/editor/parse-date', function() use($app) {
  90. $date = false;
  91. $params = $app->request()->params();
  92. if(isset($params['date'])) {
  93. if($params['date'] == 'now') {
  94. $date = 'now';
  95. } else {
  96. try {
  97. // Check if the provided date has a timezone offset
  98. $has_timezone = preg_match('/[-+]\d\d:?\d\d$/', $params['date']);
  99. if(!$has_timezone && $params['tzoffset']) {
  100. $s = (-60) * $params['tzoffset'];
  101. $h = $params['tzoffset'] / (-60);
  102. $tz = new DateTimeZone($h);
  103. $d = new DateTime($params['date'], $tz);
  104. } else {
  105. $d = new DateTime($params['date']);
  106. }
  107. $date = $d->format('c');
  108. } catch(Exception $e) {
  109. }
  110. }
  111. }
  112. $app->response()['Content-type'] = 'application/json';
  113. $app->response()->body(json_encode(['date'=>$date]));
  114. });
  115. $app->post('/editor/delete-file', function() use($app) {
  116. $app->response()['Content-type'] = 'application/json';
  117. $app->response()->body(json_encode(['result'=>'deleted']));
  118. });
  119. $app->get('/editor/oembed', function() use($app) {
  120. $url = 'http://medium.iframe.ly/api/oembed?iframe=1&url='.urlencode($app->request()->params()['url']);
  121. $json = file_get_contents($url);
  122. $app->response()['Content-type'] = 'application/json';
  123. $app->response()->body($json);
  124. });
  125. $app->post('/editor/test-login', function() use($app) {
  126. $logged_in = array_key_exists('user_id', $_SESSION);
  127. $app->response()['Content-type'] = 'application/json';
  128. $app->response()->body(json_encode(['logged_in'=>$logged_in]));
  129. });