forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
499 lines (423 loc) · 16.3 KB
/
locallib.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Private url module utility functions
*
* @package mod-url
* @copyright 2009 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->libdir/file/file_browser.php");
require_once("$CFG->libdir/filelib.php");
require_once("$CFG->libdir/resourcelib.php");
require_once("$CFG->dirroot/mod/url/lib.php");
/**
* Return full url with all extra parameters
* @param string $url
* @param object $cm
* @param object $course
* @return string url
*/
function url_get_full_url($url, $cm, $course, $config=null) {
$parameters = empty($url->parameters) ? array() : unserialize($url->parameters);
if (empty($parameters)) {
// easy - no params
return $url->externalurl;
}
if (!$config) {
$config = get_config('url');
}
$paramvalues = url_get_variable_values($url, $cm, $course, $config);
foreach ($parameters as $parse=>$parameter) {
if (isset($paramvalues[$parameter])) {
$parameters[$parse] = urlencode($parse).'='.urlencode($paramvalues[$parameter]);
} else {
unset($parameters[$parse]);
}
}
if (empty($parameters)) {
// easy - no params available
return $url->externalurl;
}
if (stripos($url->externalurl, 'teamspeak://') === 0) {
return $url->externalurl.'?'.implode('?', $parameters);
} else {
$join = (strpos($url->externalurl, '?') === false) ? '?' : '&';
return $url->externalurl.$join.implode('&', $parameters);
}
}
/**
* Print url header.
* @param object $url
* @param object $cm
* @param object $course
* @return void
*/
function url_print_header($url, $cm, $course) {
global $PAGE, $OUTPUT;
$PAGE->set_title($course->shortname.': '.$url->name);
$PAGE->set_heading($course->fullname);
$PAGE->set_activity_record($url);
echo $OUTPUT->header();
}
/**
* Print url heading.
* @param object $url
* @param object $cm
* @param object $course
* @param bool $ignoresettings print even if not specified in modedit
* @return void
*/
function url_print_heading($url, $cm, $course, $ignoresettings=false) {
global $OUTPUT;
$options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
if ($ignoresettings or !empty($options['printheading'])) {
echo $OUTPUT->heading(format_string($url->name), 2, 'main', 'urlheading');
}
}
/**
* Print url introduction.
* @param object $url
* @param object $cm
* @param object $course
* @param bool $ignoresettings print even if not specified in modedit
* @return void
*/
function url_print_intro($url, $cm, $course, $ignoresettings=false) {
global $OUTPUT;
$options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
if ($ignoresettings or !empty($options['printintro'])) {
if (trim(strip_tags($url->intro))) {
echo $OUTPUT->box_start('mod_introbox', 'urlintro');
echo format_module_intro('url', $url, $cm->id);
echo $OUTPUT->box_end();
}
}
}
/**
* Display url frames.
* @param object $url
* @param object $cm
* @param object $course
* @return does not return
*/
function url_display_frame($url, $cm, $course) {
global $PAGE, $OUTPUT, $CFG;
$frame = optional_param('frameset', 'main', PARAM_ALPHA);
if ($frame === 'top') {
$PAGE->set_pagelayout('frametop');
url_print_header($url, $cm, $course);
url_print_heading($url, $cm, $course);
url_print_intro($url, $cm, $course);
echo $OUTPUT->footer();
die;
} else {
$config = get_config('url');
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$exteurl = s(url_get_full_url($url, $cm, $course, $config));
$navurl = "$CFG->wwwroot/mod/url/view.php?id=$cm->id&frameset=top";
$title = strip_tags(format_string($course->shortname.': '.$url->name));
$framesize = $config->framesize;
$modulename = s(get_string('modulename','url'));
$dir = get_string('thisdirection', 'langconfig');
$extframe = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html dir="$dir">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>$title</title>
</head>
<frameset rows="$framesize,*">
<frame src="$navurl" title="$modulename"/>
<frame src="$exteurl" title="$modulename"/>
</frameset>
</html>
EOF;
@header('Content-Type: text/html; charset=utf-8');
echo $extframe;
die;
}
}
/**
* Print url info and link.
* @param object $url
* @param object $cm
* @param object $course
* @return does not return
*/
function url_print_workaround($url, $cm, $course) {
global $OUTPUT;
url_print_header($url, $cm, $course);
url_print_heading($url, $cm, $course, true);
url_print_intro($url, $cm, $course, true);
$fullurl = url_get_full_url($url, $cm, $course);
$display = url_get_final_display_type($url);
if ($display == RESOURCELIB_DISPLAY_POPUP) {
$options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
$width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
$height = empty($options['popupheight']) ? 450 : $options['popupheight'];
$wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
$extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
} else if ($display == RESOURCELIB_DISPLAY_NEW) {
$extra = "onclick=\"this.target='_blank';\"";
} else {
$extra = '';
}
echo '<div class="urlworkaround">';
print_string('clicktoopen', 'url', "<a href=\"$fullurl\" $extra>$fullurl</a>");
echo '</div>';
echo $OUTPUT->footer();
die;
}
/**
* Display embedded url file.
* @param object $url
* @param object $cm
* @param object $course
* @param stored_file $file main file
* @return does not return
*/
function url_display_embed($url, $cm, $course) {
global $CFG, $PAGE, $OUTPUT;
$clicktoopen = get_string('clicktoopen', 'url', $url->externalurl);
$mimetype = resourcelib_guess_url_mimetype($url->externalurl);
$fullurl = url_get_full_url($url, $cm, $course);
$title = $url->name;
if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
$code = resourcelib_embed_image($fullurl, $title);
} else if ($mimetype == 'audio/mp3') {
// MP3 audio file
$code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen);
} else if ($mimetype == 'video/x-flv') {
// Flash video file
$code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen);
} else if ($mimetype == 'application/x-shockwave-flash') {
// Flash file
$code = resourcelib_embed_flash($fullurl, $title, $clicktoopen);
} else if (substr($mimetype, 0, 10) == 'video/x-ms') {
// Windows Media Player file
$code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen);
} else if ($mimetype == 'video/quicktime') {
// Quicktime file
$code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen);
} else if ($mimetype == 'video/mpeg') {
// Mpeg file
$code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen);
} else if ($mimetype == 'audio/x-pn-realaudio-plugin') {
// RealMedia file
$code = resourcelib_embed_real($fullurl, $title, $clicktoopen);
} else {
// anything else - just try object tag enlarged as much as possible
$code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
}
url_print_header($url, $cm, $course);
url_print_heading($url, $cm, $course);
echo $code;
url_print_intro($url, $cm, $course);
echo $OUTPUT->footer();
die;
}
/**
* Decide the best diaply format.
* @param object $url
* @return int display type constant
*/
function url_get_final_display_type($url) {
global $CFG;
if ($url->display != RESOURCELIB_DISPLAY_AUTO) {
return $url->display;
}
// detect links to local moodle pages
if (strpos($url->externalurl, $CFG->wwwroot) === 0) {
if (strpos($url->externalurl, 'file.php') === false and strpos($url->externalurl, '.php') !== false ) {
// most probably our moodle page with navigation
return RESOURCELIB_DISPLAY_OPEN;
}
}
static $download = array('application/zip', 'application/x-tar', 'application/g-zip', // binary formats
'application/pdf', 'text/html'); // these are known to cause trouble for external links, sorry
static $embed = array('image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', // images
'application/x-shockwave-flash', 'video/x-flv', 'video/x-ms-wm', // video formats
'video/quicktime', 'video/mpeg',
'audio/mp3', 'audio/x-realaudio-plugin', 'x-realaudio-plugin', // audio formats,
);
$mimetype = mimeinfo('type', $url->externalurl);
if (in_array($mimetype, $download)) {
return RESOURCELIB_DISPLAY_DOWNLOAD;
}
if (in_array($mimetype, $embed)) {
return RESOURCELIB_DISPLAY_EMBED;
}
// let the browser deal with it somehow
return RESOURCELIB_DISPLAY_OPEN;
}
/**
* Get the parameters that may be appended to URL
* @param object $config url module config options
* @return array array describing opt groups
*/
function url_get_variable_options($config) {
global $CFG;
$options = array();
$options[''] = array('' => get_string('chooseavariable', 'url'));
$options[get_string('course')] = array(
'courseid' => 'id',
'coursefullname' => get_string('fullnamecourse'),
'courseshortname' => get_string('shortnamecourse'),
'courseidnumber' => get_string('idnumbercourse'),
'coursesummary' => get_string('summary'),
'courseformat' => get_string('format'),
);
$options[get_string('modulename', 'url')] = array(
'urlinstance' => 'id',
'urlcmid' => 'cmid',
'urlname' => get_string('name'),
'urlidnumber' => get_string('idnumbermod'),
);
$options[get_string('miscellaneous')] = array(
'sitename' => get_string('fullsitename'),
'serverurl' => get_string('serverurl', 'resource', (object)array('wwwroot'=>$CFG->wwwroot)),
'currenttime' => get_string('time'),
'lang' => get_string('language'),
);
if (!empty($config->secretphrase)) {
$options[get_string('miscellaneous')]['encryptedcode'] = get_string('encryptedcode');
}
$options[get_string('user')] = array(
'userid' => 'id',
'userusername' => get_string('username'),
'useridnumber' => get_string('idnumber'),
'userfirstname' => get_string('firstname'),
'userlastname' => get_string('lastname'),
'userfullname' => get_string('fullnameuser'),
'useremail' => get_string('email'),
'usericq' => get_string('icqnumber'),
'userphone1' => get_string('phone').' 1',
'userphone2' => get_string('phone2').' 2',
'userinstitution' => get_string('institution'),
'userdepartment' => get_string('department'),
'useraddress' => get_string('address'),
'usercity' => get_string('city'),
'usertimezone' => get_string('timezone'),
'userurl' => get_string('webpage'),
);
if ($config->rolesinparams) {
$roles = get_all_roles();
$roleoptions = array();
foreach ($roles as $role) {
$roleoptions['course'.$role->shortname] = get_string('yourwordforx', '', $role->name);
}
$options[get_string('roles')] = $roleoptions;
}
return $options;
}
/**
* Get the parameter values that may be appended to URL
* @param object $url module instance
* @param object $cm
* @param object $course
* @param object $config module config options
* @return array of parameter values
*/
function url_get_variable_values($url, $cm, $course, $config) {
global $USER, $CFG;
$site = get_site();
$values = array (
'courseid' => $course->id,
'coursefullname' => format_string($course->fullname),
'courseshortname' => $course->shortname,
'courseidnumber' => $course->idnumber,
'coursesummary' => $course->summary,
'courseformat' => $course->format,
'lang' => current_language(),
'sitename' => format_string($site->fullname),
'serverurl' => $CFG->wwwroot,
'currenttime' => time(),
'urlinstance' => $url->id,
'urlcmid' => $cm->id,
'urlname' => format_string($url->name),
'urlidnumber' => $cm->idnumber,
);
if (isloggedin()) {
$values['userid'] = $USER->id;
$values['userusername'] = $USER->username;
$values['useridnumber'] = $USER->idnumber;
$values['userfirstname'] = $USER->firstname;
$values['userlastname'] = $USER->lastname;
$values['userfullname'] = fullname($USER);
$values['useremail'] = $USER->email;
$values['usericq'] = $USER->icq;
$values['userphone1'] = $USER->phone1;
$values['userphone2'] = $USER->phone2;
$values['userinstitution'] = $USER->institution;
$values['userdepartment'] = $USER->department;
$values['useraddress'] = $USER->address;
$values['usercity'] = $USER->city;
$values['usertimezone'] = get_user_timezone_offset();
$values['userurl'] = $USER->url;
}
// weak imitation of Single-Sign-On, for backwards compatibility only
// NOTE: login hack is not included in 2.0 any more, new contrib auth plugin
// needs to be createed if somebody needs the old functionality!
if (!empty($config->secretphrase)) {
$values['encryptedcode'] = url_get_encrypted_parameter($url, $config);
}
//hmm, this is pretty fragile and slow, why do we need it here??
if ($config->rolesinparams) {
$roles = get_all_roles();
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS);
foreach ($roles as $role) {
$values['course'.$role->shortname] = $role->localname;
}
}
return $values;
}
/**
* BC internal function
* @param object $url
* @param object $config
* @return string
*/
function url_get_encrypted_parameter($url, $config) {
global $CFG;
if (file_exists("$CFG->dirroot/local/externserverfile.php")) {
require_once("$CFG->dirroot/local/externserverfile.php");
if (function_exists('extern_server_file')) {
return extern_server_file($url, $config);
}
}
return md5(getremoteaddr().$config->secretphrase);
}
/**
* Optimised mimetype detection from general URL
* @param $fullurl
* @return string mimetype
*/
function url_guess_icon($fullurl) {
global $CFG;
require_once("$CFG->libdir/filelib.php");
if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
// most probably default directory - index.php, index.html, etc.
return 'f/web';
}
$icon = mimeinfo('icon', $fullurl);
$icon = 'f/'.str_replace(array('.gif', '.png'), '', $icon);
if ($icon === 'f/html' or $icon === 'f/unknown') {
$icon = 'f/web';
}
return $icon;
}