PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/functions.inc.php

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