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.

81 lines
2.4 KiB

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