diff options
author | Florian Pritz <bluewind@xinu.at> | 2011-12-18 17:17:34 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2011-12-18 17:17:34 +0100 |
commit | f502313f517af07b8edc8ef3d6ffee748831fd73 (patch) | |
tree | 2cabc6faf2cb0f757ad962f3cc15b9005dba004a | |
parent | 74bda3f086ab1922246107df01b577c5e7723bb9 (diff) |
factorise mode detection
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | application/models/file_mod.php | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/application/models/file_mod.php b/application/models/file_mod.php index af425b8df..2e51d5544 100644 --- a/application/models/file_mod.php +++ b/application/models/file_mod.php @@ -230,13 +230,14 @@ class File_mod extends CI_Model { // autodetect the mode for highlighting if the URL contains a / after the ID (/ID/) // /ID/mode disables autodetection - if (!$mode && substr_count(ltrim($this->uri->uri_string(), "/"), '/') >= 1) { - $mode = $this->mime2extension($type); - $mode = $this->filename2extension($filedata['filename']) ? $this->filename2extension($filedata['filename']) : $mode; + $autodetect_mode = !$mode && substr_count(ltrim($this->uri->uri_string(), "/"), '/') >= 1; + + if ($autodetect_mode) { + $mode = $this->get_highlight_mode($type, $filedata["filename"]); } // resolve aliases of modes // this is mainly used for compatibility - $mode = $this->extension_aliases($mode); + $mode = $this->resolve_mode_alias($mode); header("Last-Modified: ".date('D, d M Y H:i:s', $filedata["date"])." GMT"); header('Etag: "'.$etag.'"'); @@ -256,7 +257,7 @@ class File_mod extends CI_Model { } // if there is no mimetype mapping we can't highlight it - $can_highlight = $this->mime2extension($type); + $can_highlight = $this->can_highlight($type); $filesize_too_big = filesize($file) > $this->config->item('upload_max_text_size'); @@ -380,8 +381,35 @@ class File_mod extends CI_Model { return $random; } + // Allow certain types to be highlight without doing it automatically + function can_highlight($type) + { + $typearray = array( + 'image/svg+xml', + ); + if (in_array($type, $typearray)) return true; + + if ($this->mime2mode($type)) return true; + + return false; + } + + // Return the mode that should be used for highlighting + function get_highlight_mode($type, $filename) + { + $mode = $this->mime2mode($type); + + // filename modes overwrite mime type mappings + $filename_mode = $this->filename2mode($filename); + if ($filename_mode) { + return $filename_mode; + } + + return $mode; + } + // Map MIME types to extensions needed for highlighting - function mime2extension($type) + private function mime2mode($type) { $typearray = array( 'text/plain' => 'text', @@ -431,7 +459,7 @@ class File_mod extends CI_Model { } // Map special filenames to extensions - function filename2extension($name) + private function filename2mode($name) { $namearray = array( 'PKGBUILD' => 'bash', @@ -443,7 +471,7 @@ class File_mod extends CI_Model { } // Handle alias extensions - function extension_aliases($alias) + function resolve_mode_alias($alias) { if ($alias === false) return false; $aliasarray = array( |