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.

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