diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc index 14b020604ce25884e280cce338208e17f1e96c90..ba7d89ab7dacfd8b84e0af6fb959897f3da6f17c 100755 --- a/vas/rest/class/vmlib/phpUtil.inc +++ b/vas/rest/class/vmlib/phpUtil.inc @@ -36,343 +36,647 @@ function stripslashes_deep($aString) { *@param $aFileValues File structure generated by extractFileStruct. *@return $sErrorMsg The error message or the final file path on success. */ -function uploadFile($sNomObjet, $sFileType, $sServerPath, $sMaxSize, $aFileValues) { - global $properties, $sFolderLib; - loadLang($sFolderLib, $properties["language"], $sFolderLib . "/"); - - $aExtension = array( - "image" => array('gif', 'jpg', 'jpeg', 'png'), - "image-pdf" => array('gif', 'jpg', 'jpeg', 'png', 'pdf'), - "document" => array('pdf', 'gif', 'jpg', 'jpeg', 'png', 'txt'), - "pdf" => array('pdf'), - "zip" => array('zip', 'gex'), - "fmw" => array('fmw') - ); - $aForbiddenExtension = explode('|', str_replace("*.", "", $properties['forbidden_extension'])); - $sTmpFile = ""; - $sErrorMsg = ""; - // si pas de aValues il y a eu une erreur pendant l'upload dans tmp du PUT - if (!empty($aFileValues)){ - $sTmpFile = $aFileValues['tmp_name']; - - // Si l'utilisateur n'a indiqué aucun fichier à uploader, il ne se passe rien - if ($sTmpFile == '') { - if ($aFileValues['name'] != "") { - switch ($aFileValues['error']) { - case "1" : - $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_UPLOAD_MAX_FILE_SIZE; - break; - case "2" : - $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_MAX_FILE_SIZE; - break; - case "3" : - $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_PARTIAL_DOWNLOAD; - break; - case "4" : - $sErrorMsg = ERROR_NO_FILE_DOWNLOADED; - break; - case "6" : - $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_MISSING_TEMP_FOLDER; - break; - case "7" : - $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_WRITING_DISK; - break; - case "8" : - $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_PHP_EXT_SEND; - break; - } - writeToErrorLog($sErrorMsg); - } - return $sErrorMsg; - } - - $aTemp = explode("/", $sServerPath); - - $sFileName = end($aTemp); - $sFileExtension = extension($sFileName); - - if (!in_array($sFileExtension, $aForbiddenExtension)) { - //Teste si le fichier correspont au format voulu. - $bAllowUpload = false; - $sFormat = ""; - - if (in_array($sFileType, array_keys($aExtension))){ - if (in_array($sFileExtension, $aExtension[$sFileType])) { - $bAllowUpload = true; - } else { - foreach ($aExtensionPicture as $sValue) { - $sFormat .= " " . $sValue; - } - writeToErrorLog(ERROR_FILE . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType); - $sErrorMsg = FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType . ERROR_VALID_FILE . $sFormat . '.'; - } - } else { - $bAllowUpload = true; - } - - //Teste si le fichier n'est pas de trop grande taille. - if ($aFileValues['size'] > $sMaxSize || $aFileValues['error'] == 1) { - $bAllowUpload = false; - if ($aFileValues['size'] > $sMaxSize) - $sErrorMsg .= FILE_LABEL_PHPUTIL . $aFileValues['name'] . OF_LABEL_PHPUTIL . $aFileValues['size'] . ERROR_EXCEED_MAX_SIZE . ' (' . $sMaxSize . LABEL_BYTES_PHPUTIL . ').'; - if ($aFileValues['error'] > $sMaxSize) - $sErrorMsg .= FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_EXCEED_MAX_SIZE_PHP . ' (' . $sMaxSize . LABEL_BYTES_PHPUTIL . ').'; - } - - //Lance l'upload. - if ($bAllowUpload) { - if ($properties['fileS3Uploader']){ - require_once ("aws_sdk/aws-autoloader.php"); - - // Création du client S3 - $s3 = new Aws\S3\S3Client(array( - 'version'=>'latest', - 'region'=> $properties['fileS3UploaderRegion'], - 'profile'=> $properties['fileS3UploaderProfil'], - 'debug' => false - )); - // traitement du bucket et de ses sous-dossiers - $sBucket = $properties['fileS3UploaderBucket']; - $sPrefix = ""; - - if (strpos($sBucket, "/") > -1){ - $aBucket = explode("/", $sBucket ); - $sBucket = $aBucket[0]; - $sPrefix = implode("/", array_slice($aBucket, 1)); - } - - $sServerPath = str_replace($properties["vas_home"], $sPrefix , $sServerPath); - - // envoi de l'objet sur S3 - $aResult = $s3->putObject(array( - 'Bucket' => $sBucket, - 'Key' => $sServerPath, - 'Body' => file_get_contents($sTmpFile) - )); - } else if (!copy($sTmpFile, $sServerPath)) { - writeToErrorLog(ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . ', ' . $sTmpFile . ', ' . $sServerPath); - $sErrorMsg = ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . '.'; - } - unlink($sTmpFile); - $sErrorMsg = $sServerPath; - //chmod($sServerPath,755); - } - } else { - writeToErrorLog(ERROR_FILE . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType); - $sErrorMsg = FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType . ERROR_VALID_FILE . $sFormat . '.'; - } - } else { - writeToErrorLog(ERROR_FILE . "File" . ERROR_DOWNLOAD_SERVER); - $sErrorMsg = FILE_LABEL_PHPUTIL . "File" . ERROR_DOWNLOAD_SERVER . '.'; - } - - return $sErrorMsg; -} - -/** - *This method convert $_FILE struct or the aValues File to a File Struct usable by uploadFile. - *@file vmlib/phpUtil.inc - *@param $sField Name of the field. - *@param $aValues $aValues to copy file in tmp. - *@return $aFileStruct FileStuct or null if an error block the write in tmp. - */ -function extractFileStruct ($sField, $aValues = null){ - global $properties; - if (empty($aValues)){ - // Extract From Post $File Struct - return $aFileStruc = array( - "name" => $_FILES[$sField]['name'], - "tmp_name" => $_FILES[$sField]['tmp_name'], - "error" => $_FILES[$sField]['error'], - "size" => $_FILES[$sField]['size'] - ); - } else { - // Extraction de $aValues, on le met dans tmp pour préparer la copie dans upload file - $sTmpFile = $properties['extract_dir'] . "/" . getUniqRandomId(); - $oFile = fopen($sTmpFile, 'w+'); - if (!$oFile){ - writeToErrorLog("Can't open file in " . $properties['extract_dir']); - return null; - }else{ - fwrite($oFile, $aValues[$sField . "_file"]); - fclose($oFile); - return $aFileStruc = array( - "name" => $aValues[$sField . "_name"], - "tmp_name" => $sTmpFile, - "error" => "0", - "size" => filesize($sTmpFile) - ); - } - } -} - -/** - *This method upload a file in ws_data. - *@file vmlib/phpUtil.inc - *@param $sModule Name of the module. - *@param $sObject Name of the object. - *@param $mId Id of the current object. - *@param $sField field name (generally DB column name). - *@param $aValues Vitis $aValues. - *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control) - *@param $sFileTypeCtrl Type of the document. (set to all to disable this control) - *@return $sErrorMsg The error message. - */ -function uploadInWsDataDir ($sModule, $sObject, $mId, $sField, $aValues, $iMaxSize = -1, $sFileTypeCtrl = "all"){ - global $properties; - - // on controle les attributs pour éviter les mauvais placements - if (strpos($sModule, '/') > -1){ - writeToErrorLog("Module can't contain path : " . $sModule); - return "Module can't contain path : " . $sModule; - } - - if (strpos($sObject, "/") > -1){ - writeToErrorLog("Object can't contain path : " . $sObject); - return "Object can't contain path : " . $sObject; - } - - if (strpos($mId, "/") > -1){ - writeToErrorLog("Id can't contain path : " . $mId); - return "Id can't contain path : " . $mId; - } - - if (strpos($sField, "/") > -1){ - writeToErrorLog("Field can't contain path : " . $sField); - return "Field can't contain path : " . $sField; - } - // on génére la Structure fichier - $aFileStruct = extractFileStruct ($sField, $aValues); - // on génére la destination - $sDestDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId . "/" . $sField; - $sDestPath = $sDestDir . "/" . $aFileStruct["name"]; - - // on controle la destination pour éviter les mauvais placements - if (strpos($sDestPath, "/\.\./") > -1){ - writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath); - return "This function doesn't accept relative reference : " . $sDestPath; - } - // si taille max vaut -1 alors taille max = taille fichier + 1 - if ($iMaxSize == -1){ - $iMaxSize = $aFileStruct["size"] + 1; - } - // création du fichier si besoin - if (!is_dir($sDestDir) && !$properties['fileS3Uploader']){ - if(!mkdir($sDestDir, 0777, true)){ - writeToErrorLog("Can't create directory " . $sDestDir); - return "Can't create directory " . $sDestDir; - } - } - // Upload du fichier - return uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct); -} -/** - *This method upload a file in Public. - *@file vmlib/phpUtil.inc - *@param $sModule Name of the module. - *@param $sRandomUniqId Uniq folder to use to stock file(s) (set it to "auto" to let the function create this folder). - *@param $sField field name (generally DB column name). - *@param $aValues Vitis $aValues. - *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control) - *@param $sFileTypeCtrl Type of the document. (set to all to disable this control) - *@return $sErrorMsg The error message or the file path if success. - */ -function uploadInPublicDir($sModule, $sField, $aValues, $sRandomUniqId = "auto", $iMaxSize = -1, $sFileTypeCtrl = "all"){ - global $properties; - - // on controle les attributs pour éviter les mauvais placements - if (strpos($sModule, '/') > -1){ - writeToErrorLog("Module can't contain path : " . $sModule); - return "Module can't contain path : " . $sModule; - } - if (strpos($sField, "/") > -1){ - writeToErrorLog("Field can't contain path : " . $sField); - return "Field can't contain path : " . $sField; - } - // on génére la Structure fichier - $aFileStruct = extractFileStruct ($sField, $aValues); - // on génére le dossier unique si besoin - if($sRandomUniqId == "auto"){ - $sRandomUniqId = getUniqRandomId(); - } - // on génére la destination - $sDestDir = $properties['dir_export'] . "/" . $sModule . "/" . $sRandomUniqId; - $sDestPath = $sDestDir . "/" . $aFileStruct["name"]; - - // on controle la destination pour éviter les mauvais placements - if (strpos($sDestPath, "/\.\./") > -1){ - writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath); - return "This function doesn't accept relative reference : " . $sDestPath; - } - // si taille max vaut -1 alors taille max = taille fichier + 1 - if ($iMaxSize == -1){ - $iMaxSize = $aFileStruct["size"] + 1; - } - - // création du fichier si besoin - if (!is_dir($sDestDir) && !$properties['fileS3Uploader']){ - if(!mkdir($sDestDir, 0777, true)){ - writeToErrorLog("Can't create directory " . $sDestDir); - return "Can't create directory " . $sDestDir; - } - } - // Upload du fichier - return uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct); -} - -/** - *This method upload a file in Upload. - *@file vmlib/phpUtil.inc - *@param $sModule Name of the module. - *@param $sRandomUniqId Uniq folder to use to stock file(s) (set it to "auto" to let the function create this folder). - *@param $sField field name (generally DB column name). - *@param $aValues Vitis $aValues. - *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control) - *@param $sFileTypeCtrl Type of the document. (set to all to disable this control) - *@return $sErrorMsg The error message or the file path if success. - */ -function uploadInUploadDir($sModule, $sField, $aValues, $sRandomUniqId = "auto", $iMaxSize = -1, $sFileTypeCtrl = "all"){ - global $properties; - - // on controle les attributs pour éviter les mauvais placements - if (strpos($sModule, '/') > -1){ - writeToErrorLog("Module can't contain path : " . $sModule); - return "Module can't contain path : " . $sModule; - } - if (strpos($sField, "/") > -1){ - writeToErrorLog("Field can't contain path : " . $sField); - return "Field can't contain path : " . $sField; - } - // on génére la Structure fichier - $aFileStruct = extractFileStruct ($sField, $aValues); - // on génére le dossier unique si besoin - if($sRandomUniqId == "auto"){ - $sRandomUniqId = getUniqRandomId(); - } - // on génére la destination - $sDestDir = $properties['upload_dir'] . "/" . $sModule . "/" . $sRandomUniqId; - $sDestPath = $sDestDir . "/" . $aFileStruct["name"]; - - // on controle la destination pour éviter les mauvais placements - if (strpos($sDestPath, "/\.\./") > -1){ - writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath); - return "This function doesn't accept relative reference : " . $sDestPath; - } - // si taille max vaut -1 alors taille max = taille fichier + 1 - if ($iMaxSize == -1){ - $iMaxSize = $aFileStruct["size"] + 1; - } - - // création du fichier si besoin - if (!is_dir($sDestDir) && !$properties['fileS3Uploader']){ - if(!mkdir($sDestDir, 0777, true)){ - writeToErrorLog("Can't create directory " . $sDestDir); - return "Can't create directory " . $sDestDir; - } - } - // Upload du fichier - return uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct); -} + function uploadFile($sNomObjet, $sFileType, $sServerPath, $sMaxSize, $aFileValues) { + global $properties, $sFolderLib; + loadLang($sFolderLib, $properties["language"], $sFolderLib . "/"); + + $aExtension = array( + "image" => array('gif', 'jpg', 'jpeg', 'png'), + "image-pdf" => array('gif', 'jpg', 'jpeg', 'png', 'pdf'), + "document" => array('pdf', 'gif', 'jpg', 'jpeg', 'png', 'txt'), + "pdf" => array('pdf'), + "zip" => array('zip', 'gex'), + "fmw" => array('fmw') + ); + $aForbiddenExtension = explode('|', str_replace("*.", "", $properties['forbidden_extension'])); + $sTmpFile = ""; + $sErrorMsg = ""; + // si pas de aValues il y a eu une erreur pendant l'upload dans tmp du PUT + if (!empty($aFileValues)){ + $sTmpFile = $aFileValues['tmp_name']; + + // Si l'utilisateur n'a indiqué aucun fichier à uploader, il ne se passe rien + if ($sTmpFile == '') { + if ($aFileValues['name'] != "") { + switch ($aFileValues['error']) { + case "1" : + $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_UPLOAD_MAX_FILE_SIZE; + break; + case "2" : + $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_MAX_FILE_SIZE; + break; + case "3" : + $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_PARTIAL_DOWNLOAD; + break; + case "4" : + $sErrorMsg = ERROR_NO_FILE_DOWNLOADED; + break; + case "6" : + $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_MISSING_TEMP_FOLDER; + break; + case "7" : + $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_WRITING_DISK; + break; + case "8" : + $sErrorMsg = ERROR_DOWNLOAD_FILE . $aFileValues['name'] . " : " . ERROR_PHP_EXT_SEND; + break; + } + writeToErrorLog($sErrorMsg); + } + return $sErrorMsg; + } + + $aTemp = explode("/", $sServerPath); + + $sFileName = end($aTemp); + $sFileExtension = extension($sFileName); + + if (!in_array($sFileExtension, $aForbiddenExtension)) { + //Teste si le fichier correspont au format voulu. + $bAllowUpload = false; + $sFormat = ""; + + if (in_array($sFileType, array_keys($aExtension))){ + if (in_array($sFileExtension, $aExtension[$sFileType])) { + $bAllowUpload = true; + } else { + foreach ($aExtensionPicture as $sValue) { + $sFormat .= " " . $sValue; + } + writeToErrorLog(ERROR_FILE . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType); + $sErrorMsg = FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType . ERROR_VALID_FILE . $sFormat . '.'; + } + } else { + $bAllowUpload = true; + } + + //Teste si le fichier n'est pas de trop grande taille. + if ($aFileValues['size'] > $sMaxSize || $aFileValues['error'] == 1) { + $bAllowUpload = false; + if ($aFileValues['size'] > $sMaxSize) + $sErrorMsg .= FILE_LABEL_PHPUTIL . $aFileValues['name'] . OF_LABEL_PHPUTIL . $aFileValues['size'] . ERROR_EXCEED_MAX_SIZE . ' (' . $sMaxSize . LABEL_BYTES_PHPUTIL . ').'; + if ($aFileValues['error'] > $sMaxSize) + $sErrorMsg .= FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_EXCEED_MAX_SIZE_PHP . ' (' . $sMaxSize . LABEL_BYTES_PHPUTIL . ').'; + } + + //Lance l'upload. + if ($bAllowUpload) { + if ($properties['fileS3Uploader']){ + require_once ("aws_sdk/aws-autoloader.php"); + + $s3 = new Aws\S3\S3Client(array( + 'version'=>'latest', + 'region'=> $properties['fileS3UploaderRegion'], + 'profile'=> $properties['fileS3UploaderProfil'], + 'debug' => false + )); + + $sBucket = $properties['fileS3UploaderBucket']; + $sPrefix = ""; + + if (strpos($sBucket, "/") > -1){ + $aBucket = explode("/", $sBucket ); + $sBucket = $aBucket[0]; + $sPrefix = implode("/", array_slice($aBucket, 1)); + } + + $sServerPath = str_replace($properties["vas_home"], $sPrefix , $sServerPath); + + $aResult = $s3->putObject(array( + 'Bucket' => $sBucket, + 'Key' => $sServerPath, + 'Body' => file_get_contents($sTmpFile) + )); + + } else if (!copy($sTmpFile, $sServerPath)) { + writeToErrorLog(ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . ', ' . $sTmpFile . ', ' . $sServerPath); + $sErrorMsg = ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . '.'; + } + unlink($sTmpFile); + $sErrorMsg = $sServerPath; + //chmod($sServerPath,755); + } + } else { + writeToErrorLog(ERROR_FILE . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType); + $sErrorMsg = FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_NOT_FILE . $sFileType . ERROR_VALID_FILE . $sFormat . '.'; + } + } else { + writeToErrorLog(ERROR_FILE . "File" . ERROR_DOWNLOAD_SERVER); + $sErrorMsg = FILE_LABEL_PHPUTIL . "File" . ERROR_DOWNLOAD_SERVER . '.'; + } + + return $sErrorMsg; + } + + /** + *This method convert $_FILE struct or the aValues File to a File Struct usable by uploadFile. + *@file vmlib/phpUtil.inc + *@param $sField Name of the field. + *@param $aValues $aValues to copy file in tmp. + *@return $aFileStruct FileStuct or null if an error block the write in tmp. + */ + function extractFileStruct ($sField, $aValues = null){ + global $properties; + if (empty($aValues)){ + // Extract From Post $File Struct + return $aFileStruc = array( + "name" => $_FILES[$sField]['name'], + "tmp_name" => $_FILES[$sField]['tmp_name'], + "error" => $_FILES[$sField]['error'], + "size" => $_FILES[$sField]['size'] + ); + } else { + // Extraction de $aValues, on le met dans tmp pour préparer la copie dans upload file + $sTmpFile = $properties['extract_dir'] . "/" . getUniqRandomId(); + $oFile = fopen($sTmpFile, 'w+'); + if (!$oFile){ + writeToErrorLog("Can't open file in " . $properties['extract_dir']); + return null; + }else{ + fwrite($oFile, $aValues[$sField . "_file"]); + fclose($oFile); + return $aFileStruc = array( + "name" => $aValues[$sField . "_name"], + "tmp_name" => $sTmpFile, + "error" => "0", + "size" => filesize($sTmpFile) + ); + } + } + } + /** + *This method upload a file in ws_data. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sObject Name of the object. + *@param $mId Id of the current object. + *@param $sField field name (generally DB column name). + *@param $aValues Vitis $aValues. + *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control) + *@param $sFileTypeCtrl Type of the document. (set to all to disable this control) + *@return $sErrorMsg The error message. + */ + function uploadInWsDataDir ($sModule, $sObject, $mId, $sField, $aValues, $iMaxSize = -1, $sFileTypeCtrl = "all"){ + global $properties; + + // on controle les attributs pour éviter les mauvais placements + if (strpos($sModule, '/') > -1){ + writeToErrorLog("Module can't contain path : " . $sModule); + return "Module can't contain path : " . $sModule; + } + + if (strpos($sObject, "/") > -1){ + writeToErrorLog("Object can't contain path : " . $sObject); + return "Object can't contain path : " . $sObject; + } + + if (strpos($mId, "/") > -1){ + writeToErrorLog("Id can't contain path : " . $mId); + return "Id can't contain path : " . $mId; + } + + if (strpos($sField, "/") > -1){ + writeToErrorLog("Field can't contain path : " . $sField); + return "Field can't contain path : " . $sField; + } + // on génére la Structure fichier + $aFileStruct = extractFileStruct ($sField, $aValues); + // on génére la destination + $sDestDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId; + if(!empty($sField)){ + $sDestDir .= "/" . $sField; + } + $sDestPath = $sDestDir . "/" . $aFileStruct["name"]; + + // on controle la destination pour éviter les mauvais placements + if (strpos($sDestPath, "/\.\./") > -1){ + writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath); + return "This function doesn't accept relative reference : " . $sDestPath; + } + // si taille max vaut -1 alors taille max = taille fichier + 1 + if ($iMaxSize == -1){ + $iMaxSize = $aFileStruct["size"] + 1; + } + // création du fichier si besoin + if (!is_dir($sDestDir)){ + if(!mkdir($sDestDir, 0777, true)){ + writeToErrorLog("Can't create directory " . $sDestDir); + return "Can't create directory " . $sDestDir; + } + } + // Upload du fichier + return uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct); + } + /** + *This method upload a file in Public. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sField field name (generally DB column name). + *@param $aValues Vitis $aValues. + *@param $sRandomUniqId random folder to write the file.(set to auto to let the function create the folder) + *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control) + *@param $sFileTypeCtrl Type of the document. (set to all to disable this control) + *@return $sErrorMsg The error message. + */ + function uploadInPublicDir($sModule, $sField, $aValues, $sRandomUniqId = "auto", $iMaxSize = -1, $sFileTypeCtrl = "all"){ + global $properties; + + // on controle les attributs pour éviter les mauvais placements + if (strpos($sModule, '/') > -1){ + writeToErrorLog("Module can't contain path : " . $sModule); + return "Module can't contain path : " . $sModule; + } + if (strpos($sField, "/") > -1){ + writeToErrorLog("Field can't contain path : " . $sField); + return "Field can't contain path : " . $sField; + } + // on génére la Structure fichier + $aFileStruct = extractFileStruct ($sField, $aValues); + // on génére le dossier unique si besoin + if($sRandomUniqId == "auto"){ + $sRandomUniqId = getUniqRandomId(); + } + // on génére la destination + $sDestDir = $properties['dir_export'] . "/" . $sModule . "/" . $sRandomUniqId; + $sDestPath = $sDestDir . "/" . $aFileStruct["name"]; + + // on controle la destination pour éviter les mauvais placements + if (strpos($sDestPath, "/\.\./") > -1){ + writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath); + return "This function doesn't accept relative reference : " . $sDestPath; + } + // si taille max vaut -1 alors taille max = taille fichier + 1 + if ($iMaxSize == -1){ + $iMaxSize = $aFileStruct["size"] + 1; + } + + // création du fichier si besoin + if (!is_dir($sDestDir)){ + if(!mkdir($sDestDir, 0777, true)){ + writeToErrorLog("Can't create directory " . $sDestDir); + return "Can't create directory " . $sDestDir; + } + } + // Upload du fichier + return uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct); + } + /** + *This method upload a file in Upload. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sField field name (generally DB column name). + *@param $aValues Vitis $aValues. + *@param $sRandomUniqId random folder to write the file.(set to auto to let the function create the folder) + *@param $iMaxSize Maximum size to upload on server. (set to -1 to disable this control) + *@param $sFileTypeCtrl Type of the document. (set to all to disable this control) + *@return $sErrorMsg The error message. + */ + function uploadInUploadDir($sModule, $sField, $aValues, $sRandomUniqId = "auto", $iMaxSize = -1, $sFileTypeCtrl = "all"){ + global $properties; + + // on controle les attributs pour éviter les mauvais placements + if (strpos($sModule, '/') > -1){ + writeToErrorLog("Module can't contain path : " . $sModule); + return "Module can't contain path : " . $sModule; + } + if (strpos($sField, "/") > -1){ + writeToErrorLog("Field can't contain path : " . $sField); + return "Field can't contain path : " . $sField; + } + // on génére la Structure fichier + $aFileStruct = extractFileStruct ($sField, $aValues); + // on génére le dossier unique si besoin + if($sRandomUniqId == "auto"){ + $sRandomUniqId = getUniqRandomId(); + } + // on génére la destination + $sDestDir = $properties['upload_dir'] . "/" . $sModule . "/" . $sRandomUniqId; + $sDestPath = $sDestDir . "/" . $aFileStruct["name"]; + + // on controle la destination pour éviter les mauvais placements + if (strpos($sDestPath, "/\.\./") > -1){ + writeToErrorLog("This function doesn't accept relative reference : " . $sDestPath); + return "This function doesn't accept relative reference : " . $sDestPath; + } + // si taille max vaut -1 alors taille max = taille fichier + 1 + if ($iMaxSize == -1){ + $iMaxSize = $aFileStruct["size"] + 1; + } + + // création du fichier si besoin + if (!is_dir($sDestDir)){ + if(!mkdir($sDestDir, 0777, true)){ + writeToErrorLog("Can't create directory " . $sDestDir); + return "Can't create directory " . $sDestDir; + } + } + // Upload du fichier + return uploadFile($sField, $sFileTypeCtrl, $sDestPath, $iMaxSize, $aFileStruct); + } + /** + *This method upload a file in ws_data. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sObject Name of the object. + *@param $mId Id of the current object. + *@param $sField field name (generally DB column name). + *@return $aArray Tree structure from S3 or ws_data. + */ + function getDirectoryInfosFromWsDataDir ($sModule, $sObject, $mId, $sField){ + global $properties; + $sScanDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId; + + if (!empty($sField)){ + $sScanDir .= "/" . $sField; + } + + if ($properties['fileS3Uploader']){ + return array(getS3FolderInfos($sScanDir)); + }else{ + return array(getFolderInfos($sScanDir)); + } + } + /** + *This method upload a file in ws_data. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sObject Name of the object. + *@param $mId Id of the current object. + *@param $sField field name (generally DB column name). + *@return $aArray Tree structure from S3 or ws_data. + */ + function deleteFileFromWsDataDirTree ($sModule, $sObject, $mId, $sField, $sFilePath){ + global $properties; + require_once ("aws_sdk/aws-autoloader.php"); + $sFilePath = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId . "/" . $sField . "/" . $sFilePath; + + if ($properties['fileS3Uploader']){ + // traitement du bucket et de ses sous-dossiers + $sBucket = $properties['fileS3UploaderBucket']; + $sPrefix = ""; + + if (strpos($sBucket, "/") > -1){ + $aBucket = explode("/", $sBucket ); + $sBucket = $aBucket[0]; + $sPrefix = implode("/", array_slice($aBucket, 1)); + } + // Création du client S3 + $s3 = new Aws\S3\S3Client(array( + 'version'=>'latest', + 'region'=> $properties['fileS3UploaderRegion'], + 'profile'=> $properties['fileS3UploaderProfil'], + 'debug' => false + )); + + try{ + + $aResult = $s3->deleteObject(array( + 'Bucket' => $sBucket, + 'Key' => $sPrefix . str_replace($properties['ws_data_dir'], "/ws_data", $sFilePath) + )); + + }catch(Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + } + }else{ + unlink($sFilePath); + } + } + /** + *This method upload a file in ws_data. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sObject Name of the object. + *@param $mId Id of the current object. + *@param $sField field name (generally DB column name). + *@return $aArray Tree structure from S3 or ws_data. + */ + function postFileInWsDataDirTree ($sModule, $sObject, $mId, $sField, $aValues, $sAttrName){ + global $properties; + require_once ("aws_sdk/aws-autoloader.php"); + $sFilePath = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId . "/"; + + if(!empty($sField)){ + $sFilePath .= $sField . "/"; + } + + $aFileStruct = extractFileStruct ($sAttrName, $aValues); + + if ($properties['fileS3Uploader']){ + // traitement du bucket et de ses sous-dossiers + $sBucket = $properties['fileS3UploaderBucket']; + $sPrefix = ""; + + if (strpos($sBucket, "/") > -1){ + $aBucket = explode("/", $sBucket ); + $sBucket = $aBucket[0]; + $sPrefix = implode("/", array_slice($aBucket, 1)); + } + // Création du client S3 + $s3 = new Aws\S3\S3Client(array( + 'version'=>'latest', + 'region'=> $properties['fileS3UploaderRegion'], + 'profile'=> $properties['fileS3UploaderProfil'], + 'debug' => false + )); + + try{ + + $aResult = $s3->putObject(array( + 'Bucket' => $sBucket, + 'Key' => $sPrefix . str_replace($properties['ws_data_dir'], "/ws_data", $sFilePath) . $aFileStruct["name"], + 'Body' => file_get_contents($aFileStruct["tmp_name"]) + )); + + unlink($aFileStruct["tmp_name"]); + + }catch(Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + } + }else{ + copy($aFileStruc["tmp_name"], $sFilePath . $aFileStruct["name"]); + } + } + /** + *This method scan a path to get metrics of a file/directory. + *@file vmlib/phpUtil.inc + *@param $sFilePath path to scan. + *@return $aArray file structure from ws_data. + */ + function getFileInfos ($sFilePath){ + global $properties; + $iFileSize = filesize($sFilePath); + + $sFileSize = $iFileSize . " octets"; + if($iFileSize > 1024){ + if($iFileSize > 1024*1024){ + if($iFileSize > 1024*1024*1024){ + $sFileSize = "-"; + } else { + $sFileSize = (ceil($iFileSize/(1024*1024))) . "Mo"; + } + }else{ + $sFileSize = (ceil($iFileSize/1024)) . "Ko"; + } + } + + $aFileName = explode("/", $sFilePath); + $date = new DateTime(); + $sDataUrl = $properties['web_server_name'] . "/rest/vitis/file_downloader?key=[KEY]&eTag=[ETAG]&d=" . $date->getTimestamp(); + + $sFileUrl = str_replace("[KEY]", str_replace($properties['ws_data_dir'], "ws_data" , $sFilePath), $sDataUrl); + $sFileUrl = str_replace("[ETAG]", sha1(file_get_contents($sFilePath), false), $sFileUrl); + + return array("filename"=>$aFileName[count($aFileName) - 1], "size"=>$sFileSize, "lastModification"=> date ("d/m/Y H:i:s", filemtime($sFilePath)), "href" => $sFileUrl); + } + + /** + *This method scan a path to get metrics of an object (S3). + *@file vmlib/phpUtil.inc + *@param $sDirectoryPath path/prefix to scan. + *@return $aArray file structure from S3. + */ + function getS3FolderInfos ($sDirectoryPath){ + global $properties; + require_once ("aws_sdk/aws-autoloader.php"); + //$oS3 = new AmazonS3($this->aProperties["awsAccessKey"], $this->aProperties["awsSecretKey"], $this->aProperties["s3Region"]); + //$oS3Scan = $oS3->scanDir($properties["bucketName"], $sDirectoryPath, true); + + // traitement du bucket et de ses sous-dossiers + $sBucket = $properties['fileS3UploaderBucket']; + $sPrefix = ""; + + if (strpos($sBucket, "/") > -1){ + $aBucket = explode("/", $sBucket ); + $sBucket = $aBucket[0]; + $sPrefix = implode("/", array_slice($aBucket, 1)); + } + // Création du client S3 + $s3 = new Aws\S3\S3Client(array( + 'version'=>'latest', + 'region'=> $properties['fileS3UploaderRegion'], + 'profile'=> $properties['fileS3UploaderProfil'], + 'debug' => false + )); + + $sListPrefix = str_replace($properties['ws_data_dir'], $sPrefix . "/ws_data" , $sDirectoryPath); + + try{ + + $aList = $s3->listObjectsV2(array( + 'Bucket' => $sBucket, + 'Prefix' => $sListPrefix + )); + + $date = new DateTime(); + $sDataUrl = $properties['web_server_name'] . "/rest/vitis/file_downloader?key=[KEY]&eTag=[ETAG]&d=" . $date->getTimestamp(); + $aPath = explode("/", $sDirectoryPath); + $aTree = array( + "filename"=> end($aPath), + "lastModification" => "none", + "size"=> "0 Octet", + "content" => array() + ); + if ($aList["KeyCount"] > 0){ + foreach ($aList["Contents"] as $aObject){ + $sObjectPath = str_replace($sListPrefix, "", $aObject["Key"]); + $aObjectPath = explode("/", $sObjectPath); + + $sFileUrl = str_replace("[KEY]", $aObject["Key"], $sDataUrl); + $sFileUrl = str_replace("[ETAG]", str_replace('"', '', $aObject["ETag"]), $sFileUrl); + + $iFileSize = $aObject["Size"]; + $sFileSize = $iFileSize . " octets"; + if($iFileSize > 1024){ + if($iFileSize > 1024*1024){ + if($iFileSize > 1024*1024*1024){ + $sFileSize = "-"; + } else { + $sFileSize = (ceil($iFileSize/(1024*1024))) . "Mo"; + } + }else{ + $sFileSize = (ceil($iFileSize/1024)) . "Ko"; + } + } + + $sDate = $aObject["LastModified"]->__toString(); + $aTree = s3TreeBuilder($aTree, $sObjectPath, array( + "filename"=> end($aObjectPath), + "lastModification" => date ("d/m/Y H:i:s", strtotime($sDate)), + "size" => $sFileSize, + "href" => $sFileUrl + )); + } + } + + return $aTree; + + }catch(Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + } + } + /** + *This method scan an object key to generate a tree struct (S3). + *@file vmlib/phpUtil.inc + *@param $aTree an existing tree. + *@param $sPath Path to scan. + *@param $FileStructure object metrics structure. + *@return $aArray file structure from S3. + */ + function s3TreeBuilder ($aTree, $sPath, $FileStructure){ + $aTreeTmp = &$aTree["content"]; + $aPath = explode("/", $sPath); + foreach ($aPath as $sFragment){ + if(!empty($sFragment)){ + if($sFragment !== $FileStructure["filename"]){ + $key = array_search($sFragment, array_column($aTreeTmp, 'filename')); + + if ($key !== false){ + $aTreeTmp = &$aTreeTmp[$key]["content"]; + } else { + $aFolder = array( + "filename"=> $sFragment, + "lastModification" => "none", + "size"=> "0 Octet", + "content" => array() + ); + + array_push($aTreeTmp, $aFolder); + $key = array_search($sFragment, array_column($aTreeTmp, 'filename')); + $aTreeTmp = &$aTreeTmp[$key]["content"]; + } + }else { + array_push($aTreeTmp, $FileStructure); + } + } + } + return $aTree; + } + /** + *This method scan a path to get metrics of a directory. + *@file vmlib/phpUtil.inc + *@param $sDirectoryPath path to scan. + *@return $aArray file structure from ws_data. + */ + function getFolderInfos ($sDirectoryPath){ + + if(is_dir($sDirectoryPath)){ + $aContentDir = scandir($sDirectoryPath); + $aTree = getFileInfos($sDirectoryPath); + $aDir = array(); + for ($i =0 ; $i < count($aContentDir); $i++){ + if($aContentDir[$i] !== "." && $aContentDir[$i] !== ".."){ + array_push($aDir, getFolderInfos($sDirectoryPath . "/" . $aContentDir[$i])); + } + } + + $aTree["content"] = $aDir; + unset($aTree["href"]); + return $aTree; + } else { + return getFileInfos($sDirectoryPath); + } + } /** *This method return the extension of a file.