Skip to content

Commit

Permalink
Refactor finfo usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vboctor committed Jan 10, 2018
1 parent 44a060b commit bbc631e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 53 deletions.
34 changes: 18 additions & 16 deletions core/file_api.php
Expand Up @@ -989,6 +989,22 @@ function file_ensure_uploaded( array $p_file ) {
}
}

/**
* Return instance of fileinfo class if enabled in php
* @return finfo instance of finfo class.
*/
function file_create_finfo() {

This comment has been minimized.

Copy link
@Ashwin-Kapes

Ashwin-Kapes Jan 23, 2018

Now getting error in this file. @vboctor
<b>Fatal error</b>: Class 'finfo' not found in <b>/home/dummy/www.example.com/dash/core/file_api.php</b> on line <b>1000</b><br />

This comment has been minimized.

Copy link
@atrol

atrol Jan 23, 2018

Member

@Ashwin-Kapes you have to install the PHP fileinfo extension http://www.php.net/manual/en/fileinfo.installation.php

@vboctor should we reintroduce the former runtime check or should we document the extension as mandatory and treat non existing finfo class as error instead of warning when running admin/check.php?

I introduced the current behavior in 2.9.0, see the commit comment why I thought it could be implemented this way 5f6292f

$t_info_file = config_get_global( 'fileinfo_magic_db_file' );

if( is_blank( $t_info_file ) ) {
$t_finfo = new finfo( FILEINFO_MIME );
} else {
$t_finfo = new finfo( FILEINFO_MIME, $t_info_file );
}

return $t_finfo;
}

/**
* Get mime type for the specified file.
*
Expand All @@ -1000,14 +1016,7 @@ function file_get_mime_type( $p_file_path ) {
return false;
}

$t_info_file = config_get_global( 'fileinfo_magic_db_file' );

if( is_blank( $t_info_file ) ) {
$t_finfo = new finfo( FILEINFO_MIME );
} else {
$t_finfo = new finfo( FILEINFO_MIME, $t_info_file );
}

$t_finfo = file_create_finfo();
return $t_finfo->file( $p_file_path );
}

Expand All @@ -1018,14 +1027,7 @@ function file_get_mime_type( $p_file_path ) {
* @return boolean|string The mime type or false on failure.
*/
function file_get_mime_type_for_content( $p_content ) {
$t_info_file = config_get_global( 'fileinfo_magic_db_file' );

if( is_blank( $t_info_file ) ) {
$t_finfo = new finfo( FILEINFO_MIME );
} else {
$t_finfo = new finfo( FILEINFO_MIME, $t_info_file );
}

$t_finfo = file_create_finfo();
return $t_finfo->buffer( $p_content );
}

Expand Down
12 changes: 5 additions & 7 deletions core/plugin_api.php
Expand Up @@ -32,6 +32,7 @@
* @uses database_api.php
* @uses error_api.php
* @uses event_api.php
* @uses file_api.php
* @uses helper_api.php
* @uses history_api.php
* @uses lang_api.php
Expand All @@ -44,6 +45,7 @@
require_api( 'database_api.php' );
require_api( 'error_api.php' );
require_api( 'event_api.php' );
require_api( 'file_api.php' );
require_api( 'helper_api.php' );
require_api( 'history_api.php' );
require_api( 'lang_api.php' );
Expand Down Expand Up @@ -214,13 +216,9 @@ function plugin_file_include( $p_filename, $p_basename = null ) {
}

$t_content_type = '';
$t_finfo = finfo_get_if_available();

if( $t_finfo ) {
$t_file_info_type = $t_finfo->file( $t_file_path );
if( $t_file_info_type !== false ) {
$t_content_type = $t_file_info_type;
}
$t_file_info_type = file_get_mime_type( $t_file_path );
if( $t_file_info_type !== false ) {
$t_content_type = $t_file_info_type;
}

# allow overriding the content type for specific text and image extensions
Expand Down
21 changes: 0 additions & 21 deletions core/utility_api.php
Expand Up @@ -295,24 +295,3 @@ function get_font_path() {
}
return $t_font_path;
}

/**
* Return instance of fileinfo class if enabled in php
* @return finfo
*/
function finfo_get_if_available() {

$t_info_file = config_get_global( 'fileinfo_magic_db_file' );

if( is_blank( $t_info_file ) ) {
$t_finfo = new finfo( FILEINFO_MIME );
} else {
$t_finfo = new finfo( FILEINFO_MIME, $t_info_file );
}

if( $t_finfo ) {
return $t_finfo;
}

return null;
}
12 changes: 3 additions & 9 deletions file_download.php
Expand Up @@ -156,10 +156,6 @@
$t_filename = file_get_display_name( $v_filename );

# Content headers

# If finfo is available (always true for PHP >= 5.3.0) we can use it to determine the MIME type of files
$t_finfo = finfo_get_if_available();

$t_content_type = $v_file_type;

$t_content_type_override = file_get_content_type_override( $t_filename );
Expand All @@ -168,14 +164,12 @@
switch( $t_upload_method ) {
case DISK:
$t_local_disk_file = file_normalize_attachment_path( $v_diskfile, $t_project_id );
if( file_exists( $t_local_disk_file ) && $t_finfo ) {
$t_file_info_type = $t_finfo->file( $t_local_disk_file );
if( file_exists( $t_local_disk_file ) ) {
$t_file_info_type = file_get_mime_type( $t_local_disk_file );
}
break;
case DATABASE:
if ( $t_finfo ) {
$t_file_info_type = $t_finfo->buffer( $v_content );
}
$t_file_info_type = file_get_mime_type_for_content( $v_content );
break;
default:
trigger_error( ERROR_GENERIC, ERROR );
Expand Down

0 comments on commit bbc631e

Please sign in to comment.