<?php
/**
 *
 * Klasa sprawdzająca na różne sposoby MimeType pliku.
 *
 *
 * @date 8.03.2009
 * @author Krystian Kuczek
 */

class MimeType {

	const BY_FINFO = 0;
	const BY_MIME_MAGIC = 1;
	const BY_EXTENSION = 2;

	const DEFAULT_BY = self::BY_EXTENSION;
	const MAGIC_FILE = './mime.magic';

	public static function GetFileMimeType($file) {

		if (is_array($file)) {
			$arr = true;
			$fileTmp = $file['tmp_name'];
		} else {
			$arr = false;
		}

		switch(self::DEFAULT_BY) {
			case self::BY_FINFO:
				return self::GetMimeByFInfo($fileTmp);
			case self::BY_MIME_MAGIC:
				return self::GetMimeByMimeMagic($fileTmp);
			case self::BY_EXTENSION:
				if ($arr) {
					return self::GetMimeByExtension($file['name']);
				}
				return self::GetMimeByExtension($file['name']);
		}
	}

	public static function GetMimeByExtension($file) {

		$exp = explode(".", $file);

		$ext = $exp[sizeof($exp) - 1];

		$mimeArray = self::ExtToMimeArray();
		
		if (isset($mimeArray[$ext])) {
			return $mimeArray[strtolower($ext)];
		} else {
			return "application/octet-stream";
		}
	}

	public static function GetMimeByMimeMagic($file) {
		return mime_content_type($file);
	}

	public static function GetMimeByFInfo($file) {
		$res = finfo_open(FILEINFO_MIME, MAGIC_FILE);
		$ret = $res->file($file);
		finfo_close($file);
		return $ret;
	}

	public function GetFileExtByMimeType($file) {
		$mime = self::GetFileMimeType($file);

		$mimeArray = self::MimeToExtArray();
	}

	public static function IsImage($file) {
		$mime = self::GetFileMimeType($file);

		switch ($mime) {
			case "image/bmp":
			case "image/gif":
			case "image/jpeg":
			case "image/png":
			case "image/tiff":
				return true;
			default:
				return false;
		}
	}

	public static function IsMovie($file) {

		$mime = self::GetFileMimeType($file);

		switch ($mime) {
			case "video/x-flv";
			case "video/mpeg";
				return true;
			default:
				return false;
		}
	}
	
