PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/kerphi/contrib/pfcInstaller2/MIME/Type.php

https://bitbucket.org/shashwat_dinasource/bitscentral
PHP | 395 lines | 189 code | 42 blank | 164 comment | 27 complexity | fc4b7ea3d1ecaefa1c6ec01af761b3b8 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Ian Eure <ieure@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Type.php,v 1.2 2004/08/07 22:19:04 ieure Exp $
  20. require_once 'PEAR.php';
  21. $_fileCmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
  22. $_fileCmd = 'file';
  23. /**
  24. * Class for working with MIME types
  25. *
  26. * @version 1.0.0
  27. * @package MIME_Type
  28. * @author Ian Eure <ieure@php.net>
  29. */
  30. class MIME_Type {
  31. /**
  32. * The MIME media type
  33. *
  34. * @var string
  35. */
  36. var $media = '';
  37. /**
  38. * The MIME media sub-type
  39. *
  40. * @var string
  41. */
  42. var $subType = '';
  43. /**
  44. * Optional MIME parameters
  45. *
  46. * @var array
  47. */
  48. var $parameters = array();
  49. /**
  50. * List of valid media types
  51. *
  52. * @var array
  53. */
  54. var $validMediaTypes = array(
  55. 'text',
  56. 'image',
  57. 'audio',
  58. 'video',
  59. 'application',
  60. 'multipart',
  61. 'message'
  62. );
  63. /**
  64. * Constructor.
  65. *
  66. * If $type is set, if will be parsed and the appropriate class vars set. If not,
  67. * you get an empty class. This is useful, but not quite as useful as parsing a
  68. * type.
  69. *
  70. * @param string $type MIME type
  71. * @return void
  72. */
  73. function MIME_Type($type = false)
  74. {
  75. if ($type) {
  76. $this->parse($type);
  77. }
  78. }
  79. /**
  80. * Parse a mime-type
  81. *
  82. * @param $type string MIME type to parse
  83. * @return void
  84. */
  85. function parse($type)
  86. {
  87. $this->media = $this->getMedia($type);
  88. $this->subType = $this->getSubType($type);
  89. if (MIME_Type::hasParameters($type)) {
  90. require_once 'MIME/Type/Parameter.php';
  91. foreach (MIME_Type::getParameters($type) as $param) {
  92. $param = &new MIME_Type_Parameter($param);
  93. $this->parameters[$param->name] = $param;
  94. }
  95. }
  96. }
  97. /**
  98. * Does this type have any parameters?
  99. *
  100. * @param $type string MIME type to check
  101. * @return boolean true if $type has parameters, false otherwise
  102. * @static
  103. */
  104. function hasParameters($type)
  105. {
  106. if (strstr($type, ';')) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Get a MIME type's parameters
  113. *
  114. * @param $type string MIME type to get parameters of
  115. * @return array $type's parameters
  116. * @static
  117. */
  118. function getParameters($type)
  119. {
  120. $params = array();
  121. $tmp = explode(';', $type);
  122. for ($i = 1; $i < count($tmp); $i++) {
  123. $params[] = trim($tmp[$i]);
  124. }
  125. return $params;
  126. }
  127. /**
  128. * Strip paramaters from a MIME type string
  129. *
  130. * @param string $type MIME type string
  131. * @return string MIME type with parameters removed
  132. * @static
  133. */
  134. function stripParameters($type)
  135. {
  136. if (strstr($type, ';')) {
  137. return substr($type, 0, strpos($type, ';'));
  138. }
  139. return $type;
  140. }
  141. /**
  142. * Get a MIME type's media
  143. *
  144. * @note 'media' refers to the portion before the first slash
  145. * @param $type string MIME type to get media of
  146. * @return string $type's media
  147. * @static
  148. */
  149. function getMedia($type)
  150. {
  151. $tmp = explode('/', $type);
  152. return strtolower($tmp[0]);
  153. }
  154. /**
  155. * Get a MIME type's subtype
  156. *
  157. * @param $type string MIME type to get subtype of
  158. * @return string $type's subtype
  159. * @static
  160. */
  161. function getSubType($type)
  162. {
  163. $tmp = explode('/', $type);
  164. $tmp = explode(';', $tmp[1]);
  165. return strtolower(trim($tmp[0]));
  166. }
  167. /**
  168. * Create a textual MIME type from object values
  169. *
  170. * This function performs the opposite function of parse().
  171. *
  172. * @return string MIME type string
  173. */
  174. function get()
  175. {
  176. $type = strtolower($this->media.'/'.$this->subType);
  177. if (count($this->parameters)) {
  178. foreach ($this->parameters as $key => $null) {
  179. $type .= '; '.$this->parameters[$key]->get();
  180. }
  181. }
  182. return $type;
  183. }
  184. /**
  185. * Is this type experimental?
  186. *
  187. * @note Experimental types are denoted by a leading 'x-' in the media or
  188. * subtype, e.g. text/x-vcard or x-world/x-vrml.
  189. * @param string $type MIME type to check
  190. * @return boolean true if $type is experimental, false otherwise
  191. * @static
  192. */
  193. function isExperimental($type)
  194. {
  195. if (substr(MIME_Type::getMedia($type), 0, 2) == 'x-' ||
  196. substr(MIME_Type::getSubType($type), 0, 2) == 'x-') {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /**
  202. * Is this a vendor MIME type?
  203. *
  204. * @note Vendor types are denoted with a leading 'vnd. in the subtype.
  205. * @param string $type MIME type to check
  206. * @return boolean true if $type is a vendor type, false otherwise
  207. * @static
  208. */
  209. function isVendor($type)
  210. {
  211. if (substr(MIME_Type::getSubType($type), 0, 4) == 'vnd.') {
  212. return true;
  213. }
  214. return false;
  215. }
  216. /**
  217. * Is this a wildcard type?
  218. *
  219. * @param string $type MIME type to check
  220. * @return boolean true if $type is a wildcard, false otherwise
  221. * @static
  222. */
  223. function isWildcard($type)
  224. {
  225. if ($type == '*/*' || MIME_Type::getSubtype($type) == '*') {
  226. return true;
  227. }
  228. return false;
  229. }
  230. /**
  231. * Perform a wildcard match on a MIME type
  232. *
  233. * Example:
  234. * MIME_Type::wildcardMatch('image/*', 'image/png')
  235. *
  236. * @param string $card Wildcard to check against
  237. * @param string $type MIME type to check
  238. * @return boolean true if there was a match, false otherwise
  239. */
  240. function wildcardMatch($card, $type)
  241. {
  242. if (!MIME_Type::isWildcard($card)) {
  243. return false;
  244. }
  245. if ($card == '*/*') {
  246. return true;
  247. }
  248. if (MIME_Type::getMedia($card) ==
  249. MIME_Type::getMedia($type)) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. /**
  255. * Add a parameter to this type
  256. *
  257. * @param string $name Attribute name
  258. * @param string $value Attribute value
  259. * @param string $comment Comment for this parameter
  260. * @return void
  261. */
  262. function addParameter($name, $value, $comment = false)
  263. {
  264. $tmp = &new MIME_Type_Parameter;
  265. $tmp->name = $name;
  266. $tmp->value = $value;
  267. $tmp->comment = $comment;
  268. $this->parameters[$name] = $tmp;
  269. }
  270. /**
  271. * Remove a parameter from this type
  272. *
  273. * @param string $name Parameter name
  274. * @return void
  275. */
  276. function removeParameter($name)
  277. {
  278. unset ($this->parameters[$name]);
  279. }
  280. /**
  281. * Autodetect a file's MIME-type
  282. *
  283. * This function may be called staticly.
  284. *
  285. * @param string $file Path to the file to get the type of
  286. * @param bool $params Append MIME parameters if true
  287. * @return string $file's MIME-type on success, PEAR_Error otherwise
  288. * @since 1.0.0beta1
  289. * @static
  290. */
  291. function autoDetect($file, $params = false)
  292. {
  293. @include_once 'System/Command.php';
  294. if (function_exists('mime_content_type')) {
  295. $type = mime_content_type($file);
  296. } else if (class_exists('System_Command')) {
  297. $type = MIME_Type::_fileAutoDetect($file);
  298. } else {
  299. return PEAR::raiseError("Sorry, can't autodetect; you need the mime_magic extension or System_Command and 'file' installed to use this function.");
  300. }
  301. // _fileAutoDetect() may have returned an error.
  302. if (PEAR::isError($type)) {
  303. return $type;
  304. }
  305. // Don't return an empty string
  306. if (!$type || !strlen($type)) {
  307. return PEAR::raiseError("Sorry, couldn't determine file type.");
  308. }
  309. // Strip parameters if present & requested
  310. if (MIME_Type::hasParameters($type) && !$params) {
  311. $type = MIME_Type::stripParameters($type);
  312. }
  313. return $type;
  314. }
  315. /**
  316. * Autodetect a file's MIME-type with 'file' and System_Command
  317. *
  318. * This function may be called staticly.
  319. *
  320. * @param string $file Path to the file to get the type of
  321. * @return string $file's MIME-type
  322. * @since 1.0.0beta1
  323. * @static
  324. */
  325. function _fileAutoDetect($file)
  326. {
  327. // Sanity checks
  328. if (!file_exists($file)) {
  329. return PEAR::raiseError("File \"$file\" doesn't exist");
  330. }
  331. if (!is_readable($file)) {
  332. return PEAR::raiseError("File \"$file\" is not readable");
  333. }
  334. $cmd = new System_Command;
  335. // Make sure we have the 'file' command.
  336. $fileCmd = PEAR::getStaticProperty('MIME_Type', 'fileCmd');
  337. if (!$cmd->which($fileCmd)) {
  338. unset($cmd);
  339. return PEAR::raiseError("Can't find file command \"{$fileCmd}\"");
  340. }
  341. $cmd->pushCommand($fileCmd, "-bi '{$file}'");
  342. $res = $cmd->execute();
  343. unset($cmd);
  344. return $res;
  345. }
  346. }