From 90719148341ca38e8d8a331879d62053b1fd9195 Mon Sep 17 00:00:00 2001 From: Kevin Hamer <kevin@imarc.net> Date: Mon, 23 Jul 2012 15:24:33 -0400 Subject: [PATCH] Changes to fFile to better handle text filetype detection Improved the comment Changes to fFile to better handle text filetype detection Improved the comment --- fFile.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/fFile.php b/fFile.php index bbd902fe..36747062 100644 --- a/fFile.php +++ b/fFile.php @@ -338,8 +338,15 @@ static private function determineMimeTypeByContents($content, $extension) } - // Text files - if (strpos($content, '<?xml') !== FALSE) { + // Better detection for text files based on the first line or so. + if (strpos($content, '<?php') !== FALSE || strpos($content, '<?=') !== FALSE) { + return 'application/x-httpd-php'; + } + + preg_match('/(\S.*?)\s*\n/m', $content, $lines); + $first_line = count($lines) > 1 ? $lines[1] : ''; + + if (strpos($first_line, '<?xml') !== FALSE) { if (stripos($content, '<!DOCTYPE') !== FALSE) { return 'application/xhtml+xml'; } @@ -349,14 +356,17 @@ static private function determineMimeTypeByContents($content, $extension) if (strpos($content, '<rss') !== FALSE) { return 'application/rss+xml'; } - return 'application/xml'; - } + return 'application/xml'; + } - if (strpos($content, '<?php') !== FALSE || strpos($content, '<?=') !== FALSE) { - return 'application/x-httpd-php'; + if (stripos($first_line, '<html') !== FALSE) { + return 'text/html'; + } + if (stripos($first_line, '<!DOCTYPE') !== FALSE) { + return 'text/html'; } - if (preg_match('#^\#\![/a-z0-9]+(python|perl|php|ruby)$#mi', $content, $matches)) { + if (preg_match('#^\#\![/a-z0-9]+(python|perl|php|ruby)$#mi', $first_line, $matches)) { switch (strtolower($matches[1])) { case 'php': return 'application/x-httpd-php'; @@ -1341,4 +1351,4 @@ public function write($data) * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - */ \ No newline at end of file + */