-
Notifications
You must be signed in to change notification settings - Fork 40
/
module_page.inc.php
276 lines (237 loc) · 7.14 KB
/
module_page.inc.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/*
* module_page.inc.php
* Module for managing pages
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('common.inc.php');
require_once('html.inc.php');
require_once('util.inc.php');
// module_image.inc.php has more information on what's going on inside modules
// (they can be easier than that one though)
/**
* clear the page's current background image
*
* @param array $args arguments
* key 'page' is the page (i.e. page.rev)
* @return array response
*/
function page_clear_background_img($args)
{
if (!isset($args['page'])) {
return response('Required argument "page" missing', 400);
}
if (!page_exists($args['page'])) {
return response('Page '.quot($args['page']).' does not exist', 400);
}
load_modules('glue');
$obj = load_object(array('name'=>$args['page'].'.page'));
if ($obj['#error']) {
// page object does not exist, hence no background image to clear
return response(true);
} else {
$obj = $obj['#data'];
}
if (!empty($obj['page-background-file'])) {
// delete file
delete_upload(array('pagename'=>get_first_item(expl('.', $args['page'])), 'file'=>$obj['page-background-file'], 'max_cnt'=>1));
// and remove attributes
return object_remove_attr(array('name'=>$obj['name'], 'attr'=>array('page-background-file', 'page-background-mime')));
} else {
return response(true);
}
}
register_service('page.clear_background_img', 'page_clear_background_img', array('auth'=>true));
function page_delete_page($args)
{
$page = $args['page'];
// check if there is a page object
$obj = load_object(array('name'=>$page.'.page'));
if ($obj['#error']) {
return false;
} else {
$obj = $obj['#data'];
}
// check if there is a background-image
if (!empty($obj['page-background-file'])) {
// delete it
delete_upload(array('pagename'=>get_first_item(expl('.', $page)), 'file'=>$obj['page-background-file'], 'max_cnt'=>1));
return true;
} else {
return false;
}
}
function page_has_reference($args)
{
$obj = $args['obj'];
if (array_pop(expl('.', $obj['name'])) != 'page') {
return false;
}
if (!empty($obj['page-background-file']) && $obj['page-background-file'] == $args['file']) {
return true;
} else {
return false;
}
}
/**
* get the current grid size
*
* @param array $args arguments
* @return array response
* 'x', 'y' the grid size
*/
function page_get_grid($args)
{
if (($s = @file_get_contents(CONTENT_DIR.'/grid')) !== false) {
$a = expl(' ', $s);
return response(array('x'=>intval($a[0]), 'y'=>intval($a[1])));
} else {
return response(array('x'=>PAGE_DEFAULT_GRID_X, 'y'=>PAGE_DEFAULT_GRID_Y));
}
}
register_service('page.get_grid', 'page_get_grid');
function page_render_object($args)
{
$obj = $args['obj'];
$a = expl('.', $obj['name']);
if ($a[2] != 'page') {
return false;
}
// background-attachment
if (!empty($obj['page-background-attachment'])) {
html_css('background-attachment', $obj['page-background-attachment']);
}
// background-color
if (!empty($obj['page-background-color'])) {
html_css('background-color', $obj['page-background-color']);
}
// background-image
if (!empty($obj['page-background-file'])) {
if (SHORT_URLS) {
html_css('background-image', 'url('.base_url().htmlspecialchars(urlencode($obj['name']), ENT_NOQUOTES, 'UTF-8').')');
} else {
html_css('background-image', 'url('.base_url().'?'.htmlspecialchars(urlencode($obj['name']), ENT_NOQUOTES, 'UTF-8').')');
}
}
// background-image-position
if (!empty($obj['page-background-image-position'])) {
html_css('background-position', $obj['page-background-image-position']);
}
// set the html title
if (isset($obj['page-title'])) {
html_title($obj['page-title']);
}
}
function page_render_page_early($args)
{
if ($args['edit']) {
if (USE_MIN_FILES) {
html_add_js(base_url().'modules/page/page-edit.min.js');
} else {
html_add_js(base_url().'modules/page/page-edit.js');
}
html_add_css(base_url().'modules/page/page-edit.css');
// set default grid
$grid = page_get_grid(array());
$grid = $grid['#data'];
html_add_js_var('$.glue.conf.page.default_grid_x', $grid['x']);
html_add_js_var('$.glue.conf.page.default_grid_y', $grid['y']);
// set guides
$guide = expl(' ', PAGE_GUIDES_X);
for ($i=0; $i < count($guide); $i++) {
$guide[$i] = intval(trim($guide[$i]));
}
html_add_js_var('$.glue.conf.page.guides_x', $guide);
$guide = expl(' ', PAGE_GUIDES_Y);
for ($i=0; $i < count($guide); $i++) {
$guide[$i] = intval(trim($guide[$i]));
}
html_add_js_var('$.glue.conf.page.guides_y', $guide);
}
// set the html title to the page name by default
html_title(page_short($args['page']));
}
function page_serve_resource($args)
{
$obj = $args['obj'];
if (array_pop(expl('.', $obj['name'])) != 'page') {
return false;
}
$pn = get_first_item(expl('.', $obj['name']));
if (!empty($obj['page-background-file'])) {
$fn = CONTENT_DIR.'/'.$pn.'/shared/'.$obj['page-background-file'];
if (isset($obj['page-background-mime'])) {
$mime = $obj['page-background-mime'];
} else {
$mime = '';
}
serve_file($fn, false, $mime);
}
// if everything fails
return false;
}
/**
* get the current grid size
*
* @param array $args arguments
* key 'x', 'y' is the grid size
* @return array response
* true if successful
*/
function page_set_grid($args)
{
if (($x = @intval($args['x'])) == 0) {
return response('Required argument "x" missing or invalid', 400);
}
if (($y = @intval($args['y'])) == 0) {
return response('Required argument "y" missing or invalid', 400);
}
$m = umask(0111);
if (!@file_put_contents(CONTENT_DIR.'/grid', $x.' '.$y)) {
umask($m);
return response('Error saving to global grid file', 500);
} else {
umask($m);
return response(true);
}
}
register_service('page.set_grid', 'page_set_grid', array('auth'=>true));
function page_upload($args)
{
// only handle the file if the frontend wants us to
if (empty($args['preferred_module']) || $args['preferred_module'] != 'page') {
return false;
}
// check if supported file
if (!in_array($args['mime'], array('image/jpeg', 'image/png', 'image/gif')) || ($args['mime'] == '' && !in_array(filext($args['file']), array('jpg', 'jpeg', 'png', 'gif')))) {
return false;
}
// check if there is already a background-image and delete it
$obj = load_object(array('name'=>$args['page'].'.page'));
if (!$obj['#error']) {
$obj = $obj['#data'];
if (!empty($obj['page-background-file'])) {
delete_upload(array('pagename'=>get_first_item(expl('.', $args['page'])), 'file'=>$obj['page-background-file'], 'max_cnt'=>1));
}
}
// set as background-image in page object
$obj = array();
$obj['name'] = $args['page'].'.page';
$obj['page-background-file'] = $args['file'];
$obj['page-background-mime'] = $args['mime'];
// update page object
load_modules('glue');
$ret = update_object($obj);
if ($ret['#error']) {
log_msg('page_upload: error updating page object: '.quot($ret['#data']));
return false;
} else {
// we don't actually render the object here, but signal the
// frontend that everything went okay
return true;
}
}