3 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
4 * Copyright (C) 2003-2008 Frederico Caldeira Knabben
8 * Licensed under the terms of any of the following licenses at your
11 * - GNU General Public License Version 2 or later (the "GPL")
12 * http://www.gnu.org/licenses/gpl.html
14 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
15 * http://www.gnu.org/licenses/lgpl.html
17 * - Mozilla Public License Version 1.1 or later (the "MPL")
18 * http://www.mozilla.org/MPL/MPL-1.1.html
22 * Utility functions for the File Manager Connector for PHP.
25 function RemoveFromStart( $sourceString, $charToRemove )
27 $sPattern = '|^' . $charToRemove . '+|' ;
28 return preg_replace( $sPattern, '', $sourceString ) ;
31 function RemoveFromEnd( $sourceString, $charToRemove )
33 $sPattern = '|' . $charToRemove . '+$|' ;
34 return preg_replace( $sPattern, '', $sourceString ) ;
37 function FindBadUtf8( $string )
41 '|[\xC2-\xDF][\x80-\xBF]'.
42 '|\xE0[\xA0-\xBF][\x80-\xBF]'.
43 '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'.
44 '|\xED[\x80-\x9F][\x80-\xBF]'.
45 '|\xF0[\x90-\xBF][\x80-\xBF]{2}'.
46 '|[\xF1-\xF3][\x80-\xBF]{3}'.
47 '|\xF4[\x80-\x8F][\x80-\xBF]{2}'.
50 while (preg_match('/'.$regex.'/S', $string, $matches)) {
51 if ( isset($matches[2])) {
54 $string = substr($string, strlen($matches[0]));
60 function ConvertToXmlAttribute( $value )
62 if ( defined( 'PHP_OS' ) )
71 if ( strtoupper( substr( $os, 0, 3 ) ) === 'WIN' || FindBadUtf8( $value ) )
73 return ( utf8_encode( htmlspecialchars( $value ) ) ) ;
77 return ( htmlspecialchars( $value ) ) ;
82 * Check whether given extension is in html etensions list
85 * @param array $htmlExtensions
88 function IsHtmlExtension( $ext, $htmlExtensions )
90 if ( !$htmlExtensions || !is_array( $htmlExtensions ) )
94 $lcaseHtmlExtensions = array() ;
95 foreach ( $htmlExtensions as $key => $val )
97 $lcaseHtmlExtensions[$key] = strtolower( $val ) ;
99 return in_array( $ext, $lcaseHtmlExtensions ) ;
103 * Detect HTML in the first KB to prevent against potential security issue with
104 * IE/Safari/Opera file type auto detection bug.
105 * Returns true if file contain insecure HTML code at the beginning.
107 * @param string $filePath absolute path to file
110 function DetectHtml( $filePath )
112 $fp = @fopen( $filePath, 'rb' ) ;
114 //open_basedir restriction, see #1906
115 if ( $fp === false || !flock( $fp, LOCK_SH ) )
120 $chunk = fread( $fp, 1024 ) ;
121 flock( $fp, LOCK_UN ) ;
124 $chunk = strtolower( $chunk ) ;
131 $chunk = trim( $chunk ) ;
133 if ( preg_match( "/<!DOCTYPE\W*X?HTML/sim", $chunk ) )
138 $tags = array( '<body', '<head', '<html', '<img', '<pre', '<script', '<table', '<title' ) ;
140 foreach( $tags as $tag )
142 if( false !== strpos( $chunk, $tag ) )
149 if ( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) )
157 if ( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) )
163 if ( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) )
172 * Check file content.
173 * Currently this function validates only image files.
174 * Returns false if file is invalid.
176 * @param string $filePath absolute path to file
177 * @param string $extension file extension
178 * @param integer $detectionLevel 0 = none, 1 = use getimagesize for images, 2 = use DetectHtml for images
181 function IsImageValid( $filePath, $extension )
183 if (!@is_readable($filePath)) {
187 $imageCheckExtensions = array('gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 'bmp', 'iff');
189 // version_compare is available since PHP4 >= 4.0.7
190 if ( function_exists( 'version_compare' ) ) {
191 $sCurrentVersion = phpversion();
192 if ( version_compare( $sCurrentVersion, "4.2.0" ) >= 0 ) {
193 $imageCheckExtensions[] = "tiff";
194 $imageCheckExtensions[] = "tif";
196 if ( version_compare( $sCurrentVersion, "4.3.0" ) >= 0 ) {
197 $imageCheckExtensions[] = "swc";
199 if ( version_compare( $sCurrentVersion, "4.3.2" ) >= 0 ) {
200 $imageCheckExtensions[] = "jpc";
201 $imageCheckExtensions[] = "jp2";
202 $imageCheckExtensions[] = "jpx";
203 $imageCheckExtensions[] = "jb2";
204 $imageCheckExtensions[] = "xbm";
205 $imageCheckExtensions[] = "wbmp";
209 if ( !in_array( $extension, $imageCheckExtensions ) ) {
213 if ( @getimagesize( $filePath ) === false ) {