	private static function MimeToExtArray() {
		return array(
			"application/andrew-inset" => "ez",
			"application/mac-binhex40" => "hqx",
			"application/mac-compactpro" => "cpt",
			"application/msword" => "doc",
			"application/oda" => "oda",
			"application/pdf" => "pdf",
			"application/postscript" => "ps",
			"application/smil" => "smil",
			"application/vnd.wap.wbxml" => "wbxml",
			"application/vnd.wap.wmlc" => "wmlc",
			"application/vnd.wap.wmlscriptc" => "wmlsc",
			"application/x-bcpio" => "bcpio",
			"application/x-cdlink" => "vcd",
			"application/x-chess-pgn" => "pgn",
			"application/x-cpio" => "cpio",
			"application/x-csh" => "csh",
			"application/x-director" => "dir",
			"application/x-dvi" => "dvi",
			"application/x-futuresplash" => "spl",
			"application/x-gtar" => "gtar",
			"application/x-hdf" => "hdf",
			"application/x-javascript" => "js",
			"application/x-koan" => "skp",
			"application/x-latex" => "latex",
			"application/x-netcdf" => "nc",
			"application/x-sh" => "sh",
			"application/x-shar" => "shar",
			"application/x-shockwave-flash" => "swf",
			"application/x-stuffit" => "sit",
			"application/x-sv4cpio" => "sv4cpio",
			"application/x-sv4crc" => "sv4crc",
			"application/x-tar" => "tar",
			"application/x-tcl" => "tcl",
			"application/x-tex" => "tex",
			"application/x-texinfo" => "texinfo",
			"application/x-troff" => "roff",
			"application/x-troff-man" => "man",
			"application/x-troff-me" => "me",
			"application/x-troff-ms" => "ms",
			"application/x-ustar" => "ustar",
			"application/x-wais-source" => "src",
			"application/xhtml+xml" => "xhtml",
			"application/zip" => "zip",
			"audio/basic" => "au",
			"audio/midi" => "midi",
			"audio/mpeg" => "mp3",
			"audio/x-aiff" => "aiff",
			"image/tiff" => "tif",
			"audio/x-mpegurl" => "m3u",
			"audio/x-pn-realaudio" => "ram",
			"audio/x-pn-realaudio-plugin" => "rpm",
			"audio/x-realaudio" => "ra",
			"audio/x-wav" => "wav",
			"chemical/x-pdb" => "pdb",
			"chemical/x-xyz" => "xyz",
			"image/bmp" => "bmp",
			"image/gif" => "gif",
			"image/ief" => "ief",
			"image/jpeg" => "jpg",
			"image/png" => "png",
			"image/tiff" => "tiff",
			"image/vnd.djvu" => "djvu",
			"image/vnd.djvu" => "djv",
			"image/vnd.wap.wbmp" => "wbmp",
			"image/x-cmu-raster" => "ras",
			"image/x-portable-anymap" => "pnm",
			"image/x-portable-bitmap" => "pbm",
			"image/x-portable-graymap" => "pgm",
			"image/x-portable-pixmap" => "ppm",
			"image/x-rgb" => "rgb",
			"image/x-xbitmap" => "xbm",
			"image/x-xpixmap" => "xpm",
			"image/x-windowdump" => "xwd",
			"model/iges" => "iges",
			"model/mesh" => "mesh",
			"model/vrml" => "vrml",
			"text/css" => "css",
			"text/html" => "html",
			"text/plain" => "txt",
			"text/richtext" => "rtx",
			"text/rtf" => "rtf",
			"text/sgml" => "sgml",
			"text/tab-seperated-values" => "tsv",
			"text/vnd.wap.wml" => "wml",
			"text/vnd.wap.wmlscript" => "wmls",
			"text/x-setext" => "etx",
			"text/xml" => "xml",
			"video/mpeg" => "mpg",
			"video/x-flv" => "flv",
			"video/quicktime" => "qt",
			"video/vnd.mpegurl" => "mxu",
			"video/x-msvideo" => "avi",
			"video/x-sgi-movie" => "movie",
			"x-conference-xcooltalk" => "ice"
		);
	}

	public static function ExtToMimeArray() {
		$mimeTypes = array_flip(self::MimeToExtArray());

		$mimeTypes["jpeg"] = "image/jpeg";
		$mimeTypes["jpe"] = "image/jpeg";
		$mimeTypes["mov"] = "video/quicktime";
		$mimeTypes["mpeg"] = "video/mpeg";
		$mimeTypes["mpe"] = "video/mpeg";
		$mimeTypes["xsl"] = "text/xml";
		$mimeTypes["sgm"] = "text/sgml";
		$mimeTypes["asc"] = "text/plain";
		$mimeTypes["wrl"] = "model/vrml";
		$mimeTypes["igs"] = "model/iges";
		$mimeTypes["msh"] = "model/mesh";
		$mimeTypes["silo"] = "model/mesh";
		$mimeTypes["htm"] = "text/html";
		$mimeTypes["snd"] = "audio/basic";
		$mimeTypes["kar"] = "audio/midi";
		$mimeTypes["mpga"] = "audio/mpeg";
		$mimeTypes["mp2"] = "audio/mpeg";
		$mimeTypes["aif"] = "audio/x-aiff";
		$mimeTypes["aifc"] = "audio/x-aiff";
		$mimeTypes["rm"] = "audio/x-pn-realaudio";
		$mimeTypes["ai"] = "application/postscript";
		$mimeTypes["eps"] = "application/postscript";
		$mimeTypes["smi"] = "application/smil";
		$mimeTypes["dcr"] = "application/x-director";
		$mimeTypes["dxr"] = "application/x-director";
		$mimeTypes["skd"] = "application/x-koan";
		$mimeTypes["skt"] = "application/x-koan";
		$mimeTypes["skm"] = "application/x-koan";
		$mimeTypes["cdf"] = "application/x-netcdf";
		$mimeTypes["texi"] = "application/x-texinfo";
		$mimeTypes["t"] = "application/x-troff";
		$mimeTypes["tr"] = "application/x-troff";
		$mimeTypes["xht"] = "application/xhtml+xml";
		$mimeTypes["mid"] = "audio/midi";
		
		return $mimeTypes;
	}
}
?>