Commit d05d2da3 authored by Robert Lyon's avatar Robert Lyon Committed by Aaron Wells
Browse files

Autodetect the location of the magicdb



Bug 1249858

Change-Id: I1531c0da4ffb1044817e2ff6be14dd7a50ceb5e6
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent bb8ed7e0
......@@ -284,17 +284,12 @@ $cfg->maximageresizememory = 104857600;
$cfg->pathtoclam = '';
/**
* @global mixed $cfg->pathtomagicdb Whether to use PHP's internal magicdb or an external one, and if external,
* where it's located.
* If you're having problems with Mahara recognizing uploaded filetypes correctly, try changing this setting.
* @global mixed $cfg->pathtomagicdb Set this value to specify where the PHP fileinfo "magic" DB is.
*
* The following values each has a different meaning:
* * (default) FALSE: use PHP's internal magicdb
* * NULL: use the magicdb at /usr/share/misc/magic, or if that's a
* symlink or directory, use /usr/share/misc/magic.mgc
* * Anything else: Use this value as the path to the magicdb
* If this value is NULL, Mahara will attempt to use PHP's internal magic db, or to
* autolocate your system's magic DB.
*/
$cfg->pathtomagicdb = false;
$cfg->pathtomagicdb = NULL;
/**
* @global string $cfg->pathtogzip
......
......@@ -3198,5 +3198,11 @@ function xmldb_core_upgrade($oldversion=0) {
)");
}
if ($oldversion < 2014040300) {
// Figure out where the magicdb is, and stick with that.
require_once(get_config('libroot') . 'file.php');
update_magicdb_path();
}
return $status;
}
......@@ -266,28 +266,17 @@ function byteserving_send_file($filename, $mimetype, $ranges) {
function file_mime_type($file, $originalfilename=false) {
static $mimetypes = null;
if (class_exists('finfo')) {
// upstream bug in php #54714
// http://bugs.php.net/bug.php?id=54714
//
// according to manual (http://www.php.net/manual/en/function.finfo-open.php)
// default option is /usr/share/misc/magic, then /usr/share/misc/magic.mgc
//
// if /usr/share/misc/magic is a directory then finfo still succeeds and
// doesn't fall back onto the .mcg magic_file
// force /usr/share/misc/magic.mgc instead in this case
$MAGICPATH = get_config('pathtomagicdb');
if ($MAGICPATH === null) {
$MAGICPATH = '/usr/share/misc/magic';
$magicfile = null;
if (is_dir($MAGICPATH) or is_link($MAGICPATH)) {
$magicfile = '/usr/share/misc/magic.mgc';
}
}
else {
$magicfile = $MAGICPATH;
}
if (get_config('pathtomagicdb') !== null) {
// Manually specified magicdb path in config.php
$magicfile = get_config('pathtomagicdb');
}
else {
// Using one of the system presets (or if no matching system preset, this
// will return false, indicating we shouldn't bother with fileinfo
$magicfile = standard_magic_paths(get_config('defaultmagicdb'));
}
if ($magicfile !== false && class_exists('finfo') ) {
if ($finfo = @new finfo(FILEINFO_MIME_TYPE, $magicfile)) {
$type = @$finfo->file($file);
}
......@@ -324,6 +313,74 @@ function file_mime_type($file, $originalfilename=false) {
}
/**
* The standard locations we would expect the magicdb to be. The keys of the array returned
* by this value, are the values stored in the config
* @param int $key (optional)
* @return multitype:string If a key is supplied, return the path matching that key. If no
* key is supplied, return the full array of possible magic locations.
*/
function standard_magic_paths($key = 'fullarray') {
static $standardmagicpaths = array(
1=>'',
2=>'/usr/share/misc/magic',
3=>'/usr/share/misc/magic.mgc',
);
if ($key === 'fullarray') {
return $standardmagicpaths;
}
if (array_key_exists($key, $standardmagicpaths)) {
return $standardmagicpaths[$key];
}
else {
return false;
}
}
/**
* Try a few different likely possibilities for the magicdb and see which of them returns
* the correct response. Then store that configuration option for later use, in the config
* setting 'defaultmagicdb'. Because this is a DB-settable setting, we don't store the file
* path directly in it, but instead just store a key corresponding to a path specified in
* standard_magic_paths().
*/
function update_magicdb_path() {
// Determine where the server's "magic" db is\
if (class_exists('finfo')) {
$file = get_config('docroot') . 'theme/raw/static/images/powered-by-mahara.png';
$magicpathstotry = standard_magic_paths();
$workingpath = false;
foreach ($magicpathstotry as $i=>$magicfile) {
$type = false;
if (defined('FILEINFO_MIME_TYPE')) {
if ($finfo = @new finfo(FILEINFO_MIME_TYPE, $magicfile)) {
$type = @$finfo->file($file);
}
}
else if ($finfo = @new finfo(FILEINFO_MIME, $magicfile)) {
if ($typecharset = @$finfo->file($file)) {
if ($bits = explode(';', $typecharset)) {
$type = $bits[0];
}
}
}
if ($type == 'image/png') {
$workingpath = $i;
break;
}
}
if (!$workingpath) {
log_debug('Could not locate the path to your fileinfo magic db. Please set it manually using $cfg->pathtomagicdb.');
$workingpath = 0;
}
set_config('defaultmagicdb', $workingpath);
}
}
/**
* Given a mimetype (perhaps returned by {@link get_mime_type}, returns whether
* Mahara thinks it is a valid image file.
......
......@@ -654,6 +654,9 @@ function core_postinst() {
);
update_safe_iframes($iframesources, $iframedomains);
require_once(get_config('docroot') . 'lib/file.php');
update_magicdb_path();
return $status;
}
......@@ -1498,4 +1501,4 @@ function install_watchlist_notification() {
$cron->month = '*';
$cron->dayofweek = '*';
ensure_record_exists('cron', $cron, $cron);
}
\ No newline at end of file
}
......@@ -15,7 +15,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/index.php/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2014040100;
$config->version = 2014040300;
$config->release = '1.10.0dev';
$config->minupgradefrom = 2009022600;
$config->minupgraderelease = '1.1.0 (release tag 1.1.0_RELEASE)';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment