Repository navigation
Expand file tree
/
Copy pathmodule_iframe.inc.php
More file actions
288 lines (252 loc) · 8.02 KB
/
Copy pathmodule_iframe.inc.php
File metadata and controls
288 lines (252 loc) · 8.02 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* module_iframe.inc.php
* Module for embedding iframe elements
*
* 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('html.inc.php');
require_once('html_parse.inc.php');
require_once('modules.inc.php');
// module glue gets loaded on demand
// module_image.inc.php has more information on what's going on inside modules
// (they can be easier than that one though)
function iframe_alter_save($args)
{
$elem = $args['elem'];
$obj = &$args['obj'];
if (!elem_has_class($elem, 'iframe')) {
return false;
}
// parse children elements to find iframe
$childs = html_parse(elem_val($elem));
$i = false;
foreach ($childs as $c) {
if (elem_tag($c) == 'iframe') {
$i = $c;
break;
}
}
if (!$i) {
log_msg('warn', 'iframe_alter_save: no iframe element found, inner html is '.var_dump_inl($childs));
return false;
}
// url - an uploaded html file is shown through its object's own url, which
// is the server's to say, so the src is not read back over it
if (!empty($obj['iframe-file'])) {
// nothing to store
} elseif (elem_attr($i, 'src') !== NULL) {
// strip schema from url
$obj['iframe-url'] = strstr(elem_attr($i, 'src'), '//');
} else {
unset($obj['iframe-url']);
}
// scrolling
if (elem_css($i, 'overflow') == 'hidden' || (elem_css($i, 'overflow-x') == 'hidden' && elem_css($i, 'overflow-y') == 'hidden')) {
unset($obj['iframe-scroll']);
} else {
$obj['iframe-scroll'] = 'scroll';
}
return true;
}
function iframe_alter_render_early($args)
{
$elem = &$args['elem'];
$obj = $args['obj'];
if (!elem_has_class($elem, 'iframe')) {
return false;
}
// add iframe
$i = elem('iframe');
// set the name attribute
elem_attr($i, 'name', $obj['name']);
if (!empty($obj['iframe-file'])) {
// an uploaded html file runs in a sandbox of its own, in the editor
// as on the page: scripts and forms work, but without
// allow-same-origin it is a stranger to the site, and cannot reach
// the page around it or the editor's session
elem_attr($i, 'sandbox', 'allow-forms allow-modals allow-popups allow-scripts');
} elseif (!$args['edit']) {
// try to lift any restrictions
elem_attr($i, 'sandbox', 'allow-forms allow-same-origin allow-scripts allow-top-navigation');
}
elem_css($i, 'background-color', 'transparent');
elem_css($i, 'border-width', '0px');
elem_css($i, 'height', '100%');
elem_css($i, 'position', 'absolute');
elem_css($i, 'width', '100%');
// url
if (!empty($obj['iframe-file'])) {
// the url points to the object name, not the filename in the shared
// directory (the file gets served in iframe_serve_resource()), and
// stays relative - see module_image.inc.php for why
elem_attr($i, 'src', base_path().(SHORT_URLS ? '' : '?').urlencode($obj['name']));
} elseif (!empty($obj['iframe-url'])) {
// use protocol relative url
//elem_attr($i, 'src', $obj['iframe-url']);
elem_attr($i, 'src', strstr($obj['iframe-url'], '//'));
} else {
elem_attr($i, 'src', '');
}
// scrolling
if (isset($obj['iframe-scroll']) && $obj['iframe-scroll'] == 'scroll') {
elem_css($i, 'overflow', 'auto');
// attribute scrolling is not available in html5 but this effectivly
// removes the scrollbars on Chrome, so..
elem_attr($i, 'scrolling', 'auto');
} else {
elem_css($i, 'overflow', 'hidden');
elem_attr($i, 'scrolling', 'no');
elem_attr($i, 'seamless', 'seamless');
}
elem_append($elem, $i);
if ($args['edit']) {
// add shield as well
// over the upper part of the embed, like the webvideo's: see
// .glue-iframe-shield in css/edit.css
$s = elem('div');
elem_add_class($s, 'glue-iframe-shield');
elem_add_class($s, 'glue-ui');
elem_attr($s, 'title', 'click to select this embed');
elem_append($elem, $s);
}
return true;
}
function iframe_render_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'iframe') {
return false;
}
$e = elem('div');
elem_attr($e, 'id', $obj['name']);
elem_add_class($e, 'iframe');
elem_add_class($e, 'resizable');
elem_add_class($e, 'object');
// hooks
invoke_hook_first('alter_render_early', 'iframe', ['obj'=>$obj, 'elem'=>&$e, 'edit'=>$args['edit']]);
$html = elem_finalize($e);
invoke_hook_last('alter_render_late', 'iframe', ['obj'=>$obj, 'html'=>&$html, 'elem'=>$e, 'edit'=>$args['edit']]);
return $html;
}
function iframe_render_page_early($args)
{
if ($args['edit']) {
if (USE_MIN_FILES) {
html_add_js(base_url().'modules/iframe/iframe-edit.min.js');
} else {
html_add_js(base_url().'modules/iframe/iframe-edit.js');
}
html_add_css(base_url().'modules/iframe/iframe-edit.css');
}
}
function iframe_save_state($args)
{
$elem = $args['elem'];
$obj = $args['obj'];
if (get_first_item(elem_classes($elem)) != 'iframe') {
return false;
}
// make sure the type is set
$obj['type'] = 'iframe';
$obj['module'] = 'iframe';
// hook
invoke_hook('alter_save', ['obj'=>&$obj, 'elem'=>$elem]);
load_modules('glue');
$ret = save_object($obj);
if ($ret['#error']) {
log_msg('error', 'iframe_save_state: save_object returned '.quot($ret['#data']));
return false;
} else {
return true;
}
}
function iframe_delete_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'iframe' || empty($obj['iframe-file'])) {
return false;
}
load_modules('glue');
$a = expl('.', $obj['name']);
$ret = delete_upload(['pagename'=>$a[0], 'file'=>$obj['iframe-file'], 'max_cnt'=>1]);
if ($ret['#error']) {
log_msg('error', 'iframe_delete_object: delete_upload returned '.quot($ret['#data']));
}
}
function iframe_has_reference($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'iframe' || empty($obj['iframe-file'])) {
return false;
}
// symlinks have their referenced files in a different page that's why
// they are not relevant here
if (@is_link(CONTENT_DIR.'/'.str_replace('.', '/', $obj['name']))) {
return false;
}
return $obj['iframe-file'] == $args['file'];
}
/**
* implements serve_resource: the uploaded html file, which is the page the
* iframe shows
*/
function iframe_serve_resource($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'iframe' || empty($obj['iframe-file'])) {
return false;
}
$a = expl('.', $obj['name']);
if (!$args['dl']) {
// an html document served from the site's own origin is as much the
// site as its own pages are: a script inside it could act as whoever
// opens the url, the editor included. The sandbox policy keeps the
// scripts (the page is meant to run) and takes the origin away, and
// it is on the response itself, so it holds when the url is opened
// directly as well as in the frame. The download path is an
// attachment, not a document.
header('Content-Security-Policy: sandbox allow-forms allow-modals allow-popups allow-scripts');
header('X-Content-Type-Options: nosniff');
}
serve_file(CONTENT_DIR.'/'.$a[0].'/shared/'.$obj['iframe-file'], $args['dl'], 'text/html');
}
/**
* an uploaded html file becomes an iframe object that shows it, which
* resizes like any embed
*/
function iframe_upload($args)
{
$ext = strtolower(filext($args['file']));
if (!in_array($ext, ['html', 'htm', 'xhtml']) && $args['mime'] != 'text/html') {
return false;
}
// a page menu's 'upload a file' asks for a download object by name, and a
// wrap target is a download's business: neither is ours
if (isset($args['preferred_module']) && $args['preferred_module'] == 'download') {
return false;
}
load_modules('glue');
$obj = create_object($args);
if ($obj['#error']) {
return false;
} else {
$obj = $obj['#data'];
}
$obj['type'] = 'iframe';
$obj['module'] = 'iframe';
$obj['iframe-file'] = $args['file'];
// a document is longer than the frame it starts in: scrollbars on, the
// menu toggle takes them away
$obj['iframe-scroll'] = 'scroll';
save_object($obj);
$ret = render_object(['name'=>$obj['name'], 'edit'=>true]);
if ($ret['#error']) {
return false;
} else {
return $ret['#data'];
}
}