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.

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