Make WordPress Core


Ignore:
Timestamp:
12/13/2005 07:19:56 PM (21 years ago)
Author:
ryan
Message:

Attachment enhancements from skeltoac. fixes #2074

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r3295 r3303  
    743743                        @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
    744744
     745                        // If no filters change the filename, we'll do a default transformation.
     746                        if ( basename($file) == $thumb = apply_filters('thumbnail_filename', basename($file)) )
     747                                $thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1);
     748
     749                        $thumbpath = str_replace(basename($file), $thumb, $file);
     750
    745751                        // move the thumbnail to it's final destination
    746 
    747                         $path = explode('/', $file);
    748                         $thumbpath = substr($file, 0, strrpos($file, '/')).'/thumb-'.$path[count($path) - 1];
    749 
    750752                        if ($type[2] == 1) {
    751753                                if (!imagegif($thumbnail, $thumbpath)) {
    … …  
    772774                return $error;
    773775        } else {
    774                 return 1;
     776                return $thumbpath;
    775777        }
    776778}
    … …  
    16341636function wp_handle_upload(&$file, $overrides = false) {
    16351637        // The default error handler.
    1636         function wp_handle_upload_error(&$file, $message) {
    1637                 return array('error'=>$message);
     1638        if (! function_exists('wp_handle_upload_error') ) {
     1639                function wp_handle_upload_error(&$file, $message) {
     1640                        return array('error'=>$message);
     1641                }
    16381642        }
    16391643
    … …  
    16541658
    16551659        // Accepted MIME types are set here as PCRE. Override with $override['mimes'].
    1656         $mimes = apply_filters('upload_mimes', array(
    1657                         'image/jpeg'    =>      'jpg|jpeg|jpe',
    1658                         'image/gif'     =>      'gif',
    1659                         'image/(png|x-png)'     =>      'png',
    1660                         'image/(bmp|x-bmp|x-ms-bmp)'    =>      'bmp',
    1661                         'image/(tiff|x-tiff)'   =>      'tif|tiff',
    1662                         'image/(ico|x-ico)'     =>      'ico',
    1663                         'video/(asf|x-asf|x-ms-asf)'    =>      'asf|asx|wma|wax|wmv|wmx',
    1664                         'video/(wmv|x-wmv|x-ms-wmv)'    =>      'wmv',
    1665                         'video/(msvideo|x-msvideo)'     =>      'avi',
    1666                         'video/(quicktime|x-quicktime)' =>      'mov|qt',
    1667                         'video/(mpeg|x-mpeg)'   =>      'mpeg|mpg|mpe',
    1668                         'text/plain'    =>      'txt|c|cc|h|php',
    1669                         'text/richtext' =>      'rtx',
    1670                         'text/css'      =>      'css',
    1671                         'text/html'     =>      'htm|html',
    1672                         'text/javascript'       =>      'js',
    1673                         'audio/(mpeg|x-mpeg|mpeg3|x-mpeg3)'     =>      'mp3',
    1674                         'audio/x-realaudio'     =>      'ra|ram',
    1675                         'audio/(wav|x-wav)'     =>      'wav',
    1676                         'audio/(ogg|x-ogg)'     =>      'ogg',
    1677                         'audio/(midi|x-midi)'   =>      'mid|midi',
    1678                         'application/pdf'       =>      'pdf',
    1679                         'application/msword'    =>      'doc',
    1680                         'application/mspowerpoint'      =>      'pot|pps|ppt',
    1681                         'application/mswrite'   =>      'wri',
    1682                         'application/(msexcel|vnd.ms-excel)'    =>      'xla|xls|xlt|xlw',
    1683                         'application/msaccess'  =>      'mdb',
    1684                         'application/msproject' =>      'mpp',
    1685                         'application/x-shockwave-flash' =>      'swf',
    1686                         'application/java'      =>      'class',
    1687                         'application/x-tar'     =>      'tar',
    1688                         'application/(zip|x-zip-compressed)'    =>      'zip',
    1689                         'application/(x-gzip|x-gzip-compressed)'        =>      'gz|gzip'));
    1690 
    1691         // For security, we never trust HTTP Content-Type headers unless the user overrides this.
    1692         $trust_content_type = false;
     1660        $mimes = apply_filters('upload_mimes', array (
     1661                'jpg|jpeg|jpe' => 'image/jpeg',
     1662                'gif' => 'image/gif',
     1663                'png' => 'image/png',
     1664                'bmp' => 'image/bmp',
     1665                'tif|tiff' => 'image/tiff',
     1666                'ico' => 'image/x-icon',
     1667                'asf|asx|wax|wmv|wmx' => 'video/asf',
     1668                'avi' => 'video/avi',
     1669                'mov|qt' => 'video/quicktime',
     1670                'mpeg|mpg|mpe' => 'video/mpeg',
     1671                'txt|c|cc|h|php' => 'text/plain',
     1672                'rtx' => 'text/richtext',
     1673                'css' => 'text/css',
     1674                'htm|html' => 'text/html',
     1675                'mp3|mp4' => 'audio/mpeg',
     1676                'ra|ram' => 'audio/x-realaudio',
     1677                'wav' => 'audio/wav',
     1678                'ogg' => 'audio/ogg',
     1679                'mid|midi' => 'audio/midi',
     1680                'wma' => 'audio/wma',
     1681                'rtf' => 'application/rtf',
     1682                'js' => 'application/javascript',
     1683                'pdf' => 'application/pdf',
     1684                'doc' => 'application/msword',
     1685                'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
     1686                'wri' => 'application/vnd.ms-write',
     1687                'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
     1688                'mdb' => 'application/vnd.ms-access',
     1689                'mpp' => 'application/vnd.ms-project',
     1690                'swf' => 'application/x-shockwave-flash',
     1691                'class' => 'application/java',
     1692                'tar' => 'application/x-tar',
     1693                'zip' => 'application/zip',
     1694                'gz|gzip' => 'application/x-gzip',
     1695                'exe' => 'application/x-msdownload'
     1696        ));
    16931697
    16941698        // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
    16951699        $test_form = true;
    16961700        $test_size = true;
     1701
     1702        // If you override this, you must provide $ext and $type!!!!
    16971703        $test_type = true;
    16981704
    … …  
    17171723                return $upload_error_handler($file, __('Specified file failed upload test.'));
    17181724
    1719         // A correct MIME type will pass this test. We can't always determine it programatically, so we'll trust the HTTP headers.
     1725        // A correct MIME type will pass this test.
    17201726        if ( $test_type ) {
    17211727                $type = false;
    17221728                $ext = false;
    1723                 foreach ($mimes as $mime_preg => $ext_preg) {
    1724                         $mime_preg = '!^' . $mime_preg . '$!i';
     1729                foreach ($mimes as $ext_preg => $mime_match) {
    17251730                        $ext_preg = '![^.]\.(' . $ext_preg . ')$!i';
    1726                         if ( preg_match($mime_preg, $file['type'], $type) ) {
    1727                                 if ( preg_match($ext_preg, $file['name'], $ext) ) {
    1728                                         break;
    1729                                 } else {
    1730                                         return $upload_error_handler($file, __('File extension does not match file type. Try another.'));
    1731                                 }
    1732                         }
    1733                 }
    1734                 if (! $type && $ext )
     1731                        if ( preg_match($ext_preg, $file['name'], $ext_matches) ) {
     1732                                $type = $mime_match;
     1733                                $ext = $ext_matches[1];
     1734                        }
     1735                }
     1736
     1737                if ( !$type || !$ext )
    17351738                        return $upload_error_handler($file, __('File type does not meet security guidelines. Try another.'));
    1736                 $type = $type[0];
    1737                 $ext = $ext[1];
    17381739        }
    17391740
    … …  
    17551756        $new_file = $uploads['path'] . "/$filename";
    17561757        if ( false === move_uploaded_file($file['tmp_name'], $new_file) )
    1757                 die(__('The uploaded file could not be moved to $file.'));
     1758                die(printf(__('The uploaded file could not be moved to %s.'), $file['path']));
    17581759
    17591760        // Set correct file permissions
    … …  
    17651766        $url = $uploads['url'] . "/$filename";
    17661767
    1767         return array('file' => $new_file, 'url' => $url);
     1768        return array('file' => $new_file, 'url' => $url, 'type' => $type);
    17681769}
    17691770
    … …  
    18371838}
    18381839
     1840function the_attachment_links($id = false) {
     1841        $id = (int) $id;
     1842        $post = & get_post($id);
     1843
     1844        if ( $post->post_status != 'attachment' )
     1845                return false;
     1846
     1847        $icon = get_attachment_icon($post->ID);
     1848
    18391849?>
     1850<?php _e('Text linked to file') ?><br/>
     1851<textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo $post->guid ?>" class="attachmentlink"><?php echo basename($post->guid) ?></a></textarea>
     1852<?php _e('Text linked to subpost') ?><br/>
     1853<textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link($post->ID) ?>" rel="attachment" id="<?php echo $post->ID ?>"><?php echo $post->post_title ?></a></textarea>
     1854<?php if ( $icon ) : ?>
     1855<?php _e('Thumbnail linked to file') ?><br/>
     1856<textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo $post->guid ?>" class="attachmentlink"><img src="<?php echo $thumb ?>" class="attachmentthumb" alt="<?php echo $post->post_title ?>" /></a></textarea>
     1857<?php _e('Thumbnail linked to subpost') ?><br/>
     1858<textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link($post->ID) ?>" class="attachmentlink"><img src="<?php echo $icon ?>" class="attachmentthumb" alt="<?php echo $post->post_title ?>" /></a></textarea>
     1859<?php endif; ?>
     1860<?php
     1861}
     1862
     1863function get_udims($width, $height) {
     1864        if ( $height <= 96 && $width <= 128 )
     1865                return array($width, $height);
     1866        elseif ( $width / $height > 4 / 3 )
     1867                return array(128, (int) ($height / $width * 128));
     1868        else
     1869                return array((int) ($width / $height * 96), 96);
     1870}
     1871
     1872?>
Note: See TracChangeset for help on using the changeset viewer.