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.

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