summaryrefslogtreecommitdiff
path: root/controllers/editor.php
blob: 15d65ef3114e06e247e3f51913a596b7ae76eb4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

$app->get('/editor', function() use($app) {
  // Don't require login because appcache caches the whole page
  $html = $app->render('editor.php');
  $app->response()->body($html);
});

$app->post('/editor/publish', function() use($app) {

  if($user=require_login($app)) {
    $params = $app->request()->params();

    $micropub_request = array(
      'h' => 'entry',
      'name' => $params['name'],
      'content' => $params['body']
    );

    $r = micropub_post_for_user($user, $micropub_request);

    $app->response()['Content-type'] = 'application/json';
    $app->response()->body(json_encode([
      'location' => $r['location'],
      'response' => trim(htmlspecialchars($r['response']))
    ]));
  }
});

$app->post('/editor/upload', function() use($app) {
  // Fake a file uploader by echo'ing back the data URI
  $fn = $_FILES['files']['tmp_name'][0];
  $imageData = base64_encode(file_get_contents($fn));
  $src = 'data: '.mime_content_type($fn).';base64,'.$imageData;

  $app->response()['Content-type'] = 'application/json';
  $app->response()->body(json_encode([
    'files' => [
      [
        'url'=>$src
      ]
    ]
  ]));
});

$app->post('/editor/delete-file', function() use($app) {
  $app->response()['Content-type'] = 'application/json';
  $app->response()->body(json_encode(['result'=>'deleted']));
});

$app->get('/editor/oembed', function() use($app) {
  $url = 'http://medium.iframe.ly/api/oembed?iframe=1&url='.urlencode($app->request()->params()['url']);
  $json = file_get_contents($url);
  $app->response()['Content-type'] = 'application/json';
  $app->response()->body($json);  
});

$app->post('/editor/test-login', function() use($app) {
  $logged_in = array_key_exists('user_id', $_SESSION);
  $app->response()['Content-type'] = 'application/json';
  $app->response()->body(json_encode(['logged_in'=>$logged_in]));
});

// $app->get('/appcache.manifest', function() use($app) {
//   $content = partial('partials/appcache');

//   $app->response()['Content-type'] = 'text/cache-manifest';
//   $app->response()->body($content);
// });