MDL-21695 adding help string
[moodle.git] / mod / url / locallib.php
blob865ad5a51eac279b13e95d30b6a82d513df9a851
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Private url module utility functions
21 * @package mod-url
22 * @copyright 2009 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("$CFG->libdir/file/file_browser.php");
27 require_once("$CFG->libdir/filelib.php");
28 require_once("$CFG->libdir/resourcelib.php");
29 require_once("$CFG->dirroot/mod/url/lib.php");
31 /**
32 * Return full url with all extra parameters
33 * @param string $url
34 * @param object $cm
35 * @param object $course
36 * @return string url
38 function url_get_full_url($url, $cm, $course, $config=null) {
40 $parameters = empty($url->parameters) ? array() : unserialize($url->parameters);
42 if (empty($parameters)) {
43 // easy - no params
44 return $url->externalurl;
47 if (!$config) {
48 $config = get_config('url');
50 $paramvalues = url_get_variable_values($url, $cm, $course, $config);
52 foreach ($parameters as $parse=>$parameter) {
53 if (isset($paramvalues[$parameter])) {
54 $parameters[$parse] = urlencode($parse).'='.urlencode($paramvalues[$parameter]);
55 } else {
56 unset($parameters[$parse]);
60 if (empty($parameters)) {
61 // easy - no params available
62 return $url->externalurl;
65 if (stripos($url->externalurl, 'teamspeak://') === 0) {
66 return $url->externalurl.'?'.implode('?', $parameters);
67 } else {
68 $join = (strpos($url->externalurl, '?') === false) ? '?' : '&amp;';
69 return $url->externalurl.$join.implode('&amp;', $parameters);
73 /**
74 * Print url header.
75 * @param object $url
76 * @param object $cm
77 * @param object $course
78 * @return void
80 function url_print_header($url, $cm, $course) {
81 global $PAGE, $OUTPUT;
83 $PAGE->set_title($course->shortname.': '.$url->name);
84 $PAGE->set_heading($course->fullname);
85 $PAGE->set_activity_record($url);
86 echo $OUTPUT->header();
89 /**
90 * Print url heading.
91 * @param object $url
92 * @param object $cm
93 * @param object $course
94 * @param bool $ignoresettings print even if not specified in modedit
95 * @return void
97 function url_print_heading($url, $cm, $course, $ignoresettings=false) {
98 global $OUTPUT;
100 $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
102 if ($ignoresettings or !empty($options['printheading'])) {
103 echo $OUTPUT->heading(format_string($url->name), 2, 'main', 'urlheading');
108 * Print url introduction.
109 * @param object $url
110 * @param object $cm
111 * @param object $course
112 * @param bool $ignoresettings print even if not specified in modedit
113 * @return void
115 function url_print_intro($url, $cm, $course, $ignoresettings=false) {
116 global $OUTPUT;
118 $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
119 if ($ignoresettings or !empty($options['printintro'])) {
120 if (trim(strip_tags($url->intro))) {
121 echo $OUTPUT->box_start('mod_introbox', 'urlintro');
122 echo format_module_intro('url', $url, $cm->id);
123 echo $OUTPUT->box_end();
129 * Display url frames.
130 * @param object $url
131 * @param object $cm
132 * @param object $course
133 * @return does not return
135 function url_display_frame($url, $cm, $course) {
136 global $PAGE, $OUTPUT, $CFG;
138 $frame = optional_param('frameset', 'main', PARAM_ALPHA);
140 if ($frame === 'top') {
141 $PAGE->set_pagelayout('frametop');
142 url_print_header($url, $cm, $course);
143 url_print_heading($url, $cm, $course);
144 url_print_intro($url, $cm, $course);
145 echo $OUTPUT->footer();
146 die;
148 } else {
149 $config = get_config('url');
150 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
151 $exteurl = s(url_get_full_url($url, $cm, $course, $config));
152 $navurl = "$CFG->wwwroot/mod/url/view.php?id=$cm->id&amp;frameset=top";
153 $title = strip_tags(format_string($course->shortname.': '.$url->name));
154 $framesize = $config->framesize;
155 $modulename = s(get_string('modulename','url'));
156 $dir = get_string('thisdirection', 'langconfig');
158 $extframe = <<<EOF
159 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
160 <html dir="$dir">
161 <head>
162 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
163 <title>$title</title>
164 </head>
165 <frameset rows="$framesize,*">
166 <frame src="$navurl" title="$modulename"/>
167 <frame src="$exteurl" title="$modulename"/>
168 </frameset>
169 </html>
170 EOF;
172 @header('Content-Type: text/html; charset=utf-8');
173 echo $extframe;
174 die;
179 * Print url info and link.
180 * @param object $url
181 * @param object $cm
182 * @param object $course
183 * @return does not return
185 function url_print_workaround($url, $cm, $course) {
186 global $OUTPUT;
188 url_print_header($url, $cm, $course);
189 url_print_heading($url, $cm, $course, true);
190 url_print_intro($url, $cm, $course, true);
192 $fullurl = url_get_full_url($url, $cm, $course);
194 $display = url_get_final_display_type($url);
195 if ($display == RESOURCELIB_DISPLAY_POPUP) {
196 $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
197 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
198 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
199 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
200 $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
202 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
203 $extra = "onclick=\"this.target='_blank';\"";
205 } else {
206 $extra = '';
209 echo '<div class="urlworkaround">';
210 print_string('clicktoopen', 'url', "<a href=\"$fullurl\" $extra>$fullurl</a>");
211 echo '</div>';
213 echo $OUTPUT->footer();
214 die;
218 * Display embedded url file.
219 * @param object $url
220 * @param object $cm
221 * @param object $course
222 * @param stored_file $file main file
223 * @return does not return
225 function url_display_embed($url, $cm, $course) {
226 global $CFG, $PAGE, $OUTPUT;
228 $clicktoopen = get_string('clicktoopen', 'url', $url->externalurl);
230 $mimetype = resourcelib_guess_url_mimetype($url->externalurl);
231 $fullurl = url_get_full_url($url, $cm, $course);
232 $title = $url->name;
234 if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
235 $code = resourcelib_embed_image($fullurl, $title);
237 } else if ($mimetype == 'audio/mp3') {
238 // MP3 audio file
239 $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen);
241 } else if ($mimetype == 'video/x-flv') {
242 // Flash video file
243 $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen);
245 } else if ($mimetype == 'application/x-shockwave-flash') {
246 // Flash file
247 $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen);
249 } else if (substr($mimetype, 0, 10) == 'video/x-ms') {
250 // Windows Media Player file
251 $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen);
253 } else if ($mimetype == 'video/quicktime') {
254 // Quicktime file
255 $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen);
257 } else if ($mimetype == 'video/mpeg') {
258 // Mpeg file
259 $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen);
261 } else if ($mimetype == 'audio/x-pn-realaudio-plugin') {
262 // RealMedia file
263 $code = resourcelib_embed_real($fullurl, $title, $clicktoopen);
265 } else {
266 // anything else - just try object tag enlarged as much as possible
267 $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
270 url_print_header($url, $cm, $course);
271 url_print_heading($url, $cm, $course);
273 echo $code;
275 url_print_intro($url, $cm, $course);
277 echo $OUTPUT->footer();
278 die;
282 * Decide the best diaply format.
283 * @param object $url
284 * @return int display type constant
286 function url_get_final_display_type($url) {
287 global $CFG;
289 if ($url->display != RESOURCELIB_DISPLAY_AUTO) {
290 return $url->display;
293 // detect links to local moodle pages
294 if (strpos($url->externalurl, $CFG->wwwroot) === 0) {
295 if (strpos($url->externalurl, 'file.php') === false and strpos($url->externalurl, '.php') !== false ) {
296 // most probably our moodle page with navigation
297 return RESOURCELIB_DISPLAY_OPEN;
301 static $download = array('application/zip', 'application/x-tar', 'application/g-zip', // binary formats
302 'application/pdf', 'text/html'); // these are known to cause trouble for external links, sorry
303 static $embed = array('image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', // images
304 'application/x-shockwave-flash', 'video/x-flv', 'video/x-ms-wm', // video formats
305 'video/quicktime', 'video/mpeg',
306 'audio/mp3', 'audio/x-realaudio-plugin', 'x-realaudio-plugin', // audio formats,
309 $mimetype = mimeinfo('type', $url->externalurl);
311 if (in_array($mimetype, $download)) {
312 return RESOURCELIB_DISPLAY_DOWNLOAD;
314 if (in_array($mimetype, $embed)) {
315 return RESOURCELIB_DISPLAY_EMBED;
318 // let the browser deal with it somehow
319 return RESOURCELIB_DISPLAY_OPEN;
323 * Get the parameters that may be appended to URL
324 * @param object $config url module config options
325 * @return array array describing opt groups
327 function url_get_variable_options($config) {
328 global $CFG;
330 $options = array();
331 $options[''] = array('' => get_string('chooseavariable', 'url'));
333 $options[get_string('course')] = array(
334 'courseid' => 'id',
335 'coursefullname' => get_string('fullnamecourse'),
336 'courseshortname' => get_string('shortnamecourse'),
337 'courseidnumber' => get_string('idnumbercourse'),
338 'coursesummary' => get_string('summary'),
339 'courseformat' => get_string('format'),
342 $options[get_string('modulename', 'url')] = array(
343 'urlinstance' => 'id',
344 'urlcmid' => 'cmid',
345 'urlname' => get_string('name'),
346 'urlidnumber' => get_string('idnumbermod'),
349 $options[get_string('miscellaneous')] = array(
350 'sitename' => get_string('fullsitename'),
351 'serverurl' => get_string('serverurl', 'resource', (object)array('wwwroot'=>$CFG->wwwroot)),
352 'currenttime' => get_string('time'),
353 'lang' => get_string('language'),
355 if (!empty($config->secretphrase)) {
356 $options[get_string('miscellaneous')]['encryptedcode'] = get_string('encryptedcode');
359 $options[get_string('user')] = array(
360 'userid' => 'id',
361 'userusername' => get_string('username'),
362 'useridnumber' => get_string('idnumber'),
363 'userfirstname' => get_string('firstname'),
364 'userlastname' => get_string('lastname'),
365 'userfullname' => get_string('fullnameuser'),
366 'useremail' => get_string('email'),
367 'usericq' => get_string('icqnumber'),
368 'userphone1' => get_string('phone').' 1',
369 'userphone2' => get_string('phone2').' 2',
370 'userinstitution' => get_string('institution'),
371 'userdepartment' => get_string('department'),
372 'useraddress' => get_string('address'),
373 'usercity' => get_string('city'),
374 'usertimezone' => get_string('timezone'),
375 'userurl' => get_string('webpage'),
378 if ($config->rolesinparams) {
379 $roles = get_all_roles();
380 $roleoptions = array();
381 foreach ($roles as $role) {
382 $roleoptions['course'.$role->shortname] = get_string('yourwordforx', '', $role->name);
384 $options[get_string('roles')] = $roleoptions;
387 return $options;
391 * Get the parameter values that may be appended to URL
392 * @param object $url module instance
393 * @param object $cm
394 * @param object $course
395 * @param object $config module config options
396 * @return array of parameter values
398 function url_get_variable_values($url, $cm, $course, $config) {
399 global $USER, $CFG;
401 $site = get_site();
403 $values = array (
404 'courseid' => $course->id,
405 'coursefullname' => format_string($course->fullname),
406 'courseshortname' => $course->shortname,
407 'courseidnumber' => $course->idnumber,
408 'coursesummary' => $course->summary,
409 'courseformat' => $course->format,
410 'lang' => current_language(),
411 'sitename' => format_string($site->fullname),
412 'serverurl' => $CFG->wwwroot,
413 'currenttime' => time(),
414 'urlinstance' => $url->id,
415 'urlcmid' => $cm->id,
416 'urlname' => format_string($url->name),
417 'urlidnumber' => $cm->idnumber,
420 if (isloggedin()) {
421 $values['userid'] = $USER->id;
422 $values['userusername'] = $USER->username;
423 $values['useridnumber'] = $USER->idnumber;
424 $values['userfirstname'] = $USER->firstname;
425 $values['userlastname'] = $USER->lastname;
426 $values['userfullname'] = fullname($USER);
427 $values['useremail'] = $USER->email;
428 $values['usericq'] = $USER->icq;
429 $values['userphone1'] = $USER->phone1;
430 $values['userphone2'] = $USER->phone2;
431 $values['userinstitution'] = $USER->institution;
432 $values['userdepartment'] = $USER->department;
433 $values['useraddress'] = $USER->address;
434 $values['usercity'] = $USER->city;
435 $values['usertimezone'] = get_user_timezone_offset();
436 $values['userurl'] = $USER->url;
439 // weak imitation of Single-Sign-On, for backwards compatibility only
440 // NOTE: login hack is not included in 2.0 any more, new contrib auth plugin
441 // needs to be createed if somebody needs the old functionality!
442 if (!empty($config->secretphrase)) {
443 $values['encryptedcode'] = url_get_encrypted_parameter($url, $config);
446 //hmm, this is pretty fragile and slow, why do we need it here??
447 if ($config->rolesinparams) {
448 $roles = get_all_roles();
449 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
450 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS);
451 foreach ($roles as $role) {
452 $values['course'.$role->shortname] = $role->localname;
456 return $values;
460 * BC internal function
461 * @param object $url
462 * @param object $config
463 * @return string
465 function url_get_encrypted_parameter($url, $config) {
466 global $CFG;
468 if (file_exists("$CFG->dirroot/local/externserverfile.php")) {
469 require_once("$CFG->dirroot/local/externserverfile.php");
470 if (function_exists('extern_server_file')) {
471 return extern_server_file($url, $config);
474 return md5(getremoteaddr().$config->secretphrase);
478 * Optimised mimetype detection from general URL
479 * @param $fullurl
480 * @return string mimetype
482 function url_guess_icon($fullurl) {
483 global $CFG;
484 require_once("$CFG->libdir/filelib.php");
486 if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
487 // most probably default directory - index.php, index.html, etc.
488 return 'f/web';
491 $icon = mimeinfo('icon', $fullurl);
492 $icon = 'f/'.str_replace(array('.gif', '.png'), '', $icon);
494 if ($icon === 'f/html' or $icon === 'f/unknown') {
495 $icon = 'f/web';
498 return $icon;