[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/ -> mimetype.class.php (source)

   1  <?php
   2  
   3      
   4  
   5  /**
   6   * \ingroup Data
   7   * 
   8   * Copyright (C) 2002 Jason Sheets <jsheets@shadonet.com>.
   9   * All rights reserved.
  10   *
  11   * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  12   * Redistribution and use in source and binary forms, with or without
  13   * modification, are permitted provided that the following conditions
  14   * are met:
  15   *
  16   * 1. Redistributions of source code must retain the above copyright
  17   *    notice, this list of conditions and the following disclaimer.
  18   *
  19   * 2. Redistributions in binary form must reproduce the above copyright
  20   *    notice, this list of conditions and the following disclaimer in the
  21   *    documentation and/or other materials provided with the distribution.
  22   *
  23   * 3. Neither the name of the project nor the names of its contributors
  24   *    may be used to endorse or promote products derived from this software
  25   *    without specific prior written permission.
  26   *
  27   * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  28   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30   * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  31   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37   * SUCH DAMAGE.
  38   *
  39   *   Name: PHP MimeType Class
  40   *
  41   *    Version: 1.0
  42   *    Date Released: 10/20/02
  43   *
  44   *    Description: This class allows a PHP script to determine the mime type
  45   *    a file based on the file extension.  This class is handy for PHP versions
  46   *    without the benefit of other tools to determine the mime type.  The class
  47   *    defaults to octet-stream so if the file type is not recognized the user
  48   *    is presented with a file download prompt.
  49   *
  50   *    NOTES: The mime types for this version are based on Apache 1.3.27
  51   *    mime types may change or need to be added in the future, the mime types
  52   *    are stored in an array so that an awk or other script can automatically
  53   *    generate the code to make the array.
  54   *
  55   *    Usage:
  56   *
  57   *        First an instance of the mimetype class must be created, then the
  58   *     getType method should be called with the filename.  It will return
  59   *     the mime type, an example follows.
  60   *
  61   *    Example:
  62   *
  63   *       $mimetype = new MimeType();
  64   *       print $mimetype->getType('acrobat.pdf');
  65   *
  66   *    Author: Jason Sheets <jsheets@shadonet.com>
  67   *
  68   *    License: This script is distributed under the BSD License, you are free
  69   *    to use, or modify it however you like.  If you find this script useful please
  70   *    e-mail me.
  71   *
  72   *    -- NOTE --
  73   *
  74   *    Modified by me to make it extend from the Object class and to have the array of mime
  75   *    types as an static variable inside the class, so that it does not have to be rebuilt
  76   *    everytime when calling getType.
  77   */
  78  class MimeType  
  79  {
  80  
  81      var $mimetypes = Array(
  82           "ez" => "application/andrew-inset",
  83           "hqx" => "application/mac-binhex40",
  84           "cpt" => "application/mac-compactpro",
  85           "doc" => "application/msword",
  86           "bin" => "application/octet-stream",
  87           "dms" => "application/octet-stream",
  88           "lha" => "application/octet-stream",
  89           "lzh" => "application/octet-stream",
  90           "exe" => "application/octet-stream",
  91           "class" => "application/octet-stream",
  92           "so" => "application/octet-stream",
  93           "dll" => "application/octet-stream",
  94           "oda" => "application/oda",
  95           "pdf" => "application/pdf",
  96           "ai" => "application/postscript",
  97           "eps" => "application/postscript",
  98           "ps" => "application/postscript",
  99           "smi" => "application/smil",
 100           "smil" => "application/smil",
 101           "wbxml" => "application/vnd.wap.wbxml",
 102           "wmlc" => "application/vnd.wap.wmlc",
 103           "wmlsc" => "application/vnd.wap.wmlscriptc",
 104           "bcpio" => "application/x-bcpio",
 105           "vcd" => "application/x-cdlink",
 106           "pgn" => "application/x-chess-pgn",
 107           "cpio" => "application/x-cpio",
 108           "csh" => "application/x-csh",
 109           "dcr" => "application/x-director",
 110           "dir" => "application/x-director",
 111           "dxr" => "application/x-director",
 112           "dvi" => "application/x-dvi",
 113           "spl" => "application/x-futuresplash",
 114           "gtar" => "application/x-gtar",
 115           "hdf" => "application/x-hdf",
 116           "js" => "application/x-javascript",
 117           "skp" => "application/x-koan",
 118           "skd" => "application/x-koan",
 119           "skt" => "application/x-koan",
 120           "skm" => "application/x-koan",
 121           "latex" => "application/x-latex",
 122           "nc" => "application/x-netcdf",
 123           "cdf" => "application/x-netcdf",
 124           "sh" => "application/x-sh",
 125           "shar" => "application/x-shar",
 126           "swf" => "application/x-shockwave-flash",
 127           "sit" => "application/x-stuffit",
 128           "sv4cpio" => "application/x-sv4cpio",
 129           "sv4crc" => "application/x-sv4crc",
 130           "tar" => "application/x-tar",
 131           "tcl" => "application/x-tcl",
 132           "tex" => "application/x-tex",
 133           "texinfo" => "application/x-texinfo",
 134           "texi" => "application/x-texinfo",
 135           "t" => "application/x-troff",
 136           "tr" => "application/x-troff",
 137           "roff" => "application/x-troff",
 138           "man" => "application/x-troff-man",
 139           "me" => "application/x-troff-me",
 140           "ms" => "application/x-troff-ms",
 141           "ustar" => "application/x-ustar",
 142           "src" => "application/x-wais-source",
 143           "xhtml" => "application/xhtml+xml",
 144           "xht" => "application/xhtml+xml",
 145           "zip" => "application/zip",
 146           "au" => "audio/basic",
 147           "snd" => "audio/basic",
 148           "mid" => "audio/midi",
 149           "midi" => "audio/midi",
 150           "kar" => "audio/midi",
 151           "mpga" => "audio/mpeg",
 152           "mp2" => "audio/mpeg",
 153           "mp3" => "audio/mpeg",
 154           "aif" => "audio/x-aiff",
 155           "aiff" => "audio/x-aiff",
 156           "aifc" => "audio/x-aiff",
 157           "m3u" => "audio/x-mpegurl",
 158           "ram" => "audio/x-pn-realaudio",
 159           "rm" => "audio/x-pn-realaudio",
 160           "rpm" => "audio/x-pn-realaudio-plugin",
 161           "ra" => "audio/x-realaudio",
 162           "wav" => "audio/x-wav",
 163           "pdb" => "chemical/x-pdb",
 164           "xyz" => "chemical/x-xyz",
 165           "bmp" => "image/bmp",
 166           "gif" => "image/gif",
 167           "ief" => "image/ief",
 168           "jpe" => "image/jpeg",
 169           "jpeg" => "image/jpeg",
 170           "jpg" => "image/jpeg",
 171           "png" => "image/png",
 172           "tiff" => "image/tiff",
 173           "tif" => "image/tif",
 174           "djvu" => "image/vnd.djvu",
 175           "djv" => "image/vnd.djvu",
 176           "wbmp" => "image/vnd.wap.wbmp",
 177           "ras" => "image/x-cmu-raster",
 178           "pnm" => "image/x-portable-anymap",
 179           "pbm" => "image/x-portable-bitmap",
 180           "pgm" => "image/x-portable-graymap",
 181           "ppm" => "image/x-portable-pixmap",
 182           "rgb" => "image/x-rgb",
 183           "xbm" => "image/x-xbitmap",
 184           "xpm" => "image/x-xpixmap",
 185           "xwd" => "image/x-windowdump",
 186           "igs" => "model/iges",
 187           "iges" => "model/iges",
 188           "msh" => "model/mesh",
 189           "mesh" => "model/mesh",
 190           "silo" => "model/mesh",
 191           "wrl" => "model/vrml",
 192           "vrml" => "model/vrml",
 193           "css" => "text/css",
 194           "html" => "text/html",
 195           "htm" => "text/html",
 196           "asc" => "text/plain",
 197           "txt" => "text/plain",
 198           "rtx" => "text/richtext",
 199           "rtf" => "text/rtf",
 200           "sgml" => "text/sgml",
 201           "sgm" => "text/sgml",
 202           "tsv" => "text/tab-seperated-values",
 203           "wml" => "text/vnd.wap.wml",
 204           "wmls" => "text/vnd.wap.wmlscript",
 205           "etx" => "text/x-setext",
 206           "xml" => "text/xml",
 207           "xsl" => "text/xml",
 208           "mpeg" => "video/mpeg",
 209           "mpg" => "video/mpeg",
 210           "mpe" => "video/mpeg",
 211           "qt" => "video/quicktime",
 212           "mov" => "video/quicktime",
 213           "mxu" => "video/vnd.mpegurl",
 214           "avi" => "video/x-msvideo",
 215           "movie" => "video/x-sgi-movie",
 216           "ice" => "x-conference-xcooltalk",
 217           "wmv"=>"video/x-ms-wmv",
 218           "wma"=>"audio/x-ms-wma",
 219           "asf"=>"video/x-msvideo"
 220        );
 221  
 222     // default mime type returned if it could not be found
 223     var $_defaultType = "application/octet-stream";
 224  
 225     function MimeType()
 226     {
 227             
 228     }
 229  
 230     /**
 231      * Returns the MIME type given a filename.
 232      *
 233      * @param filename The name of the file for which we would like to look up the mime type.
 234      * @return An string with the right mime type.
 235      */
 236     function getType($filename) {
 237        // get base name of the filename provided by user
 238        $filename = basename(strtolower($filename));
 239  
 240        // break file into parts seperated by .
 241        $filename = explode('.', $filename);
 242  
 243        // take the last part of the file to get the file extension
 244        $filename = $filename[count($filename)-1];    
 245     
 246  
 247        // find mime type
 248        return $this->privFindType($filename);
 249     }
 250  
 251     /**
 252      * Helper function that finds the mime type.
 253      *
 254      * @private
 255      */
 256     function privFindType($ext) {
 257        // return mime type for extension
 258        if (isset($this->mimetypes[$ext])) {
 259         // die($this->mimetypes[$ext]);
 260           return $this->mimetypes[$ext];
 261        // if the extension wasn't found return octet-stream
 262        } else {
 263           return $this->_defaultType;
 264        }
 265     }
 266  
 267     /**
 268      * Returns the extension assigned to a given mime type. If more than one extension can be found,
 269      * it returns the first that can be found.
 270      *
 271      * @param mimetype The mime type for which we'd like to find the extension.
 272      * @return The extension assigned to the given mime type, or empty if none could be found.
 273      */
 274     function getExtension( $mimetype )
 275     {
 276             // swap the keys for the values
 277             $exts = array_flip( $this->mimetypes );
 278  
 279          // and now do the same search
 280          if( isset($exts[$mimetype]))
 281              $ext = $exts[$mimetype];
 282          else
 283              $ext = "";
 284  
 285          return $ext;
 286     }
 287  }
 288  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics