/** * <p>Метод удаляет файл из таблицы зарегистрированных файлов (b_file) и с диска. Статичный метод.</p> * * * @param int $id Цифровой идентификатор файла. * * @return mixed * * <h4>Example</h4> * <pre> * <? * // удаляем изображение формы * $arFilter = array("ID" => 1, "ID_EXACT_MATCH" => "Y"); * $rsForm = CForm::GetList($by, $order, $arFilter, $is_filtered); * if ($arForm = $rsForm->Fetch()) * { * if (intval($arForm["IMAGE_ID"])>0) <b>CFile::Delete</b>($arForm["IMAGE_ID"]); * } * ?> * </pre> * * * <h4>See Also</h4> * <ul> <li> <a href="http://dev.1c-bitrix.ru/api_help/main/functions/file/deletedirfiles.php">DeleteDirFiles</a> </li> * <li> <a href="http://dev.1c-bitrix.ru/api_help/main/functions/file/deletedirfilesex.php">DeleteDirFilesEx</a> </li> * </ul><a name="examples"></a> * * * @static * @link http://dev.1c-bitrix.ru/api_help/main/reference/cfile/delete.php * @author Bitrix */ public static function Delete($ID) { global $DB; $io = CBXVirtualIo::GetInstance(); $ID = intval($ID); if ($ID <= 0) { return; } $res = CFile::GetByID($ID); if ($res = $res->Fetch()) { $delete_size = 0; $upload_dir = COption::GetOptionString("main", "upload_dir", "upload"); $dname = $_SERVER["DOCUMENT_ROOT"] . "/" . $upload_dir . "/" . $res["SUBDIR"]; $fname = $dname . "/" . $res["FILE_NAME"]; $file = $io->GetFile($fname); if ($file->isExists() && $file->unlink()) { $delete_size += $res["FILE_SIZE"]; } $delete_size += CFile::ResizeImageDelete($res); $DB->Query("DELETE FROM b_file WHERE ID = " . $ID); $directory = $io->GetDirectory($dname); if ($directory->isExists() && $directory->isEmpty()) { $directory->rmdir(); } CFile::CleanCache($ID); foreach (GetModuleEvents("main", "OnFileDelete", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($res)); } /****************************** QUOTA ******************************/ if ($delete_size > 0 && COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $delete_size, "delete"); } /****************************** QUOTA ******************************/ } }
if($obUpload->Finish()) { $NS['pos'] += $file_size; $oBucket = new CCloudStorageBucket($NS['BUCKET_ID']); $oBucket->IncFileCounter($file_size); if (file_exists($arc_name = CTar::getNextName($NS['arc_name']))) $NS['arc_name'] = $arc_name; else { $name = preg_replace('#\.[0-9]+$#','',$NS['arc_name']); while(file_exists($name)) { $size = filesize($name); if (unlink($name) && IntOption("disk_space") > 0) CDiskQuota::updateDiskQuota("file",$size , "del"); $name = CTar::getNextName($name); } $NS["step"]++; } } else { $obUpload->Delete(); RaiseErrorAndDie(GetMessage("MAIN_DUMP_ERR_FILE_SEND").basename($NS['arc_name'])); } } // partial else $pos += $NS['pos']; }
function ResizeImageGet($file, $arSize, $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL, $bInitSizes = false, $arFilters = false, $bImmediate = false, $jpgQuality = false) { if (!is_array($file) && intval($file) > 0) { $file = CFile::GetFileArray($file); } if (!is_array($file) || !array_key_exists("FILE_NAME", $file) || strlen($file["FILE_NAME"]) <= 0) { return false; } if ($resizeType != BX_RESIZE_IMAGE_EXACT && $resizeType != BX_RESIZE_IMAGE_PROPORTIONAL_ALT) { $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL; } if (!is_array($arSize)) { $arSize = array(); } if (!array_key_exists("width", $arSize) || intval($arSize["width"]) <= 0) { $arSize["width"] = 0; } if (!array_key_exists("height", $arSize) || intval($arSize["height"]) <= 0) { $arSize["height"] = 0; } $arSize["width"] = intval($arSize["width"]); $arSize["height"] = intval($arSize["height"]); $uploadDirName = COption::GetOptionString("main", "upload_dir", "upload"); $imageFile = "/" . $uploadDirName . "/" . $file["SUBDIR"] . "/" . $file["FILE_NAME"]; $arImageSize = false; $bFilters = is_array($arFilters) && !empty($arFilters); if (($arSize["width"] <= 0 || $arSize["width"] >= $file["WIDTH"]) && ($arSize["height"] <= 0 || $arSize["height"] >= $file["HEIGHT"])) { if ($bFilters) { //Only filters. Leave size unchanged $arSize["width"] = $file["WIDTH"]; $arSize["height"] = $file["HEIGHT"]; $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL; } else { global $arCloudImageSizeCache; $arCloudImageSizeCache[$file["SRC"]] = array($file["WIDTH"], $file["HEIGHT"]); return array("src" => $file["SRC"], "width" => intval($file["WIDTH"]), "height" => intval($file["HEIGHT"]), "size" => $file["FILE_SIZE"]); } } $io = CBXVirtualIo::GetInstance(); $cacheImageFile = "/" . $uploadDirName . "/resize_cache/" . $file["SUBDIR"] . "/" . $arSize["width"] . "_" . $arSize["height"] . "_" . $resizeType . (is_array($arFilters) ? md5(serialize($arFilters)) : "") . "/" . $file["FILE_NAME"]; $cacheImageFileCheck = $cacheImageFile; if ($file["CONTENT_TYPE"] == "image/bmp") { $cacheImageFileCheck .= ".jpg"; } static $cache = array(); $cache_id = $cacheImageFileCheck; if (isset($cache[$cache_id])) { return $cache[$cache_id]; } elseif (!file_exists($io->GetPhysicalName($_SERVER["DOCUMENT_ROOT"] . $cacheImageFileCheck))) { /****************************** QUOTA ******************************/ $bDiskQuota = true; if (COption::GetOptionInt("main", "disk_space") > 0) { $quota = new CDiskQuota(); $bDiskQuota = $quota->checkDiskQuota($file); } /****************************** QUOTA ******************************/ if ($bDiskQuota) { if (!is_array($arFilters)) { $arFilters = array(array("name" => "sharpen", "precision" => 15)); } $sourceImageFile = $_SERVER["DOCUMENT_ROOT"] . $imageFile; $cacheImageFileTmp = $_SERVER["DOCUMENT_ROOT"] . $cacheImageFile; $bNeedResize = true; $callbackData = null; foreach (GetModuleEvents("main", "OnBeforeResizeImage", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array($file, array($arSize, $resizeType, array(), false, $arFilters, $bImmediate), &$callbackData, &$bNeedResize, &$sourceImageFile, &$cacheImageFileTmp))) { break; } } if ($bNeedResize && CFile::ResizeImageFile($sourceImageFile, $cacheImageFileTmp, $arSize, $resizeType, array(), $jpgQuality, $arFilters)) { $cacheImageFile = substr($cacheImageFileTmp, strlen($_SERVER["DOCUMENT_ROOT"])); /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", filesize($io->GetPhysicalName($cacheImageFileTmp)), "insert"); } /****************************** QUOTA ******************************/ } else { $cacheImageFile = $imageFile; } foreach (GetModuleEvents("main", "OnAfterResizeImage", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array($file, array($arSize, $resizeType, array(), false, $arFilters), &$callbackData, &$cacheImageFile, &$cacheImageFileTmp, &$arImageSize))) { break; } } } else { $cacheImageFile = $imageFile; } $cacheImageFileCheck = $cacheImageFile; } if ($bInitSizes && !is_array($arImageSize)) { $arImageSize = CFile::GetImageSize($_SERVER["DOCUMENT_ROOT"] . $cacheImageFileCheck); $f = $io->GetFile($_SERVER["DOCUMENT_ROOT"] . $cacheImageFileCheck); $arImageSize[2] = $f->GetFileSize(); } $cache[$cache_id] = array("src" => $cacheImageFileCheck, "width" => intval($arImageSize[0]), "height" => intval($arImageSize[1]), "size" => $arImageSize[2]); return $cache[$cache_id]; }
function BaseConvertToDB($value) { $io = CBXVirtualIo::GetInstance(); $arRes = array("path" => ""); if (!is_array($value)) { $value = array(); } //In case of DB value just serialize it if (implode("|", array_keys($value)) === 'path|width|height|title|duration|author|date|desc') { return serialize($value); } if ($value["B_NEW_FILE"] != "N") { if (strlen($value["CUR_PATH"]) > 0 && $value["DEL_CUR_FILE"] == "Y" && CIBlockPropertyVideo::CheckFileInUploadDir($value["CUR_PATH"])) { // del current file $cur_path_ = $_SERVER["DOCUMENT_ROOT"] . Rel2Abs("/", $value["CUR_PATH"]); $flTmp = $io->GetFile($cur_path_); $flSzTmp = $flTmp->GetFileSize(); if ($io->Delete($cur_path_)) { // Quota if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $flSzTmp, "delete"); } } } // Get video if (strlen($value["PATH"]) > 0) { $arRes["path"] = $value["PATH"]; } else { if (isset($value["FILE"]) && strlen($value["FILE"]["tmp_name"]) > 0) { $pathToDir = CIBlockPropertyVideo::GetUploadDirPath(); if (!$io->DirectoryExists($_SERVER["DOCUMENT_ROOT"] . $pathToDir)) { CFileMan::CreateDir($pathToDir); } // 1. Convert name $name = preg_replace("/[^a-zA-Z0-9_:\\.]/is", "_", $value["FILE"]["name"]); $baseNamePart = substr($name, 0, strpos($name, '.')); $ext = GetFileExtension($name); if (strlen($ext) > 0 && !HasScriptExtension($name) && !substr($name, 0, 1) != ".") { $ind = 0; // 2. Check if file already exists while ($io->FileExists($_SERVER["DOCUMENT_ROOT"] . Rel2Abs($pathToDir, $name))) { $name = $baseNamePart . "_(" . ++$ind . ")." . $ext; } // 3. Rename $pathto = Rel2Abs($pathToDir, $name); if (is_uploaded_file($value["FILE"]["tmp_name"]) && $io->Copy($value["FILE"]["tmp_name"], $_SERVER["DOCUMENT_ROOT"] . $pathto)) { $arRes["path"] = Rel2Abs("/", $pathto); // Quota if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $value["FILE"]["size"], "add"); } } } } } } elseif (strlen($value["CUR_PATH"]) > 0) { if (preg_match("/^(http|https):\\/\\//", $value["CUR_PATH"])) { $arRes["path"] = $value["CUR_PATH"]; } else { $arRes["path"] = Rel2Abs("/", $value["CUR_PATH"]); } } // Width & height $arRes["width"] = intVal($value["WIDTH"]); $arRes["height"] = intVal($value["HEIGHT"]); if ($arRes["width"] < 0) { $arRes["width"] = 400; } if ($arRes["height"] < 0) { $arRes["height"] = 300; } // Video info $arRes["title"] = $value["TITLE"]; $arRes["duration"] = $value["DURATION"]; $arRes["author"] = $value["AUTHOR"]; $arRes["date"] = $value["DATE"]; $arRes["desc"] = $value["DESC"]; $strRes = serialize($arRes); if ($arRes["path"] == "" && $arRes["title"] == "" && $arRes["author"] == "") { return ""; } return $strRes; }
function DELETE($options) { $io = self::GetIo(); if (isset($options['path'])) { $options['path'] = $this->_udecode($options['path']); } $this->IsDir($options); if ($this->arParams["not_found"]) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("WD_FILE_ERROR3"), "DESTINATION_FILE_OR_FOLDER_IS_NOT_FOUND"); return "404 Not found"; } if (!$this->CheckRights("DELETE", true, $options["path"])) { $this->ThrowAccessDenied(); return "403 Forbidden"; } $quota = false; if (COption::GetOptionInt("main", "disk_space") > 0) { $quota = new CDiskQuota(); } $trashPath = $this->GetMetaID("TRASH"); $arPath = explode("/", $this->arParams["item_id"]); if (!$this->arParams["is_dir"]) { //$file = $io->CombinePath($this->real_path_full, $this->arParams["item_id"]); //$path = $io->CombinePath($this->real_path, $this->arParams["item_id"]); $file = CWebDavBase::CleanRelativePathString($this->arParams["item_id"], $this->real_path_full); $path = CWebDavBase::CleanRelativePathString($this->arParams["item_id"], $this->real_path); if ($file === false || $path === false) { return "404 Not found"; } $arPath = explode("/", $this->arParams["item_id"]); if ($arPath[1] != $this->meta_names["TRASH"]["name"] && !isset($options['force'])) { return $this->_move_to_trash($options, $this->arParams); } else { // in trash or options[force] $oFile = $io->GetFile($file); $file_size = $oFile->GetFileSize(); if ($io->Delete($file)) { $this->_delete_props($this->arParams['item_id']); $GLOBALS["APPLICATION"]->RemoveFileAccessPermission(array(SITE_ID, $path)); if (CModule::IncludeModule("search")) { CSearch::DeleteIndex("main", SITE_ID . "|" . $path); } if ($quota) { $quota->updateDiskQuota("file", $file_size, "delete"); } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("WD_FILE_ERROR3"), "DESTINATION_FILE_OR_FOLDER_IS_NOT_FOUND"); return "404 Not found"; } } } else { if ($arPath[1] != $this->meta_names["TRASH"]["name"] && !isset($options['force'])) { return $this->_move_to_trash($options, $this->arParams); } else { $params = $this->GetFilesAndFolders($this->arParams["item_id"]); if (empty($params)) { return true; } rsort($params, SORT_STRING); foreach ($params as $file) { $path = str_replace($this->real_path_full, "", $file); $path = $io->CombinePath("/", $path); $file = $io->CombinePath($this->real_path_full, $path); if (!$io->ValidatePathString($file)) { return "404 Not found"; } if ($io->FileExists($file)) { //$path = str_replace($_SERVER['DOCUMENT_ROOT'], "", $file); $oFile = $io->GetFile($file); $file_size = $oFile->GetFileSize(); if ($io->Delete($file)) { $this->_delete_props(str_replace(array($this->real_path_full, "///", "//"), "/", $file)); $GLOBALS["APPLICATION"]->RemoveFileAccessPermission(array(SITE_ID, $path)); if (CModule::IncludeModule("search")) { CSearch::DeleteIndex("main", SITE_ID . "|" . $path); } if ($quota) { $quota->updateDiskQuota("file", $file_size, "delete"); } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("WD_FILE_ERROR3"), "DESTINATION_FILE_OR_FOLDER_IS_NOT_FOUND"); return "404 Not found"; } } elseif ($io->DirectoryExists($file)) { $path = str_replace($_SERVER['DOCUMENT_ROOT'], "", $file); if ($io->Delete($file)) { $this->_delete_props(str_replace(array($this->real_path_full, "///", "//"), "/", $file)); $GLOBALS["APPLICATION"]->RemoveFileAccessPermission(array(SITE_ID, $path)); } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("WD_FILE_ERROR3"), "DESTINATION_FILE_OR_FOLDER_IS_NOT_FOUND"); return "404 Not found"; } } } if ($path == $trashPath) { $trashID = $this->GetMetaID('TRASH'); } } } clearstatcache(); return "204 No Content"; }
function UploadFile($Params) { $buffer = 'parent.oWaitWindow.Hide();'; $F = $Params['file']; $io = CBXVirtualIo::GetInstance(); if (isset($F["tmp_name"]) && strlen($F["tmp_name"]) > 0 && strlen($F["name"]) > 0 || is_uploaded_file($F["tmp_name"])) { global $APPLICATION, $USER; $strWarning = ''; $filename = $Params['filename']; $path = $Params['path']; $site = $Params['site']; $upload_and_open = $Params['upload_and_open']; $rootPath = CSite::GetSiteDocRoot($site); if ($filename == '') { $filename = $F["name"]; } $pathto = Rel2Abs($path, $filename); if (strlen($filename) > 0 && ($mess = self::CheckFileName($filename)) !== true) { $strWarning = $mess; } if ($strWarning == '') { $fn = $io->ExtractNameFromPath($pathto); if ($APPLICATION->GetFileAccessPermission(array($site, $pathto)) > "R" && ($USER->IsAdmin() || !HasScriptExtension($fn) && substr($fn, 0, 1) != "." && $io->ValidateFilenameString($fn))) { if (!$io->FileExists($rootPath . $pathto) || $_REQUEST["rewrite"] == "Y") { //************************** Quota **************************// $bQuota = true; if (COption::GetOptionInt("main", "disk_space") > 0) { $bQuota = false; $quota = new CDiskQuota(); if ($quota->checkDiskQuota(array("FILE_SIZE" => filesize($F["tmp_name"])))) { $bQuota = true; } } //************************** Quota **************************// if ($bQuota) { $io->Copy($F["tmp_name"], $rootPath . $pathto); $flTmp = $io->GetFile($rootPath . $pathto); $flTmp->MarkWritable(); if (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $flTmp->GetFileSize(), "copy"); } $buffer = 'setTimeout(function(){parent.oBXDialogControls.Uploader.OnAfterUpload("' . $filename . '", ' . ($upload_and_open == "Y" ? 'true' : 'false') . ');}, 50);'; } else { $strWarning = $quota->LAST_ERROR; } } else { $strWarning = GetMessage("FD_LOAD_EXIST_ALERT"); } } else { $strWarning = GetMessage("FD_LOAD_DENY_ALERT"); } } } else { $strWarning = GetMessage("FD_LOAD_ERROR_ALERT"); } if ($strWarning != '') { $buffer = 'alert("' . addslashes(htmlspecialcharsex($strWarning)) . '");'; } return '<script>' . $buffer . '</script>'; }
function CopyEx($path_from, $path_to, $bDeleteAfterCopy = false, $bOverride = false) { global $APPLICATION, $USER; CMain::InitPathVars($site_from, $path_from); $DOC_ROOT_FROM = CSite::GetSiteDocRoot($site_from); CMain::InitPathVars($site_to, $path_to); $DOC_ROOT_TO = CSite::GetSiteDocRoot($site_to); $strWarning = ''; //check: if we copy to the same directory if (strpos($DOC_ROOT_TO . $path_to . "/", $DOC_ROOT_FROM . $path_from . "/") === 0) { return GetMessage("FILEMAN_LIB_BAD_FOLDER") . ": \"" . $path_from . "\".\n"; } $io = CBXVirtualIo::GetInstance(); if ($io->DirectoryExists($DOC_ROOT_FROM . $path_from)) { // Minimal access - read/listing for copying files if (!$USER->CanDoFileOperation('fm_view_listing', array($site_from, $path_from))) { return GetMessage("FILEMAN_FILEMAN_FOLDER_READ_DENY") . " \"" . $path_from . "\".\n"; } if ($bDeleteAfterCopy && !$USER->CanDoFileOperation('fm_delete_folder', array($site_from, $path_from))) { return GetMessage("FILEMAN_FILEMAN_FOLDER_DEL_DENY") . " \"" . $path_from . "\".\n"; } //Check: folder exist or not $strWarTmp = CFileMan::CreateDir(array($site_to, $path_to)); if (strlen($strWarTmp) > 0) { return $strWarTmp; } $APPLICATION->CopyFileAccessPermission(array($site_from, $path_from), array($site_to, $path_to)); } else { // If we can write this file if (!$USER->CanDoFileOperation('fm_create_new_file', array($site_to, $path_to))) { return GetMessage("FILEMAN_FILEMAN_FILE_WRITE_DENY") . " \"" . $path_to . "\".\n"; } // If we can't read source-file if (!$USER->CanDoFileOperation('fm_view_file', array($site_from, $path_from))) { return GetMessage("FILEMAN_FILEMAN_FILE_READ_DENY") . " \"" . $path_from . "\".\n"; } // Copying php or system file without PHP or LPA access if (!($USER->CanDoOperation('edit_php') || $USER->CanDoFileOperation('fm_lpa', $arPath) || !(HasScriptExtension($Elem["NAME"]) || substr($Elem["NAME"], 0, 1) == "."))) { return GetMessage("FILEMAN_FILEMAN_FILE_READ_DENY") . " \"" . $path_from . "\".\n"; } // If we can't move source-file if ($bDeleteAfterCopy && !$USER->CanDoFileOperation('fm_delete_file', array($site_from, $path_from))) { return GetMessage("FILEMAN_FILEMAN_FILE_DEL_DENY") . " \"" . $path_from . "\".\n"; } //Check if folder already exist and trying to create if not $p = strrpos($path_to, "/"); $path_to_dir = substr($path_to, 0, $p); $strWarTmp = CFileMan::CreateDir(array($site_to, $path_to_dir)); if (strlen($strWarTmp) > 0) { return $strWarTmp; } if ($io->FileExists($DOC_ROOT_TO . $path_to) || $io->DirectoryExists($DOC_ROOT_TO . $path_to)) { if ($bOverride) { $strWarn = CFileMan::DeleteEx(array($site_to, $path_to)); if ($strWarn != "") { return $strWarn; } } else { return GetMessage("FILEMAN_FILEMAN_FILE_WITH_NAME") . " \"" . $path_to . "\" " . GetMessage("FILEMAN_FILEMAN_ALREADY_EXISTS") . "!\n"; } } $APPLICATION->CopyFileAccessPermission(array($site_from, $path_from), array($site_to, $path_to)); //************************** Quota **************************// if (COption::GetOptionInt("main", "disk_space") > 0) { $f = $io->GetFile($DOC_ROOT_FROM . $path_from); $size = $f->GetFileSize(); $quota = new CDiskQuota(); if (!$quota->checkDiskQuota(array("FILE_SIZE" => $size))) { return $quota->LAST_ERROR; } } //************************** Quota **************************// // Copy file if (DEBUG_FILE_MAN) { echo "copy(" . $DOC_ROOT_FROM . $path_from . "," . $DOC_ROOT_TO . $path_to . ");<br>"; } if (!$io->Copy($DOC_ROOT_FROM . $path_from, $DOC_ROOT_TO . $path_to)) { $strWarning .= GetMessage('FILEMAN_COPY_ERROR', array('#PATH_FROM#' => htmlspecialcharsex($path_from), '#PATH_TO#' => htmlspecialcharsex($path_to))); } //************************** Quota **************************// if (COption::GetOptionInt("main", "disk_space") > 0) { $quota->updateDiskQuota("file", $size, "copy"); } //************************** Quota **************************// if (CModule::IncludeModule("search")) { $site = CSite::GetSiteByFullPath($DOC_ROOT_TO . $path_to); CSearch::ReIndexFile(array($site_to, $path_to), $site); } if ($bDeleteAfterCopy && strlen($strWarning) <= 0) { // If was command "delete after copy"? $strWarning .= CFileMan::DeleteFile(array($site_from, $path_from)); } return $strWarning; } // Recursive $d = $io->GetDirectory($DOC_ROOT_FROM . $path_from); $arChildren = $d->GetChildren(); foreach ($arChildren as $child) { $fn = $child->GetName(); if ($child->IsDirectory()) { //go to recursion $strWarning .= CFileMan::CopyEx(array($site_from, $path_from . "/" . $fn), array($site_to, $path_to . "/" . $fn), $bDeleteAfterCopy, $bOverride); //back from recursion, in this subfolder all right //if($bDeleteAfterCopy) //necessary delete this subfolder // $strWarning .= CFileMan::DeleteDir($path_from."/".$file); } else { if ($fn == ".access.php") { continue; } //let's check, if we can to write there if (!$USER->CanDoFileOperation('fm_create_new_file', array($site_to, $path_to . "/" . $fn))) { $strWarning .= GetMessage("FILEMAN_FILEMAN_FILE_WRITE_DENY") . " \"" . $path_to . "/" . $fn . "\".\n"; } elseif (!$USER->CanDoFileOperation('fm_view_file', array($site_from, $path_from . "/" . $fn))) { $strWarning .= GetMessage("FILEMAN_FILEMAN_FILE_READ_DENY") . " \"" . $path_from . "/" . $fn . "\".\n"; } elseif (!($USER->CanDoOperation('edit_php') || $USER->CanDoFileOperation('fm_lpa', array($site_from, $path_from . "/" . $fn)) || !(HasScriptExtension($fn) || substr($fn, 0, 1) == "."))) { $strWarning .= GetMessage("FILEMAN_FILEMAN_FILE_READ_DENY") . " \"" . $path_from . "/" . $fn . "\".\n"; } else { if ($io->FileExists($DOC_ROOT_TO . $path_to . "/" . $fn)) { if ($bOverride) { $strWarn = CFileMan::DeleteEx(array($site_to, $path_to . "/" . $fn)); if ($strWarn != "") { $strWarning .= $strWarn . "\n"; } } else { $strWarning .= GetMessage("FILEMAN_FILEMAN_FILE_WITH_NAME") . " \"" . $path_to . "/" . $fn . "\" " . GetMessage("FILEMAN_FILEMAN_ALREADY_EXISTS") . "!\n"; } } if ($strWarning == "") { //it means we can copy, if we found here $APPLICATION->CopyFileAccessPermission(array($site_from, $path_from . "/" . $fn), array($site_to, $path_to . "/" . $fn)); if (DEBUG_FILE_MAN) { echo "copy(" . $DOC_ROOT_FROM . $path_from . "/" . $fn . "," . $DOC_ROOT_TO . $path_to . "/" . $fn . ");<br>"; } if (!$io->Copy($DOC_ROOT_FROM . $path_from . "/" . $fn, $DOC_ROOT_TO . $path_to . "/" . $fn)) { $strWarning .= GetMessage('FILEMAN_COPY_ERROR', array('#PATH_FROM#' => htmlspecialcharsex($path_from . "/" . $fn), '#PATH_TO#' => htmlspecialcharsex($path_to . "/" . $fn))); } //************************** Quota **************************// if (COption::GetOptionInt("main", "disk_space") > 0) { $f = $io->GetFile($DOC_ROOT_TO . $path_to . "/" . $fn); $quota = new CDiskQuota(); $quota->updateDiskQuota("file", $f->GetFileSize(), "copy"); } //************************** Quota **************************// if (CModule::IncludeModule("search")) { $site = CSite::GetSiteByFullPath($DOC_ROOT_TO, $path_to . "/" . $fn); CSearch::ReindexFile($path_to . "/" . $fn, $site); } if ($bDeleteAfterCopy && strlen($strWarning) <= 0) { $strWarning .= CFileMan::DeleteFile(array($site_from, $path_from . "/" . $fn)); } } } } } //we may be need, to delete our initial folder if ($bDeleteAfterCopy) { $strWarning .= CFileMan::DeleteDir(array($site_from, $path_from)); } return $strWarning; }
function SaveFileContent($abs_path, $strContent) { $strContent = str_replace("\r\n", "\n", $strContent); $file = array(); $this->ResetException(); foreach (GetModuleEvents("main", "OnBeforeChangeFile", true) as $arEvent) { if (ExecuteModuleEventEx($arEvent, array($abs_path, &$strContent)) == false) { if (!$this->GetException()) { $this->ThrowException(GetMessage("main_save_file_handler_error", array("#HANDLER#" => $arEvent["TO_NAME"]))); } return false; } } $io = CBXVirtualIo::GetInstance(); $fileIo = $io->GetFile($abs_path); $io->CreateDirectory($fileIo->GetPath()); if ($fileIo->IsExists()) { $file["exists"] = true; if (!$fileIo->IsWritable()) { $fileIo->MarkWritable(); } $file["size"] = $fileIo->GetFileSize(); } /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { $quota = new CDiskQuota(); if (false === $quota->checkDiskQuota(array("FILE_SIZE" => intVal(strLen($strContent) - intVal($file["size"]))))) { $this->ThrowException($quota->LAST_ERROR, "BAD_QUOTA"); return false; } } /****************************** QUOTA ******************************/ if ($fileIo->PutContents($strContent)) { $fileIo->MarkWritable(); } else { if ($file["exists"]) { $this->ThrowException(GetMessage("MAIN_FILE_NOT_CREATE"), "FILE_NOT_CREATE"); } else { $this->ThrowException(GetMessage("MAIN_FILE_NOT_OPENED"), "FILE_NOT_OPEN"); } return false; } bx_accelerator_reset(); $site = CSite::GetSiteByFullPath($abs_path); $DOC_ROOT = CSite::GetSiteDocRoot($site); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { //Fix for name case under Windows $abs_path = strtolower($abs_path); $DOC_ROOT = strtolower($DOC_ROOT); } if (strpos($abs_path, $DOC_ROOT) === 0 && $site !== false) { $DOC_ROOT = rtrim($DOC_ROOT, "/\\"); $path = "/" . ltrim(substr($abs_path, strlen($DOC_ROOT)), "/\\"); foreach (GetModuleEvents("main", "OnChangeFile", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array($path, $site)); } } /****************************** QUOTA ******************************/ if (COption::GetOptionInt("main", "disk_space") > 0) { $fs = $fileIo->GetFileSize(); CDiskQuota::updateDiskQuota("files", intVal($fs - intVal($file["size"])), "update"); } /****************************** QUOTA ******************************/ return true; }
private static function onEpilogHtmlCache() { global $USER; $bAutorized = is_object($USER) && $USER->IsAuthorized(); if(!$bAutorized && defined("HTML_PAGES_FILE")) { @setcookie(session_name(), "", time()-360000, "/"); } $bExcludeByFile = $_SERVER["SCRIPT_NAME"] == "/bitrix/admin/get_start_menu.php"; $posts = 0; $bytes = 0.0; $all_clean = false; //Check if modifyng action happend if(($_SERVER["REQUEST_METHOD"] === "POST") || ($bAutorized && check_bitrix_sessid() && !$bExcludeByFile)) { //if it was admin post if(strncmp($_SERVER["REQUEST_URI"], "/bitrix/", 8) === 0) { //Then will clean all the cache $bytes = \Bitrix\Main\Data\StaticHtmlFileStorage::deleteRecursive("/"); $all_clean = true; } //check if it was SEF post elseif(array_key_exists("SEF_APPLICATION_CUR_PAGE_URL", $_REQUEST) && file_exists($_SERVER['DOCUMENT_ROOT']."/urlrewrite.php")) { $arUrlRewrite = array(); include($_SERVER['DOCUMENT_ROOT']."/urlrewrite.php"); foreach($arUrlRewrite as $val) { if(preg_match($val["CONDITION"], $_SERVER["REQUEST_URI"]) > 0) { if (strlen($val["RULE"]) > 0) $url = preg_replace($val["CONDITION"], (StrLen($val["PATH"]) > 0 ? $val["PATH"]."?" : "").$val["RULE"], $_SERVER["REQUEST_URI"]); else $url = $val["PATH"]; $pos=strpos($url, "?"); if($pos !== false) { $url = substr($url, 0, $pos); } $url = substr($url, 0, strrpos($url, "/")+1); $bytes = \Bitrix\Main\Data\StaticHtmlFileStorage::deleteRecursive($url); break; } } } //public page post else { $folder = substr($_SERVER["REQUEST_URI"], 0, strrpos($_SERVER["REQUEST_URI"], "/")); $bytes = \Bitrix\Main\Data\StaticHtmlFileStorage::deleteRecursive($folder); } $posts++; } if($bytes > 0.0 && class_exists("cdiskquota")) { CDiskQuota::updateDiskQuota("file", $bytes, "delete"); } if($posts || $bytes) { self::writeStatistic( 0, //hit 0, //miss 0, //quota $posts, //posts ($all_clean? false: -$bytes) //files ); } }
/** * Update disk quota and cache statistic * @param float $bytes positive or negative value */ public static function updateQuota($bytes) { if ($bytes == 0.0) { return; } if (class_exists("cdiskquota")) { CDiskQuota::updateDiskQuota("file", abs($bytes), $bytes > 0.0 ? "copy" : "delete"); } CHTMLPagesCache::writeStatistic(0, 0, 0, 0, $bytes); }
function writeFile($file_name, $content) { global $USER; if (is_object($USER) && $USER->IsAuthorized()) { return; } $content_len = function_exists('mb_strlen') ? mb_strlen($content, 'latin1') : strlen($content); if ($content_len <= 0) { return; } $arHTMLPagesOptions = CHTMLPagesCache::GetOptions(true); if (!is_array($arHTMLPagesOptions)) { return; } //Let's be pessimists $bQuota = false; if (class_exists("cdiskquota")) { $quota = new CDiskQuota(); if ($quota->checkDiskQuota(array("FILE_SIZE" => $content_len))) { $bQuota = true; } } else { $bQuota = true; } $arStat = CHTMLPagesCache::readStatistic(); if ($arStat) { $cached_size = $arStat["FILE_SIZE"]; } else { $cached_size = 0.0; } $cache_quota = doubleval($arHTMLPagesOptions["~FILE_QUOTA"]); if ($bQuota && $cache_quota > 0.0) { if ($cache_quota < $cached_size + $content_len) { $bQuota = false; } } if ($bQuota) { CheckDirPath($file_name); $written = 0; $tmp_filename = $file_name . md5(mt_rand()) . ".tmp"; $file = fopen($tmp_filename, "wb"); if ($file !== false) { $written = fwrite($file, $content); if ($written == $content_len) { fclose($file); if (file_exists($file_name)) { unlink($file_name); } rename($tmp_filename, $file_name); @chmod($file_name, defined("BX_FILE_PERMISSIONS") ? BX_FILE_PERMISSIONS : 0664); if (class_exists("cdiskquota")) { CDiskQuota::updateDiskQuota("file", $content_len, "copy"); } } else { $written = 0; fclose($file); if (file_exists($file_name)) { unlink($file_name); } if (file_exists($tmp_filename)) { unlink($tmp_filename); } } } $arStat = CHTMLPagesCache::writeStatistic(0, 1, 0, 0, $written); } else { //Fire cleanup CHTMLPagesCache::CleanAll(); CHTMLPagesCache::writeStatistic(0, 0, 1, 0, false); } }
} else { $bQuota = true; if (COption::GetOptionInt("main", "disk_space") > 0) { $f = $io->GetFile($arFile["tmp_name"]); $bQuota = false; $size = $f->GetFileSize(); $quota = new CDiskQuota(); if ($quota->checkDiskQuota(array("FILE_SIZE" => $size))) { $bQuota = true; } } if ($bQuota) { if (!$io->Copy($arFile["tmp_name"], $DOC_ROOT . $pathto)) { $strWarning .= GetMessage("FILEMAN_FILEUPLOAD_FILE_CREATE_ERROR") . " \"" . $pathto . "\"\n"; } elseif (COption::GetOptionInt("main", "disk_space") > 0) { CDiskQuota::updateDiskQuota("file", $size, "copy"); } $f = $io->GetFile($DOC_ROOT . $pathto); $f->MarkWritable(); $module_id = 'fileman'; if (COption::GetOptionString($module_id, "log_page", "Y") == "Y") { $res_log['path'] = substr($pathto, 1); CEventLog::Log("content", "FILE_ADD", "main", "", serialize($res_log)); } } else { $strWarning .= $quota->LAST_ERROR . "\n"; } } } } }