/**
* Return mime type for the given file extension.
* Slow, but useful if PHP's mime_content_type() cannot be used.
* Uses Wikka's mime_types.txt and some hardcoded associations.
*
* @author {@link http://wikkawiki.org/ChewBakka ChewBakka} (first draft)
* @input $extension file extension, e.g. 'png', 'jpg', 'ogg'
* @output mime content type, e.g. 'image/png', or empty string.
*/
function MimeTypeFromExtension( $extension )
{
// Quick resonse for most frequently used file types
if ($extension == 'png') return 'image/png';
if ($extension == 'jpg') return 'image/jpeg';
if ($extension == 'ogg') return 'application/ogg';
// If not recoginzed, use mime_types.txt
{
if ($line !=
'' &&
substr($line,
0,
1) !=
'#' &&
strpos($line,
$extension))
{
{
return $a[0];
}
}
}
return ''; // if nothing was found
}
/**
* Return an array describing the file which is stored in a page.
*
* @author {@link http://wikkawiki.org/ChewBakka ChewBakka} (first draft)
* @input $tag name of the page; default = current page.
* @output Array or (if no file in page) null. Keys:
* extension file extension, e.g. 'png', 'jpg', 'ogg'
* content-type mime content type, e.g. 'image/png'
* uploaded when and who,. e.g. '2007-01-31 ChewBakka'
* image 'yes' or 'no'
* width Width in pixels (images only)
* height Height in pixels (images only)
* filename PageName.extension
* path upload_path/PageName.extension
* url 'http://..../PageName/file?action=get'
*/
function GetFile( $tag = '' )
{
$data = null; // this variable will be returned
if (!$tag) $tag = $this->tag;
$metafile = $this->config['upload_path'].'/'.$tag.'.file'; // metadata
{
$lines =
explode( "\n",
$contents );
// Lines look like "key: value" -> convert into array
'extension' => '',
'content-type' => '',
'uploaded' => '<unknown> <unknown>',
'image' => 'false',
'width' => '',
'height' => '',
'filename' => '',
'path' => '',
'url' => ''
);
foreach ($lines as $line)
{
$data[ $a[0] ] =
trim( $a[1] );
}
// Convenient attributes which can not be stored permanently
$data['filename'] = $tag . '.' . $data['extension'];
$data['path'] = $this->config['upload_path'] .'/' . $data['filename'];
$data['url'] = $this->config['base_url'] . $tag . '/file' .
($this->config['rewrite_mode']=='1' ? '?' : '&') .
'action=get';
// Final check: file must exist.
$data = null;
}
return $data;
}
/**
* Remove the file from the current page.
*
* @author {@link http://wikkawiki.org/ChewBakka ChewBakka} (first draft)
* @input none
* @output none
*/
function RemoveFile()
{
if ($data = $this->GetFile())
{
unlink( $this->
config['upload_path'] .
'/' .
$this->
tag .
'.file' );
unlink( $this->
config['upload_path'] .
'/' .
$data['filename'] );
}
}
/**
* Store an http-uploaded file in the current page.
* Any previous file will be replaced with the new file.
*
* @author {@link http://wikkawiki.org/ChewBakka ChewBakka} (first draft)
* @input $uploaded_file An item from PHP's $_FILES array (see there)
* @output None
*/
function SaveFile( $uploaded_file )
{
$this->RemoveFile();
$uploaded_file['name'] =
strtolower( $uploaded_file['name'] );
$pathinfo =
pathinfo( $uploaded_file['name'] );
$extension = $pathinfo['extension'];
if ($extension=='gz') {
$extension =
$a[count($a)-
2] .
'.gz';
}
}
$pathname = $this->config['upload_path'] . '/' . $this->tag;
$path = $pathname .