32 function CombinePaths($sBasePath, $sFolder)
34 return RemoveFromEnd($sBasePath,
'/').
'/'.RemoveFromStart($sFolder,
'/');
43 function GetResourceTypePath($resourceType, $sCommand)
47 if ($sCommand ==
"QuickUpload") {
48 return $Config[
'QuickUploadPath'][$resourceType];
50 return $Config[
'FileTypesPath'][$resourceType];
61 function GetResourceTypeDirectory($resourceType, $sCommand)
64 if ($sCommand ==
"QuickUpload") {
65 if (strlen($Config[
'QuickUploadAbsolutePath'][$resourceType]) > 0) {
66 return $Config[
'QuickUploadAbsolutePath'][$resourceType];
70 return Server_MapPath($Config[
'QuickUploadPath'][$resourceType]);
72 if (strlen($Config[
'FileTypesAbsolutePath'][$resourceType]) > 0) {
73 return $Config[
'FileTypesAbsolutePath'][$resourceType];
77 return Server_MapPath($Config[
'FileTypesPath'][$resourceType]);
89 function GetUrlFromPath($resourceType, $folderPath, $sCommand)
91 return CombinePaths(GetResourceTypePath($resourceType, $sCommand), $folderPath);
100 function RemoveExtension($fileName)
102 return substr($fileName, 0, strrpos($fileName,
'.'));
112 function ServerMapFolder($resourceType, $folderPath, $sCommand)
115 $sResourceTypePath = GetResourceTypeDirectory($resourceType, $sCommand);
118 $sErrorMsg = CreateServerFolder($sResourceTypePath);
119 if ($sErrorMsg !=
'') {
120 SendError(1,
"Error creating folder \"{$sResourceTypePath}\" ({$sErrorMsg})");
124 return CombinePaths($sResourceTypePath, $folderPath);
133 function GetParentFolder($folderPath)
135 $sPattern =
"-[/\\\\][^/\\\\]+[/\\\\]?$-";
136 return preg_replace($sPattern,
'', $folderPath);
146 function CreateServerFolder($folderPath, $lastFolder =
null)
149 $sParent = GetParentFolder($folderPath);
152 while (strpos($folderPath,
'//') !==
false) {
153 $folderPath = str_replace(
'//',
'/', $folderPath);
157 if (!empty($sParent) && !file_exists($sParent)) {
159 if (!is_null($lastFolder) && $lastFolder === $sParent) {
160 return "Can't create $folderPath directory";
163 $sErrorMsg = CreateServerFolder($sParent, $folderPath);
164 if ($sErrorMsg !=
'') {
169 if (!file_exists($folderPath)) {
175 ini_set(
'track_errors',
'1');
177 if (isset($Config[
'ChmodOnFolderCreate']) && !$Config[
'ChmodOnFolderCreate']) {
180 $permissions =
'0777';
181 if (isset($Config[
'ChmodOnFolderCreate']) && $Config[
'ChmodOnFolderCreate']) {
182 $permissions = (string) $Config[
'ChmodOnFolderCreate'];
184 $permissionsdec = octdec($permissions);
185 $permissionsdec |= octdec(
'0111');
186 dol_syslog(
"io.php permission = ".$permissions.
" ".$permissionsdec.
" ".decoct($permissionsdec));
188 $oldumask = umask(0);
189 mkdir($folderPath, $permissionsdec);
193 $sErrorMsg = $php_errormsg;
196 ini_restore(
'track_errors');
197 ini_restore(
'error_reporting');
210 function GetRootPath()
212 if (!isset($_SERVER)) {
215 $sRealPath = realpath(
'./');
217 $sRealPath = rtrim($sRealPath,
"\\/");
219 $sSelfPath = $_SERVER[
'PHP_SELF'];
220 $sSelfPath = substr($sSelfPath, 0, strrpos($sSelfPath,
'/'));
222 $sSelfPath = str_replace(
'/', DIRECTORY_SEPARATOR, $sSelfPath);
224 $position = strpos($sRealPath, $sSelfPath);
228 if ($position ===
false || $position <> strlen($sRealPath) - strlen($sSelfPath)) {
229 SendError(1,
'Sorry, can\'t map "UserFilesPath" to a physical path. You must set the "UserFilesAbsolutePath" value in "editor/filemanager/connectors/php/config.php".');
232 return substr($sRealPath, 0, $position);
240 function Server_MapPath($path)
243 if (function_exists(
'apache_lookup_uri')) {
244 $info = apache_lookup_uri($path);
245 return $info->filename.$info->path_info;
250 return GetRootPath().$path;
260 function IsAllowedExt($sExtension, $resourceType)
264 $arAllowed = $Config[
'AllowedExtensions'][$resourceType];
265 $arDenied = $Config[
'DeniedExtensions'][$resourceType];
267 if (count($arAllowed) > 0 && !in_array($sExtension, $arAllowed)) {
271 if (count($arDenied) > 0 && in_array($sExtension, $arDenied)) {
284 function IsAllowedType($resourceType)
287 if (!in_array($resourceType, $Config[
'ConfigAllowedTypes'])) {
300 function IsAllowedCommand($sCommand)
304 if (!in_array($sCommand, $Config[
'ConfigAllowedCommands'])) {
316 function GetCurrentFolder()
321 $sCurrentFolder = isset($_GET[
'CurrentFolder']) ?
GETPOST(
'CurrentFolder',
'', 1) :
'/';
324 if (!preg_match(
'|/$|', $sCurrentFolder)) {
325 $sCurrentFolder .=
'/';
327 if (strpos($sCurrentFolder,
'/') !== 0) {
328 $sCurrentFolder =
'/'.$sCurrentFolder;
332 while (strpos($sCurrentFolder,
'//') !==
false) {
333 $sCurrentFolder = str_replace(
'//',
'/', $sCurrentFolder);
337 if (strpos($sCurrentFolder,
'..') || strpos($sCurrentFolder,
"\\")) {
341 if (preg_match(
",(/\.)|[[:cntrl:]]|(//)|(\\\\)|([\:\*\?\"<>\|]),", $sCurrentFolder)) {
345 return $sCurrentFolder;
354 function SanitizeFolderName($sNewFolderName)
356 $sNewFolderName = stripslashes($sNewFolderName);
359 $sNewFolderName = preg_replace(
'/\\.|\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/',
'_', $sNewFolderName);
361 return $sNewFolderName;
370 function SanitizeFileName($sNewFileName)
374 $sNewFileName = stripslashes($sNewFileName);
377 if ($Config[
'ForceSingleExtension']) {
378 $sNewFileName = preg_replace(
'/\\.(?![^.]*$)/',
'_', $sNewFileName);
382 $sNewFileName = preg_replace(
'/\\\\|\\/|\\||\\:|\\?|\\*|"|<|>|[[:cntrl:]]/',
'_', $sNewFileName);
384 return $sNewFileName;
396 function SendUploadResults($errorNumber, $fileUrl =
'', $fileName =
'', $customMsg =
'')
401 <script
type=
"text/javascript">
402 (
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;}}})();
405 if ($errorNumber && $errorNumber != 201) {
410 $rpl = array(
'\\' =>
'\\\\',
'"' =>
'\\"');
411 echo
'console.log('.$errorNumber.
');';
412 echo
'window.parent.OnUploadCompleted('.$errorNumber.
', "'.strtr($fileUrl, $rpl).
'", "'.strtr($fileName, $rpl).
'", "'.strtr($customMsg, $rpl).
'");';
429 function SendCKEditorResults($callback, $sFileUrl, $customMsg =
'')
431 echo
'<script type="text/javascript">';
433 $rpl = array(
'\\' =>
'\\\\',
'"' =>
'\\"');
435 echo
'window.parent.CKEDITOR.tools.callFunction("'.$callback.
'","'.strtr($sFileUrl, $rpl).
'", "'.strtr($customMsg, $rpl).
'");';