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.

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