Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of InDefero, an open source project management application. # Copyright (C) 2008-2011 Céondo Ltd and contributors. # # InDefero is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # InDefero is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * File utilities. * */ class IDF_FileUtil { /** * Extension supported by the syntax highlighter. */ public static $supportedExtenstions = array ( 'ascx' , 'ashx' , 'asmx' , 'aspx' , 'browser' , 'bsh' , 'c' , 'cl' , 'cc' , 'config' , 'cpp' , 'cs' , 'csh' , 'csproj' , 'css' , 'cv' , 'cyc' , 'el' , 'fs' , 'h' , 'hh' , 'hpp' , 'hs' , 'html' , 'html' , 'java' , 'js' , 'lisp' , 'master' , 'pas' , 'perl' , 'php' , 'pl' , 'pm' , 'py' , 'rb' , 'scm' , 'sh' , 'sitemap' , 'skin' , 'sln' , 'svc' , 'vala' , 'vb' , 'vbproj' , 'vbs' , 'wsdl' , 'xhtml' , 'xml' , 'xsd' , 'xsl' , 'xslt' ); /** * Test if an extension is supported by the syntax highlighter. * * @param string The extension to test * @return bool */ public static function isSupportedExtension( $extension ) { return in_array( $extension , self:: $supportedExtenstions ); } /** * Returns a HTML snippet with a line-by-line pre-rendered table * for the given source content * * @param array file information as returned by getMimeType or getMimeTypeFromContent * @param string the content of the file * @return string */ public static function highLight( $fileinfo , $content ) { $pretty = '' ; if (self::isSupportedExtension( $fileinfo [2])) { $pretty = ' prettyprint' ; } $table = array (); $i = 1; foreach (preg_split( "/\015\012|\015|\012/" , $content ) as $line ) { $table [] = '<tr class="c-line"><td class="code-lc" id="L' . $i . '"><a href="#L' . $i . '">' . $i . '</a></td>' . '<td class="code mono' . $pretty . '">' .IDF_Diff::padLine(Pluf_esc( $line )). '</td></tr>' ; $i ++; } return Pluf_Template::markSafe(implode( "\n" , $table )); } /** * Find the mime type of a file. * * Use /etc/mime.types to find the type. * * @param string Filename/Filepath * @param array Mime type found or 'application/octet-stream', basename, extension */ public static function getMimeType( $file ) { static $mimes = null; if ( $mimes == null) { $mimes = array (); $src = Pluf::f( 'idf_mimetypes_db' , '/etc/mime.types' ); $filecontent = @ file_get_contents ( $src ); if ( $filecontent !== false) { $mimes = preg_split( "/\015\012|\015|\012/" , $filecontent ); } } $info = pathinfo ( $file ); if (isset( $info [ 'extension' ])) { foreach ( $mimes as $mime ) { if ( '#' != substr ( $mime , 0, 1)) { $elts = preg_split( '/ |\t/' , $mime , -1, PREG_SPLIT_NO_EMPTY); if (in_array( $info [ 'extension' ], $elts )) { return array ( $elts [0], $info [ 'basename' ], $info [ 'extension' ]); } } } } else { // we consider that if no extension and base name is all // uppercase, then we have a text file. if ( $info [ 'basename' ] == strtoupper ( $info [ 'basename' ])) { return array ( 'text/plain' , $info [ 'basename' ], 'txt' ); } $info [ 'extension' ] = 'bin' ; } return array ( 'application/octet-stream' , $info [ 'basename' ], $info [ 'extension' ]); } /** * Find the mime type of a file using the fileinfo class. * * @param string Filename/Filepath * @param string File content * @return array Mime type found or 'application/octet-stream', basename, extension */ public static function getMimeTypeFromContent( $file , $filedata ) { $info = pathinfo ( $file ); $res = array ( 'application/octet-stream' , $info [ 'basename' ], isset( $info [ 'extension' ]) ? $info [ 'extension' ] : 'bin' ); if (function_exists( 'finfo_open' )) { $finfo = finfo_open(FILEINFO_MIME); $mime = finfo_buffer( $finfo , $filedata ); finfo_close( $finfo ); if ( $mime ) { $res [0] = $mime ; } if (!isset( $info [ 'extension' ]) && $mime ) { $res [2] = (0 === strpos ( $mime , 'text/' )) ? 'txt' : 'bin' ; } elseif (!isset( $info [ 'extension' ])) { $res [2] = 'bin' ; } } return $res ; } /** * Find if a given mime type is a text file. * This uses the output of the self::getMimeType function. * * @param array (Mime type, file name, extension) * @return bool Is text */ public static function isText( $fileinfo ) { if (0 === strpos ( $fileinfo [0], 'text/' )) { return true; } $ext = 'mdtext php-dist h gitignore diff patch' ; $extra_ext = trim(Pluf::f( 'idf_extra_text_ext' , '' )); if (! empty ( $extra_ext )) $ext .= ' ' . $extra_ext ; $ext = array_merge (self:: $supportedExtenstions , explode ( ' ' , $ext )); return (in_array( $fileinfo [2], $ext )); } } |