2005-02-24 02:36:45 +00:00
|
|
|
<?php //$Id$
|
|
|
|
|
2007-01-02 22:44:59 +00:00
|
|
|
require_once($CFG->libdir.'/libcurlemu/libcurlemu.inc.php'); // might be moved to setup.php later
|
|
|
|
|
2006-01-16 22:21:41 +00:00
|
|
|
define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant
|
|
|
|
|
2007-01-02 22:44:59 +00:00
|
|
|
/**
|
|
|
|
* Fetches content of file from Internet (using proxy if defined).
|
|
|
|
*
|
|
|
|
* @return mixed false if request failed or content of the file as string if ok.
|
|
|
|
*/
|
|
|
|
function download_file_content($url) {
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
|
|
if (!empty($CFG->proxyhost)) {
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
|
|
|
|
if (empty($CFG->proxyport)) {
|
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxy);
|
|
|
|
} else {
|
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxy.':'.$CFG->proxyport);
|
|
|
|
}
|
|
|
|
if(!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
|
|
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2006-03-09 12:07:11 +00:00
|
|
|
/**
|
2007-08-31 13:54:13 +00:00
|
|
|
* @return List of information about file types based on extensions.
|
2006-03-09 12:07:11 +00:00
|
|
|
* Associative array of extension (lower-case) to associative array
|
|
|
|
* from 'element name' to data. Current element names are 'type' and 'icon'.
|
2007-08-31 13:54:13 +00:00
|
|
|
* Unknown types should use the 'xxx' entry which includes defaults.
|
2006-03-09 12:07:11 +00:00
|
|
|
*/
|
|
|
|
function get_mimetypes_array() {
|
|
|
|
return array (
|
2005-03-07 11:42:52 +00:00
|
|
|
'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown.gif'),
|
|
|
|
'3gp' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
|
|
|
|
'ai' => array ('type'=>'application/postscript', 'icon'=>'image.gif'),
|
|
|
|
'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'),
|
|
|
|
'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'),
|
|
|
|
'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'),
|
|
|
|
'applescript' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
|
|
|
'asc' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-04-21 12:40:45 +00:00
|
|
|
'asm' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'au' => array ('type'=>'audio/au', 'icon'=>'audio.gif'),
|
|
|
|
'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi.gif'),
|
|
|
|
'bmp' => array ('type'=>'image/bmp', 'icon'=>'image.gif'),
|
2005-04-21 12:40:45 +00:00
|
|
|
'c' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash.gif'),
|
2005-04-21 12:40:45 +00:00
|
|
|
'cpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'cs' => array ('type'=>'application/x-csh', 'icon'=>'text.gif'),
|
2007-08-31 13:54:13 +00:00
|
|
|
'css' => array ('type'=>'text/css', 'icon'=>'text.gif'),
|
2007-06-11 15:36:08 +00:00
|
|
|
'csv' => array ('type'=>'text/csv', 'icon'=>'excel.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'dv' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'),
|
2005-08-04 18:31:59 +00:00
|
|
|
'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'dmg.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'doc' => array ('type'=>'application/msword', 'icon'=>'word.gif'),
|
2007-08-23 06:50:37 +00:00
|
|
|
'docx' => array ('type'=>'application/msword', 'icon'=>'docx.gif'),
|
|
|
|
'docm' => array ('type'=>'application/msword', 'icon'=>'docm.gif'),
|
|
|
|
'dotx' => array ('type'=>'application/msword', 'icon'=>'dotx.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
|
|
|
|
'dif' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'),
|
|
|
|
'dir' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
|
|
|
|
'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
|
|
|
|
'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'),
|
2007-01-06 15:22:23 +00:00
|
|
|
'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
|
2007-07-19 05:43:33 +00:00
|
|
|
'flv' => array ('type'=>'video/x-flv', 'icon'=>'video.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'gif' => array ('type'=>'image/gif', 'icon'=>'image.gif'),
|
|
|
|
'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip.gif'),
|
2007-07-19 05:43:33 +00:00
|
|
|
'tgz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
|
|
|
|
'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
|
|
|
|
'h' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-04-21 12:40:45 +00:00
|
|
|
'hpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip.gif'),
|
2006-12-29 20:57:48 +00:00
|
|
|
'htc' => array ('type'=>'text/x-component', 'icon'=>'text.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'html' => array ('type'=>'text/html', 'icon'=>'html.gif'),
|
2007-04-17 22:14:15 +00:00
|
|
|
'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'htm' => array ('type'=>'text/html', 'icon'=>'html.gif'),
|
2007-05-03 07:15:30 +00:00
|
|
|
'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image.gif'),
|
|
|
|
'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf.gif'),
|
|
|
|
'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf.gif'),
|
2005-04-21 12:40:45 +00:00
|
|
|
'java' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
2005-04-25 14:09:19 +00:00
|
|
|
'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb.gif'),
|
|
|
|
'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl.gif'),
|
|
|
|
'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw.gif'),
|
|
|
|
'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt.gif'),
|
|
|
|
'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
|
|
|
|
'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
|
|
|
|
'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
|
2005-04-25 14:09:19 +00:00
|
|
|
'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'js' => array ('type'=>'application/x-javascript', 'icon'=>'text.gif'),
|
|
|
|
'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text.gif'),
|
|
|
|
'm' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
|
|
|
'mov' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
|
|
|
|
'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video.gif'),
|
|
|
|
'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio.gif'),
|
|
|
|
'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio.gif'),
|
|
|
|
'mp4' => array ('type'=>'video/mp4', 'icon'=>'video.gif'),
|
|
|
|
'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
|
|
|
|
'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
|
|
|
|
'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
|
2005-05-04 15:19:00 +00:00
|
|
|
|
|
|
|
'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt.gif'),
|
|
|
|
'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt.gif'),
|
|
|
|
'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt.gif'),
|
2007-01-20 15:00:17 +00:00
|
|
|
'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'odm.gif'),
|
|
|
|
'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'odg.gif'),
|
|
|
|
'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'odg.gif'),
|
|
|
|
'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'odp.gif'),
|
|
|
|
'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'odp.gif'),
|
|
|
|
'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'ods.gif'),
|
|
|
|
'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'ods.gif'),
|
|
|
|
'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'odc.gif'),
|
|
|
|
'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'odf.gif'),
|
|
|
|
'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'odb.gif'),
|
|
|
|
'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'odi.gif'),
|
2005-05-04 15:19:00 +00:00
|
|
|
|
2005-03-07 11:42:52 +00:00
|
|
|
'pct' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
|
|
|
|
'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
|
|
|
|
'php' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
|
|
|
'pic' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
|
|
|
|
'pict' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
|
|
|
|
'png' => array ('type'=>'image/png', 'icon'=>'image.gif'),
|
|
|
|
'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'),
|
|
|
|
'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'),
|
2007-08-23 06:50:37 +00:00
|
|
|
'pptx' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'pptx.gif'),
|
|
|
|
'pptm' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'pptm.gif'),
|
|
|
|
'potx' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'potx.gif'),
|
|
|
|
'potm' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'potm.gif'),
|
|
|
|
'ppam' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'ppam.gif'),
|
|
|
|
'ppsx' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'ppsx.gif'),
|
|
|
|
'ppsm' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'ppsm.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'),
|
|
|
|
'qt' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
|
|
|
|
'ra' => array ('type'=>'audio/x-realaudio', 'icon'=>'audio.gif'),
|
|
|
|
'ram' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'),
|
2005-04-25 14:09:19 +00:00
|
|
|
'rhb' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'rm' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'),
|
|
|
|
'rtf' => array ('type'=>'text/rtf', 'icon'=>'text.gif'),
|
|
|
|
'rtx' => array ('type'=>'text/richtext', 'icon'=>'text.gif'),
|
|
|
|
'sh' => array ('type'=>'application/x-sh', 'icon'=>'text.gif'),
|
|
|
|
'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'zip.gif'),
|
|
|
|
'smi' => array ('type'=>'application/smil', 'icon'=>'text.gif'),
|
|
|
|
'smil' => array ('type'=>'application/smil', 'icon'=>'text.gif'),
|
2005-04-25 14:09:19 +00:00
|
|
|
'sqt' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
|
2006-10-08 08:13:41 +00:00
|
|
|
'svg' => array ('type'=>'image/svg+xml', 'icon'=>'image.gif'),
|
|
|
|
'svgz' => array ('type'=>'image/svg+xml', 'icon'=>'image.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'swa' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
|
|
|
|
'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'),
|
|
|
|
'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'),
|
2005-05-04 15:19:00 +00:00
|
|
|
|
|
|
|
'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'odt.gif'),
|
|
|
|
'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'odt.gif'),
|
|
|
|
'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'odt.gif'),
|
|
|
|
'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'odt.gif'),
|
|
|
|
'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'odt.gif'),
|
|
|
|
'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'odt.gif'),
|
|
|
|
'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'odt.gif'),
|
|
|
|
'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'odt.gif'),
|
|
|
|
'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'odt.gif'),
|
|
|
|
'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'odt.gif'),
|
|
|
|
|
2005-03-07 11:42:52 +00:00
|
|
|
'tar' => array ('type'=>'application/x-tar', 'icon'=>'zip.gif'),
|
|
|
|
'tif' => array ('type'=>'image/tiff', 'icon'=>'image.gif'),
|
|
|
|
'tiff' => array ('type'=>'image/tiff', 'icon'=>'image.gif'),
|
|
|
|
'tex' => array ('type'=>'application/x-tex', 'icon'=>'text.gif'),
|
|
|
|
'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'),
|
|
|
|
'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'),
|
|
|
|
'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text.gif'),
|
|
|
|
'txt' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
|
|
|
|
'wav' => array ('type'=>'audio/wav', 'icon'=>'audio.gif'),
|
|
|
|
'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'avi.gif'),
|
|
|
|
'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'avi.gif'),
|
2007-01-06 15:22:23 +00:00
|
|
|
'xdp' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
|
|
|
|
'xfd' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
|
|
|
|
'xfdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'excel.gif'),
|
2007-08-23 06:50:37 +00:00
|
|
|
'xlsx' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlsx.gif'),
|
|
|
|
'xlsm' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlsm.gif'),
|
|
|
|
'xltx' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xltx.gif'),
|
|
|
|
'xltm' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xltm.gif'),
|
|
|
|
'xlsb' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlsb.gif'),
|
|
|
|
'xlam' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlam.gif'),
|
2005-03-07 11:42:52 +00:00
|
|
|
'xml' => array ('type'=>'application/xml', 'icon'=>'xml.gif'),
|
|
|
|
'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
|
|
|
|
'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif')
|
2005-03-07 11:32:03 +00:00
|
|
|
);
|
2006-03-09 12:07:11 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 13:54:13 +00:00
|
|
|
/**
|
2006-03-09 12:07:11 +00:00
|
|
|
* Obtains information about a filetype based on its extension. Will
|
|
|
|
* use a default if no information is present about that particular
|
|
|
|
* extension.
|
2007-08-31 13:54:13 +00:00
|
|
|
* @param string $element Desired information (usually 'icon'
|
2006-03-09 12:07:11 +00:00
|
|
|
* for icon filename or 'type' for MIME type)
|
2007-08-31 13:54:13 +00:00
|
|
|
* @param string $filename Filename we're looking up
|
2006-03-09 12:07:11 +00:00
|
|
|
* @return string Requested piece of information from array
|
|
|
|
*/
|
|
|
|
function mimeinfo($element, $filename) {
|
|
|
|
static $mimeinfo;
|
|
|
|
$mimeinfo=get_mimetypes_array();
|
2005-03-07 11:32:03 +00:00
|
|
|
|
2005-03-07 11:42:52 +00:00
|
|
|
if (eregi('\.([a-z0-9]+)$', $filename, $match)) {
|
2005-03-07 11:32:03 +00:00
|
|
|
if (isset($mimeinfo[strtolower($match[1])][$element])) {
|
|
|
|
return $mimeinfo[strtolower($match[1])][$element];
|
|
|
|
} else {
|
2005-03-07 11:42:52 +00:00
|
|
|
return $mimeinfo['xxx'][$element]; // By default
|
2005-03-07 11:32:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-03-07 11:42:52 +00:00
|
|
|
return $mimeinfo['xxx'][$element]; // By default
|
2005-03-07 11:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-31 13:54:13 +00:00
|
|
|
/**
|
2006-03-09 12:07:11 +00:00
|
|
|
* Obtains information about a filetype based on the MIME type rather than
|
|
|
|
* the other way around.
|
|
|
|
* @param string $element Desired information (usually 'icon')
|
2007-08-31 13:54:13 +00:00
|
|
|
* @param string $mimetype MIME type we're looking up
|
2006-03-09 12:07:11 +00:00
|
|
|
* @return string Requested piece of information from array
|
|
|
|
*/
|
|
|
|
function mimeinfo_from_type($element, $mimetype) {
|
|
|
|
static $mimeinfo;
|
|
|
|
$mimeinfo=get_mimetypes_array();
|
2007-08-31 13:54:13 +00:00
|
|
|
|
2006-03-09 12:07:11 +00:00
|
|
|
foreach($mimeinfo as $values) {
|
|
|
|
if($values['type']==$mimetype) {
|
|
|
|
if(isset($values[$element])) {
|
|
|
|
return $values[$element];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $mimeinfo['xxx'][$element]; // Default
|
|
|
|
}
|
2006-02-21 12:54:24 +00:00
|
|
|
|
2007-09-25 14:59:16 +00:00
|
|
|
/**
|
|
|
|
* Get information about a filetype based on the icon file.
|
|
|
|
* @param string $element Desired information (usually 'icon')
|
|
|
|
* @param string $icon Icon file path.
|
|
|
|
* @return string Requested piece of information from array
|
|
|
|
*/
|
|
|
|
function mimeinfo_from_icon($element, $icon) {
|
|
|
|
static $mimeinfo;
|
|
|
|
$mimeinfo=get_mimetypes_array();
|
|
|
|
|
|
|
|
if (preg_match("/\/(.*)/", $icon, $matches)) {
|
|
|
|
$icon = $matches[1];
|
|
|
|
}
|
|
|
|
$info = $mimeinfo['xxx'][$element]; // Default
|
|
|
|
foreach($mimeinfo as $values) {
|
|
|
|
if($values['icon']==$icon) {
|
|
|
|
if(isset($values[$element])) {
|
|
|
|
$info = $values[$element];
|
|
|
|
}
|
|
|
|
//No break, for example for 'excel.gif' we don't want 'csv'!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2006-03-09 13:14:33 +00:00
|
|
|
/**
|
2007-08-31 13:54:13 +00:00
|
|
|
* Obtains descriptions for file types (e.g. 'Microsoft Word document') from the
|
|
|
|
* mimetypes.php language file.
|
2006-03-09 13:14:33 +00:00
|
|
|
* @param string $mimetype MIME type (can be obtained using the mimeinfo function)
|
|
|
|
* @param bool $capitalise If true, capitalises first character of result
|
2007-08-31 13:54:13 +00:00
|
|
|
* @return string Text description
|
2006-03-09 13:14:33 +00:00
|
|
|
*/
|
|
|
|
function get_mimetype_description($mimetype,$capitalise=false) {
|
|
|
|
$result=get_string($mimetype,'mimetypes');
|
|
|
|
// Surrounded by square brackets indicates that there isn't a string for that
|
|
|
|
// (maybe there is a better way to find this out?)
|
|
|
|
if(strpos($result,'[')===0) {
|
|
|
|
$result=get_string('document/unknown','mimetypes');
|
2007-08-31 13:54:13 +00:00
|
|
|
}
|
2006-03-09 13:14:33 +00:00
|
|
|
if($capitalise) {
|
|
|
|
$result=ucfirst($result);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-08-31 13:54:13 +00:00
|
|
|
/**
|
|
|
|
* Handles the sending of file data to the user's browser, including support for
|
|
|
|
* byteranges etc.
|
2006-07-05 11:10:37 +00:00
|
|
|
* @param string $path Path of file on disk (including real filename), or actual content of file as string
|
|
|
|
* @param string $filename Filename to send
|
|
|
|
* @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
|
|
|
|
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
|
|
|
|
* @param bool $pathisstring If true (default false), $path is the content to send and not the pathname
|
|
|
|
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
|
|
|
|
* @param string $mimetype Include to specify the MIME type; leave blank to have it guess the type from $filename
|
2006-02-21 12:54:24 +00:00
|
|
|
*/
|
2006-07-05 11:10:37 +00:00
|
|
|
function send_file($path, $filename, $lifetime=86400 , $filter=0, $pathisstring=false, $forcedownload=false, $mimetype='') {
|
2007-01-19 10:29:03 +00:00
|
|
|
global $CFG, $COURSE;
|
2005-03-07 11:32:03 +00:00
|
|
|
|
2006-09-20 17:51:59 +00:00
|
|
|
// Use given MIME type if specified, otherwise guess it using mimeinfo.
|
|
|
|
// IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O
|
|
|
|
// only Firefox saves all files locally before opening when content-disposition: attachment stated
|
|
|
|
$isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested
|
2007-08-31 13:54:13 +00:00
|
|
|
$mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' :
|
2006-09-20 17:51:59 +00:00
|
|
|
($mimetype ? $mimetype : mimeinfo('type', $filename));
|
2005-03-07 11:32:03 +00:00
|
|
|
$lastmodified = $pathisstring ? time() : filemtime($path);
|
|
|
|
$filesize = $pathisstring ? strlen($path) : filesize($path);
|
|
|
|
|
2007-01-06 15:22:23 +00:00
|
|
|
//Adobe Acrobat Reader XSS prevention
|
|
|
|
if ($mimetype=='application/pdf' or mimeinfo('type', $filename)=='application/pdf') {
|
|
|
|
//please note that it prevents opening of pdfs in browser when http referer disabled
|
|
|
|
//or file linked from another site; browser caching of pdfs is now disabled too
|
2007-04-04 19:45:17 +00:00
|
|
|
if (!empty($_SERVER['HTTP_RANGE'])) {
|
|
|
|
//already byteserving
|
2007-08-31 13:54:13 +00:00
|
|
|
$lifetime = 1; // >0 needed for byteserving
|
2007-04-04 19:45:17 +00:00
|
|
|
} else if (empty($_SERVER['HTTP_REFERER']) or strpos($_SERVER['HTTP_REFERER'], $CFG->wwwroot)!==0) {
|
2007-01-06 15:22:23 +00:00
|
|
|
$mimetype = 'application/x-forcedownload';
|
|
|
|
$forcedownload = true;
|
|
|
|
$lifetime = 0;
|
|
|
|
} else {
|
2007-08-31 13:54:13 +00:00
|
|
|
$lifetime = 1; // >0 needed for byteserving
|
2007-01-06 15:22:23 +00:00
|
|
|
}
|
2007-01-03 23:35:03 +00:00
|
|
|
}
|
2007-01-04 13:15:04 +13:00
|
|
|
|
2005-07-12 08:05:42 +00:00
|
|
|
//IE compatibiltiy HACK!
|
2006-01-16 22:21:41 +00:00
|
|
|
if (ini_get('zlib.output_compression')) {
|
2005-07-12 08:05:42 +00:00
|
|
|
ini_set('zlib.output_compression', 'Off');
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:21:41 +00:00
|
|
|
//try to disable automatic sid rewrite in cookieless mode
|
2006-01-16 22:23:52 +00:00
|
|
|
@ini_set("session.use_trans_sid", "false");
|
2006-01-16 22:21:41 +00:00
|
|
|
|
|
|
|
//do not put '@' before the next header to detect incorrect moodle configurations,
|
|
|
|
//error should be better than "weird" empty lines for admins/users
|
|
|
|
//TODO: should we remove all those @ before the header()? Are all of the values supported on all servers?
|
|
|
|
header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
|
|
|
|
|
|
|
|
if ($forcedownload) {
|
|
|
|
@header('Content-Disposition: attachment; filename='.$filename);
|
|
|
|
} else {
|
|
|
|
@header('Content-Disposition: inline; filename='.$filename);
|
|
|
|
}
|
|
|
|
|
2005-03-07 11:32:03 +00:00
|
|
|
if ($lifetime > 0) {
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Cache-Control: max-age='.$lifetime);
|
|
|
|
@header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
|
2005-03-07 11:32:03 +00:00
|
|
|
@header('Pragma: ');
|
2006-01-16 22:21:41 +00:00
|
|
|
|
|
|
|
if (empty($CFG->disablebyteserving) && !$pathisstring && $mimetype != 'text/plain' && $mimetype != 'text/html') {
|
|
|
|
|
|
|
|
@header('Accept-Ranges: bytes');
|
|
|
|
|
|
|
|
if (!empty($_SERVER['HTTP_RANGE']) && strpos($_SERVER['HTTP_RANGE'],'bytes=') !== FALSE) {
|
|
|
|
// byteserving stuff - for acrobat reader and download accelerators
|
|
|
|
// see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
|
|
|
|
// inspired by: http://www.coneural.org/florian/papers/04_byteserving.php
|
|
|
|
$ranges = false;
|
|
|
|
if (preg_match_all('/(\d*)-(\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) {
|
|
|
|
foreach ($ranges as $key=>$value) {
|
|
|
|
if ($ranges[$key][1] == '') {
|
|
|
|
//suffix case
|
|
|
|
$ranges[$key][1] = $filesize - $ranges[$key][2];
|
|
|
|
$ranges[$key][2] = $filesize - 1;
|
|
|
|
} else if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) {
|
|
|
|
//fix range length
|
|
|
|
$ranges[$key][2] = $filesize - 1;
|
|
|
|
}
|
|
|
|
if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) {
|
|
|
|
//invalid byte-range ==> ignore header
|
|
|
|
$ranges = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//prepare multipart header
|
|
|
|
$ranges[$key][0] = "\r\n--".BYTESERVING_BOUNDARY."\r\nContent-Type: $mimetype\r\n";
|
|
|
|
$ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/$filesize\r\n\r\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ranges = false;
|
|
|
|
}
|
|
|
|
if ($ranges) {
|
|
|
|
byteserving_send_file($path, $mimetype, $ranges);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/// Do not byteserve (disabled, strings, text and html files).
|
|
|
|
@header('Accept-Ranges: none');
|
|
|
|
}
|
|
|
|
} else { // Do not cache files in proxies and browsers
|
2005-09-19 17:13:06 +00:00
|
|
|
if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
|
|
|
|
@header('Cache-Control: max-age=10');
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
|
2005-09-19 17:13:06 +00:00
|
|
|
@header('Pragma: ');
|
|
|
|
} else { //normal http - prevent caching at all cost
|
|
|
|
@header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
|
2005-09-19 17:13:06 +00:00
|
|
|
@header('Pragma: no-cache');
|
|
|
|
}
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Accept-Ranges: none'); // Do not allow byteserving when caching disabled
|
2005-07-12 08:05:42 +00:00
|
|
|
}
|
2005-03-07 11:32:03 +00:00
|
|
|
|
2006-02-21 12:54:24 +00:00
|
|
|
if (empty($filter)) {
|
2006-01-16 22:21:41 +00:00
|
|
|
if ($mimetype == 'text/html' && !empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
|
|
|
|
//cookieless mode - rewrite links
|
|
|
|
@header('Content-Type: text/html');
|
|
|
|
$path = $pathisstring ? $path : implode('', file($path));
|
|
|
|
$path = sid_ob_rewrite($path);
|
|
|
|
$filesize = strlen($path);
|
|
|
|
$pathisstring = true;
|
|
|
|
} else if ($mimetype == 'text/plain') {
|
2006-11-11 17:23:20 +00:00
|
|
|
@header('Content-Type: Text/plain; charset=utf-8'); //add encoding
|
2005-03-07 11:32:03 +00:00
|
|
|
} else {
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Content-Type: '.$mimetype);
|
2005-03-07 11:32:03 +00:00
|
|
|
}
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Content-Length: '.$filesize);
|
|
|
|
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
|
2005-03-07 11:32:03 +00:00
|
|
|
if ($pathisstring) {
|
|
|
|
echo $path;
|
2006-01-16 22:21:41 +00:00
|
|
|
} else {
|
2005-07-12 08:05:42 +00:00
|
|
|
readfile_chunked($path);
|
2005-03-07 11:32:03 +00:00
|
|
|
}
|
|
|
|
} else { // Try to put the file through filters
|
|
|
|
if ($mimetype == 'text/html') {
|
2006-09-22 18:08:53 +00:00
|
|
|
$options = new object();
|
2005-03-07 11:32:03 +00:00
|
|
|
$options->noclean = true;
|
2006-09-22 18:08:53 +00:00
|
|
|
$options->nocache = true; // temporary workaround for MDL-5136
|
2005-03-07 11:32:03 +00:00
|
|
|
$text = $pathisstring ? $path : implode('', file($path));
|
2007-08-31 13:54:13 +00:00
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
$text = file_modify_html_header($text);
|
2007-01-19 10:29:03 +00:00
|
|
|
$output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
|
2006-01-16 22:21:41 +00:00
|
|
|
if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
|
|
|
|
//cookieless mode - rewrite links
|
|
|
|
$output = sid_ob_rewrite($output);
|
|
|
|
}
|
2005-03-07 11:32:03 +00:00
|
|
|
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Content-Length: '.strlen($output));
|
|
|
|
@header('Content-Type: text/html');
|
|
|
|
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
|
2005-03-07 11:32:03 +00:00
|
|
|
echo $output;
|
2006-02-21 12:54:24 +00:00
|
|
|
// only filter text if filter all files is selected
|
|
|
|
} else if (($mimetype == 'text/plain') and ($filter == 1)) {
|
2007-01-19 10:29:03 +00:00
|
|
|
$options = new object();
|
2005-03-07 11:32:03 +00:00
|
|
|
$options->newlines = false;
|
|
|
|
$options->noclean = true;
|
|
|
|
$text = htmlentities($pathisstring ? $path : implode('', file($path)));
|
2007-01-19 10:29:03 +00:00
|
|
|
$output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
|
2006-01-16 22:21:41 +00:00
|
|
|
if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
|
|
|
|
//cookieless mode - rewrite links
|
|
|
|
$output = sid_ob_rewrite($output);
|
|
|
|
}
|
2005-03-07 11:32:03 +00:00
|
|
|
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Content-Length: '.strlen($output));
|
2006-11-11 17:23:20 +00:00
|
|
|
@header('Content-Type: text/html; charset=utf-8'); //add encoding
|
2006-01-16 22:21:41 +00:00
|
|
|
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
|
2005-03-07 11:32:03 +00:00
|
|
|
echo $output;
|
|
|
|
} else { // Just send it out raw
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('Content-Length: '.$filesize);
|
|
|
|
@header('Content-Type: '.$mimetype);
|
|
|
|
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
|
2005-03-07 11:32:03 +00:00
|
|
|
if ($pathisstring) {
|
|
|
|
echo $path;
|
|
|
|
}else {
|
2005-07-12 08:05:42 +00:00
|
|
|
readfile_chunked($path);
|
2005-03-07 11:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die; //no more chars to output!!!
|
|
|
|
}
|
|
|
|
|
2005-02-25 02:06:42 +00:00
|
|
|
function get_records_csv($file, $table) {
|
2005-02-24 02:36:45 +00:00
|
|
|
global $CFG, $db;
|
|
|
|
|
|
|
|
if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-25 01:54:06 +00:00
|
|
|
if(!($handle = @fopen($file, 'r'))) {
|
2005-02-24 02:36:45 +00:00
|
|
|
error('get_records_csv failed to open '.$file);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldnames = fgetcsv($handle, 4096);
|
|
|
|
if(empty($fieldnames)) {
|
|
|
|
fclose($handle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$columns = array();
|
|
|
|
|
|
|
|
foreach($metacolumns as $metacolumn) {
|
|
|
|
$ord = array_search($metacolumn->name, $fieldnames);
|
|
|
|
if(is_int($ord)) {
|
|
|
|
$columns[$metacolumn->name] = $ord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
|
|
|
|
while (($data = fgetcsv($handle, 4096)) !== false) {
|
|
|
|
$item = new stdClass;
|
|
|
|
foreach($columns as $name => $ord) {
|
|
|
|
$item->$name = $data[$ord];
|
|
|
|
}
|
|
|
|
$rows[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
return $rows;
|
|
|
|
}
|
|
|
|
|
2005-02-25 01:54:06 +00:00
|
|
|
function put_records_csv($file, $records, $table = NULL) {
|
|
|
|
global $CFG, $db;
|
|
|
|
|
2005-04-10 09:31:58 +00:00
|
|
|
if (empty($records)) {
|
2005-02-25 01:54:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metacolumns = NULL;
|
|
|
|
if ($table !== NULL && !$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-10 09:31:58 +00:00
|
|
|
echo "x";
|
|
|
|
|
2005-02-25 01:54:06 +00:00
|
|
|
if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) {
|
|
|
|
error('put_records_csv failed to open '.$file);
|
|
|
|
}
|
|
|
|
|
2005-02-25 02:06:42 +00:00
|
|
|
$proto = reset($records);
|
|
|
|
if(is_object($proto)) {
|
|
|
|
$fields_records = array_keys(get_object_vars($proto));
|
|
|
|
}
|
|
|
|
else if(is_array($proto)) {
|
|
|
|
$fields_records = array_keys($proto);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-10 09:31:58 +00:00
|
|
|
echo "x";
|
2005-02-25 01:54:06 +00:00
|
|
|
|
|
|
|
if(!empty($metacolumns)) {
|
|
|
|
$fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
|
|
|
|
$fields = array_intersect($fields_records, $fields_table);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$fields = $fields_records;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite($fp, implode(',', $fields));
|
|
|
|
fwrite($fp, "\r\n");
|
|
|
|
|
|
|
|
foreach($records as $record) {
|
2005-02-25 02:06:42 +00:00
|
|
|
$array = (array)$record;
|
2005-02-25 01:54:06 +00:00
|
|
|
$values = array();
|
|
|
|
foreach($fields as $field) {
|
2005-02-25 02:06:42 +00:00
|
|
|
if(strpos($array[$field], ',')) {
|
|
|
|
$values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
|
2005-02-25 01:54:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-02-25 02:06:42 +00:00
|
|
|
$values[] = $array[$field];
|
2005-02-25 01:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fwrite($fp, implode(',', $values)."\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-26 15:51:31 +00:00
|
|
|
|
2006-03-01 12:13:09 +00:00
|
|
|
/**
|
2007-08-31 13:54:13 +00:00
|
|
|
* Recursively delete the file or folder with path $location. That is,
|
2006-03-01 12:13:09 +00:00
|
|
|
* if it is a file delete it. If it is a folder, delete all its content
|
2007-08-31 13:54:13 +00:00
|
|
|
* then delete it. If $location does not exist to start, that is not
|
|
|
|
* considered an error.
|
|
|
|
*
|
2006-03-01 12:13:09 +00:00
|
|
|
* @param $location the path to remove.
|
|
|
|
*/
|
2006-01-16 22:21:41 +00:00
|
|
|
function fulldelete($location) {
|
2005-04-26 15:51:31 +00:00
|
|
|
if (is_dir($location)) {
|
|
|
|
$currdir = opendir($location);
|
|
|
|
while (false !== ($file = readdir($currdir))) {
|
|
|
|
if ($file <> ".." && $file <> ".") {
|
|
|
|
$fullfile = $location."/".$file;
|
2006-01-16 22:21:41 +00:00
|
|
|
if (is_dir($fullfile)) {
|
2005-04-26 15:51:31 +00:00
|
|
|
if (!fulldelete($fullfile)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!unlink($fullfile)) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-01-16 22:21:41 +00:00
|
|
|
}
|
2005-04-26 15:51:31 +00:00
|
|
|
}
|
2006-01-16 22:21:41 +00:00
|
|
|
}
|
2005-04-26 15:51:31 +00:00
|
|
|
closedir($currdir);
|
|
|
|
if (! rmdir($location)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-01 12:13:09 +00:00
|
|
|
} else if (file_exists($location)) {
|
2005-04-26 15:51:31 +00:00
|
|
|
if (!unlink($location)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:21:41 +00:00
|
|
|
/**
|
|
|
|
* Improves memory consumptions and works around buggy readfile() in PHP 5.0.4 (2MB readfile limit).
|
|
|
|
*/
|
|
|
|
function readfile_chunked($filename, $retbytes=true) {
|
|
|
|
$chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB!
|
2005-07-12 08:05:42 +00:00
|
|
|
$buffer = '';
|
2007-08-31 13:54:13 +00:00
|
|
|
$cnt =0;
|
2005-07-12 08:05:42 +00:00
|
|
|
$handle = fopen($filename, 'rb');
|
|
|
|
if ($handle === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-01-16 04:02:28 +00:00
|
|
|
|
2005-07-12 08:05:42 +00:00
|
|
|
while (!feof($handle)) {
|
2006-01-16 22:55:17 +00:00
|
|
|
@set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk
|
2005-07-12 08:05:42 +00:00
|
|
|
$buffer = fread($handle, $chunksize);
|
|
|
|
echo $buffer;
|
2006-01-16 04:02:28 +00:00
|
|
|
flush();
|
2005-07-12 08:05:42 +00:00
|
|
|
if ($retbytes) {
|
2006-01-16 22:21:41 +00:00
|
|
|
$cnt += strlen($buffer);
|
|
|
|
}
|
2005-07-12 08:05:42 +00:00
|
|
|
}
|
|
|
|
$status = fclose($handle);
|
|
|
|
if ($retbytes && $status) {
|
|
|
|
return $cnt; // return num. bytes delivered like readfile() does.
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:21:41 +00:00
|
|
|
/**
|
|
|
|
* Send requested byterange of file.
|
|
|
|
*/
|
|
|
|
function byteserving_send_file($filename, $mimetype, $ranges) {
|
|
|
|
$chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB!
|
|
|
|
$handle = fopen($filename, 'rb');
|
|
|
|
if ($handle === false) {
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
if (count($ranges) == 1) { //only one range requested
|
|
|
|
$length = $ranges[0][2] - $ranges[0][1] + 1;
|
|
|
|
@header('HTTP/1.1 206 Partial content');
|
|
|
|
@header('Content-Length: '.$length);
|
|
|
|
@header('Content-Range: bytes '.$ranges[0][1].'-'.$ranges[0][2].'/'.filesize($filename));
|
|
|
|
@header('Content-Type: '.$mimetype);
|
|
|
|
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
|
|
|
|
$buffer = '';
|
|
|
|
fseek($handle, $ranges[0][1]);
|
|
|
|
while (!feof($handle) && $length > 0) {
|
2006-01-16 22:55:17 +00:00
|
|
|
@set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk
|
2006-01-16 22:21:41 +00:00
|
|
|
$buffer = fread($handle, ($chunksize < $length ? $chunksize : $length));
|
|
|
|
echo $buffer;
|
|
|
|
flush();
|
|
|
|
$length -= strlen($buffer);
|
|
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
die;
|
|
|
|
} else { // multiple ranges requested - not tested much
|
|
|
|
$totallength = 0;
|
|
|
|
foreach($ranges as $range) {
|
2006-06-30 07:23:08 +00:00
|
|
|
$totallength += strlen($range[0]) + $range[2] - $range[1] + 1;
|
2006-01-16 22:21:41 +00:00
|
|
|
}
|
2006-06-30 07:23:08 +00:00
|
|
|
$totallength += strlen("\r\n--".BYTESERVING_BOUNDARY."--\r\n");
|
2006-01-16 22:21:41 +00:00
|
|
|
@header('HTTP/1.1 206 Partial content');
|
|
|
|
@header('Content-Length: '.$totallength);
|
|
|
|
@header('Content-Type: multipart/byteranges; boundary='.BYTESERVING_BOUNDARY);
|
|
|
|
//TODO: check if "multipart/x-byteranges" is more compatible with current readers/browsers/servers
|
|
|
|
while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite
|
|
|
|
foreach($ranges as $range) {
|
|
|
|
$length = $range[2] - $range[1] + 1;
|
|
|
|
echo $range[0];
|
|
|
|
$buffer = '';
|
|
|
|
fseek($handle, $range[1]);
|
|
|
|
while (!feof($handle) && $length > 0) {
|
2006-01-16 22:55:17 +00:00
|
|
|
@set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk
|
2006-01-16 22:21:41 +00:00
|
|
|
$buffer = fread($handle, ($chunksize < $length ? $chunksize : $length));
|
|
|
|
echo $buffer;
|
|
|
|
flush();
|
|
|
|
$length -= strlen($buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "\r\n--".BYTESERVING_BOUNDARY."--\r\n";
|
|
|
|
fclose($handle);
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
2005-04-26 15:51:31 +00:00
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
/**
|
|
|
|
* add includes (js and css) into uploaded files
|
|
|
|
* before returning them, useful for themes and utf.js includes
|
|
|
|
* @param string text - text to search and replace
|
|
|
|
* @return string - text with added head includes
|
|
|
|
*/
|
|
|
|
function file_modify_html_header($text) {
|
|
|
|
// first look for <head> tag
|
|
|
|
global $CFG;
|
2007-08-31 13:54:13 +00:00
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
$stylesheetshtml = '';
|
|
|
|
foreach ($CFG->stylesheets as $stylesheet) {
|
|
|
|
$stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
|
2007-08-31 13:54:13 +00:00
|
|
|
}
|
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
$filters = explode(",", $CFG->textfilters);
|
|
|
|
if (in_array('filter/mediaplugin', $filters)) {
|
2007-08-31 13:54:13 +00:00
|
|
|
// this script is needed by most media filter plugins.
|
|
|
|
$ufo = "\n".'<script type="text/javascript" src="'.$CFG->wwwroot.'/lib/ufo.js"></script>'."\n";
|
2007-08-17 04:57:52 +00:00
|
|
|
} else {
|
2007-08-31 13:54:13 +00:00
|
|
|
$ufo = '';
|
2007-08-17 04:57:52 +00:00
|
|
|
}
|
2007-08-31 13:54:13 +00:00
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
preg_match('/\<head\>|\<HEAD\>/', $text, $matches);
|
|
|
|
if ($matches) {
|
|
|
|
$replacement = '<head>'.$ufo.$stylesheetshtml;
|
|
|
|
$text = preg_replace('/\<head\>|\<HEAD\>/', $replacement, $text, 1);
|
2007-08-31 13:54:13 +00:00
|
|
|
return $text;
|
2007-08-17 04:57:52 +00:00
|
|
|
}
|
2007-08-31 13:54:13 +00:00
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
// if not, look for <html> tag, and stick <head> right after
|
|
|
|
preg_match('/\<html\>|\<HTML\>/', $text, $matches);
|
|
|
|
if ($matches) {
|
|
|
|
// replace <html> tag with <html><head>includes</head>
|
2007-08-20 10:52:59 +00:00
|
|
|
$replacement = '<html>'."\n".'<head>'.$ufo.$stylesheetshtml.'</head>';
|
2007-08-17 04:57:52 +00:00
|
|
|
$text = preg_replace('/\<html\>|\<HTML\>/', $replacement, $text, 1);
|
2007-08-31 13:54:13 +00:00
|
|
|
return $text;
|
2007-08-17 04:57:52 +00:00
|
|
|
}
|
2007-08-31 13:54:13 +00:00
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
// if not, look for <body> tag, and stick <head> before body
|
|
|
|
preg_match('/\<body\>|\<BODY\>/', $text, $matches);
|
|
|
|
if ($matches) {
|
2007-08-20 10:52:59 +00:00
|
|
|
$replacement = '<head>'.$ufo.$stylesheetshtml.'</head>'."\n".'<body>';
|
2007-08-17 04:57:52 +00:00
|
|
|
$text = preg_replace('/\<body\>|\<BODY\>/', $replacement, $text, 1);
|
2007-08-31 13:54:13 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2007-08-17 04:57:52 +00:00
|
|
|
// if not, just stick a <head> tag at the beginning
|
|
|
|
$text = '<head>'.$ufo.$stylesheetshtml.'</head>'."\n".$text;
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2005-02-25 01:54:06 +00:00
|
|
|
?>
|