-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.php
117 lines (104 loc) · 3.59 KB
/
json.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
try {
if (!$user->isLoggedIn()) {
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
if (!$session->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
throw new Exception;
}
} else {
throw new Exception;
}
}
} catch (Exception $e) {
header('WWW-Authenticate: Basic realm="Happy-CSS"');
http_response_code(401);
die;
}
$method = $_SERVER['REQUEST_METHOD'];
//$json = ['message'=>'NONE'];
//$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));
switch ($method) {
case 'POST':
$pageFields = json_decode(file_get_contents('php://input'));
// if page exists:
// http_response_code(409); - conflict
// create new page, if not already there
//$p = new Page();
// content-text
// content-code
// content-cta
// content-media
// content-preview
// $p->template = 'content-';
// $p->parent = $page;
// $p->score_name = $scoreName;
// $p->score = $score;
//$p->of(false); // turns off output formatting
//$p->title = 'score';
//$p->save();
http_response_code(201);
$json = ['id'=>$id];
break;
case 'PUT':
if($user->hasPermission('page-edit', $page)) {
$pageFields = json_decode(file_get_contents('php://input'));
$page->of(false);
// foreach ($pageFields as $field => $value) {
// echo $field . " " . $value."\n";
// $page->$field = $value;
// //$json[$field] = [$page->${field}];
//
// }
$page->set('published','1486297829');
$page->published = '1486297829';
$page->save();
die;
$json = ['message'=>'RESOURCE UPDATED'];
http_response_code(201);
} else {
$json = ['message'=>'NOACCESS'];
}
break;
case 'GET':
$json = [];
$connections = [];
foreach ($page->children as $key => $child) {
$connections[$child->id] = $child->httpUrl;
}
$additionalFields = [
'created' => $page->created.'000',
'published' => $page->published.'000',
'modified' => $page->modified.'000',
'createdUser' => $page->createdUser->name,
'modifiedUser' => $page->modifiedUser->name,
'parent' => (string) $page->parent,
'template' => $page->template->name
];
$json['children'] = $connections;
foreach ($page->fields as $field) {
$json[$field->name] = [
'value' => htmlentities($page->{$field->name}),
'field' => (array) $page->{$field->name}
];
}
foreach ($additionalFields as $field => $value) {
$json[$field] = [
'value' => $value
];
}
break;
default:
//handle_error($request);
break;
}
// ob_clean();
// access from somewhere else than the origin?
// header('Access-Control-Allow-Origin: https://martinmuzatko.github.io');
// header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS");
// header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// http_response_code(200);
// only set content-type when successfull, otherwise we set html so we can still read errors WHILE developing
header("Content-Type: application/json");
echo json_encode($json);