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.

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