- Модуль: fileman
- Путь к файлу: ~/bitrix/modules/fileman/classes/general/snippets.php
- Класс: CSnippets
- Вызов: CSnippets::Update
static function Update($params = array())
{
global $APPLICATION;
$res = false;
$title = $params['title'];
$description = $params['description'];
$currentPath = $params['new'] ? '' : CFileMan::SecurePathVar($params['current_path']);
$path = CFileMan::SecurePathVar($params['path']);
$template = CFileMan::SecurePathVar($params['template']);
$code = $params['code'];
$basePath = self::GetBasePath($template);
$snippetPath = $basePath.($path == '' ? '' : '/'.$path);
$io = CBXVirtualIo::GetInstance();
if(!$io->DirectoryExists($basePath))
{
$io->CreateDirectory($basePath);
}
if ($params['new'])
{
$fileName = CSnippets::GetDefaultFileName($snippetPath).'.snp';
}
else
{
$currentPath = $basePath.'/'.$currentPath;
$oldSnippetPath = $io->ExtractPathFromPath($currentPath);
if ($snippetPath !== $oldSnippetPath && $io->FileExists($currentPath))
{
$io->Delete($currentPath);
$fileName = CSnippets::GetDefaultFileName($snippetPath).'.snp';
}
else
{
$fileName = $io->ExtractNameFromPath($currentPath);
}
}
$key = ($path === '' ? '' : $path.'/').$fileName;
if (!$io->ValidatePathString('/'.$fileName) ||
IsFileUnsafe($snippetPath.'/'.$fileName) ||
HasScriptExtension($snippetPath.'/'.$fileName))
{
return false;
}
// 1. Save new snippet with new content
if ($code)
{
$APPLICATION->SaveFileContent($snippetPath.'/'.$fileName, $code);
}
// 2. Rewrite title & description in .content.php
if ($title || $description)
{
$SNIPPETS = array();
if ($io->FileExists($basePath."/.content.php"))
@include($basePath."/.content.php");
if ($title)
$SNIPPETS[$key]['title'] = $title;
if ($description)
$SNIPPETS[$key]['description'] = $description;
$contentSrc = ''.chr(10);
$contentSrc .= ''.chr(10).'$SNIPPETS = Array();'.chr(10);
foreach ($SNIPPETS as $k => $snip)
{
if($io->FileExists(CFileMan::SecurePathVar($basePath.'/'.$k)))
{
$contentSrc .= '$SNIPPETS[''.CUtil::addslashes($k).''] = Array(';
if (isset($snip['title']) && $snip['title'] !== '')
{
$contentSrc .= ''title' => ''.Cutil::addslashes($snip['title']).''';
if (isset($snip['description']) && $snip['description'] !== '')
$contentSrc .= ', ';
}
if (isset($snip['description']) && $snip['description'] !== '')
{
$contentSrc .= ''description' => ''.Cutil::addslashes($snip['description']).''';
}
$contentSrc .= ');'.chr(10);
}
}
$contentSrc .= '?>';
$APPLICATION->SaveFileContent($basePath."/.content.php", $contentSrc);
}
$res = array('result' => true);
CSnippets::ClearCache();
return $res;
}