dolibarr  16.0.5
util.php
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  * https://www.gnu.org/licenses/gpl.html
13  *
14  * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
15  * https://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 
32 function RemoveFromStart($sourceString, $charToRemove)
33 {
34  $sPattern = '|^'.$charToRemove.'+|';
35  return preg_replace($sPattern, '', $sourceString);
36 }
37 
45 function RemoveFromEnd($sourceString, $charToRemove)
46 {
47  $sPattern = '|'.$charToRemove.'+$|';
48  return preg_replace($sPattern, '', $sourceString);
49 }
50 
57 function FindBadUtf8($string)
58 {
59  $regex = '([\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]';
60  $regex .= '|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2}|(.{1}))';
61 
62  $matches = array();
63  while (preg_match('/'.$regex.'/S', $string, $matches)) {
64  if (isset($matches[2])) {
65  return true;
66  }
67  $string = substr($string, strlen($matches[0]));
68  }
69 
70  return false;
71 }
72 
79 function ConvertToXmlAttribute($value)
80 {
81  if (defined('PHP_OS')) {
82  $os = PHP_OS;
83  } else {
84  $os = php_uname();
85  }
86 
87  if (strtoupper(substr($os, 0, 3)) === 'WIN' || FindBadUtf8($value)) {
88  return (utf8_encode(htmlspecialchars($value)));
89  } else {
90  return (htmlspecialchars($value));
91  }
92 }
93 
101 function IsHtmlExtension($ext, $formExtensions)
102 {
103  if (!$formExtensions || !is_array($formExtensions)) {
104  return false;
105  }
106  $lcaseHtmlExtensions = array();
107  foreach ($formExtensions as $key => $val) {
108  $lcaseHtmlExtensions[$key] = strtolower($val);
109  }
110  return in_array($ext, $lcaseHtmlExtensions);
111 }
112 
121 function DetectHtml($filePath)
122 {
123  $fp = @fopen($filePath, 'rb');
124 
125  //open_basedir restriction, see #1906
126  if ($fp === false || !flock($fp, LOCK_SH)) {
127  return -1;
128  }
129 
130  $chunk = fread($fp, 1024);
131  flock($fp, LOCK_UN);
132  fclose($fp);
133 
134  $chunk = strtolower($chunk);
135 
136  if (!$chunk) {
137  return false;
138  }
139 
140  $chunk = trim($chunk);
141 
142  if (preg_match("/<!DOCTYPE\W*X?HTML/sim", $chunk)) {
143  return true;
144  }
145 
146  $tags = array('<body', '<head', '<html', '<img', '<pre', '<script', '<table', '<title');
147 
148  foreach ($tags as $tag) {
149  if (false !== strpos($chunk, $tag)) {
150  return true;
151  }
152  }
153 
154  //type = javascript
155  if (preg_match('!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk)) {
156  return true;
157  }
158 
159  //href = javascript
160  //src = javascript
161  //data = javascript
162  if (preg_match('!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk)) {
163  return true;
164  }
165 
166  //url(javascript
167  if (preg_match('!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk)) {
168  return true;
169  }
170 
171  return false;
172 }
173 
183 function IsImageValid($filePath, $extension)
184 {
185  if (!@is_readable($filePath)) {
186  return -1;
187  }
188 
189  $imageCheckExtensions = array(
190  'gif',
191  'jpeg',
192  'jpg',
193  'png',
194  'swf',
195  'psd',
196  'bmp',
197  'iff',
198  'tiff',
199  'tif',
200  'swc',
201  'jpc',
202  'jp2',
203  'jpx',
204  'jb2',
205  'xbm',
206  'wbmp'
207  );
208 
209  if (!in_array($extension, $imageCheckExtensions)) {
210  return true;
211  }
212 
213  if (@getimagesize($filePath) === false) {
214  return false;
215  }
216 
217  return true;
218 }