dolibarr  13.0.2
io.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  * This is the File Manager Connector for PHP.
23  */
24 
32 function CombinePaths($sBasePath, $sFolder)
33 {
34  return RemoveFromEnd($sBasePath, '/').'/'.RemoveFromStart($sFolder, '/');
35 }
43 function GetResourceTypePath($resourceType, $sCommand)
44 {
45  global $Config;
46 
47  if ($sCommand == "QuickUpload")
48  return $Config['QuickUploadPath'][$resourceType];
49  else return $Config['FileTypesPath'][$resourceType];
50 }
51 
59 function GetResourceTypeDirectory($resourceType, $sCommand)
60 {
61  global $Config;
62  if ($sCommand == "QuickUpload")
63  {
64  if (strlen($Config['QuickUploadAbsolutePath'][$resourceType]) > 0)
65  return $Config['QuickUploadAbsolutePath'][$resourceType];
66 
67  // Map the "UserFiles" path to a local directory.
68  return Server_MapPath($Config['QuickUploadPath'][$resourceType]);
69  } else {
70  if (strlen($Config['FileTypesAbsolutePath'][$resourceType]) > 0)
71  return $Config['FileTypesAbsolutePath'][$resourceType];
72 
73  // Map the "UserFiles" path to a local directory.
74  return Server_MapPath($Config['FileTypesPath'][$resourceType]);
75  }
76 }
77 
86 function GetUrlFromPath($resourceType, $folderPath, $sCommand)
87 {
88  return CombinePaths(GetResourceTypePath($resourceType, $sCommand), $folderPath);
89 }
90 
97 function RemoveExtension($fileName)
98 {
99  return substr($fileName, 0, strrpos($fileName, '.'));
100 }
109 function ServerMapFolder($resourceType, $folderPath, $sCommand)
110 {
111  // Get the resource type directory.
112  $sResourceTypePath = GetResourceTypeDirectory($resourceType, $sCommand);
113 
114  // Ensure that the directory exists.
115  $sErrorMsg = CreateServerFolder($sResourceTypePath);
116  if ($sErrorMsg != '')
117  SendError(1, "Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})");
118 
119  // Return the resource type directory combined with the required path.
120  return CombinePaths($sResourceTypePath, $folderPath);
121 }
122 
129 function GetParentFolder($folderPath)
130 {
131  $sPattern = "-[/\\\\][^/\\\\]+[/\\\\]?$-";
132  return preg_replace($sPattern, '', $folderPath);
133 }
134 
142 function CreateServerFolder($folderPath, $lastFolder = null)
143 {
144  global $Config;
145  $sParent = GetParentFolder($folderPath);
146 
147  // Ensure the folder path has no double-slashes, or mkdir may fail on certain platforms
148  while (strpos($folderPath, '//') !== false)
149  {
150  $folderPath = str_replace('//', '/', $folderPath);
151  }
152 
153  // Check if the parent exists, or create it.
154  if (!empty($sParent) && !file_exists($sParent))
155  {
156  //prevents agains infinite loop when we can't create root folder
157  if (!is_null($lastFolder) && $lastFolder === $sParent) {
158  return "Can't create $folderPath directory";
159  }
160 
161  $sErrorMsg = CreateServerFolder($sParent, $folderPath);
162  if ($sErrorMsg != '')
163  return $sErrorMsg;
164  }
165 
166  if (!file_exists($folderPath))
167  {
168  // Turn off all error reporting.
169  error_reporting(0);
170 
171  $php_errormsg = '';
172  // Enable error tracking to catch the error.
173  ini_set('track_errors', '1');
174 
175  if (isset($Config['ChmodOnFolderCreate']) && !$Config['ChmodOnFolderCreate'])
176  {
177  mkdir($folderPath);
178  } else {
179  $permissions = '0777';
180  if (isset($Config['ChmodOnFolderCreate']) && $Config['ChmodOnFolderCreate'])
181  {
182  $permissions = (string) $Config['ChmodOnFolderCreate'];
183  }
184  $permissionsdec = octdec($permissions);
185  $permissionsdec |= octdec('0111'); // Set x bit required for directories
186  dol_syslog("io.php permission = ".$permissions." ".$permissionsdec." ".decoct($permissionsdec));
187  // To create the folder with 0777 permissions, we need to set umask to zero.
188  $oldumask = umask(0);
189  mkdir($folderPath, $permissionsdec);
190  umask($oldumask);
191  }
192 
193  $sErrorMsg = $php_errormsg;
194 
195  // Restore the configurations.
196  ini_restore('track_errors');
197  ini_restore('error_reporting');
198 
199  return $sErrorMsg;
200  } else return '';
201 }
202 
208 function GetRootPath()
209 {
210  if (!isset($_SERVER)) {
211  global $_SERVER;
212  }
213  $sRealPath = realpath('./');
214  // #2124 ensure that no slash is at the end
215  $sRealPath = rtrim($sRealPath, "\\/");
216 
217  $sSelfPath = $_SERVER['PHP_SELF'];
218  $sSelfPath = substr($sSelfPath, 0, strrpos($sSelfPath, '/'));
219 
220  $sSelfPath = str_replace('/', DIRECTORY_SEPARATOR, $sSelfPath);
221 
222  $position = strpos($sRealPath, $sSelfPath);
223 
224  // This can check only that this script isn't run from a virtual dir
225  // But it avoids the problems that arise if it isn't checked
226  if ($position === false || $position <> strlen($sRealPath) - strlen($sSelfPath))
227  SendError(1, 'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".');
228 
229  return substr($sRealPath, 0, $position);
230 }
231 
237 function Server_MapPath($path)
238 {
239  // This function is available only for Apache
240  if (function_exists('apache_lookup_uri')) {
241  $info = apache_lookup_uri($path);
242  return $info->filename.$info->path_info;
243  }
244 
245  // This isn't correct but for the moment there's no other solution
246  // If this script is under a virtual directory or symlink it will detect the problem and stop
247  return GetRootPath().$path;
248 }
249 
257 function IsAllowedExt($sExtension, $resourceType)
258 {
259  global $Config;
260  // Get the allowed and denied extensions arrays.
261  $arAllowed = $Config['AllowedExtensions'][$resourceType];
262  $arDenied = $Config['DeniedExtensions'][$resourceType];
263 
264  if (count($arAllowed) > 0 && !in_array($sExtension, $arAllowed))
265  return false;
266 
267  if (count($arDenied) > 0 && in_array($sExtension, $arDenied))
268  return false;
269 
270  return true;
271 }
272 
279 function IsAllowedType($resourceType)
280 {
281  global $Config;
282  if (!in_array($resourceType, $Config['ConfigAllowedTypes']))
283  return false;
284 
285  return true;
286 }
287 
294 function IsAllowedCommand($sCommand)
295 {
296  global $Config;
297 
298  if (!in_array($sCommand, $Config['ConfigAllowedCommands']))
299  return false;
300 
301  return true;
302 }
303 
309 function GetCurrentFolder()
310 {
311  if (!isset($_GET)) {
312  global $_GET;
313  }
314  $sCurrentFolder = isset($_GET['CurrentFolder']) ? GETPOST('CurrentFolder', '', 1) : '/';
315 
316  // Check the current folder syntax (must begin and start with a slash).
317  if (!preg_match('|/$|', $sCurrentFolder))
318  $sCurrentFolder .= '/';
319  if (strpos($sCurrentFolder, '/') !== 0)
320  $sCurrentFolder = '/'.$sCurrentFolder;
321 
322  // Ensure the folder path has no double-slashes
323  while (strpos($sCurrentFolder, '//') !== false) {
324  $sCurrentFolder = str_replace('//', '/', $sCurrentFolder);
325  }
326 
327  // Check for invalid folder paths (..)
328  if (strpos($sCurrentFolder, '..') || strpos($sCurrentFolder, "\\"))
329  SendError(102, '');
330 
331  if (preg_match(",(/\.)|[[:cntrl:]]|(//)|(\\\\)|([\:\*\?\"<>\|]),", $sCurrentFolder))
332  SendError(102, '');
333 
334  return $sCurrentFolder;
335 }
336 
343 function SanitizeFolderName($sNewFolderName)
344 {
345  $sNewFolderName = stripslashes($sNewFolderName);
346 
347  // Remove . \ / | : ? * " < >
348  $sNewFolderName = preg_replace('/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFolderName);
349 
350  return $sNewFolderName;
351 }
352 
359 function SanitizeFileName($sNewFileName)
360 {
361  global $Config;
362 
363  $sNewFileName = stripslashes($sNewFileName);
364 
365  // Replace dots in the name with underscores (only one dot can be there... security issue).
366  if ($Config['ForceSingleExtension'])
367  $sNewFileName = preg_replace('/\\.(?![^.]*$)/', '_', $sNewFileName);
368 
369  // Remove \ / | : ? * " < >
370  $sNewFileName = preg_replace('/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/', '_', $sNewFileName);
371 
372  return $sNewFileName;
373 }
374 
384 function SendUploadResults($errorNumber, $fileUrl = '', $fileName = '', $customMsg = '')
385 {
386  // Minified version of the document.domain automatic fix script (#1919).
387  // The original script can be found at _dev/domain_fix_template.js
388  echo <<<EOF
389 <script type="text/javascript">
390 (function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
391 EOF;
392 
393  if ($errorNumber && $errorNumber != 201) {
394  $fileUrl = "";
395  $fileName = "";
396  }
397 
398  $rpl = array('\\' => '\\\\', '"' => '\\"');
399  echo 'window.parent.OnUploadCompleted('.$errorNumber.',"'.strtr($fileUrl, $rpl).'","'.strtr($fileName, $rpl).'", "'.strtr($customMsg, $rpl).'");';
400  echo '</script>';
401  exit;
402 }
403 
404 
405 // @CHANGE
406 
407 // This is the function that sends the results of the uploading process to CKE.
416 function SendCKEditorResults($callback, $sFileUrl, $customMsg = '')
417 {
418  echo '<script type="text/javascript">';
419 
420  $rpl = array('\\' => '\\\\', '"' => '\\"');
421 
422  echo 'window.parent.CKEDITOR.tools.callFunction("'.$callback.'","'.strtr($sFileUrl, $rpl).'", "'.strtr($customMsg, $rpl).'");';
423 
424  echo '</script>';
425 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print $_SERVER["PHP_SELF"]
Edit parameters.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105