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