<?php
/*
	--Содержание файла--
	Все функции обработки текста 
	Смайлы, вв-коды, транслит, 
	обработка ссылок, и т.д. 
	
*/
	// Подсветка кода \\
	function bbcodehightlight($arr)
	{
	$arr[0]=html_entity_decode($arr[0], ENT_QUOTES, 'UTF-8');
		return '<div class="cit" style="overflow:scroll;clip:auto;max-width:480px;">'.preg_replace('#<code>(.*?)</code>#si', '\\1' ,highlight_string($arr[0],1)).'</div>'."\n";
	}
	// ВВ коды \\
	function BBcode($msg)
	{
	global $set;
	$bbcode=array();
		if ($set['bb_i'])$bbcode['/\[i\](.+)\[\/i\]/isU']='<em>$1</em>';
		if ($set['bb_b'])$bbcode['/\[b\](.+)\[\/b\]/isU']='<strong>$1</strong>';
		if ($set['bb_u'])$bbcode['/\[u\](.+)\[\/u\]/isU']='<span style="text-decoration:underline;">$1</span>';
		if ($set['bb_big'])$bbcode['/\[big\](.+)\[\/big\]/isU']='<span style="font-size:large;">$1</span>';
		if ($set['bb_small'])$bbcode['/\[small\](.+)\[\/small\]/isU']='<span style="font-size:small;">$1</span>';
		if ($set['bb_red'])$bbcode['/\[red\](.+)\[\/red\]/isU']='<span style="color:#ff0000;">$1</span>';
		if ($set['bb_yellow'])$bbcode['/\[yellow\](.+)\[\/yellow\]/isU']='<span style="color:#ffff22;">$1</span>';
		if ($set['bb_green'])$bbcode['/\[green\](.+)\[\/green\]/isU']='<span style="color:#00bb00;">$1</span>';
		if ($set['bb_blue'])$bbcode['/\[blue\](.+)\[\/blue\]/isU']='<span style="color:#0000bb;">$1</span>';
		if ($set['bb_white'])$bbcode['/\[white\](.+)\[\/white\]/isU']='<span style="color:#ffffff;">$1</span>';
		if ($set['bb_size'])$bbcode['/\[size=([0-9]+)\](.+)\[\/size\]/isU']='<span style="font-size:$1px;">$2</span>';
		if (count($bbcode))$msg= preg_replace(array_keys($bbcode), array_values($bbcode), $msg);

		if ($set['bb_code']){
			$msg=preg_replace_callback('#&lt;\?(.*?)\?&gt;#sui', 'bbcodehightlight', $msg);
			$msg=preg_replace('#\[code\](.*?)\[/code\]#si', '\1', $msg);
		}
	return $msg;
	}
	// Смайлы \\
	function smiles($msg){
	$msg = strtr($msg, array(
		':лол:' => '<img src="/style/smiles/rofl.gif" alt="смайл" />',
		':лол2:' => '<img src="/style/smiles/lol.gif" alt="смайл" />',
		':браво:' => '<img src="/style/smiles/clapping.gif" alt="смайл" />',
		':-D' => '<img src="/style/smiles/biggrin.gif" alt="смайл" />',
		':скучно:' => '<img src="/style/smiles/boredom.gif" alt="смайл" />',
		':плохой:' => '<img src="/style/smiles/bad.gif" alt="смайл" />',
		':-)' => '<img src="/style/smiles/smile.gif" alt="смайл" />',
		':грусть:' => '<img src="/style/smiles/sad.gif" alt="смайл" />',
		';-)' => '<img src="/style/smiles/wink.gif" alt="смайл" />',
		':язык:' => '<img src="/style/smiles/blum3.gif" alt="смайл" />',
		':жвачка:' => '<img src="/style/smiles/dirol.gif" alt="смайл" />',
		':стесняюсь:' => '<img src="/style/smiles/blush2.gif" alt="смайл" />',
		':чмок:' => '<img src="/style/smiles/kiss.gif" alt="смайл" />',
		':плачу:' => '<img src="/style/smiles/cray.gif" alt="смайл" />',
		':секрет:' => '<img src="/style/smiles/secret.gif" alt="смайл" />',
		':агрессия:' => '<img src="/style/smiles/aggressive.gif" alt="смайл" />',
		':спятил:' => '<img src="/style/smiles/fool.gif" alt="смайл" />',
		':бее:' => '<img src="/style/smiles/beee.gif" alt="смайл" />',
		':усмешка:' => '<img src="/style/smiles/mosking.gif" alt="смайл" />',
		':диабло:' => '<img src="/style/smiles/diablo.gif" alt="смайл" />',
		':музыка:' => '<img src="/style/smiles/music2.gif" alt="смайл" />',
		':поцелуй:' => '<img src="/style/smiles/air_kiss.gif" alt="смайл" />',
		':поцелуй2:' => '<img src="/style/smiles/kiss_3.gif" alt="смайл" />',
		':стоп:' => '<img src="/style/smiles/stop.gif" alt="смайл" />', 
		':роза:' => '<img src="/style/smiles/give_rose.gif" alt="смайл" />',
		':хорошо:' => '<img src="/style/smiles/good.gif" alt="смайл" />',
		':пью:' => '<img src="/style/smiles/drinks.gif" alt="смайл" />',
		':любовь:' => '<img src="/style/smiles/man_in_love.gif" alt="смайл" />',
		':бомба:' => '<img src="/style/smiles/bomb.gif" alt="смайл" />',
		':аут:' =>  '<img src="/style/smiles/wacko.gif" alt="смайл" />',
		':мамба:' =>  '<img src="/style/smiles/mamba.gif" alt="смайл" />',
		':пардон:' =>  '<img src="/style/smiles/pardon.gif" alt="смайл" />', 
		':нет:' =>  '<img src="/style/smiles/nea.gif" alt="смайл" />',
		':чокнутый:' =>  '<img src="/style/smiles/crazy.gif" alt="смайл" />',
		':незнаю:' =>  '<img src="/style/smiles/dntknw.gif" alt="смайл" />',
		':извени:' =>  '<img src="/style/smiles/sorry.gif" alt="смайл" />',
		':ура:' =>  '<img src="/style/smiles/yahoo.gif" alt="смайл" />',
		':танцую:' =>  '<img src="/style/smiles/dance4.gif" alt="смайл" />',
		':помогите:' =>  '<img src="/style/smiles/help.gif" alt="смайл" />',
		':ок:' =>  '<img src="/style/smiles/ok.gif" alt="смайл" />',
		':привет:' =>  '<img src="/style/smiles/preved.gif" alt="смайл" />',
		':шалун:' =>  '<img src="/style/smiles/acute.gif" alt="смайл" />',
		':пока:' =>  '<img src="/style/smiles/bye.gif" alt="смайл" />',
		':пишу:' =>  '<img src="/style/smiles/mail1.gif" alt="смайл" />',
		':обстену:' =>  '<img src="/style/smiles/dash1.gif" alt="смайл" />',
		':да:' =>  '<img src="/style/smiles/yes3.gif" alt="смайл" />',
		':думаю:' =>  '<img src="/style/smiles/scratch_one-s_head.gif" alt="смайл" />',
		':счастье:' => '<img src="/style/smiles/i-m_so_happy.gif" alt="смайл" />',
		':вковычках:' =>  '<img src="/style/smiles/pleasantry.gif" alt="смайл" />',
		':жалею:' =>  '<img src="/style/smiles/posle.gif" alt="смайл" />',
		':подзатыльник:' =>  '<img src="/style/smiles/punish.gif" alt="смайл" />',
		':шок:' =>  '<img src="/style/smiles/shok.gif" alt="смайл" />',
	));
	return $msg;
	}
	// Функции для работы внешних ссылок \\
	
	function img_preg($arr)
	{
	return '<img src="http://'.$_SERVER['HTTP_HOST'].'/go.php?go='.base64_encode(html_entity_decode($arr[1])).'" alt="img" />';
	}

	function links_preg1($arr)
	{
	global $set;
		if (preg_match('#^http://'.preg_quote($_SERVER['HTTP_HOST']).'#',$arr[1]) || !preg_match('#://#',$arr[1]))
			return '<a href="'.$arr[1].'">'.$arr[2].'</a>';
		else
			return '<a'.($set['web']?' target="_blank"':null).' href="http://'.$_SERVER['HTTP_HOST'].'/go.php?go='.base64_encode(html_entity_decode($arr[1])).'">'.$arr[2].'</a>';

	}
	
	function links_preg2($arr)
	{
	global $set;
		if (preg_match('#^http://'.preg_quote($_SERVER['HTTP_HOST']).'#',$arr[2]))
			return $arr[1].'<a href="'.$arr[2].'">'.$arr[2].'</a>'.$arr[4];
		else
			return $arr[1].'<a'.($set['web']?' target="_blank"':null).' href="http://'.$_SERVER['HTTP_HOST'].'/go.php?go='.base64_encode(html_entity_decode($arr[2])).'">'.$arr[2].'</a>'.$arr[4];
	}
	
	function links($msg)
	{
	global $set;
		if ($set['bb_img'])$msg=preg_replace_callback('/\[img\](.+)\[\/img\]/isU', 'img_preg', $msg);
		if ($set['bb_url'])$msg=preg_replace_callback('/\[url=(.+)\](.+)\[\/url\]/isU', 'links_preg1', $msg); 
		if ($set['bb_http'])$msg=preg_replace_callback('~(^|\s)([a-z]+://([^ \r\n\t`\'"]+))(\s|$)~iu', 'links_preg2', $msg);
		return $msg;
	}
	
	function strlen2($str)
	{
		$rus=array('й','ц','у','к','е','н','г','ш','щ','з','х','ъ','ф','ы','в','а','п','р','о','л','д','ж','э','я','ч','с','м','и','т','ь','б','ю','Й','Ц','У','К','Е','Н','Г','Ш','Щ','З','Х','Ъ','Ф','Ы','В','А','П','Р','О','Л','Д','Ж','Э','Я','Ч','С','М','И','Т','Ь','Б','Ю');
		return strlen(str_replace($rus, '0', $str));
	}
	
	function translit($in)
	{
		$trans1= array("JO","ZH","CH","SH","SCH","JE","JY","JA","jo","zh","ch","sh","sch","je","jy","ja","A","B","V","G","D","E","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","C","'","Y","a","b","v","g","d","e","z","i","j","k","l","m","n","o","p","r","s","t","u","f","h","c","'","y");
		$trans2= array("Ё","Ж","Ч","Ш","Щ","Э","Ю","Я","ё","ж","ч","ш","щ","э","ю","я","А","Б","В","Г","Д","Е","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ь","Ы","а","б","в","г","д","е","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ь","ы");
		return str_replace($trans1,$trans2,$in);
	}

	function retranslit($in)
	{
		$trans1= array("'",'`',',',' ',"Ё","Ж","Ч","Ш","Щ","Э","Ю","Я","ё","ж","ч","ш","щ","э","ю","я","А","Б","В","Г","Д","Е","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ь","Ы","а","б","в","г","д","е","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ь","ы");
		$trans2= array('_','_','_','_',"JO","ZH","CH","SH","SCH","Je","Jy","Ja","jo","zh","ch","sh","sch","je","jy","ja","A","B","V","G","D","E","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","C","","Y","a","b","v","g","d","e","z","i","j","k","l","m","n","o","p","r","s","t","u","f","h","c","","y");
		return str_replace($trans1,$trans2,$in);
	}

	function tr_loads($in)
	{
		return preg_replace('#[^A-z0-9\_\-\.\(\)]+#ui', '_', $in);
	}
	
	function url($url)
	{
		$url2=preg_split('#&(amp;)?#', $url);
		$url3=NULL;
			for ($i=0;$i<count($url2);$i++)
			{
				$url4=explode('=', $url2[$i]);
					if (isset($url4[1]))
						$url3.=$url4[0].'='.urlencode($url4[1]);
					else $url3.=$url4[0];
				if ($i<count($url2)-1)$url3.='&amp;';
			}
		return $url3;
	}


	function url2($url)
	{
		$url2=explode('/', $url);
			for ($i=0;$i<sizeof($url2);$i++)
			{
				$url2[$i]=urlencode($url2[$i]);
			}
		return implode('/',$url2);
	}
	
	// функция обрабатывает текстовые строки перед выводом в браузер
	// настоятельно не рекомундуется тут что-либо менять
	function output_text($str,$br=1,$html=1,$smiles=1,$links=1,$bbcode=1)
	{
	global $theme_ini;
		if ($html)$str=htmlentities($str, ENT_QUOTES, 'UTF-8'); // преобразуем все к нормальному перевариванию браузером
		if ($links)$str=links($str); // обработка ссылок

		if ($smiles)
			$str=smiles($str); // вставка смайлов

		if ($bbcode)
		{
			$tmp_str=$str;
			$str=bbcode($str); // обработка bbcode
		}

		if ($br)
			$str=br($str); // переносы строк
	return stripslashes($str); // возвращаем обработанную строку
	}
	
	// для форм \\
	function input_value_text($str){
		return output_text($str,0,1,0,0,0);
	}
?>
