PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/js/fckeditor/editor/filemanager/connectors/php/util.php

https://bitbucket.org/waqar4at/scrumie
PHP | 220 lines | 138 code | 28 blank | 54 comment | 22 complexity | 7021268a0ec1ecb17aeff8b8e7e0acd7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  4. * Copyright (C) 2003-2010 Frederico Caldeira Knabben
  5. *
  6. * == BEGIN LICENSE ==
  7. *
  8. * Licensed under the terms of any of the following licenses at your
  9. * choice:
  10. *
  11. * - GNU General Public License Version 2 or later (the "GPL")
  12. * http://www.gnu.org/licenses/gpl.html
  13. *
  14. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  15. * http://www.gnu.org/licenses/lgpl.html
  16. *
  17. * - Mozilla Public License Version 1.1 or later (the "MPL")
  18. * http://www.mozilla.org/MPL/MPL-1.1.html
  19. *
  20. * == END LICENSE ==
  21. *
  22. * Utility functions for the File Manager Connector for PHP.
  23. */
  24. function RemoveFromStart( $sourceString, $charToRemove )
  25. {
  26. $sPattern = '|^' . $charToRemove . '+|' ;
  27. return preg_replace( $sPattern, '', $sourceString ) ;
  28. }
  29. function RemoveFromEnd( $sourceString, $charToRemove )
  30. {
  31. $sPattern = '|' . $charToRemove . '+$|' ;
  32. return preg_replace( $sPattern, '', $sourceString ) ;
  33. }
  34. function FindBadUtf8( $string )
  35. {
  36. $regex =
  37. '([\x00-\x7F]'.
  38. '|[\xC2-\xDF][\x80-\xBF]'.
  39. '|\xE0[\xA0-\xBF][\x80-\xBF]'.
  40. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'.
  41. '|\xED[\x80-\x9F][\x80-\xBF]'.
  42. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'.
  43. '|[\xF1-\xF3][\x80-\xBF]{3}'.
  44. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'.
  45. '|(.{1}))';
  46. while (preg_match('/'.$regex.'/S', $string, $matches)) {
  47. if ( isset($matches[2])) {
  48. return true;
  49. }
  50. $string = substr($string, strlen($matches[0]));
  51. }
  52. return false;
  53. }
  54. function ConvertToXmlAttribute( $value )
  55. {
  56. if ( defined( 'PHP_OS' ) )
  57. {
  58. $os = PHP_OS ;
  59. }
  60. else
  61. {
  62. $os = php_uname() ;
  63. }
  64. if ( strtoupper( substr( $os, 0, 3 ) ) === 'WIN' || FindBadUtf8( $value ) )
  65. {
  66. return ( utf8_encode( htmlspecialchars( $value ) ) ) ;
  67. }
  68. else
  69. {
  70. return ( htmlspecialchars( $value ) ) ;
  71. }
  72. }
  73. /**
  74. * Check whether given extension is in html etensions list
  75. *
  76. * @param string $ext
  77. * @param array $htmlExtensions
  78. * @return boolean
  79. */
  80. function IsHtmlExtension( $ext, $htmlExtensions )
  81. {
  82. if ( !$htmlExtensions || !is_array( $htmlExtensions ) )
  83. {
  84. return false ;
  85. }
  86. $lcaseHtmlExtensions = array() ;
  87. foreach ( $htmlExtensions as $key => $val )
  88. {
  89. $lcaseHtmlExtensions[$key] = strtolower( $val ) ;
  90. }
  91. return in_array( $ext, $lcaseHtmlExtensions ) ;
  92. }
  93. /**
  94. * Detect HTML in the first KB to prevent against potential security issue with
  95. * IE/Safari/Opera file type auto detection bug.
  96. * Returns true if file contain insecure HTML code at the beginning.
  97. *
  98. * @param string $filePath absolute path to file
  99. * @return boolean
  100. */
  101. function DetectHtml( $filePath )
  102. {
  103. $fp = @fopen( $filePath, 'rb' ) ;
  104. //open_basedir restriction, see #1906
  105. if ( $fp === false || !flock( $fp, LOCK_SH ) )
  106. {
  107. return -1 ;
  108. }
  109. $chunk = fread( $fp, 1024 ) ;
  110. flock( $fp, LOCK_UN ) ;
  111. fclose( $fp ) ;
  112. $chunk = strtolower( $chunk ) ;
  113. if (!$chunk)
  114. {
  115. return false ;
  116. }
  117. $chunk = trim( $chunk ) ;
  118. if ( preg_match( "/<!DOCTYPE\W*X?HTML/sim", $chunk ) )
  119. {
  120. return true;
  121. }
  122. $tags = array( '<body', '<head', '<html', '<img', '<pre', '<script', '<table', '<title' ) ;
  123. foreach( $tags as $tag )
  124. {
  125. if( false !== strpos( $chunk, $tag ) )
  126. {
  127. return true ;
  128. }
  129. }
  130. //type = javascript
  131. if ( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) )
  132. {
  133. return true ;
  134. }
  135. //href = javascript
  136. //src = javascript
  137. //data = javascript
  138. if ( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) )
  139. {
  140. return true ;
  141. }
  142. //url(javascript
  143. if ( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) )
  144. {
  145. return true ;
  146. }
  147. return false ;
  148. }
  149. /**
  150. * Check file content.
  151. * Currently this function validates only image files.
  152. * Returns false if file is invalid.
  153. *
  154. * @param string $filePath absolute path to file
  155. * @param string $extension file extension
  156. * @param integer $detectionLevel 0 = none, 1 = use getimagesize for images, 2 = use DetectHtml for images
  157. * @return boolean
  158. */
  159. function IsImageValid( $filePath, $extension )
  160. {
  161. if (!@is_readable($filePath)) {
  162. return -1;
  163. }
  164. $imageCheckExtensions = array('gif', 'jpeg', 'jpg', 'png', 'swf', 'psd', 'bmp', 'iff');
  165. // version_compare is available since PHP4 >= 4.0.7
  166. if ( function_exists( 'version_compare' ) ) {
  167. $sCurrentVersion = phpversion();
  168. if ( version_compare( $sCurrentVersion, "4.2.0" ) >= 0 ) {
  169. $imageCheckExtensions[] = "tiff";
  170. $imageCheckExtensions[] = "tif";
  171. }
  172. if ( version_compare( $sCurrentVersion, "4.3.0" ) >= 0 ) {
  173. $imageCheckExtensions[] = "swc";
  174. }
  175. if ( version_compare( $sCurrentVersion, "4.3.2" ) >= 0 ) {
  176. $imageCheckExtensions[] = "jpc";
  177. $imageCheckExtensions[] = "jp2";
  178. $imageCheckExtensions[] = "jpx";
  179. $imageCheckExtensions[] = "jb2";
  180. $imageCheckExtensions[] = "xbm";
  181. $imageCheckExtensions[] = "wbmp";
  182. }
  183. }
  184. if ( !in_array( $extension, $imageCheckExtensions ) ) {
  185. return true;
  186. }
  187. if ( @getimagesize( $filePath ) === false ) {
  188. return false ;
  189. }
  190. return true;
  191. }
  192. ?>