PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/html/includes/Mime.php

https://bitbucket.org/kuy/ecom
PHP | 151 lines | 88 code | 49 blank | 14 comment | 7 complexity | c537ac69944746c0860d7c70ef5b9127 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /* Copyright (c) 2009 National Research Institute for Earth Science and
  3. * Disaster Prevention (NIED).
  4. * This code is licensed under the GPL 3.0 license, availible at the root
  5. * application directory.
  6. */
  7. ?>
  8. <?php
  9. /**
  10. * Description of Mime
  11. *
  12. * @author ikeda
  13. */
  14. class Mime {
  15. const ICON_FILE = '/image/icons/page.png';
  16. const ICON_TEXT = '/image/icons/page_white_edit.gif';
  17. const ICON_PDF = '/image/icons/icon_pdf.gif';
  18. const ICON_DOC = '/image/icons/page_white_word.png';
  19. const ICON_XLS = '/image/icons/page_white_excel.png';
  20. const ICON_PPT = '/image/icons/page_white_powerpoint.png';
  21. const ICON_MSOFFICE = '/image/icons/doc_offlice.png';
  22. const ICON_PICT = '/image/icons/page_white_picture.png';
  23. const ICON_AUDIO = '/image/icons/music.png';
  24. const ICON_MOVIE = '/image/icons/film.png';
  25. static private $instance;
  26. private $knownTypes;
  27. private function __construct() {
  28. // パターン一致範囲が狭いものから登録していく.
  29. $this->knownTypes = array(
  30. new MimeType( "text/plain", "|text/plain|", "テキストファイル", Mime::ICON_TEXT ),
  31. new MimeType( "application/pdf", "|application/pdf|", "PDFファイル", Mime::ICON_PDF ),
  32. new MimeType( "application/msword", "%application/.*(word|excel|powerpoint|office)%", "MS Office ファイル", Mime::ICON_MSOFFICE ),
  33. // new MimeType( "application/msword", "|application/.*word|", "Word文書", Mime::ICON_DOC ),
  34. // new MimeType( "application/vnd.ms-excel", "|application/.*excel|", "Excelファイル", Mime::ICON_XLS ),
  35. // new MimeType( "application/vnd.ms-powerpoint", "%application/.*(powerpoint|office)%", "PPTファイル", Mime::ICON_PPT ),
  36. new MimeType( "image/bmp", "|image/bmp|", "BMP画像", Mime::ICON_PICT ),
  37. new MimeType( "image/gif", "|image/gif|", "GIF画像", Mime::ICON_PICT ),
  38. new MimeType( "image/jpeg", "|image/jpeg|", "JPEG画像", Mime::ICON_PICT ),
  39. new MimeType( "image/png", "|image/png|", "PNG画像", Mime::ICON_PICT ),
  40. new MimeType( "image/tiff", "|image/tiff|", "TIF画像", Mime::ICON_PICT ),
  41. new MimeType( "application/octet-stream", "|^image/|", "画像ファイル", Mime::ICON_PICT ),
  42. new MimeType( "video/avi", "|video/avi|", "AVI動画", Mime::ICON_MOVIE ),
  43. new MimeType( "video/mp4", "|video/mp4|", "MP4動画", Mime::ICON_MOVIE ),
  44. new MimeType( "video/mpeg", "|video/mpeg|", "MPEG動画", Mime::ICON_MOVIE ),
  45. new MimeType( "video/quicktime", "|video/quicktime|", "QuickTime動画", Mime::ICON_MOVIE ),
  46. new MimeType( "video/vnd.rn-realvideo", "|video/vnd.rn-realvideo|", "RV動画", Mime::ICON_MOVIE ),
  47. new MimeType( "application/octet-stream", "|^video/|", "動画ファイル", Mime::ICON_MOVIE ),
  48. new MimeType( "application/octet-stream", "|^audio/|", "オーディオファイル", Mime::ICON_AUDIO ),
  49. new MimeType( "application/octet-stream", "/.*/", "ファイル", Mime::ICON_FILE )
  50. );
  51. }
  52. public function getInstance() {
  53. if ( null === Mime::$instance ) {
  54. Mime::$instance = new Mime();
  55. }
  56. return Mime::$instance;
  57. }
  58. public function getMimeType( $fileData ) {
  59. $type = null;
  60. if ( "yt:" != substr( $fileData->getFilePath(), 0, 3 ) ) {
  61. $contentType = mime_content_type_wrap( $fileData->getFilePath() );
  62. if ( 0 < ( $index = strpos( $contentType, ";" ) ) ) {
  63. $contentType = substr( $contentType, 0, $index );
  64. }
  65. if ( null === ( $type = $this->searchMimeType( $contentType ) ) ) {
  66. throw new Exception();
  67. }
  68. } else {
  69. $type = new MimeType( "youtube", "", "Youtube動画", ICON_MOVIE );
  70. }
  71. return $type;
  72. }
  73. private function searchMimeType( $typeStr ) {
  74. foreach ( $this->knownTypes as $type ) {
  75. if ( $type->check( $typeStr ) ) { return $type; }
  76. }
  77. return null;
  78. }
  79. }
  80. class MimeType {
  81. private $type;
  82. private $pattern;
  83. private $name;
  84. private $icon;
  85. public function __construct( $type, $pattern, $name, $icon ) {
  86. $this->setType( $type );
  87. $this->setPattern( $pattern );
  88. $this->setName( $name );
  89. $this->setIcon( $icon );
  90. }
  91. public function getType() { return $this->type; }
  92. public function getName() { return $this->name; }
  93. public function getIcon() { return $this->icon; }
  94. protected function setType( $type ) { $this->type = $type; }
  95. protected function setPattern( $pattern ) { $this->pattern = $pattern; }
  96. protected function setName( $name ) { $this->name = $name; }
  97. protected function setIcon( $icon ) { $this->icon = $icon; }
  98. function check( $contentType ) {
  99. return preg_match( $this->pattern, $contentType );
  100. }
  101. }
  102. ?>