<?php  // vim:set et ts=4 sw=4 sts=4:
# Copyright (C) 2003-2014 hipplay Inc.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE OF hipplay Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code. The contents of this file may not
# be disclosed to third parties, copied or duplicated in any form, in
# whole or in part, without the prior written permission of
# hipplay Inc.
#
namespace HipPlayConnect\FileSystem\Types;


class FileTypes
{
    protected static $INSTANCE;


    /* MIME type for the given extension
     */
    public $mimeTypes = array();

    /* Class for the given file extension
     * Intended to make it easier to find "an image" than to find
     * "a gif or jpeg or tif or ..."
     */
    public $classes = array( '' => 'document' );

    /**
     * Singleton constructor.
     */
    protected function __construct() { }

    // semi-internal function: get the extension of a file
    function getExt( $arg )
    {
        return
            preg_match( '|\.([^/.]*)/*$|', $arg, $RE )
            ? strtoupper( $RE[1] )
            : '';
    }

    // get MIME type of a file
    function getMIMEType( $arg )
    {
        $Ext = $this->getExt($arg);
        if( $Ext && ($type = $this->mimeTypes[$Ext]) )
            return $type;
        return $this->mimeTypes[''];
    }

    // get the class for the given file
    function getClass( $arg )
    {
        $Ext = $this->getExt($arg);
        $c = '';
        if( !($Ext && ($c = $this->classes[$Ext])) )
            $c = $this->classes[''];
        return $c;
    }

    function getFullClassName( $arg )
    {
        $class = $this->getClass($arg);
        return 'Ax' . ucfirst($class) . 'File';
    }

    public static function getInstance() {
        if (self::$INSTANCE === NULL) {
            self::$INSTANCE = new FileTypes();
        }

        return self::$INSTANCE;
    }
}

$FileTypes = FileTypes::getInstance();

/** to get the list of mime types from /etc/mime.types **/
/*
foreach ( file( '/etc/mime.types' ) as $line )
{
	    $line = trim( $line );
        if( $line[0] == '#' ) continue;
        $RE = array();
        if( !preg_match( '/^([a-z0-9\-]+\/[a-z0-9\-\.]+)\s+([\w\d\s]+)\s*$/',
            $line, $RE ) ) continue;

        $exts = explode( ' ', strtoupper($RE[2]) );
        foreach( $exts as $ext )
            if($ext)
                echo "\t'$ext' => '" . $RE[1] . "',\n";
}
*/

