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.

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