use Bitrix\Main\Application;
if(
isset($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == "POST"
&& isset($_REQUEST["ajax"]) && $_REQUEST["ajax"]=="y"
&& isset($_REQUEST["clearcache"]) && $_REQUEST["clearcache"] == "Y"
)
{
define("STOP_STATISTICS", true);
}
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
require_once($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/prolog.php");
define("HELP_FILE", "settings/settings/cache.php");
/** @var CUser $USER */
/** @var CMain $APPLICATION */
$isAdmin = $USER->CanDoOperation('cache_control');
IncludeModuleLangFile(__FILE__);
if(
$_SERVER["REQUEST_METHOD"] == "POST"
&& isset($_REQUEST["ajax"])
&& $_REQUEST["ajax"]=="y"
&& isset($_REQUEST["clearcache"])
&& $_REQUEST["clearcache"] == "Y"
)
{
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_js.php");
if(!check_bitrix_sessid() || !$isAdmin)
{
?>
require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_admin_js.php");
}
if(
isset($_REQUEST["cachetype"])
&& $_REQUEST["cachetype"] == "landing"
&& \Bitrix\Main\Loader::includeModule("landing")
)
{
\Bitrix\Landing\Block::clearRepositoryCache();
CAdminMessage::ShowMessage(array(
"MESSAGE" => GetMessage("main_cache_finished"),
"HTML" => true,
"TYPE" => "OK",
));
?>
require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_admin_js.php");
}
else if(
(isset($_REQUEST["cachetype"]) && $_REQUEST["cachetype"] === "html")
|| \Bitrix\Main\Data\Cache::getCacheEngineType() == "cacheenginefiles"
)
{
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/cache_files_cleaner.php");
if(isset($_POST["path"]) && is_string($_POST["path"]) && mb_strlen($_POST["path"]))
{
$path = $_POST["path"];
}
else
{
$path = "";
Application::getInstance()->getSession()["CACHE_STAT"] = array();
}
$bDoNotCheckExpiredDate =
isset($_REQUEST["cachetype"])
&& (
$_REQUEST["cachetype"] === "all"
|| $_REQUEST["cachetype"] === "menu"
|| $_REQUEST["cachetype"] === "managed"
|| $_REQUEST["cachetype"] === "html"
)
;
$curentTime = time();
$endTime = time()+5;
$obCacheCleaner = new CFileCacheCleaner($_REQUEST["cachetype"] ?? '');
if(!$obCacheCleaner->InitPath($path))
{
ShowError(GetMessage("main_cache_wrong_cache_path"));
?>
require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_admin_js.php");
}
}
if (isset($_REQUEST["cachetype"]) && $_REQUEST["cachetype"] === "html")
{
$obCacheCleaner->Start();
$space_freed = 0;
while($file = $obCacheCleaner->GetNextFile())
{
if(
is_string($file)
&& !preg_match("/(\\.enabled|\\.size|.config\\.php)\$/", $file)
)
{
$file_size = filesize($file);
if (!isset(Application::getInstance()->getSession()["CACHE_STAT"]["scanned"]))
{
Application::getInstance()->getSession()["CACHE_STAT"]["scanned"] = 0;
}
Application::getInstance()->getSession()["CACHE_STAT"]["scanned"]++;
if (!isset(Application::getInstance()->getSession()["CACHE_STAT"]["space_total"]))
{
Application::getInstance()->getSession()["CACHE_STAT"]["space_total"] = 0;
}
Application::getInstance()->getSession()["CACHE_STAT"]["space_total"]+=$file_size;
if(@unlink($file))
{
if (!isset(Application::getInstance()->getSession()["CACHE_STAT"]["deleted"]))
{
Application::getInstance()->getSession()["CACHE_STAT"]["deleted"] = 0;
}
Application::getInstance()->getSession()["CACHE_STAT"]["deleted"]++;
if (!isset(Application::getInstance()->getSession()["CACHE_STAT"]["space_freed"]))
{
Application::getInstance()->getSession()["CACHE_STAT"]["space_freed"] = 0;
}
Application::getInstance()->getSession()["CACHE_STAT"]["space_freed"]+=$file_size;
$space_freed+=$file_size;
}
else
{
if (!isset(Application::getInstance()->getSession()["CACHE_STAT"]["errors"]))
{
Application::getInstance()->getSession()["CACHE_STAT"]["errors"] = 0;
}
Application::getInstance()->getSession()["CACHE_STAT"]["errors"]++;
}
if(time() >= $endTime)
break;
}
//no more than 200 files per second
usleep(5000);
}
\Bitrix\Main\Composite\Helper::updateCacheFileSize(-$space_freed);
}
elseif(\Bitrix\Main\Data\Cache::getCacheEngineType() == "cacheenginefiles")
{
$obCacheCleaner->Start();
while($file = $obCacheCleaner->GetNextFile())
{
if(is_string($file))
{
$date_expire = $obCacheCleaner->GetFileExpiration($file);
if($date_expire)
{
$file_size = filesize($file);
Application::getInstance()->getSession()["CACHE_STAT"]["scanned"]++;
Application::getInstance()->getSession()["CACHE_STAT"]["space_total"]+=$file_size;
if(
$bDoNotCheckExpiredDate
|| ($date_expire < $curentTime)
)
{
if(@unlink($file))
{
Application::getInstance()->getSession()["CACHE_STAT"]["deleted"]++;
Application::getInstance()->getSession()["CACHE_STAT"]["space_freed"]+=$file_size;
}
else
{
Application::getInstance()->getSession()["CACHE_STAT"]["errors"]++;
}
}
}
if(time() >= $endTime)
break;
}
//no more than 200 files per second
usleep(5000);
}
}
else
{
$file = false;
Application::getInstance()->getSession()["CACHE_STAT"] = array();
}
if(is_string($file))
{
$currentPath = mb_substr($file, mb_strlen($_SERVER["DOCUMENT_ROOT"]));
_CFileTree::ExtractFileFromPath($currentPath);
CAdminMessage::ShowMessage(array(
"MESSAGE"=>GetMessage("main_cache_in_progress"),
"DETAILS"=> ""
.GetMessage("main_cache_files_scanned_count", array("#value#" => "".intval(Application::getInstance()->getSession()["CACHE_STAT"]["scanned"]).""))."
"
.GetMessage("main_cache_files_scanned_size", array("#value#" => "".CFile::FormatSize(Application::getInstance()->getSession()["CACHE_STAT"]["space_total"]).""))."
"
.GetMessage("main_cache_files_deleted_count", array("#value#" => "".intval(Application::getInstance()->getSession()["CACHE_STAT"]["deleted"]).""))."
"
.GetMessage("main_cache_files_deleted_size", array("#value#" => "".CFile::FormatSize(Application::getInstance()->getSession()["CACHE_STAT"]["space_freed"]).""))."
"
.GetMessage("main_cache_files_delete_errors", array("#value#" => "".intval(Application::getInstance()->getSession()["CACHE_STAT"]["errors"]).""))."
"
.GetMessage("main_cache_files_last_path", array("#value#" => "".htmlspecialcharsbx($currentPath).""))."
"
,
"HTML"=>true,
"TYPE"=>"OK",
));
?>
}
else
{
if (isset($_REQUEST["cachetype"]) && $_REQUEST["cachetype"] == "menu")
{
$GLOBALS["CACHE_MANAGER"]->CleanDir("menu");
CBitrixComponent::clearComponentCache("bitrix:menu");
}
elseif (isset($_REQUEST["cachetype"]) && $_REQUEST["cachetype"] == "managed")
{
$GLOBALS["CACHE_MANAGER"]->CleanAll();
$GLOBALS["stackCacheManager"]->CleanAll();
}
elseif (isset($_REQUEST["cachetype"]) && $_REQUEST["cachetype"] == "html")
{
$page = \Bitrix\Main\Composite\Page::getInstance();
$page->deleteAll();
}
elseif (isset($_REQUEST["cachetype"]) && $_REQUEST["cachetype"] == "all")
{
BXClearCache(true);
$GLOBALS["CACHE_MANAGER"]->CleanAll();
$GLOBALS["stackCacheManager"]->CleanAll();
$taggedCache = Application::getInstance()->getTaggedCache();
$taggedCache->deleteAllTags();
$page = \Bitrix\Main\Composite\Page::getInstance();
$page->deleteAll();
}
if (Application::getInstance()->getSession()["CACHE_STAT"])
{
CAdminMessage::ShowMessage(array(
"MESSAGE"=>GetMessage("main_cache_finished"),
"DETAILS"=> ""
.GetMessage("main_cache_files_scanned_count", array("#value#" => "".intval(Application::getInstance()->getSession()["CACHE_STAT"]["scanned"]).""))."
"
.GetMessage("main_cache_files_scanned_size", array("#value#" => "".CFile::FormatSize(Application::getInstance()->getSession()["CACHE_STAT"]["space_total"]).""))."
"
.GetMessage("main_cache_files_deleted_count", array("#value#" => "".intval(Application::getInstance()->getSession()["CACHE_STAT"]["deleted"]).""))."
"
.GetMessage("main_cache_files_deleted_size", array("#value#" => "".CFile::FormatSize(Application::getInstance()->getSession()["CACHE_STAT"]["space_freed"]).""))."
"
.GetMessage("main_cache_files_delete_errors", array("#value#" => "".intval(Application::getInstance()->getSession()["CACHE_STAT"]["errors"]).""))."
"
,
"HTML"=>true,
"TYPE"=>"OK",
));
}
else
{
CAdminMessage::ShowMessage(array(
"MESSAGE"=>GetMessage("main_cache_finished"),
"HTML"=>true,
"TYPE"=>"OK",
));
}
?>
}
require($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/main/include/epilog_admin_js.php");
}
else
{
if(!$USER->CanDoOperation('cache_control') && !$USER->CanDoOperation('view_other_settings'))
$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
$errorMessage = "";
$okMessage = "";
if ($REQUEST_METHOD=="POST" && ($cache_on=="Y" || $cache_on=="N") && check_bitrix_sessid() && $isAdmin)
{
if(COption::GetOptionString("main", "component_cache_on", "Y")=="Y")
{
if ($cache_on=="N")
{
COption::SetOptionString("main", "component_cache_on", "N");
$okMessage .= GetMessage("MAIN_OPTION_CACHE_SUCCESS").". ";
}
}
else
{
if ($cache_on=="Y")
{
COption::SetOptionString("main", "component_cache_on", "Y");
$okMessage .= GetMessage("MAIN_OPTION_CACHE_SUCCESS").". ";
}
}
}
if($REQUEST_METHOD=="POST" && ($managed_cache_on=="Y" || $managed_cache_on=="N") && check_bitrix_sessid() && $isAdmin)
{
COption::SetOptionString("main", "component_managed_cache_on", $managed_cache_on);
if($managed_cache_on == "N")
{
$taggedCache = Application::getInstance()->getTaggedCache();
$taggedCache->clearByTag(true);
}
LocalRedirect($APPLICATION->GetCurPage()."?lang=".LANGUAGE_ID."&tabControl_active_tab=fedit4&res=managed_saved");
}
if (isset($_REQUEST["res"]) && $_REQUEST["res"] == "managed_saved")
$okMessage .= GetMessage("main_cache_managed_saved");
$APPLICATION->SetTitle(GetMessage("MCACHE_TITLE"));
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");
?>
if($errorMessage <> '')
echo CAdminMessage::ShowMessage(Array("DETAILS"=>$errorMessage, "TYPE"=>"ERROR", "MESSAGE"=>GetMessage("SAE_ERROR"), "HTML"=>true));
if($okMessage <> '')
echo CAdminMessage::ShowNote($okMessage);
?>