$FileTypes->mimeTypes = array
(
	'' => 'application/octet-stream', // default one, not in /etc/mime.types
	'EZ' => 'application/andrew-inset',
	'HQX' => 'application/mac-binhex40',
	'CPT' => 'application/mac-compactpro',
	'DOC' => 'application/msword',
	'BIN' => 'application/octet-stream',
	'DMS' => 'application/octet-stream',
	'LHA' => 'application/octet-stream',
	'LZH' => 'application/octet-stream',
	'EXE' => 'application/octet-stream',
	'CLASS' => 'application/octet-stream',
	'SO' => 'application/octet-stream',
	'DLL' => 'application/octet-stream',
	'ODA' => 'application/oda',
	'OGG' => 'application/ogg',
	'PDF' => 'application/pdf',
	'AI' => 'application/postscript',
	'EPS' => 'application/postscript',
	'PS' => 'application/postscript',
//	'RTF' => 'application/rtf',
	'SMI' => 'application/smil',
	'SMIL' => 'application/smil',
	'MIF' => 'application/vnd.mif',
	'XLS' => 'application/vnd.ms-excel',
	'PPT' => 'application/vnd.ms-powerpoint',
	'SXW' => 'application/vnd.sun.xml.writer',
	'STW' => 'application/vnd.sun.xml.writer.template',
	'SXC' => 'application/vnd.sun.xml.calc',
	'STC' => 'application/vnd.sun.xml.calc.template',
	'SXD' => 'application/vnd.sun.xml.draw',
	'STD' => 'application/vnd.sun.xml.draw.template',
	'SXI' => 'application/vnd.sun.xml.impress',
	'STI' => 'application/vnd.sun.xml.impress.template',
	'SXG' => 'application/vnd.sun.xml.writer.global',
	'SXM' => 'application/vnd.sun.xml.math',
	'WBXML' => 'application/vnd.wap.wbxml',
	'WMLC' => 'application/vnd.wap.wmlc',
	'WMLSC' => 'application/vnd.wap.wmlscriptc',
	'BCPIO' => 'application/x-bcpio',
	'TORRENT' => 'application/x-bittorrent',
	'BZ2' => 'application/x-bzip2',
	'VCD' => 'application/x-cdlink',
	'PGN' => 'application/x-chess-pgn',
	'CPIO' => 'application/x-cpio',
	'CSH' => 'application/x-csh',
	'DCR' => 'application/x-director',
	'DIR' => 'application/x-director',
	'DXR' => 'application/x-director',
	'DVI' => 'application/x-dvi',
	'SPL' => 'application/x-futuresplash',
	'GTAR' => 'application/x-gtar',
	'GZ' => 'application/x-gzip',
	'TGZ' => 'application/x-gzip',
	'HDF' => 'application/x-hdf',
	'JS' => 'application/x-javascript',
	'KWD' => 'application/x-kword',
	'KWT' => 'application/x-kword',
	'KSP' => 'application/x-kspread',
	'KPR' => 'application/x-kpresenter',
	'KPT' => 'application/x-kpresenter',
	'CHRT' => 'application/x-kchart',
	'KIL' => 'application/x-killustrator',
	'SKP' => 'application/x-koan',
	'SKD' => 'application/x-koan',
	'SKT' => 'application/x-koan',
	'SKM' => 'application/x-koan',
	'LATEX' => 'application/x-latex',
	'NC' => 'application/x-netcdf',
	'CDF' => 'application/x-netcdf',
	'RPM' => 'application/x-rpm',
	'SH' => 'application/x-sh',
	'SHAR' => 'application/x-shar',
	'SWF' => 'application/x-shockwave-flash',
	'SIT' => 'application/x-stuffit',
	'SV4CPIO' => 'application/x-sv4cpio',
	'SV4CRC' => 'application/x-sv4crc',
	'TAR' => 'application/x-tar',
	'TCL' => 'application/x-tcl',
	'TEX' => 'application/x-tex',
	'TEXINFO' => 'application/x-texinfo',
	'TEXI' => 'application/x-texinfo',
	'T' => 'application/x-troff',
	'TR' => 'application/x-troff',
	'ROFF' => 'application/x-troff',
	'MAN' => 'application/x-troff-man',
	'ME' => 'application/x-troff-me',
	'MS' => 'application/x-troff-ms',
	'USTAR' => 'application/x-ustar',
	'SRC' => 'application/x-wais-source',
	'ZIP' => 'application/zip',
	'AU' => 'audio/basic',
	'SND' => 'audio/basic',
	'MID' => 'audio/midi',
	'MIDI' => 'audio/midi',
	'KAR' => 'audio/midi',
	'MPGA' => 'audio/mpeg',
	'MP2' => 'audio/mpeg',
	'MP3' => 'audio/mpeg',
	'AIF' => 'audio/x-aiff',
	'AIFF' => 'audio/x-aiff',
	'AIFC' => 'audio/x-aiff',
	'M3U' => 'audio/x-mpegurl',
	'RAM' => 'audio/x-pn-realaudio',
	'RM' => 'audio/x-pn-realaudio',
	'RA' => 'audio/x-realaudio',
	'WAV' => 'audio/x-wav',
	'PDB' => 'chemical/x-pdb',
	'XYZ' => 'chemical/x-xyz',
	'BMP' => 'image/bmp',
	'GIF' => 'image/gif',
	'IEF' => 'image/ief',
	'JPEG' => 'image/jpeg',
	'JPG' => 'image/jpeg',
	'JPE' => 'image/jpeg',
	'PNG' => 'image/png',
	'TIFF' => 'image/tiff',
	'TIF' => 'image/tiff',
	'DJVU' => 'image/vnd.djvu',
	'DJV' => 'image/vnd.djvu',
	'WBMP' => 'image/vnd.wap.wbmp',
	'RAS' => 'image/x-cmu-raster',
	'PNM' => 'image/x-portable-anymap',
	'PBM' => 'image/x-portable-bitmap',
	'PGM' => 'image/x-portable-graymap',
	'PPM' => 'image/x-portable-pixmap',
	'RGB' => 'image/x-rgb',
	'XBM' => 'image/x-xbitmap',
	'XPM' => 'image/x-xpixmap',
	'XWD' => 'image/x-xwindowdump',
	'IGS' => 'model/iges',
	'IGES' => 'model/iges',
	'MSH' => 'model/mesh',
	'MESH' => 'model/mesh',
	'SILO' => 'model/mesh',
	'WRL' => 'model/vrml',
	'VRML' => 'model/vrml',
	'CSS' => 'text/css',
	'HTML' => 'text/html',
	'HTM' => 'text/html',
	'ASC' => 'text/plain',
	'TXT' => 'text/plain',
	'RTX' => 'text/richtext',
	'RTF' => 'text/rtf',
	'SGML' => 'text/sgml',
	'SGM' => 'text/sgml',
	'TSV' => 'text/tab-separated-values',
	'WML' => 'text/vnd.wap.wml',
	'WMLS' => 'text/vnd.wap.wmlscript',
	'ETX' => 'text/x-setext',
	'XML' => 'text/xml',
	'XSL' => 'text/xml',
	'MPEG' => 'video/mpeg',
	'MPG' => 'video/mpeg',
	'MPE' => 'video/mpeg',
	'QT' => 'video/quicktime',
	'MOV' => 'video/quicktime',
	'MXU' => 'video/vnd.mpegurl',
	'AVI' => 'video/x-msvideo',
	'MOVIE' => 'video/x-sgi-movie',
	'FLV' => 'video/x-flv',
	'ICE' => 'x-conference/x-cooltalk'
);

