dolibarr  13.0.2
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  {
83  $os = PHP_OS;
84  } else {
85  $os = php_uname();
86  }
87 
88  if (strtoupper(substr($os, 0, 3)) === 'WIN' || FindBadUtf8($value))
89  {
90  return (utf8_encode(htmlspecialchars($value)));
91  } else {
92  return (htmlspecialchars($value));
93  }
94 }
95 
103 function IsHtmlExtension($ext, $formExtensions)
104 {
105  if (!$formExtensions || !is_array($formExtensions))
106  {
107  return false;
108  }
109  $lcaseHtmlExtensions = array();
110  foreach ($formExtensions as $key => $val)
111  {
112  $lcaseHtmlExtensions[$key] = strtolower($val);
113  }
114  return in_array($ext, $lcaseHtmlExtensions);
115 }
116 
125 function DetectHtml($filePath)
126 {
127  $fp = @fopen($filePath, 'rb');
128 
129  //open_basedir restriction, see #1906
130  if ($fp === false || !flock($fp, LOCK_SH))
131  {
132  return -1;
133  }
134 
135  $chunk = fread($fp, 1024);
136  flock($fp, LOCK_UN);
137  fclose($fp);
138 
139  $chunk = strtolower($chunk);
140 
141  if (!$chunk)
142  {
143  return false;
144  }
145 
146  $chunk = trim($chunk);
147 
148  if (preg_match("/<!DOCTYPE\W*X?HTML/sim", $chunk))
149  {
150  return true;
151  }
152 
153  $tags = array('<body', '<head', '<html', '<img', '<pre', '<script', '<table', '<title');
154 
155  foreach ($tags as $tag)
156  {
157  if (false !== strpos($chunk, $tag))
158  {
159  return true;
160  }
161  }
162 
163  //type = javascript
164  if (preg_match('!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk))
165  {
166  return true;
167  }
168 
169  //href = javascript
170  //src = javascript
171  //data = javascript
172  if (preg_match('!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk))
173  {
174  return true;
175  }
176 
177  //url(javascript
178  if (preg_match('!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk))
179  {
180  return true;
181  }
182 
183  return false;
184 }
185 
195 function IsImageValid($filePath, $extension)
196 {
197  if (!@is_readable($filePath)) {
198  return -1;
199  }
200 
201  $imageCheckExtensions = array(
202  'gif',
203  'jpeg',
204  'jpg',
205  'png',
206  'swf',
207  'psd',
208  'bmp',
209  'iff',
210  'tiff',
211  'tif',
212  'swc',
213  'jpc',
214  'jp2',
215  'jpx',
216  'jb2',
217  'xbm',
218  'wbmp'
219  );
220 
221  if (!in_array($extension, $imageCheckExtensions)) {
222  return true;
223  }
224 
225  if (@getimagesize($filePath) === false) {
226  return false;
227  }
228 
229  return true;
230 }