• Модуль: fileman
  • Путь к файлу: ~/bitrix/modules/fileman/classes/general/fileman_utils.php
  • Класс: CFilemanSearch
  • Вызов: CFilemanSearch::Search
public function Search($file)
{
	global $APPLICATION, $USER;

	if ($this->maxResultCount && count($this->Result) >= $this->maxResultCount)
		return "stop";

	if ($this->bSkip)
	{
		if ($file == $this->Params['lastPath'])
			$this->bSkip = false; // continue handle files from last path
		else
			return; // Files was handled earlier
	}

	$io = CBXVirtualIo::GetInstance();

	$bIsDir = $io->DirectoryExists($file);
	$replFileCount = 0;
	if ($bIsDir && !$this->Params['bDirsToo'])
		return;

	$entity = $bIsDir ? $io->GetDirectory($file) : $io->GetFile($file);

	$path = CFilemanUtils::TrimPath($file, $this->docRoot);
	$arPath = array($this->Params['site'], $path);

	// Check access
	if (!$USER->CanDoFileOperation('fm_view_file', $arPath))
		return;

	$name = CFileman::GetFileName($file); // Name of file or dir

	// Check filename
	if ($this->Params['fileName'] != "")
	{
		if (!$this->Params['bCaseSens'])
		{
			$name = mb_strtolower($name);
			$this->Params['fileName'] = mb_strtolower($this->Params['fileName']);
		}

		// Simple find in file name
		if (mb_strpos($this->Params['fileName'], "*") === false)
		{
			if (mb_strpos($name, $this->Params['fileName']) === false)
				return;
		}
		else // name pattern with "*"
		{
			$pattern = str_replace('.', '.',$this->Params['fileName']);
			$pattern = str_replace('/', '', $pattern);
			$pattern = str_replace('*', '.*', $pattern);
			if (!preg_match('/^'.$pattern.'$/i', $io->ExtractNameFromPath($file)))
				return;
		}
	}

	if (!$bIsDir)
	{
		// Check filesize
		$size = $entity->GetFileSize();
		// Filesize limits in Kb
		if (
			($this->Params['sizeFrom'] > 0 && $size < $this->Params['sizeFrom'] * 1024)
			||
			($this->Params['sizeTo'] > 0 && $size > $this->Params['sizeTo'] * 1024)
		)
			return;
	}
	else
	{
		$size = 0;
	}

	// Check filetime
	$time = $entity->GetModificationTime()+CTimeZone::GetOffset();
	if (
		($this->Params['dateFrom'] && $time < MakeTimeStamp($this->Params['dateFrom'], CLang::GetDateFormat("FULL")))
		||
		($this->Params['dateTo'] && $time > MakeTimeStamp($this->Params['dateTo'], CLang::GetDateFormat("FULL")))
	)
		return;

	if ($this->Params['phrase'] != "")
	{
		// File size limits or it's dir or access denied
		if ($size > $this->maxFileOpenSize || $bIsDir || ($this->bReplace && !$USER->CanDoFileOperation('fm_edit_existent_file', $arPath)))
			return;

		$fTmp = $io->GetFile($file);

		$phrase = $this->Params['phrase'];
		$fileContent = str_replace("rn","n", $fTmp->GetContents());
		$origFileContent = $fileContent;
		$isPHP = CFileman::IsPHP($fileContent) || HasScriptExtension($path) || mb_substr($name, 0, 1) == ".";

		if (!$this->Params['bCaseSens'])
		{
			$phrase = mb_strtolower($phrase);
			$fileContent = mb_strtolower($fileContent);
		}

		$I_PCRE_MODIFIER = $this->Params['bCaseSens'] ? '' : 'i';

		// TODO: Add check Entire word
		//$this->Params['entire']

		if (mb_strpos($fileContent, $phrase) === false)
			return;

		if ($this->bReplace) // Replace
		{
			if ($isPHP && !$USER->CanDoOperation('edit_php'))
				return; // User can't write PHP files

			$pattern = '/'.preg_quote($this->Params['phrase'], '/').'/'.$I_PCRE_MODIFIER.BX_UTF_PCRE_MODIFIER;

			$res = array();
			preg_match_all($pattern, $origFileContent, $res);
			$origFileContent = preg_replace($pattern, $this->Params['replacePhrase'], $origFileContent);
			$replFileCount = count($res[0]);

			$APPLICATION->SaveFileContent($file, $origFileContent);
		}
		else
		{
			if ($isPHP && !($USER->CanDoOperation('edit_php') || $USER->CanDoFileOperation('fm_lpa', $arPath)))
				return; // User can't read PHP files

			$pattern = '/'.preg_quote($this->Params['phrase'], '/').'/'.$I_PCRE_MODIFIER.BX_UTF_PCRE_MODIFIER;

			// Only for LPA. All php fragments will be cutted off
			if ($USER->CanDoFileOperation('fm_lpa', $arPath) && !$USER->CanDoOperation('edit_php'))
				$origFileContent = CMain::ProcessLPA($origFileContent, '');

			$res = array();
			preg_match_all($pattern, $origFileContent, $res);
			$replFileCount = count($res[0]);
		}
	}

	$this->Result[] = array(
		'path' => $path,
		'size' => $size,
		'b_dir' => $bIsDir,
		'time' => $time,
		'str_date' => date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL")), $time),
		'str_size' => $bIsDir ? "" : CFile::FormatSize($size),
		'type_src' => "/bitrix/images/fileman/types/".($bIsDir ? "folder" : CFileMan::GetFileTypeEx($file)).".gif",
		'repl_count' => $replFileCount // used only in replace-mode to count matches
	);
}