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.

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