$Sitemap = 'LINK_WITH_SITEMAP';
$file = 'robots.txt';
$searchText = "User-agent: Baiduspider\nDisallow: /\nUser-agent: AhrefsBot\nDisallow: /\nUser-agent: MJ12bot\nDisallow: /\nUser-agent: BLEXBot\nDisallow: /\nUser-agent: DotBot\nDisallow: /\nUser-agent: SemrushBot\nDisallow: /\nUser-agent: YandexBot\nDisallow: /\nUser-agent: *\nAllow: /";
$searchPattern = "/User-agent: Baiduspider\s*\nDisallow: \/\s*\nUser-agent: AhrefsBot\s*\nDisallow: \/\s*\nUser-agent: MJ12bot\s*\nDisallow: \/\s*\nUser-agent: BLEXBot\s*\nDisallow: \/\s*\nUser-agent: DotBot\s*\nDisallow: \/\s*\nUser-agent: SemrushBot\s*\nDisallow: \/\s*\nUser-agent: YandexBot\s*\nDisallow: \/\s*\nUser-agent: \*\s*\nAllow: /";
$additionalText = "\nSitemap: {$Sitemap}";

if (file_exists($file)) {
	$content = file_get_contents($file);

	if (preg_match($searchPattern, $content)) {
		if (strpos($content, $additionalText) === false) {
			file_put_contents($file, $content . $additionalText, LOCK_EX);
			echo "Sitemap added\n";
		} else {
			echo "Sitemap already exists\n";
		}
	} else {
		file_put_contents($file, $searchText . $additionalText, LOCK_EX);
		echo "robots was correctly changed\n";
	}
} else {
	if (file_put_contents($file, $searchText . $additionalText, LOCK_EX) !== false) {
		echo "robots was created correctly\n";
	} else {
		echo "Something went wrong!\n";
	}
}