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.

79 lines
2.3 KiB

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