PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/functions.inc.php

https://github.com/cauld/simple-php-framework
PHP | 751 lines | 626 code | 71 blank | 54 comment | 109 complexity | ef1a7ee862a5b607245fda3a00e5d2f7 MD5 | raw file
  1. <?PHP
  2. function printr($var)
  3. {
  4. $output = print_r($var, true);
  5. $output = str_replace("\n", "<br>", $output);
  6. $output = str_replace(' ', '&nbsp;', $output);
  7. echo "<div style='font-family:courier;'>$output</div>";
  8. }
  9. // Formats a given number of seconds into proper mm:ss format
  10. function format_time($seconds)
  11. {
  12. return floor($seconds / 60) . ':' . str_pad($seconds % 60, 2, '0');
  13. }
  14. // Given a string such as "comment_123" or "id_57", it returns the final, numeric id.
  15. function split_id($str)
  16. {
  17. return match('/[_-]([0-9]+)$/', $str, 1);
  18. }
  19. // Creates a friendly URL slug from a string
  20. function slugify($str)
  21. {
  22. $str = preg_replace('/[^a-zA-Z0-9 -]/', '', $str);
  23. $str = strtolower(str_replace(' ', '-', trim($str)));
  24. $str = preg_replace('/-+/', '-', $str);
  25. return $str;
  26. }
  27. // Computes the *full* URL of the current page (protocol, server, path, query parameters, etc)
  28. function full_url()
  29. {
  30. $s = empty($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'on') ? 's' : '';
  31. $protocol = substr(strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos(strtolower($_SERVER['SERVER_PROTOCOL']), '/')) . $s;
  32. $port = ($_SERVER['SERVER_PORT'] == '80') ? '' : (":".$_SERVER['SERVER_PORT']);
  33. return $protocol . "://" . $_SERVER['HTTP_HOST'] . $port . $_SERVER['REQUEST_URI'];
  34. }
  35. // Returns an English representation of a past date within the last month
  36. // Graciously stolen from http://ejohn.org/files/pretty.js
  37. function time2str($ts)
  38. {
  39. if(!ctype_digit($ts))
  40. $ts = strtotime($ts);
  41. $diff = time() - $ts;
  42. if($diff == 0)
  43. return 'now';
  44. elseif($diff > 0)
  45. {
  46. $day_diff = floor($diff / 86400);
  47. if($day_diff == 0)
  48. {
  49. if($diff < 60) return 'just now';
  50. if($diff < 120) return '1 minute ago';
  51. if($diff < 3600) return floor($diff / 60) . ' minutes ago';
  52. if($diff < 7200) return '1 hour ago';
  53. if($diff < 86400) return floor($diff / 3600) . ' hours ago';
  54. }
  55. if($day_diff == 1) return 'Yesterday';
  56. if($day_diff < 7) return $day_diff . ' days ago';
  57. if($day_diff < 31) return ceil($day_diff / 7) . ' weeks ago';
  58. if($day_diff < 60) return 'last month';
  59. return date('F Y', $ts);
  60. }
  61. else
  62. {
  63. $diff = abs($diff);
  64. $day_diff = floor($diff / 86400);
  65. if($day_diff == 0)
  66. {
  67. if($diff < 120) return 'in a minute';
  68. if($diff < 3600) return 'in ' . floor($diff / 60) . ' minutes';
  69. if($diff < 7200) return 'in an hour';
  70. if($diff < 86400) return 'in ' . floor($diff / 3600) . ' hours';
  71. }
  72. if($day_diff == 1) return 'Tomorrow';
  73. if($day_diff < 4) return date('l', $ts);
  74. if($day_diff < 7 + (7 - date('w'))) return 'next week';
  75. if(ceil($day_diff / 7) < 4) return 'in ' . ceil($day_diff / 7) . ' weeks';
  76. if(date('n', $ts) == date('n') + 1) return 'next month';
  77. return date('F Y', $ts);
  78. }
  79. }
  80. // Returns an array representation of the given calendar month.
  81. // The array values are timestamps which allow you to easily format
  82. // and manipulate the dates as needed.
  83. function calendar($month = null, $year = null)
  84. {
  85. if(is_null($month)) $month = date('n');
  86. if(is_null($year)) $year = date('Y');
  87. $first = mktime(0, 0, 0, $month, 1, $year);
  88. $last = mktime(23, 59, 59, $month, date('t', $first), $year);
  89. $start = $first - (86400 * date('w', $first));
  90. $stop = $last + (86400 * (7 - date('w', $first)));
  91. $out = array();
  92. while($start < $stop)
  93. {
  94. $week = array();
  95. if($start > $last) break;
  96. for($i = 0; $i < 7; $i++)
  97. {
  98. $week[$i] = $start;
  99. $start += 86400;
  100. }
  101. $out[] = $week;
  102. }
  103. return $out;
  104. }
  105. // Processes mod_rewrite URLs into key => value pairs
  106. // See .htacess for more info.
  107. function pick_off($grab_first = false, $sep = '/')
  108. {
  109. $ret = array();
  110. $arr = explode($sep, trim($_SERVER['REQUEST_URI'], $sep));
  111. if($grab_first) $ret[0] = array_shift($arr);
  112. while(count($arr) > 0)
  113. $ret[array_shift($arr)] = array_shift($arr);
  114. return (count($ret) > 0) ? $ret : false;
  115. }
  116. // Creates a list of <option>s from the given database table.
  117. // table name, column to use as value, column(s) to use as text, default value(s) to select (can accept an array of values), extra sql to limit results
  118. function get_options($table, $val, $text, $default = null, $sql = '')
  119. {
  120. $db = Database::getDatabase(true);
  121. $out = '';
  122. $table = $db->escape($table);
  123. $rows = $db->getRows("SELECT * FROM `$table` $sql");
  124. foreach($rows as $row)
  125. {
  126. $the_text = '';
  127. if(!is_array($text)) $text = array($text); // Allows you to concat multiple fields for display
  128. foreach($text as $t)
  129. $the_text .= $row[$t] . ' ';
  130. $the_text = htmlspecialchars(trim($the_text));
  131. if(!is_null($default) && $row[$val] == $default)
  132. $out .= '<option value="' . htmlspecialchars($row[$val], ENT_QUOTES) . '" selected="selected">' . $the_text . '</option>';
  133. elseif(is_array($default) && in_array($row[$val],$default))
  134. $out .= '<option value="' . htmlspecialchars($row[$val], ENT_QUOTES) . '" selected="selected">' . $the_text . '</option>';
  135. else
  136. $out .= '<option value="' . htmlspecialchars($row[$val], ENT_QUOTES) . '">' . $the_text . '</option>';
  137. }
  138. return $out;
  139. }
  140. // More robust strict date checking for string representations
  141. function chkdate($str)
  142. {
  143. // Requires PHP 5.2
  144. if(function_exists('date_parse'))
  145. {
  146. $info = date_parse($str);
  147. if($info !== false && $info['error_count'] == 0)
  148. {
  149. if(checkdate($info['month'], $info['day'], $info['year']))
  150. return true;
  151. }
  152. return false;
  153. }
  154. // Else, for PHP < 5.2
  155. return strtotime($str);
  156. }
  157. // Converts a date/timestamp into the specified format
  158. function dater($date = null, $format = null)
  159. {
  160. if(is_null($format))
  161. $format = 'Y-m-d H:i:s';
  162. if(is_null($date))
  163. $date = time();
  164. // if $date contains only numbers, treat it as a timestamp
  165. if(ctype_digit($date) === true)
  166. return date($format, $date);
  167. else
  168. return date($format, strtotime($date));
  169. }
  170. // Formats a phone number as (xxx) xxx-xxxx or xxx-xxxx depending on the length.
  171. function format_phone($phone)
  172. {
  173. $phone = preg_replace("/[^0-9]/", '', $phone);
  174. if(strlen($phone) == 7)
  175. return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
  176. elseif(strlen($phone) == 10)
  177. return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
  178. else
  179. return $phone;
  180. }
  181. // Outputs hour, minute, am/pm dropdown boxes
  182. function hourmin($hid = 'hour', $mid = 'minute', $pid = 'ampm', $hval = null, $mval = null, $pval = null)
  183. {
  184. // Dumb hack to let you just pass in a timestamp instead
  185. if(func_num_args() == 1)
  186. {
  187. list($hval, $mval, $pval) = explode(' ', date('g i a', strtotime($hid)));
  188. $hid = 'hour';
  189. $mid = 'minute';
  190. $aid = 'ampm';
  191. }
  192. else
  193. {
  194. if(is_null($hval)) $hval = date('h');
  195. if(is_null($mval)) $mval = date('i');
  196. if(is_null($pval)) $pval = date('a');
  197. }
  198. $hours = array(12, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11);
  199. $out = "<select name='$hid' id='$hid'>";
  200. foreach($hours as $hour)
  201. if(intval($hval) == intval($hour)) $out .= "<option value='$hour' selected>$hour</option>";
  202. else $out .= "<option value='$hour'>$hour</option>";
  203. $out .= "</select>";
  204. $minutes = array('00', 15, 30, 45);
  205. $out .= "<select name='$mid' id='$mid'>";
  206. foreach($minutes as $minute)
  207. if(intval($mval) == intval($minute)) $out .= "<option value='$minute' selected>$minute</option>";
  208. else $out .= "<option value='$minute'>$minute</option>";
  209. $out .= "</select>";
  210. $out .= "<select name='$pid' id='$pid'>";
  211. $out .= "<option value='am'>am</option>";
  212. if($pval == 'pm') $out .= "<option value='pm' selected>pm</option>";
  213. else $out .= "<option value='pm'>pm</option>";
  214. $out .= "</select>";
  215. return $out;
  216. }
  217. // Returns the HTML for a month, day, and year dropdown boxes.
  218. // You can set the default date by passing in a timestamp OR a parseable date string.
  219. // $prefix_ will be appened to the name/id's of each dropdown, allowing for multiple calls in the same form.
  220. // $output_format lets you specify which dropdowns appear and in what order.
  221. function mdy($date = null, $prefix = null, $output_format = 'm d y')
  222. {
  223. if(is_null($date)) $date = time();
  224. if(!ctype_digit($date)) $date = strtotime($date);
  225. if(!is_null($prefix)) $prefix .= '_';
  226. list($yval, $mval, $dval) = explode(' ', date('Y n j', $date));
  227. $month_dd = "<select name='{$prefix}month' id='{$prefix}month'>";
  228. for($i = 1; $i <= 12; $i++)
  229. {
  230. $selected = ($mval == $i) ? ' selected="selected"' : '';
  231. $month_dd .= "<option value='$i'$selected>" . date('F', mktime(0, 0, 0, $i, 1, 2000)) . "</option>";
  232. }
  233. $month_dd .= "</select>";
  234. $day_dd = "<select name='{$prefix}day' id='{$prefix}day'>";
  235. for($i = 1; $i <= 31; $i++)
  236. {
  237. $selected = ($dval == $i) ? ' selected="selected"' : '';
  238. $day_dd .= "<option value='$i'$selected>$i</option>";
  239. }
  240. $day_dd .= "</select>";
  241. $year_dd = "<select name='{$prefix}year' id='{$prefix}year'>";
  242. for($i = date('Y'); $i < date('Y') + 10; $i++)
  243. {
  244. $selected = ($yval == $i) ? ' selected="selected"' : '';
  245. $year_dd .= "<option value='$i'$selected>$i</option>";
  246. }
  247. $year_dd .= "</select>";
  248. $trans = array('m' => $month_dd, 'd' => $day_dd, 'y' => $year_dd);
  249. return strtr($output_format, $trans);
  250. }
  251. // Redirects user to $url
  252. function redirect($url = null)
  253. {
  254. if(is_null($url)) $url = $_SERVER['PHP_SELF'];
  255. header("Location: $url");
  256. exit();
  257. }
  258. // Ensures $str ends with a single /
  259. function slash($str)
  260. {
  261. return rtrim($str, '/') . '/';
  262. }
  263. // Ensures $str DOES NOT end with a /
  264. function unslash($str)
  265. {
  266. return rtrim($str, '/');
  267. }
  268. // Returns an array of the values of the specified column from a multi-dimensional array
  269. function gimme($arr, $key = null)
  270. {
  271. if(is_null($key))
  272. $key = current(array_keys($arr));
  273. $out = array();
  274. foreach($arr as $a)
  275. $out[] = $a[$key];
  276. return $out;
  277. }
  278. // Fixes MAGIC_QUOTES
  279. function fix_slashes($arr = '')
  280. {
  281. if(is_null($arr) || $arr == '') return null;
  282. if(!get_magic_quotes_gpc()) return $arr;
  283. return is_array($arr) ? array_map('fix_slashes', $arr) : stripslashes($arr);
  284. }
  285. // Returns the first $num words of $str
  286. function max_words($str, $num, $suffix = '')
  287. {
  288. $words = explode(' ', $str);
  289. if(count($words) < $num)
  290. return $str;
  291. else
  292. return implode(' ', array_slice($words, 0, $num)) . $suffix;
  293. }
  294. // Serves an external document for download as an HTTP attachment.
  295. function download_document($filename, $mimetype = 'application/octet-stream')
  296. {
  297. if(!file_exists($filename) || !is_readable($filename)) return false;
  298. $base = basename($filename);
  299. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  300. header("Content-Disposition: attachment; filename=$base");
  301. header("Content-Length: " . filesize($filename));
  302. header("Content-Type: $mimetype");
  303. readfile($filename);
  304. exit();
  305. }
  306. // Retrieves the filesize of a remote file.
  307. function remote_filesize($url, $user = null, $pw = null)
  308. {
  309. $ch = curl_init($url);
  310. curl_setopt($ch, CURLOPT_HEADER, 1);
  311. curl_setopt($ch, CURLOPT_NOBODY, 1);
  312. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  313. if(!is_null($user) && !is_null($pw))
  314. {
  315. $headers = array('Authorization: Basic ' . base64_encode("$user:$pw"));
  316. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  317. }
  318. $head = curl_exec($ch);
  319. curl_close($ch);
  320. preg_match('/Content-Length:\s([0-9].+?)\s/', $head, $matches);
  321. return isset($matches[1]) ? $matches[1] : false;
  322. }
  323. // Outputs a filesize in human readable format.
  324. function bytes2str($val, $round = 0)
  325. {
  326. $unit = array('','K','M','G','T','P','E','Z','Y');
  327. while($val >= 1000)
  328. {
  329. $val /= 1024;
  330. array_shift($unit);
  331. }
  332. return round($val, $round) . array_shift($unit) . 'B';
  333. }
  334. // Tests for a valid email address and optionally tests for valid MX records, too.
  335. function valid_email($email, $test_mx = false)
  336. {
  337. if(preg_match("/^([_a-z0-9+-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $email))
  338. {
  339. if($test_mx)
  340. {
  341. list( , $domain) = explode("@", $email);
  342. return getmxrr($domain, $mxrecords);
  343. }
  344. else
  345. return true;
  346. }
  347. else
  348. return false;
  349. }
  350. // Grabs the contents of a remote URL. Can perform basic authentication if un/pw are provided.
  351. function geturl($url, $username = null, $password = null)
  352. {
  353. if(function_exists('curl_init'))
  354. {
  355. $ch = curl_init();
  356. if(!is_null($username) && !is_null($password))
  357. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$username:$password")));
  358. curl_setopt($ch, CURLOPT_URL, $url);
  359. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  360. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  361. $html = curl_exec($ch);
  362. curl_close($ch);
  363. return $html;
  364. }
  365. elseif(ini_get('allow_url_fopen') == true)
  366. {
  367. if(!is_null($username) && !is_null($password))
  368. $url = str_replace("://", "://$username:$password@", $url);
  369. $html = file_get_contents($url);
  370. return $html;
  371. }
  372. else
  373. {
  374. // Cannot open url. Either install curl-php or set allow_url_fopen = true in php.ini
  375. return false;
  376. }
  377. }
  378. // Returns the user's browser info.
  379. // browscap.ini must be available for this to work.
  380. // See the PHP manual for more details.
  381. function browser_info()
  382. {
  383. $info = get_browser(null, true);
  384. $browser = $info['browser'] . ' ' . $info['version'];
  385. $os = $info['platform'];
  386. $ip = $_SERVER['REMOTE_ADDR'];
  387. return array('ip' => $ip, 'browser' => $browser, 'os' => $os);
  388. }
  389. // Quick wrapper for preg_match
  390. function match($regex, $str, $i = 0)
  391. {
  392. if(preg_match($regex, $str, $match) == 1)
  393. return $match[$i];
  394. else
  395. return false;
  396. }
  397. // Sends an HTML formatted email
  398. function send_html_mail($to, $subject, $msg, $from, $plaintext = '')
  399. {
  400. if(!is_array($to)) $to = array($to);
  401. foreach($to as $address)
  402. {
  403. $boundary = uniqid(rand(), true);
  404. $headers = "From: $from\n";
  405. $headers .= "MIME-Version: 1.0\n";
  406. $headers .= "Content-Type: multipart/alternative; boundary = $boundary\n";
  407. $headers .= "This is a MIME encoded message.\n\n";
  408. $headers .= "--$boundary\n" .
  409. "Content-Type: text/plain; charset=ISO-8859-1\n" .
  410. "Content-Transfer-Encoding: base64\n\n";
  411. $headers .= chunk_split(base64_encode($plaintext));
  412. $headers .= "--$boundary\n" .
  413. "Content-Type: text/html; charset=ISO-8859-1\n" .
  414. "Content-Transfer-Encoding: base64\n\n";
  415. $headers .= chunk_split(base64_encode($msg));
  416. $headers .= "--$boundary--\n" .
  417. mail($address, $subject, '', $headers);
  418. }
  419. }
  420. // Returns the lat, long of an address via Yahoo!'s geocoding service.
  421. // You'll need an App ID, which is available from here:
  422. // http://developer.yahoo.com/maps/rest/V1/geocode.html
  423. function geocode($location, $appid)
  424. {
  425. $location = urlencode($location);
  426. $appid = urlencode($appid);
  427. $data = file_get_contents("http://local.yahooapis.com/MapsService/V1/geocode?output=php&appid=$appid&location=$location");
  428. $data = unserialize($data);
  429. if($data === false) return false;
  430. $data = $data['ResultSet']['Result'];
  431. return array('lat' => $data['Latitude'], 'lng' => $data['Longitude']);
  432. }
  433. // Quick and dirty wrapper for curl scraping.
  434. function curl($url, $referer = null, $post = null)
  435. {
  436. static $tmpfile;
  437. if(!isset($tmpfile) || ($tmpfile == '')) $tmpfile = tempnam('/tmp', 'FOO');
  438. $ch = curl_init($url);
  439. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  440. curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
  441. curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
  442. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  443. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
  444. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  445. // curl_setopt($ch, CURLOPT_VERBOSE, 1);
  446. if($referer) curl_setopt($ch, CURLOPT_REFERER, $referer);
  447. if(!is_null($post))
  448. {
  449. curl_setopt($ch, CURLOPT_POST, true);
  450. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  451. }
  452. $html = curl_exec($ch);
  453. // $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  454. return $html;
  455. }
  456. // Accepts any number of arguments and returns the first non-empty one
  457. function pick()
  458. {
  459. foreach(func_get_args() as $arg)
  460. if(!empty($arg))
  461. return $arg;
  462. return '';
  463. }
  464. // Secure a PHP script using basic HTTP authentication
  465. function http_auth($un, $pw, $realm = "Secured Area")
  466. {
  467. if(!(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_USER'] == $un && $_SERVER['PHP_AUTH_PW'] == $pw))
  468. {
  469. header('WWW-Authenticate: Basic realm="' . $realm . '"');
  470. header('Status: 401 Unauthorized');
  471. exit();
  472. }
  473. }
  474. // This is easier than typing 'echo WEB_ROOT'
  475. function WEBROOT()
  476. {
  477. echo WEB_ROOT;
  478. }
  479. // Class Autloader
  480. function __autoload($class_name)
  481. {
  482. require DOC_ROOT . '/includes/class.' . strtolower($class_name) . '.php';
  483. }
  484. // Returns a file's mimetype based on its extension
  485. function mime_type($filename, $default = 'application/octet-stream')
  486. {
  487. $mime_types = array('323' => 'text/h323',
  488. 'acx' => 'application/internet-property-stream',
  489. 'ai' => 'application/postscript',
  490. 'aif' => 'audio/x-aiff',
  491. 'aifc' => 'audio/x-aiff',
  492. 'aiff' => 'audio/x-aiff',
  493. 'asf' => 'video/x-ms-asf',
  494. 'asr' => 'video/x-ms-asf',
  495. 'asx' => 'video/x-ms-asf',
  496. 'au' => 'audio/basic',
  497. 'avi' => 'video/x-msvideo',
  498. 'axs' => 'application/olescript',
  499. 'bas' => 'text/plain',
  500. 'bcpio' => 'application/x-bcpio',
  501. 'bin' => 'application/octet-stream',
  502. 'bmp' => 'image/bmp',
  503. 'c' => 'text/plain',
  504. 'cat' => 'application/vnd.ms-pkiseccat',
  505. 'cdf' => 'application/x-cdf',
  506. 'cer' => 'application/x-x509-ca-cert',
  507. 'class' => 'application/octet-stream',
  508. 'clp' => 'application/x-msclip',
  509. 'cmx' => 'image/x-cmx',
  510. 'cod' => 'image/cis-cod',
  511. 'cpio' => 'application/x-cpio',
  512. 'crd' => 'application/x-mscardfile',
  513. 'crl' => 'application/pkix-crl',
  514. 'crt' => 'application/x-x509-ca-cert',
  515. 'csh' => 'application/x-csh',
  516. 'css' => 'text/css',
  517. 'dcr' => 'application/x-director',
  518. 'der' => 'application/x-x509-ca-cert',
  519. 'dir' => 'application/x-director',
  520. 'dll' => 'application/x-msdownload',
  521. 'dms' => 'application/octet-stream',
  522. 'doc' => 'application/msword',
  523. 'dot' => 'application/msword',
  524. 'dvi' => 'application/x-dvi',
  525. 'dxr' => 'application/x-director',
  526. 'eps' => 'application/postscript',
  527. 'etx' => 'text/x-setext',
  528. 'evy' => 'application/envoy',
  529. 'exe' => 'application/octet-stream',
  530. 'fif' => 'application/fractals',
  531. 'flac' => 'audio/flac',
  532. 'flr' => 'x-world/x-vrml',
  533. 'gif' => 'image/gif',
  534. 'gtar' => 'application/x-gtar',
  535. 'gz' => 'application/x-gzip',
  536. 'h' => 'text/plain',
  537. 'hdf' => 'application/x-hdf',
  538. 'hlp' => 'application/winhlp',
  539. 'hqx' => 'application/mac-binhex40',
  540. 'hta' => 'application/hta',
  541. 'htc' => 'text/x-component',
  542. 'htm' => 'text/html',
  543. 'html' => 'text/html',
  544. 'htt' => 'text/webviewhtml',
  545. 'ico' => 'image/x-icon',
  546. 'ief' => 'image/ief',
  547. 'iii' => 'application/x-iphone',
  548. 'ins' => 'application/x-internet-signup',
  549. 'isp' => 'application/x-internet-signup',
  550. 'jfif' => 'image/pipeg',
  551. 'jpe' => 'image/jpeg',
  552. 'jpeg' => 'image/jpeg',
  553. 'jpg' => 'image/jpeg',
  554. 'js' => 'application/x-javascript',
  555. 'latex' => 'application/x-latex',
  556. 'lha' => 'application/octet-stream',
  557. 'lsf' => 'video/x-la-asf',
  558. 'lsx' => 'video/x-la-asf',
  559. 'lzh' => 'application/octet-stream',
  560. 'm13' => 'application/x-msmediaview',
  561. 'm14' => 'application/x-msmediaview',
  562. 'm3u' => 'audio/x-mpegurl',
  563. 'man' => 'application/x-troff-man',
  564. 'mdb' => 'application/x-msaccess',
  565. 'me' => 'application/x-troff-me',
  566. 'mht' => 'message/rfc822',
  567. 'mhtml' => 'message/rfc822',
  568. 'mid' => 'audio/mid',
  569. 'mny' => 'application/x-msmoney',
  570. 'mov' => 'video/quicktime',
  571. 'movie' => 'video/x-sgi-movie',
  572. 'mp2' => 'video/mpeg',
  573. 'mp3' => 'audio/mpeg',
  574. 'mpa' => 'video/mpeg',
  575. 'mpe' => 'video/mpeg',
  576. 'mpeg' => 'video/mpeg',
  577. 'mpg' => 'video/mpeg',
  578. 'mpp' => 'application/vnd.ms-project',
  579. 'mpv2' => 'video/mpeg',
  580. 'ms' => 'application/x-troff-ms',
  581. 'mvb' => 'application/x-msmediaview',
  582. 'nws' => 'message/rfc822',
  583. 'oda' => 'application/oda',
  584. 'oga' => 'audio/ogg',
  585. 'ogg' => 'audio/ogg',
  586. 'ogv' => 'video/ogg',
  587. 'ogx' => 'application/ogg',
  588. 'p10' => 'application/pkcs10',
  589. 'p12' => 'application/x-pkcs12',
  590. 'p7b' => 'application/x-pkcs7-certificates',
  591. 'p7c' => 'application/x-pkcs7-mime',
  592. 'p7m' => 'application/x-pkcs7-mime',
  593. 'p7r' => 'application/x-pkcs7-certreqresp',
  594. 'p7s' => 'application/x-pkcs7-signature',
  595. 'pbm' => 'image/x-portable-bitmap',
  596. 'pdf' => 'application/pdf',
  597. 'pfx' => 'application/x-pkcs12',
  598. 'pgm' => 'image/x-portable-graymap',
  599. 'pko' => 'application/ynd.ms-pkipko',
  600. 'pma' => 'application/x-perfmon',
  601. 'pmc' => 'application/x-perfmon',
  602. 'pml' => 'application/x-perfmon',
  603. 'pmr' => 'application/x-perfmon',
  604. 'pmw' => 'application/x-perfmon',
  605. 'pnm' => 'image/x-portable-anymap',
  606. 'pot' => 'application/vnd.ms-powerpoint',
  607. 'ppm' => 'image/x-portable-pixmap',
  608. 'pps' => 'application/vnd.ms-powerpoint',
  609. 'ppt' => 'application/vnd.ms-powerpoint',
  610. 'prf' => 'application/pics-rules',
  611. 'ps' => 'application/postscript',
  612. 'pub' => 'application/x-mspublisher',
  613. 'qt' => 'video/quicktime',
  614. 'ra' => 'audio/x-pn-realaudio',
  615. 'ram' => 'audio/x-pn-realaudio',
  616. 'ras' => 'image/x-cmu-raster',
  617. 'rgb' => 'image/x-rgb',
  618. 'rmi' => 'audio/mid',
  619. 'roff' => 'application/x-troff',
  620. 'rtf' => 'application/rtf',
  621. 'rtx' => 'text/richtext',
  622. 'scd' => 'application/x-msschedule',
  623. 'sct' => 'text/scriptlet',
  624. 'setpay' => 'application/set-payment-initiation',
  625. 'setreg' => 'application/set-registration-initiation',
  626. 'sh' => 'application/x-sh',
  627. 'shar' => 'application/x-shar',
  628. 'sit' => 'application/x-stuffit',
  629. 'snd' => 'audio/basic',
  630. 'spc' => 'application/x-pkcs7-certificates',
  631. 'spl' => 'application/futuresplash',
  632. 'src' => 'application/x-wais-source',
  633. 'sst' => 'application/vnd.ms-pkicertstore',
  634. 'stl' => 'application/vnd.ms-pkistl',
  635. 'stm' => 'text/html',
  636. 'svg' => "image/svg+xml",
  637. 'sv4cpio' => 'application/x-sv4cpio',
  638. 'sv4crc' => 'application/x-sv4crc',
  639. 't' => 'application/x-troff',
  640. 'tar' => 'application/x-tar',
  641. 'tcl' => 'application/x-tcl',
  642. 'tex' => 'application/x-tex',
  643. 'texi' => 'application/x-texinfo',
  644. 'texinfo' => 'application/x-texinfo',
  645. 'tgz' => 'application/x-compressed',
  646. 'tif' => 'image/tiff',
  647. 'tiff' => 'image/tiff',
  648. 'tr' => 'application/x-troff',
  649. 'trm' => 'application/x-msterminal',
  650. 'tsv' => 'text/tab-separated-values',
  651. 'txt' => 'text/plain',
  652. 'uls' => 'text/iuls',
  653. 'ustar' => 'application/x-ustar',
  654. 'vcf' => 'text/x-vcard',
  655. 'vrml' => 'x-world/x-vrml',
  656. 'wav' => 'audio/x-wav',
  657. 'wcm' => 'application/vnd.ms-works',
  658. 'wdb' => 'application/vnd.ms-works',
  659. 'wks' => 'application/vnd.ms-works',
  660. 'wmf' => 'application/x-msmetafile',
  661. 'wps' => 'application/vnd.ms-works',
  662. 'wri' => 'application/x-mswrite',
  663. 'wrl' => 'x-world/x-vrml',
  664. 'wrz' => 'x-world/x-vrml',
  665. 'xaf' => 'x-world/x-vrml',
  666. 'xbm' => 'image/x-xbitmap',
  667. 'xla' => 'application/vnd.ms-excel',
  668. 'xlc' => 'application/vnd.ms-excel',
  669. 'xlm' => 'application/vnd.ms-excel',
  670. 'xls' => 'application/vnd.ms-excel',
  671. 'xlt' => 'application/vnd.ms-excel',
  672. 'xlw' => 'application/vnd.ms-excel',
  673. 'xof' => 'x-world/x-vrml',
  674. 'xpm' => 'image/x-xpixmap',
  675. 'xwd' => 'image/x-xwindowdump',
  676. 'z' => 'application/x-compress',
  677. 'zip' => 'application/zip');
  678. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  679. return isset($mime_types[$ext]) ? $mime_types[$ext] : $default;
  680. }