Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/resourcelib.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 
00030 define('RESOURCELIB_DISPLAY_AUTO', 0);
00032 define('RESOURCELIB_DISPLAY_EMBED', 1);
00034 define('RESOURCELIB_DISPLAY_FRAME', 2);
00036 define('RESOURCELIB_DISPLAY_NEW', 3);
00038 define('RESOURCELIB_DISPLAY_DOWNLOAD', 4);
00040 define('RESOURCELIB_DISPLAY_OPEN', 5);
00042 define('RESOURCELIB_DISPLAY_POPUP', 6);
00043 
00045 define('RESOURCELIB_LEGACYFILES_NO', 0);
00047 define('RESOURCELIB_LEGACYFILES_DONE', 1);
00049 define('RESOURCELIB_LEGACYFILES_ACTIVE', 2);
00050 
00051 
00062 function resourcelib_try_file_migration($filepath, $cmid, $courseid, $component, $filearea, $itemid) {
00063     $fs = get_file_storage();
00064 
00065     if (stripos($filepath, '/backupdata/') === 0 or stripos($filepath, '/moddata/') === 0) {
00066         // do not steal protected files!
00067         return false;
00068     }
00069 
00070     if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
00071         return false;
00072     }
00073     if (!$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid)) {
00074         return false;
00075     }
00076 
00077     $fullpath = rtrim("/$coursecontext->id/course/legacy/0".$filepath, '/');
00078     do {
00079         if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
00080             if ($file = $fs->get_file_by_hash(sha1("$fullpath/.")) and $file->is_directory()) {
00081                 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
00082                     break;
00083                 }
00084                 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
00085                     break;
00086                 }
00087                 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
00088                     break;
00089                 }
00090             }
00091             return false;
00092         }
00093     } while (false);
00094 
00095     // copy and keep the same path, name, etc.
00096     $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
00097     try {
00098         return $fs->create_file_from_storedfile($file_record, $file);
00099     } catch (Exception $e) {
00100         // file may exist - highly unlikely, we do not want upgrades to stop here
00101         return false;
00102     }
00103 }
00104 
00111 function resourcelib_get_displayoptions(array $enabled, $current=null) {
00112     if (is_number($current)) {
00113         $enabled[] = $current;
00114     }
00115 
00116     $options = array(RESOURCELIB_DISPLAY_AUTO     => get_string('resourcedisplayauto'),
00117                      RESOURCELIB_DISPLAY_EMBED    => get_string('resourcedisplayembed'),
00118                      RESOURCELIB_DISPLAY_FRAME    => get_string('resourcedisplayframe'),
00119                      RESOURCELIB_DISPLAY_NEW      => get_string('resourcedisplaynew'),
00120                      RESOURCELIB_DISPLAY_DOWNLOAD => get_string('resourcedisplaydownload'),
00121                      RESOURCELIB_DISPLAY_OPEN     => get_string('resourcedisplayopen'),
00122                      RESOURCELIB_DISPLAY_POPUP    => get_string('resourcedisplaypopup'));
00123 
00124     $result = array();
00125 
00126     foreach ($options as $key=>$value) {
00127         if (in_array($key, $enabled)) {
00128             $result[$key] = $value;
00129         }
00130     }
00131 
00132     if (empty($result)) {
00133         // there should be always something in case admin misconfigures module
00134         $result[RESOURCELIB_DISPLAY_OPEN] = $options[RESOURCELIB_DISPLAY_OPEN];
00135     }
00136 
00137     return $result;
00138 }
00139 
00145 function resourcelib_guess_url_mimetype($fullurl) {
00146     global $CFG;
00147     require_once("$CFG->libdir/filelib.php");
00148 
00149     if ($fullurl instanceof moodle_url) {
00150         $fullurl = $fullurl->out(false);
00151     }
00152 
00153     $matches = null;
00154     if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/[^&\?#]*)|", $fullurl, $matches)) {
00155         // remove the special moodle file serving hacks so that the *file.php is ignored
00156         $fullurl = $matches[1].$matches[3];
00157     }
00158 
00159     if (preg_match("|^(.*)#.*|", $fullurl, $matches)) {
00160         // ignore all anchors
00161         $fullurl = $matches[1];
00162     }
00163 
00164     if (strpos($fullurl, '.php')){
00165         // we do not really know what is in general php script
00166         return 'text/html';
00167 
00168     } else if (substr($fullurl, -1) === '/') {
00169         // directory index (http://example.com/smaples/)
00170         return 'text/html';
00171 
00172     } else if (strpos($fullurl, '//') !== false and substr_count($fullurl, '/') == 2) {
00173         // just a host name (http://example.com), solves Australian servers "audio" problem too
00174         return 'text/html';
00175 
00176     } else {
00177         // ok, this finally looks like a real file
00178         $parts = explode('?', $fullurl);
00179         $url = reset($parts);
00180         return mimeinfo('type', $url);
00181     }
00182 }
00183 
00190 function resourcelib_get_extension($fullurl) {
00191 
00192     if ($fullurl instanceof moodle_url) {
00193         $fullurl = $fullurl->out(false);
00194     }
00195 
00196     $matches = null;
00197     if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/.*)|", $fullurl, $matches)) {
00198         // remove the special moodle file serving hacks so that the *file.php is ignored
00199         $fullurl = $matches[1].$matches[3];
00200     }
00201 
00202     $matches = null;
00203     if (preg_match('/^[^#\?]+\.([a-z0-9]+)([#\?].*)?$/i', $fullurl, $matches)) {
00204         return strtolower($matches[1]);
00205     }
00206 
00207     return '';
00208 }
00209 
00216 function resourcelib_embed_image($fullurl, $title) {
00217     $code = '';
00218     $code .= '<div class="resourcecontent resourceimg">';
00219     $code .= "<img title=\"".strip_tags(format_string($title))."\" class=\"resourceimage\" src=\"$fullurl\" alt=\"\" />";
00220     $code .= '</div>';
00221 
00222     return $code;
00223 }
00224 
00232 function resourcelib_embed_mp3($fullurl, $title, $clicktoopen) {
00233 
00234     if ($fullurl instanceof moodle_url) {
00235         $fullurl = $fullurl->out(false);
00236     } else {
00237         $fullurl = str_replace('&amp;', '&', $fullurl);
00238     }
00239 
00240     $id = 'resource_mp3_'.time(); //we need something unique because it might be stored in text cache
00241 
00242     // note: size is specified in theme, it can be made as wide as necessary, but the height can not be changed
00243 
00244     $output = '<div class="resourcecontent resourcemp3">';
00245     $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_mp3', 'title'=>$title));
00246     $output .= html_writer::script(js_writer::function_call('M.util.add_audio_player', array($id, $fullurl, false)));
00247     $output .= '</div>';
00248 
00249     return $output;
00250 }
00251 
00259 function resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen) {
00260     global $CFG, $PAGE;
00261 
00262     if ($fullurl instanceof moodle_url) {
00263         $fullurl = $fullurl->out(false);
00264     } else {
00265         $fullurl = str_replace('&amp;', '&', $fullurl);
00266     }
00267 
00268     $id = 'resource_flv_'.time(); //we need something unique because it might be stored in text cache
00269 
00270     //note: nobody should be adding any dimensions to themes!!!
00271 
00272     if (preg_match('/\?d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) {
00273         $width    = $matches[1];
00274         $height   = $matches[2];
00275         $autosize = false;
00276     } else {
00277         $width    = 400;
00278         $height   = 300;
00279         $autosize = true;
00280     }
00281     $output = '<div class="resourcecontent resourceflv">';
00282     $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_flv', 'title'=>$title));
00283     $output .= html_writer::script(js_writer::function_call('M.util.add_video_player', array($id, rawurlencode($fullurl), $width, $height, $autosize)));
00284     $output .= '</div>';
00285 
00286     return $output;
00287 }
00288 
00296 function resourcelib_embed_flash($fullurl, $title, $clicktoopen) {
00297     if (preg_match('/[#\?]d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) {
00298         $width    = $matches[1];
00299         $height   = $matches[2];
00300     } else {
00301         $width    = 400;
00302         $height   = 300;
00303     }
00304 
00305     $code = <<<EOT
00306 <div class="resourcecontent resourceswf">
00307   <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="$width" height="$height">
00308     <param name="movie" value="$fullurl" />
00309     <param name="autoplay" value="true" />
00310     <param name="loop" value="true" />
00311     <param name="controller" value="true" />
00312     <param name="scale" value="aspect" />
00313     <param name="base" value="." />
00314 <!--[if !IE]>-->
00315     <object type="application/x-shockwave-flash" data="$fullurl" width="$width" height="$height">
00316       <param name="controller" value="true" />
00317       <param name="autoplay" value="true" />
00318       <param name="loop" value="true" />
00319       <param name="scale" value="aspect" />
00320       <param name="base" value="." />
00321 <!--<![endif]-->
00322 $clicktoopen
00323 <!--[if !IE]>-->
00324     </object>
00325 <!--<![endif]-->
00326   </object>
00327 </div>
00328 EOT;
00329 
00330     return $code;
00331 }
00332 
00340 function resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen) {
00341     $code = <<<EOT
00342 <div class="resourcecontent resourcewmv">
00343   <object type="video/x-ms-wmv" data="$fullurl">
00344     <param name="controller" value="true" />
00345     <param name="autostart" value="true" />
00346     <param name="src" value="$fullurl" />
00347     <param name="scale" value="noScale" />
00348     $clicktoopen
00349   </object>
00350 </div>
00351 EOT;
00352 
00353     return $code;
00354 }
00355 
00363 function resourcelib_embed_quicktime($fullurl, $title, $clicktoopen) {
00364     $code = <<<EOT
00365 <div class="resourcecontent resourceqt">
00366   <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
00367     <param name="src" value="$fullurl" />
00368     <param name="autoplay" value="true" />
00369     <param name="loop" value="true" />
00370     <param name="controller" value="true" />
00371     <param name="scale" value="aspect" />
00372 <!--[if !IE]>-->
00373     <object type="video/quicktime" data="$fullurl">
00374       <param name="controller" value="true" />
00375       <param name="autoplay" value="true" />
00376       <param name="loop" value="true" />
00377       <param name="scale" value="aspect" />
00378 <!--<![endif]-->
00379 $clicktoopen
00380 <!--[if !IE]>-->
00381     </object>
00382 <!--<![endif]-->
00383   </object>
00384 </div>
00385 EOT;
00386 
00387     return $code;
00388 }
00389 
00397 function resourcelib_embed_mpeg($fullurl, $title, $clicktoopen) {
00398     $code = <<<EOT
00399 <div class="resourcecontent resourcempeg">
00400   <object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsm p2inf.cab#Version=5,1,52,701" type="application/x-oleobject">
00401     <param name="fileName" value="$fullurl" />
00402     <param name="autoStart" value="true" />
00403     <param name="animationatStart" value="true" />
00404     <param name="transparentatStart" value="true" />
00405     <param name="showControls" value="true" />
00406     <param name="Volume" value="-450" />
00407 <!--[if !IE]>-->
00408     <object type="video/mpeg" data="$fullurl">
00409       <param name="controller" value="true" />
00410       <param name="autostart" value="true" />
00411       <param name="src" value="$fullurl" />
00412 <!--<![endif]-->
00413 $clicktoopen
00414 <!--[if !IE]>-->
00415     </object>
00416 <!--<![endif]-->
00417   </object>
00418 </div>
00419 EOT;
00420 
00421     return $code;
00422 }
00423 
00431 function resourcelib_embed_real($fullurl, $title, $clicktoopen) {
00432     $code = <<<EOT
00433 <div class="resourcecontent resourcerm">
00434   <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" data="$fullurl" width="320" height="240">
00435     <param name="src" value="$fullurl" />
00436     <param name="controls" value="All" />
00437 <!--[if !IE]>-->
00438     <object type="audio/x-pn-realaudio-plugin" data="$fullurl" width="320" height="240">
00439     <param name="src" value="$fullurl" />
00440       <param name="controls" value="All" />
00441 <!--<![endif]-->
00442 $clicktoopen
00443 <!--[if !IE]>-->
00444     </object>
00445 <!--<![endif]-->
00446   </object>
00447 </div>
00448 EOT;
00449 
00450     return $code;
00451 }
00452 
00460 function resourcelib_embed_pdf($fullurl, $title, $clicktoopen) {
00461     global $CFG, $PAGE;
00462 
00463     $code = <<<EOT
00464 <div class="resourcecontent resourcepdf">
00465   <object id="resourceobject" data="$fullurl" type="application/pdf" width="800" height="600">
00466     <param name="src" value="$fullurl" />
00467     $clicktoopen
00468   </object>
00469 </div>
00470 EOT;
00471 
00472     // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
00473     $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
00474 
00475     return $code;
00476 }
00477 
00478 
00487 function resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype) {
00488     global $CFG, $PAGE;
00489 
00490     if ($fullurl instanceof moodle_url) {
00491         $fullurl = $fullurl->out();
00492     }
00493 
00494     $iframe = false;
00495 
00496     $param = '<param name="src" value="'.$fullurl.'" />';
00497 
00498     // IE can not embed stuff properly, that is why we use iframe instead.
00499     // Unfortunately this tag does not validate in xhtml strict mode,
00500     // but in any case it is undeprecated in HTML 5 - we will use it everywhere soon!
00501     if ($mimetype === 'text/html' and check_browser_version('MSIE', 5)) {
00502         $iframe = true;
00503     }
00504 
00505     if ($iframe) {
00506         $code = <<<EOT
00507 <div class="resourcecontent resourcegeneral">
00508   <iframe id="resourceobject" src="$fullurl">
00509     $clicktoopen
00510   </iframe>
00511 </div>
00512 EOT;
00513     } else {
00514         $code = <<<EOT
00515 <div class="resourcecontent resourcegeneral">
00516   <object id="resourceobject" data="$fullurl" type="$mimetype"  width="800" height="600">
00517     $param
00518     $clicktoopen
00519   </object>
00520 </div>
00521 EOT;
00522     }
00523 
00524     // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
00525     $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
00526 
00527     return $code;
00528 }
 All Data Structures Namespaces Files Functions Variables Enumerations