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.

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