This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
/
jsapi.php
205 lines (135 loc) · 4.91 KB
/
jsapi.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
// these controllers are used by AJAX calls
// POST level=debug, message=foo bar
function json_js_log() {
$msg = get_string('message', 'POST');
if (!$msg) { halt(HTTP_BAD_REQUEST); }
$level = get_string('level', 'POST');
$now = $_SERVER['REQUEST_TIME'];
$path = Config::$app_dir.'/../data/jslog';
file_put_contents($path, "[$now][$level]: $msg\n", FILE_APPEND|LOCK_EX);
return json(array('status' => 'ok'));
}
// === PAGES ===
// used by Jeditable
function jsapi_get_page_markdown() {
$id = (int)get_string('id', 'GET');
$page = PageQuery::create()
->withColumn('Page.text')
->findOneById($id);
if (!$page) {
halt(NOT_FOUND);
}
return $page->getText();
}
// used by Jeditable
function jsapi_post_page() {
$id = (int)get_string('id', 'POST');
$newvalue = get_string('value', 'POST');
$page = PageQuery::create()->findOneById($id);
if (!$page) {
halt(NOT_FOUND);
}
if (!is_connected() || !is_moderator()) {
halt(HTTP_FORBIDDEN);
}
$page->setText(truncate_string($newvalue, 4096));
$page->save();
return tpl_render('utils/md.html', array('content'=>$page->getText()));
}
// === CURSUS ===
// used by Jeditable
function jsapi_get_cursus_intro_markdown() {
$id = (int)get_string('id', 'GET');
$cursus = CursusQuery::create()
->withColumn('Cursus.description')
->findOneById($id);
if (!$cursus) { halt(NOT_FOUND); }
return $cursus->getDescription();
}
// used by Jeditable
function jsapi_post_cursus_intro() {
$id = (int)get_string('id', 'POST');
$newvalue = get_string('value', 'POST');
$cursus = CursusQuery::create()
->findOneById($id);
if (!$cursus) { halt(NOT_FOUND); }
if (!is_connected() || (!is_admin() && !user()->isResponsibleFor($cursus))) {
halt(HTTP_FORBIDDEN);
}
$cursus->setDescription(truncate_string($newvalue, 1024));
$cursus->save();
return tpl_render('utils/md.html', array('content'=>$cursus->getDescription()));
}
// === CONTENTS ===
// used by Jeditable
function jsapi_post_change_content_title() {
$content_id = (int)get_string('id', 'POST');
$newvalue = get_string('value', 'POST');
$content = ContentQuery::create()->findOneById($content_id);
if (!$content) { halt(HTTP_NOT_FOUND); }
$len = strlen($newvalue);
if ($len === 0) {
return $content->getTitle();
}
$cursus = $content->getCursus();
// user can only edit the title if he/she is a moderator/admin and/or the
// content has not been validated yet and he/she is its author.
if ( !is_connected()
|| ( !is_moderator()
&& !user()->isResponsibleFor($cursus)
&& ($content->isValidated()
|| user()->getId() !== $content->getAuthorId() ))) {
halt(HTTP_FORBIDDEN);
}
if ($content->isDeleted()) {
halt(HTTP_NOT_FOUND);
}
$title_is_taken = ContentQuery::create()
->filterByCursus($cursus)
->filterByCourseId($content->getCourseId())
->filterByYear($content->getYear())
->filterByDeleted(0)
->findOneByTitle($newvalue);
if ($title_is_taken && $title_is_taken->getId() !== $content->getId()) {
halt(HTTP_BAD_REQUEST);
}
$content->setTitle($newvalue);
$content->save();
return htmlentities($content->getTitle(), ENT_COMPAT, 'UTF-8');
}
// used by Jeditable
function jsapi_post_change_content_year() {
$content_id = (int)get_string('id', 'POST');
$newvalue = get_string('value', 'POST');
$content = ContentQuery::create()
->filterByDeleted(false)
->findOneById($content_id);
if (!$content) { halt(HTTP_NOT_FOUND); }
$len = strlen($newvalue);
if ($len === 0) {
return tpl_year($content->getYear());
}
$cursus = $content->getCursus();
// user can only edit the title if he/she is a moderator/admin and/or the
// content has not been validated yet and he/she is its author.
if ( !is_connected()
|| ( !is_moderator()
&& !user()->isResponsibleFor($cursus)
&& ($content->isValidated()
|| user()->getId() !== $content->getAuthorId() ))) {
halt(HTTP_FORBIDDEN);
}
$title_is_taken = ContentQuery::create()
->filterByCursus($cursus)
->filterByCourseId($content->getCourseId())
->filterByTitle($content->getTitle())
->filterByDeleted(0)
->findOneByYear($newvalue);
if ($title_is_taken && $title_is_taken->getId() !== $content->getId()) {
halt(HTTP_BAD_REQUEST);
}
$content->setYear($newvalue);
$content->save();
return tpl_year($content->getYear());
}