• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/functions.php
  • Класс: CSocNetTextParser
  • Вызов: CSocNetTextParser::convert
function convert($text, $bPreview = True, $arImages = array(), $allow = array("HTML" => "N", "ANCHOR" => "Y", "BIU" => "Y", "IMG" => "Y", "QUOTE" => "Y", "CODE" => "Y", "FONT" => "Y", "LIST" => "Y", "SMILES" => "Y", "NL2BR" => "N", "VIDEO" => "Y"), $type = "html")	//, "KEEP_AMP" => "N"
{
	global $DB;

	$text = preg_replace("#([?&;])PHPSESSID=([0-9a-zA-Z]{32})#is", "\1PHPSESSID1=", $text);
	$type = ($type == "rss" ? "rss" : "html");

	$this->quote_error = 0;
	$this->quote_open = 0;
	$this->quote_closed = 0;
	$this->code_error = 0;
	$this->code_open = 0;
	$this->code_closed = 0;
	if ($allow["HTML"] != "Y")
	{
		if ($allow["CODE"]=="Y")
		{
			$text = str_replace(array("01", "02", chr(5), chr(6), "'", """), array("", "", "", "", chr(5), chr(6)), $text);
			$text = preg_replace(
				array(
					"#]*>|>)(.+?)]*>|>)#is".BX_UTF_PCRE_MODIFIER,
					"/[code([^]])*]/is".BX_UTF_PCRE_MODIFIER,
					"/[/code([^]])*]/is".BX_UTF_PCRE_MODIFIER),
				array(
					"[code]\2[/code]",
					"01",
					"02",
				),
				$text
			);
			$this->matchNum = 2;
			$text = preg_replace_callback(
				"/(?<=[01])(([^02]+))(?=([02]))/is".BX_UTF_PCRE_MODIFIER, 
				array($this, "pre_convert_code_tag_callback"), 
				$text
			);
			$text = preg_replace(
				array(
					"/01/",
					"/02/"),
				array(
					"[code]",
					"[/code]"
				), $text
			);
			$text = str_replace(array(chr(5), chr(6)), array("'", """), $text);
		}
		if ($allow["ANCHOR"]=="Y")
		{
			$text = preg_replace(
				array(
					"#]+hrefs*=s*["]+(([^"])+)["]+[^>]*>(.+?)]*>#is".BX_UTF_PCRE_MODIFIER,
					"#]+hrefs*=s*[']+(([^'])+)[']+[^>]*>(.+?)]*>#is".BX_UTF_PCRE_MODIFIER,
					"#]+hrefs*=s*(([^'">])+)>(.+?)]*>#is".BX_UTF_PCRE_MODIFIER),
				"[url=\1]\3[/url]", $text);
		}
		if ($allow["BIU"]=="Y")
		{
			$text = preg_replace(
				array(
					"/]*)>(.+?)]*)>/is".BX_UTF_PCRE_MODIFIER,
					"/]*)>(.+?)]*)>/is".BX_UTF_PCRE_MODIFIER,
					"/a-z]*)>(.+?)a-z]*)>/is".BX_UTF_PCRE_MODIFIER,
					"/]*)>(.+?)]*)>/is".BX_UTF_PCRE_MODIFIER),
				array(
					"[b]\2[/b]",
					"[u]\2[/u]",
					"[s]\2[/s]",
					"[i]\2[/i]"),
				$text);
		}
		if ($allow["IMG"]=="Y")
		{
			$text = preg_replace(
				"#]+srcs*=[s"']*(((http|https|ftp)://[.-_:a-z0-9@]+)*(/[-_/=:.a-z0-9@{}&?%]+)+)[s"']*[^>]*>#is".BX_UTF_PCRE_MODIFIER,
				"[img]\1[/img]", $text);
		}
		if ($allow["QUOTE"]=="Y")
		{
			//$text = preg_replace("#((.*))#is", "[quote]\3[/quote]", $text);
			$text = preg_replace("#<(/?)quote(.*?)>#is", "[\1quote]", $text);
		}
		if ($allow["FONT"]=="Y")
		{
			$text = preg_replace(
				array(
					"/]+sizes*=[s"']*([0-9]+)[s"']*[^>]*>(.+?)]*>/is".BX_UTF_PCRE_MODIFIER,
					"/]+colors*=[s"']*(#[a-f0-9]{6})[^>]*>(.+?)]*>/is".BX_UTF_PCRE_MODIFIER,
					"/]+faces*=[s"']*([a-zs-]+)[s"']*[^>]*>(.+?)]*>/is".BX_UTF_PCRE_MODIFIER),
				array(
					"[size=\1]\2[/size]",
					"[color=\1]\2[/color]",
					"[font=\1]\2[/font]"),
				$text);
		}
		if ($allow["LIST"]=="Y")
		{
			$text = preg_replace(
				array(
					"/]*)|(s*))>(.+?)]*)>/is".BX_UTF_PCRE_MODIFIER,
					"/]*)|(s*))>/is".BX_UTF_PCRE_MODIFIER),
				array(
					"[list]\4[/list]",
					"[*]"),
				$text);
		}
		if ($text <> '')
		{
			$text = str_replace(
				array("<", ">", """),
				array("<", ">", """),
				$text);
		}
	}
	elseif ($allow["NL2BR"]=="Y")
	{
		$text = str_replace("n", "
", $text); } if ($allow["ANCHOR"]=="Y") { $word_separator = str_replace("]", "", $this->word_separator); $text = preg_replace("'(?<=^|[".$word_separator."]|s)((http|https|news|ftp|aim|mailto)://[.-_:a-z0-9@]([^"s'[]{}()])*)'is", "[url]\1[/url]", $text); } if ($allow["CODE"] == "Y") { $text = preg_replace( array( "/[code([^]])*]/is".BX_UTF_PCRE_MODIFIER, "/[/code([^]])*]/is".BX_UTF_PCRE_MODIFIER ), array( "01", "02" ), $text ); $this->matchNum = 2; $this->matchType = $type; $text = preg_replace_callback( "/(01)([^02]+)(02)/is".BX_UTF_PCRE_MODIFIER, array($this, "convert_code_tag_callback"), $text ); $text = preg_replace( array( "/01/", "/02/" ), array( "[code]", "[/code]" ), $text ); } if ($allow["QUOTE"] == "Y") { $this->matchNum = 1; $this->matchType = $type; $text = preg_replace_callback( "#([quote([^]])*](.*)[/quote([^]])*])#is", array($this, "convert_quote_tag_callback"), $text ); } if ($allow["IMG"]=="Y") { $this->matchNum = 1; $this->matchType = $type; $text = preg_replace_callback( "#[img](.+?)[/img]#i", array($this, "convert_image_tag_callback"), $text ); } if ($allow["ANCHOR"] == "Y") { $this->matchNum = 1; $this->matchNum2 = 1; $text = preg_replace_callback( "/[url]([^]]+?)[/url]/i".BX_UTF_PCRE_MODIFIER, array($this, "convert_anchor_tag_callback"), $text ); $this->matchNum = 1; $this->matchNum2 = 2; $text = preg_replace_callback( "/[urls*=s*([^]]+?)s*](.*?)[/url]/i".BX_UTF_PCRE_MODIFIER, array($this, "convert_anchor_tag_callback"), $text ); } if ($allow["BIU"]=="Y") { $text = preg_replace( array( "/[b](.+?)[/b]/is".BX_UTF_PCRE_MODIFIER, "/[i](.+?)[/i]/is".BX_UTF_PCRE_MODIFIER, "/[s](.+?)[/s]/is".BX_UTF_PCRE_MODIFIER, "/[u](.+?)[/u]/is".BX_UTF_PCRE_MODIFIER), array( "\1", "\1", "\1", "\1"), $text); } if ($allow["LIST"]=="Y") { $text = preg_replace( array( "/[list](.+?)[/list]/is".BX_UTF_PCRE_MODIFIER, "/[*]/".BX_UTF_PCRE_MODIFIER), array( "
    \1
