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.

91 lines
2.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. if($user->micropub_optin_html_content) {
  14. $content = ['html' => $params['body']];
  15. }
  16. $micropub_request = array(
  17. 'h' => 'entry',
  18. 'name' => $params['name'],
  19. 'content' => $content
  20. );
  21. if(array_key_exists('category', $params) && $params['category'])
  22. $micropub_request['category'] = $params['category'];
  23. if(array_key_exists('slug', $params) && $params['slug'])
  24. $micropub_request[$user->micropub_slug_field] = $params['slug'];
  25. if(array_key_exists('status', $params) && $params['status']) {
  26. if($params['status'] == 'draft')
  27. $micropub_request['post-status'] = $params['status'];
  28. }
  29. $r = micropub_post_for_user($user, $micropub_request);
  30. $app->response()['Content-type'] = 'application/json';
  31. $app->response()->body(json_encode([
  32. 'location' => $r['location'],
  33. 'response' => trim(htmlspecialchars($r['response']))
  34. ]));
  35. }
  36. });
  37. $app->post('/editor/upload', function() use($app) {
  38. if($user=require_login($app)) {
  39. $fn = $_FILES['files']['tmp_name'][0];
  40. $imageURL = false;
  41. if($user->micropub_media_endpoint) {
  42. // If the user has a media endpoint, upload to that and return that URL
  43. correct_photo_rotation($fn);
  44. $r = micropub_media_post_for_user($user, $fn);
  45. if(!empty($r['location'])) {
  46. $imageURL = $r['location'];
  47. }
  48. }
  49. if(!$imageURL) {
  50. // Otherwise, fake a file uploader by echo'ing back the data URI
  51. $imageData = base64_encode(file_get_contents($fn));
  52. $imageURL = 'data:'.mime_content_type($fn).';base64,'.$imageData;
  53. }
  54. $app->response()['Content-type'] = 'application/json';
  55. $app->response()->body(json_encode([
  56. 'files' => [
  57. ['url'=>$imageURL]
  58. ]
  59. ]));
  60. }
  61. });
  62. $app->post('/editor/delete-file', function() use($app) {
  63. $app->response()['Content-type'] = 'application/json';
  64. $app->response()->body(json_encode(['result'=>'deleted']));
  65. });
  66. $app->get('/editor/oembed', function() use($app) {
  67. $url = 'http://medium.iframe.ly/api/oembed?iframe=1&url='.urlencode($app->request()->params()['url']);
  68. $json = file_get_contents($url);
  69. $app->response()['Content-type'] = 'application/json';
  70. $app->response()->body($json);
  71. });
  72. $app->post('/editor/test-login', function() use($app) {
  73. $logged_in = array_key_exists('user_id', $_SESSION);
  74. $app->response()['Content-type'] = 'application/json';
  75. $app->response()->body(json_encode(['logged_in'=>$logged_in]));
  76. });