Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/url/locallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die;
00028 
00029 require_once("$CFG->libdir/filelib.php");
00030 require_once("$CFG->libdir/resourcelib.php");
00031 require_once("$CFG->dirroot/mod/url/lib.php");
00032 
00040 function url_appears_valid_url($url) {
00041     if (preg_match('/^(\/|https?:|ftp:)/i', $url)) {
00042         // note: this is not exact validation, we look for severely malformed URLs only
00043         return preg_match('/^[a-z]+:\/\/([^:@\s]+:[^@\s]+@)?[a-z0-9_\.\-]+(:[0-9]+)?(\/[^#]*)?(#.*)?$/i', $url);
00044     } else {
00045         return preg_match('/^[a-z]+:\/\/...*$/i', $url);
00046     }
00047 }
00048 
00058 function url_fix_submitted_url($url) {
00059     // note: empty urls are prevented in form validation
00060     $url = trim($url);
00061 
00062     // remove encoded entities - we want the raw URI here
00063     $url = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
00064 
00065     if (!preg_match('|^[a-z]+:|i', $url) and !preg_match('|^/|', $url)) {
00066         // invalid URI, try to fix it by making it normal URL,
00067         // please note relative urls are not allowed, /xx/yy links are ok
00068         $url = 'http://'.$url;
00069     }
00070 
00071     return $url;
00072 }
00073 
00085 function url_get_full_url($url, $cm, $course, $config=null) {
00086 
00087     $parameters = empty($url->parameters) ? array() : unserialize($url->parameters);
00088 
00089     // make sure there are no encoded entities, it is ok to do this twice
00090     $fullurl = html_entity_decode($url->externalurl, ENT_QUOTES, 'UTF-8');
00091 
00092     if (preg_match('/^(\/|https?:|ftp:)/i', $fullurl) or preg_match('|^/|', $fullurl)) {
00093         // encode extra chars in URLs - this does not make it always valid, but it helps with some UTF-8 problems
00094         $allowed = "a-zA-Z0-9".preg_quote(';/?:@=&$_.+!*(),-#%', '/');
00095         $fullurl = preg_replace_callback("/[^$allowed]/", 'url_filter_callback', $fullurl);
00096     } else {
00097         // encode special chars only
00098         $fullurl = str_replace('"', '%22', $fullurl);
00099         $fullurl = str_replace('\'', '%27', $fullurl);
00100         $fullurl = str_replace(' ', '%20', $fullurl);
00101         $fullurl = str_replace('<', '%3C', $fullurl);
00102         $fullurl = str_replace('>', '%3E', $fullurl);
00103     }
00104 
00105     // add variable url parameters
00106     if (!empty($parameters)) {
00107         if (!$config) {
00108             $config = get_config('url');
00109         }
00110         $paramvalues = url_get_variable_values($url, $cm, $course, $config);
00111 
00112         foreach ($parameters as $parse=>$parameter) {
00113             if (isset($paramvalues[$parameter])) {
00114                 $parameters[$parse] = rawurlencode($parse).'='.rawurlencode($paramvalues[$parameter]);
00115             } else {
00116                 unset($parameters[$parse]);
00117             }
00118         }
00119 
00120         if (!empty($parameters)) {
00121             if (stripos($fullurl, 'teamspeak://') === 0) {
00122                 $fullurl = $fullurl.'?'.implode('?', $parameters);
00123             } else {
00124                 $join = (strpos($fullurl, '?') === false) ? '?' : '&';
00125                 $fullurl = $fullurl.$join.implode('&', $parameters);
00126             }
00127         }
00128     }
00129 
00130     // encode all & to &amp; entity
00131     $fullurl = str_replace('&', '&amp;', $fullurl);
00132 
00133     return $fullurl;
00134 }
00135 
00142 function url_filter_callback($matches) {
00143     return rawurlencode($matches[0]);
00144 }
00145 
00153 function url_print_header($url, $cm, $course) {
00154     global $PAGE, $OUTPUT;
00155 
00156     $PAGE->set_title($course->shortname.': '.$url->name);
00157     $PAGE->set_heading($course->fullname);
00158     $PAGE->set_activity_record($url);
00159     echo $OUTPUT->header();
00160 }
00161 
00170 function url_print_heading($url, $cm, $course, $ignoresettings=false) {
00171     global $OUTPUT;
00172 
00173     $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
00174 
00175     if ($ignoresettings or !empty($options['printheading'])) {
00176         echo $OUTPUT->heading(format_string($url->name), 2, 'main', 'urlheading');
00177     }
00178 }
00179 
00188 function url_print_intro($url, $cm, $course, $ignoresettings=false) {
00189     global $OUTPUT;
00190 
00191     $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
00192     if ($ignoresettings or !empty($options['printintro'])) {
00193         if (trim(strip_tags($url->intro))) {
00194             echo $OUTPUT->box_start('mod_introbox', 'urlintro');
00195             echo format_module_intro('url', $url, $cm->id);
00196             echo $OUTPUT->box_end();
00197         }
00198     }
00199 }
00200 
00208 function url_display_frame($url, $cm, $course) {
00209     global $PAGE, $OUTPUT, $CFG;
00210 
00211     $frame = optional_param('frameset', 'main', PARAM_ALPHA);
00212 
00213     if ($frame === 'top') {
00214         $PAGE->set_pagelayout('frametop');
00215         url_print_header($url, $cm, $course);
00216         url_print_heading($url, $cm, $course);
00217         url_print_intro($url, $cm, $course);
00218         echo $OUTPUT->footer();
00219         die;
00220 
00221     } else {
00222         $config = get_config('url');
00223         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00224         $exteurl = url_get_full_url($url, $cm, $course, $config);
00225         $navurl = "$CFG->wwwroot/mod/url/view.php?id=$cm->id&amp;frameset=top";
00226         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00227         $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
00228         $title = strip_tags($courseshortname.': '.format_string($url->name));
00229         $framesize = $config->framesize;
00230         $modulename = s(get_string('modulename','url'));
00231         $dir = get_string('thisdirection', 'langconfig');
00232 
00233         $extframe = <<<EOF
00234 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
00235 <html dir="$dir">
00236   <head>
00237     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
00238     <title>$title</title>
00239   </head>
00240   <frameset rows="$framesize,*">
00241     <frame src="$navurl" title="$modulename"/>
00242     <frame src="$exteurl" title="$modulename"/>
00243   </frameset>
00244 </html>
00245 EOF;
00246 
00247         @header('Content-Type: text/html; charset=utf-8');
00248         echo $extframe;
00249         die;
00250     }
00251 }
00252 
00260 function url_print_workaround($url, $cm, $course) {
00261     global $OUTPUT;
00262 
00263     url_print_header($url, $cm, $course);
00264     url_print_heading($url, $cm, $course, true);
00265     url_print_intro($url, $cm, $course, true);
00266 
00267     $fullurl = url_get_full_url($url, $cm, $course);
00268 
00269     $display = url_get_final_display_type($url);
00270     if ($display == RESOURCELIB_DISPLAY_POPUP) {
00271         $jsfullurl = addslashes_js($fullurl);
00272         $options = empty($url->displayoptions) ? array() : unserialize($url->displayoptions);
00273         $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
00274         $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
00275         $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
00276         $extra = "onclick=\"window.open('$jsfullurl', '', '$wh'); return false;\"";
00277 
00278     } else if ($display == RESOURCELIB_DISPLAY_NEW) {
00279         $extra = "onclick=\"this.target='_blank';\"";
00280 
00281     } else {
00282         $extra = '';
00283     }
00284 
00285     echo '<div class="urlworkaround">';
00286     print_string('clicktoopen', 'url', "<a href=\"$fullurl\" $extra>$fullurl</a>");
00287     echo '</div>';
00288 
00289     echo $OUTPUT->footer();
00290     die;
00291 }
00292 
00300 function url_display_embed($url, $cm, $course) {
00301     global $CFG, $PAGE, $OUTPUT;
00302 
00303     $mimetype = resourcelib_guess_url_mimetype($url->externalurl);
00304     $fullurl  = url_get_full_url($url, $cm, $course);
00305     $title    = $url->name;
00306 
00307     $link = html_writer::tag('a', $fullurl, array('href'=>str_replace('&amp;', '&', $fullurl)));
00308     $clicktoopen = get_string('clicktoopen', 'url', $link);
00309 
00310     $extension = resourcelib_get_extension($url->externalurl);
00311 
00312     if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) {  // It's an image
00313         $code = resourcelib_embed_image($fullurl, $title);
00314 
00315     } else if ($mimetype == 'audio/mp3') {
00316         // MP3 audio file
00317         $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen);
00318 
00319     } else if ($mimetype == 'video/x-flv' or $extension === 'f4v') {
00320         // Flash video file
00321         $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen);
00322 
00323     } else if ($mimetype == 'application/x-shockwave-flash') {
00324         // Flash file
00325         $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen);
00326 
00327     } else if (substr($mimetype, 0, 10) == 'video/x-ms') {
00328         // Windows Media Player file
00329         $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen);
00330 
00331     } else if ($mimetype == 'video/quicktime') {
00332         // Quicktime file
00333         $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen);
00334 
00335     } else if ($mimetype == 'video/mpeg') {
00336         // Mpeg file
00337         $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen);
00338 
00339     } else if ($mimetype == 'audio/x-pn-realaudio-plugin') {
00340         // RealMedia file
00341         $code = resourcelib_embed_real($fullurl, $title, $clicktoopen);
00342 
00343     } else {
00344         // anything else - just try object tag enlarged as much as possible
00345         $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
00346     }
00347 
00348     url_print_header($url, $cm, $course);
00349     url_print_heading($url, $cm, $course);
00350 
00351     echo $code;
00352 
00353     url_print_intro($url, $cm, $course);
00354 
00355     echo $OUTPUT->footer();
00356     die;
00357 }
00358 
00364 function url_get_final_display_type($url) {
00365     global $CFG;
00366 
00367     if ($url->display != RESOURCELIB_DISPLAY_AUTO) {
00368         return $url->display;
00369     }
00370 
00371     // detect links to local moodle pages
00372     if (strpos($url->externalurl, $CFG->wwwroot) === 0) {
00373         if (strpos($url->externalurl, 'file.php') === false and strpos($url->externalurl, '.php') !== false ) {
00374             // most probably our moodle page with navigation
00375             return RESOURCELIB_DISPLAY_OPEN;
00376         }
00377     }
00378 
00379     static $download = array('application/zip', 'application/x-tar', 'application/g-zip',     // binary formats
00380                              'application/pdf', 'text/html');  // these are known to cause trouble for external links, sorry
00381     static $embed    = array('image/gif', 'image/jpeg', 'image/png', 'image/svg+xml',         // images
00382                              'application/x-shockwave-flash', 'video/x-flv', 'video/x-ms-wm', // video formats
00383                              'video/quicktime', 'video/mpeg', 'video/mp4',
00384                              'audio/mp3', 'audio/x-realaudio-plugin', 'x-realaudio-plugin',   // audio formats,
00385                             );
00386 
00387     $mimetype = resourcelib_guess_url_mimetype($url->externalurl);
00388 
00389     if (in_array($mimetype, $download)) {
00390         return RESOURCELIB_DISPLAY_DOWNLOAD;
00391     }
00392     if (in_array($mimetype, $embed)) {
00393         return RESOURCELIB_DISPLAY_EMBED;
00394     }
00395 
00396     // let the browser deal with it somehow
00397     return RESOURCELIB_DISPLAY_OPEN;
00398 }
00399 
00405 function url_get_variable_options($config) {
00406     global $CFG;
00407 
00408     $options = array();
00409     $options[''] = array('' => get_string('chooseavariable', 'url'));
00410 
00411     $options[get_string('course')] = array(
00412         'courseid'        => 'id',
00413         'coursefullname'  => get_string('fullnamecourse'),
00414         'courseshortname' => get_string('shortnamecourse'),
00415         'courseidnumber'  => get_string('idnumbercourse'),
00416         'coursesummary'   => get_string('summary'),
00417         'courseformat'    => get_string('format'),
00418     );
00419 
00420     $options[get_string('modulename', 'url')] = array(
00421         'urlinstance'     => 'id',
00422         'urlcmid'         => 'cmid',
00423         'urlname'         => get_string('name'),
00424         'urlidnumber'     => get_string('idnumbermod'),
00425     );
00426 
00427     $options[get_string('miscellaneous')] = array(
00428         'sitename'        => get_string('fullsitename'),
00429         'serverurl'       => get_string('serverurl', 'url'),
00430         'currenttime'     => get_string('time'),
00431         'lang'            => get_string('language'),
00432     );
00433     if (!empty($config->secretphrase)) {
00434         $options[get_string('miscellaneous')]['encryptedcode'] = get_string('encryptedcode');
00435     }
00436 
00437     $options[get_string('user')] = array(
00438         'userid'          => 'id',
00439         'userusername'    => get_string('username'),
00440         'useridnumber'    => get_string('idnumber'),
00441         'userfirstname'   => get_string('firstname'),
00442         'userlastname'    => get_string('lastname'),
00443         'userfullname'    => get_string('fullnameuser'),
00444         'useremail'       => get_string('email'),
00445         'usericq'         => get_string('icqnumber'),
00446         'userphone1'      => get_string('phone').' 1',
00447         'userphone2'      => get_string('phone2').' 2',
00448         'userinstitution' => get_string('institution'),
00449         'userdepartment'  => get_string('department'),
00450         'useraddress'     => get_string('address'),
00451         'usercity'        => get_string('city'),
00452         'usertimezone'    => get_string('timezone'),
00453         'userurl'         => get_string('webpage'),
00454     );
00455 
00456     if ($config->rolesinparams) {
00457         $roles = get_all_roles();
00458         $roleoptions = array();
00459         foreach ($roles as $role) {
00460             $roleoptions['course'.$role->shortname] = get_string('yourwordforx', '', $role->name);
00461         }
00462         $options[get_string('roles')] = $roleoptions;
00463     }
00464 
00465     return $options;
00466 }
00467 
00476 function url_get_variable_values($url, $cm, $course, $config) {
00477     global $USER, $CFG;
00478 
00479     $site = get_site();
00480 
00481     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00482 
00483     $values = array (
00484         'courseid'        => $course->id,
00485         'coursefullname'  => format_string($course->fullname),
00486         'courseshortname' => format_string($course->shortname, true, array('context' => $coursecontext)),
00487         'courseidnumber'  => $course->idnumber,
00488         'coursesummary'   => $course->summary,
00489         'courseformat'    => $course->format,
00490         'lang'            => current_language(),
00491         'sitename'        => format_string($site->fullname),
00492         'serverurl'       => $CFG->wwwroot,
00493         'currenttime'     => time(),
00494         'urlinstance'     => $url->id,
00495         'urlcmid'         => $cm->id,
00496         'urlname'         => format_string($url->name),
00497         'urlidnumber'     => $cm->idnumber,
00498     );
00499 
00500     if (isloggedin()) {
00501         $values['userid']          = $USER->id;
00502         $values['userusername']    = $USER->username;
00503         $values['useridnumber']    = $USER->idnumber;
00504         $values['userfirstname']   = $USER->firstname;
00505         $values['userlastname']    = $USER->lastname;
00506         $values['userfullname']    = fullname($USER);
00507         $values['useremail']       = $USER->email;
00508         $values['usericq']         = $USER->icq;
00509         $values['userphone1']      = $USER->phone1;
00510         $values['userphone2']      = $USER->phone2;
00511         $values['userinstitution'] = $USER->institution;
00512         $values['userdepartment']  = $USER->department;
00513         $values['useraddress']     = $USER->address;
00514         $values['usercity']        = $USER->city;
00515         $values['usertimezone']    = get_user_timezone_offset();
00516         $values['userurl']         = $USER->url;
00517     }
00518 
00519     // weak imitation of Single-Sign-On, for backwards compatibility only
00520     // NOTE: login hack is not included in 2.0 any more, new contrib auth plugin
00521     //       needs to be createed if somebody needs the old functionality!
00522     if (!empty($config->secretphrase)) {
00523         $values['encryptedcode'] = url_get_encrypted_parameter($url, $config);
00524     }
00525 
00526     //hmm, this is pretty fragile and slow, why do we need it here??
00527     if ($config->rolesinparams) {
00528         $roles = get_all_roles();
00529         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00530         $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS);
00531         foreach ($roles as $role) {
00532             $values['course'.$role->shortname] = $role->localname;
00533         }
00534     }
00535 
00536     return $values;
00537 }
00538 
00545 function url_get_encrypted_parameter($url, $config) {
00546     global $CFG;
00547 
00548     if (file_exists("$CFG->dirroot/local/externserverfile.php")) {
00549         require_once("$CFG->dirroot/local/externserverfile.php");
00550         if (function_exists('extern_server_file')) {
00551             return extern_server_file($url, $config);
00552         }
00553     }
00554     return md5(getremoteaddr().$config->secretphrase);
00555 }
00556 
00562 function url_guess_icon($fullurl) {
00563     global $CFG;
00564     require_once("$CFG->libdir/filelib.php");
00565 
00566     if (substr_count($fullurl, '/') < 3 or substr($fullurl, -1) === '/') {
00567         // most probably default directory - index.php, index.html, etc.
00568         return 'f/web';
00569     }
00570 
00571     $icon = mimeinfo('icon', $fullurl);
00572     $icon = 'f/'.str_replace(array('.gif', '.png'), '', $icon);
00573 
00574     if ($icon === 'f/html' or $icon === 'f/unknown') {
00575         $icon = 'f/web';
00576     }
00577 
00578     return $icon;
00579 }
 All Data Structures Namespaces Files Functions Variables Enumerations