", "
  • "), $text); } if ($allow["FONT"]=="Y") { while (preg_match("/[sizes*=s*([^]]+)](.+?)[/size]/is".BX_UTF_PCRE_MODIFIER, $text)) { $this->matchNum = 1; $this->matchNum2 = 2; $this->matchType = 'size'; $text = preg_replace_callback( "/[sizes*=s*([^]]+)](.+?)[/size]/is".BX_UTF_PCRE_MODIFIER, array($this, "convert_font_attr_callback"), $text ); } while (preg_match("/[fonts*=s*([^]]+)](.*?)[/font]/is".BX_UTF_PCRE_MODIFIER, $text)) { $this->matchNum = 1; $this->matchNum2 = 2; $this->matchType = 'font'; $text = preg_replace_callback( "/[fonts*=s*([^]]+)](.*?)[/font]/is".BX_UTF_PCRE_MODIFIER, array($this, "convert_font_attr_callback"), $text ); } while (preg_match("/[colors*=s*([^]]+)](.+?)[/color]/is".BX_UTF_PCRE_MODIFIER, $text)) { $this->matchNum = 1; $this->matchNum2 = 2; $this->matchType = 'color'; $text = preg_replace_callback( "/[colors*=s*([^]]+)](.+?)[/color]/is".BX_UTF_PCRE_MODIFIER, array($this, "convert_font_attr_callback"), $text ); } } $text = str_replace( array( "(c)", "(C)", "(tm)", "(TM)", "(Tm)", "(tM)", "(r)", "(R)", "n" ), array( "©", "©", "™", "™", "™", "™", "®", "®", "
    " ), $text ); if ($this->MaxStringLen > 0) { $this->matchNum = 1; $text = preg_replace_callback( "/(?<=^|>)([^<]+)(?=<|$)/is".BX_UTF_PCRE_MODIFIER, array($this, "part_long_words_callback"), $text ); } if ($allow["SMILES"]=="Y") { if ( is_array($this->smiles) && !empty($this->smiles) ) { if ($this->path_to_smile !== false) { $path_to_smile = $this->path_to_smile; } else { $path_to_smile = "/bitrix/images/socialnetwork/smile/"; } $arSmiles = array(); $arQuoted = array(); foreach ($this->smiles as $a_id => $row) { if($row["TYPING"] == '' || $row["IMAGE"] == '') continue; $typing = htmlspecialcharsbx($row["TYPING"]); $arSmiles[$typing] = 'smile'.$typing.''; $arQuoted[] = preg_quote($typing, "/"); } $ar = preg_split("/(?<=[s>])(".implode("|", $arQuoted).")/".BX_UTF_PCRE_MODIFIER, " ".$text, -1, PREG_SPLIT_DELIM_CAPTURE); $text = ""; foreach($ar as $piece) { if(array_key_exists($piece, $arSmiles)) $text .= $arSmiles[$piece]; else $text .= $piece; } } } if (($allow["VIDEO"] ?? null) === "Y") { while (preg_match("/[video(.+?)](.+?)[/video[s]*]/is".BX_UTF_PCRE_MODIFIER, $text)) { $this->matchNum = 1; $this->matchNum2 = 2; $text = preg_replace_callback( "/[video([^]]*)](.+?)[/video[s]*]/is".BX_UTF_PCRE_MODIFIER, array($this, "convert_video_callback"), $text ); } } return trim($text); }