/** Override any types we want to be sure of **/

$FileTypes->mimeTypes['TXT'] = 'text/plain';
$FileTypes->mimeTypes['CSS'] = 'text/css';

$FileTypes->mimeTypes['ICS'] = 'text/calendar';
$FileTypes->mimeTypes['PDF'] = 'application/pdf';
$FileTypes->mimeTypes['XLS'] = 'application/vnd.ms-excel';

$FileTypes->mimeTypes['XML'] = 'text/xml';
$FileTypes->mimeTypes['XUL'] = 'application/vnd.mozilla.xul+xml';
$FileTypes->mimeTypes['XPI'] = 'application/x-xpinstall';

//
// new MS mime types
//
$FileTypes->mimeTypes[ 'DOCM' ] = 'application/vnd.ms-word.document.macroEnabled.12';
$FileTypes->mimeTypes[ 'DOCX' ] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
$FileTypes->mimeTypes[ 'DOTM' ] = 'application/vnd.ms-word.template.macroEnabled.12';
$FileTypes->mimeTypes[ 'DOTX' ] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
$FileTypes->mimeTypes[ 'POTM' ] = 'application/vnd.ms-powerpoint.template.macroEnabled.12';
$FileTypes->mimeTypes[ 'POTX' ] = 'application/vnd.openxmlformats-officedocument.presentationml.template';
$FileTypes->mimeTypes[ 'PPAM' ] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
$FileTypes->mimeTypes[ 'PPSM' ] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
$FileTypes->mimeTypes[ 'PPSX' ] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
$FileTypes->mimeTypes[ 'PPTM' ] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
$FileTypes->mimeTypes[ 'PPTX' ] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
$FileTypes->mimeTypes[ 'XLAM' ] = 'application/vnd.ms-excel.addin.macroEnabled.12';
$FileTypes->mimeTypes[ 'XLSB' ] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
$FileTypes->mimeTypes[ 'XLSM' ] = 'application/vnd.ms-excel.sheet.macroEnabled.12';
$FileTypes->mimeTypes[ 'XLSX' ] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
$FileTypes->mimeTypes[ 'XLTM' ] = 'application/vnd.ms-excel.template.macroEnabled.12';
$FileTypes->mimeTypes[ 'XLTX' ] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';

/** Split-out types **/
include_once('type_zip.php');
include_once('type_image.php');
include_once('type_html.php');

/** Functions **/

function alwaysRaw()
{
    return 'RAW';
}

/* Update files (bug 8689) */
$FileTypes->mimeTypes['UPD'] = 'application/octet-stream';
