<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================

// ====================================================================
// <<-- HOEHE DER CODEBOXEN ERMITTELN -->>
// ====================================================================
function divheight($text)
{
	$lines = '';
	$maxlines = 10;
	$mupi = 1;
	$lines = max(substr_count($text, "\n"), substr_count($text, "<br />")) + 1;//count(explode("\n", $text));

	if(empty($lines) || !isset($lines) || $lines <= 1)
	{
		return '20';
	} else {
		if ($lines > $maxlines && $maxlines > 0)
		{
			$lines = $maxlines;
			$mupi = 15;
		}
		else if ($lines < 1)
		{
			$mupi = 1;
			$lines = 1;
		}
		return ($lines * $mupi );
	}
}


// ====================================================================
// <<-- HTML CODES ZURUECKGEBEN -->>
// ====================================================================
function  unkspecialchars( $string , $extra="0")
{
	$string = str_replace ( array('<br>','<BR>', '<br />'), '', $string );
	$string = str_replace ( '&amp;', '&', $string );
	$string = str_replace ( '&#039;', '\'', $string );
	$string = str_replace ( '&quot;', '\"', $string );
	
	$string = str_replace ( '&lt;', '<', $string );
	$string = str_replace ( '&gt;', '>', $string );
	return $string;
}

function kspecialchars($string)
{
	//return htmlspecialchars($string);
	return koobispecialchars($string);
}

// ====================================================================
// <<-- BB- CODES VORBEREITEN -->>
// ====================================================================
function pre_kcodes($text, $location='', $imgcode='') 
{
	
	$agent = AGENT;
	switch($agent)
	{
		case 'ie6':
			$bwidth = 'auto';
			$nowrap = '';
		break;
		
		case 'ie7':
			$bwidth = 'auto';
			$nowrap = '';
		break;
		
		default:
			$bwidth = 'auto';
			$nowrap = ';white-space:nowrap;';
		break;
	}
	
	$divheight = '';
	$divheight = divheight($text);
	$row->maxlengthword = '500';

	$head = '<div class="divcode_header">%%boxtitle%%</div><div class="divcode" style="width:'.$bwidth. $nowrap .'; overflow:auto"><code>';
	$foot = "</code></div>";
	
	$head_quote = '<div class="divcode_header">%%boxtitle%%</div><div class="divcode" style=""><span style="font-style:italic;">';
	$foot_quote = '</span></div>';
	
	$pstring = time() . mt_rand(0,10000000);
	$treffer = "/\[php\](.*?)\[\/php\]/si";
	@preg_match_all ($treffer, $text, $erg);
	for($i=0;$i<count($erg[1]);$i++) { $text = str_replace($erg[1][$i], $pstring.$i.$pstring, $text); }
	
	$text = str_replace(array(
		'[MAIL]',
		'[/MAIL]',
		'[EMAIL]',
		'[/EMAIL]',
		'[mail]',
		'[/mail]',
		'[PHP]',
		'[/PHP]'
		),array(
		'[email]',
		'[/email]',
		'[email]',
		'[/email]',
		'[email]',
		'[/email]',
		'[php]',
		'[/php]'
		),$text
	);
	$text = koobispecialchars($text);
	$lines = explode("\n", $text);
	$c_mlength = $row->maxlengthword;
	for($n=0;$n<count($lines);$n++) 
	{
		$words = explode(" ",$lines[$n]);
		$pstringount_w = count($words)-1;
		if($pstringount_w >= 0) {
			for($i=0;$i<=$pstringount_w;$i++) 
			{
				$max_length_word = $c_mlength;
				$tword = trim($words[$i]);
				$tword = preg_replace("/\[(.*?)\]/si", "", $tword);
				$displaybox = substr_count($tword, "http://") + substr_count($tword, "https://") + substr_count($tword, "www.") + substr_count($tword, "ftp://");
				if($displaybox > 0) {
					$max_length_word = $c_mlength;
				}
				if(strlen($tword)>$max_length_word)
				{
					$words[$i] = chunk_split($words[$i], $max_length_word, "\n");
					$length = strlen($words[$i])-5;
					$words[$i] = substr($words[$i],0,$length);
				}
			}
			$lines[$n] = implode(" ", $words);
		} else {
			$lines[$n] = chunk_split($lines[$n], $c_mlength, "\n");
		}
	}
	$text = implode("\n", $lines);
	
	
	
	$text = nl2br($text);
	$text = preg_replace("#\[color=(\#?[\da-fA-F]{6}|[a-z\ \-]{3,})\](.*?)\[/color\]+#i","<span style=\"color:\\1\"> \\2 </span>",$text);
	$text = preg_replace("#\[size=([0-9+]{1,2}+)\](.*?)\[/size\]#si", "<span style=\"font-size:\\1pt\"> \\2 </span>", $text);
	$text = preg_replace("#\[face=()?(.*?)\](.*?)\[/face\]#si", "<span style=\"font-family:\\2\"> \\3 </span>", $text);
	$text = preg_replace("#\[font=()?(.*?)\](.*?)\[/font\]#si", "<span style=\"font-family:\\2\"> \\3 </span>", $text);
	$text = preg_replace("#\[highlight\](.*?)\[/highlight\]#si", "<span class=\"forums_highlight\">\\1</span>", $text);
	$text = preg_replace("#\[left\](.*?)\[/left\]#si", "<div style=\"text-align:left\"> \\1 </div>", $text);
	$text = preg_replace("#\[center\](.*?)\[/center\]#si", "<div style=\"text-align:center\"> \\1 </div>", $text);
	$text = preg_replace("#\[right\](.*?)\[/right\]#si", "<div style=\"text-align:right\"> \\1 </div>", $text);
	$text = preg_replace("#\[b\](.*?)\[/b\]#si", "<span style=\"font-weight:bold\"> \\1 </span>", $text);
	$text = preg_replace("#\[s\](.*?)\[/s\]#si", "<span style=\"text-decoration:line-through\"> \\1 </span>", $text);
	$text = preg_replace("#\[u\](.*?)\[/u\]#si", "<span style=\"text-decoration:underline\"> \\1 </span>", $text);
	$text = preg_replace("#\[i\](.*?)\[/i\]#si", "<span style=\"font-style:italic\"> \\1 </span>", $text);
	$text = preg_replace("!\[(?i)email\]([a-zA-Z0-9-._]+@[a-zA-Z0-9-.]+)\[/(?i)email\]!", "<a href=\"mailto:\\1\">\\1</a>", $text);
	$text = preg_replace("#\[email=()?(.*?)\](.*?)\[/email\]#si", "<A HREF=\"mailto:\\2\">\\3</A>", $text);
	
	$text = str_replace(array(
		'font-size:1pt',
		'font-size:2pt',
		'font-size:3pt',
		'font-size:4pt',
		'font-size:5pt',
		'font-size:6pt',
		'font-size:7pt'
		),array(
		'font-size:8pt',
		'font-size:10pt',
		'font-size:12pt',
		'font-size:14pt',
		'font-size:18pt',
		'font-size:24pt',
		'font-size:36pt'
		),$text
	);
	
	switch($location)
	{
		case 'user_guestbook':
			if($imgcode == 1 && $GLOBALS['settings']['KoobiCode_Bild'] == 1)
			{
				$text = preg_replace("!\[(?i)img\]([_-a-zA-Z0-9:/\?\[\]=.@-]+)\[(?i)/img\]!", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $text);
				$text = preg_replace("!\[(?i)IMG\]([_-a-zA-Z0-9:/\?\[\]=.@-]+)\[(?i)/IMG\]!", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $text);
			}
			if($GLOBALS['settings']['KoobiCode_Links'] == 1)
			{
				$text = preg_replace("!\[(?i)url\](http://|ftp://)(.*?)\[/(?i)url\]+!", "<a rel=\"nofollow\" href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", $text);
				$text = preg_replace("!\[(?i)url\](.*?)\[/(?i)url\]+!", "<a rel=\"nofollow\" href=\"http://\\1\" target=\"_blank\">\\1</a>", $text);
				$text = preg_replace("#\[url=(http://)?(.*?)\](.*?)\[/url\]#si", "<a rel=\"nofollow\" href=\"http://\\2\" target=\"_blank\">\\3</a>", $text);
			}
		break;
		
		case '':
			if($GLOBALS['settings']['KoobiCode_Bild'] == 1)
			{
				$text = preg_replace("!\[(?i)img\]([_-a-zA-Z0-9:/\?\[\]=.@-]+)\[(?i)/img\]!", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $text);
				$text = preg_replace("!\[(?i)IMG\]([_-a-zA-Z0-9:/\?\[\]=.@-]+)\[(?i)/IMG\]!", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $text);
			}
			
			if($GLOBALS['settings']['KoobiCode_Links'] == 1)
			{
				$text = preg_replace("!\[(?i)url\](http://|ftp://)(.*?)\[/(?i)url\]+!", "<a rel=\"nofollow\" href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", $text);
				$text = preg_replace("!\[(?i)url\](.*?)\[/(?i)url\]+!", "<a rel=\"nofollow\" href=\"http://\\1\" target=\"_blank\">\\1</a>", $text);
				$text = preg_replace("#\[url=(http://)?(.*?)\](.*?)\[/url\]#si", "<A rel=\"nofollow\" HREF=\"http://\\2\" TARGET=\"_blank\">\\3</A>", $text);
			}
		break;
	}
	
	
	$text = preg_replace("/\[code\](.*?)\[\/code\]/si",str_replace("%%boxtitle%%", $GLOBALS['config_vars']['code'], $head).'\\1'.$foot, $text);
	$text = preg_replace("!\[(?i)quote\]!", str_replace("%%boxtitle%%", $GLOBALS['config_vars']['quote'], $head_quote), $text);
	$text = preg_replace("!\[/(?i)quote\]!", $foot_quote, $text);
	$text = preg_replace("/\[list\](.*?)\[\/list\]/si","<ul>\\1</ul>", $text);
	$text = preg_replace("/\[list=(.*?)\](.*?)\[\/list\]/si","<ol type=\"\\1\">\\2</ol>", $text);
	$text = preg_replace("/\[\*\](.*?)\\n/si","<li>\\1</li>", $text);
	$text = Commentskoobispecialchars($text);
	
	
	for($i=0;$i<count($erg[1]);$i++)
	{
		
		$erg[1][$i] = "____PHPCODE_DEL____<?php ____PHPCODE_DEL_END____" . $erg[1][$i] . "____PHPCODE_DEL____?>____PHPCODE_DEL_END____";
		ob_start();
		@highlight_string(trim($erg[1][$i]));
		$highlight_string = ob_get_contents();
		ob_end_clean();
		$displaybox = "";
		$displaybox = str_replace("%%boxtitle%%", $GLOBALS['config_vars']['Forums_phpcode'], $head) . $highlight_string.$foot;
		$text = str_replace("[PHP]", "[php]", $text);
		$text = str_replace("[/PHP]", "[/php]", $text);
		$text = str_replace("[php]" . $pstring . $i . $pstring . "[/php]",$displaybox,$text);
		$text = preg_replace("/____PHPCODE_DEL____(.*?)____PHPCODE_DEL_END____/si","<span style=\"display:none\"></span>", $text);
		
	}

	
	return $text;
}


// ====================================================================
// <<-- BB- CODES SEITE -->>
// ====================================================================
function kcodes($text) 
{
	if (BBCODESITE==1){
		$text = pre_kcodes($text);
	} else {
		$text = nl2br(kspecialchars($text));
	}
	return $text;
}

// ====================================================================
// <<-- BB- CODES KOMMENTARE -->>
// ====================================================================
function kcodes_comments($text)
{
	if (COMMENTSBBCODE == 1)
	{
		$text = pre_kcodes($text);
	}else {
		$text = nl2br(kspecialchars($text));
	}
	return $text;
}

function phpdecode($text){return $text;}
function phpencode($text){return $text;}
function kstripslashes($text){return $text;}

// ====================================================================
// <<-- URLS AUTOMATISCH UMWANDELN -->>
// ====================================================================

function parseurl($text)
{
	$urlsearch[]="/([^]_a-z0-9-=\"'\/])((https?|ftp):\/\/|www\.)([^ \r\n\(\)\*\^\$!`\"'\|\[\]\{\};<>]*)/si";
	$urlsearch[]="/^((https?|ftp):\/\/|www\.)([^ \r\n\(\)\*\^\$!`\"'\|\[\]\{\};<>]*)/si";
	$urlreplace[]="\\1[URL]\\2\\4[/URL]";
	$urlreplace[]="[URL]\\1\\3[/URL]";
	$emailsearch[]="/([\s])([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
	$emailsearch[]="/^([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
	$emailreplace[]="\\1[EMAIL]\\2[/EMAIL]";
	$emailreplace[]="[EMAIL]\\0[/EMAIL]";
	$text = preg_replace($urlsearch, $urlreplace,$text);
	if (strpos($text, "@")) $text = preg_replace($emailsearch, $emailreplace, $text);
	return $text;

}

// ====================================================================
// <<-- Code durch Smilies ersetzen -->>
// ====================================================================
function dosmilies($texts)
{
	if(SMILIES==1)
	{
		$sql = $GLOBALS['db']->Query("SELECT code,path FROM ".PREFIX."_smileys WHERE active='1' AND area='" . $_SESSION['area'] . "'");
		while($row_s = $sql->fetchrow())
		{
			$texts = @str_replace($row_s->code,'<img src="theme/' . $GLOBALS['theme'] . '/images/smilies/'.$row_s->path.'" border="0" alt="" />',$texts);
		}
	}
	return $texts;
}

// ====================================================================
// <<-- Smilies auflisten -->>
// ====================================================================
function listsmilies($smiliebr='', $notable='', $normcomments='')
{
	$row->smiliebr = ($smiliebr != '') ? $smiliebr : 4;
	$smilie_id = 0;
	$smiliesw  = '';
	if($notable!=1) $smiliesw = '<table cellpadding="0" cellspacing="1" class="smilie_box" border="0"><tr>';
	
	$sql = $GLOBALS['db']->Query("SELECT code,path FROM ".PREFIX."_smileys WHERE active='1' AND area='" . $_SESSION['area'] . "' ORDER BY posi ASC LIMIT 12");
	while($row_s = $sql->fetchrow())
	{
		$val = $row_s->code;
		$key = $row_s->path;
		if($notable!=1) $smiliesw .= '<td class="smilie_row">';
		$smiliesw .= '<a href="javascript:smilie(\''.$val.'\')"><img hspace="2" vspace="2" src="theme/' . $GLOBALS['theme'] . '/images/smilies/'.$key.'" border="0" alt="" /></a>';
		$smilie_id++;
		if($notable!=1) $smiliesw .= '</td>';
		if($smilie_id==$row->smiliebr) { $smiliesw .= "</tr><tr>"; $smilie_id=0; }
	}
	if($notable!=1) $smiliesw .= '</tr></table>';
	
	$smiliesw_ext = '';
	$smilie_id = 0;
	$smiliesw_ext .= "&lt;table cellpadding=\'0\' cellspacing=\'0\' border=\'0\' width=\'100%\'&gt;&lt;tr&gt;";
	
	$sql = $GLOBALS['db']->Query("SELECT code,path FROM ".PREFIX."_smileys WHERE active='1' AND area='" . $_SESSION['area'] . "'");
	$num = $sql->numrows();
	
	$sql = $GLOBALS['db']->Query("SELECT code,path FROM ".PREFIX."_smileys WHERE active='1' AND area='" . $_SESSION['area'] . "' order by posi asc");
	while($row_s = $sql->fetchrow())
	{
		$val = $row_s->code;
		$key = $row_s->path;
		$smiliesw_ext .= '&lt;td width=10&gt;';
		$smiliesw_ext .= "&lt;a class=&quot;user_pop_links&quot; href=javascript:InsertCode(\'$val\') onclick=javascript:nd()&gt;&lt;img hspace=2 vspace=2 src=theme/" . $GLOBALS['theme'] . "/images/smilies/".$key." border=0 alt=\'\' /&gt;&lt;/a&gt;";
		$smiliesw_ext .= "&lt;/td&gt;&lt;td width=\'25%\'&gt;&lt;a class=&quot;user_pop_links&quot; href=javascript:InsertCode(\'$val\') onclick=javascript:nd()&gt;".$val."&lt;/a&gt;&lt;/td&gt;";
		$smilie_id++;
		if($smilie_id==4) 
		{ 
			$smiliesw_ext .= "&lt;/tr&gt;&lt;tr&gt;"; $smilie_id=0;
		}
		
	}
	$smiliesw_ext .= '&lt;/tr&gt;&lt;/table&gt;';
	
	
	$more_s = $smiliesw_ext; 
	if($normcomments==1)
	{
		$smiliesw_ext_more = '<a id="more_s" href="javascript:void(0);" onclick="return overlib(\''.$more_s.'\',FIXX,elemX(elemObj(\'more_s\')),FIXY,elemY(elemObj(\'more_s\'))+20,TIMEOUT,4000,MOUSEOFF,NOCLOSE,WIDTH,600,STICKY);" onmouseout="nd();"><img class="format_buttons"  src="' . IMG_PATH . '/comment/text_smilie.png" border="0" alt="" /></a>';
	} else {
		$smiliesw_ext_more =  (($notable==1) ? '<!--<br />-->' : '') . '<hr noshade="noshade" size="1" />&nbsp;<a id="more_s" href="javascript:void(0);" onclick="return overlib(\''.$more_s.'\',FIXX,elemX(elemObj(\'more_s\')),FIXY,elemY(elemObj(\'more_s\'))+20,TIMEOUT,4000,MOUSEOFF,NOCLOSE,WIDTH,600,STICKY);" onmouseout="nd();">'.$GLOBALS['config_vars']['Forums_MoreSmileys'].'</a>';
	}
	
	if($notable==1)
	{
		$smiliesw = $smiliesw_ext_more;
	} else {
		$smiliesw .= $smiliesw_ext_more;
	}
		
	return $smiliesw;
}

// ====================================================================
//  POST- ICONS
// ====================================================================
function getPosticons($icon = "0")
{
	$posticons = '';
	$i = 1;
	$sql = $GLOBALS['db']->Query("SELECT id,path FROM ".PREFIX."_posticons WHERE active='1' AND area = '" . $_SESSION['area'] . "' ORDER BY posi ASC");
	while($rows = $sql->fetchrow()){
		$br = "&nbsp;";
		if ( ($i % 10) == 0 ) $br = "<!-- <br /> -->";
		
		if ($icon == $rows->id) $checked = "checked=\"checked\"";
		else $checked = "";
		
		$posticons .= "<input class=\"noborder\" type=\"radio\" name=\"posticon\" value=\"$rows->id\" $checked /><img class=\"absmiddle\" src=\"theme/" . $GLOBALS['theme'] . "/images/posticons/$rows->path\" alt=\"\" />&nbsp;&nbsp; $br";
		$i++;
		
	}
	$posticons .= "<input class=\"noborder\" type=\"radio\" name=\"posticon\" value=\"0\" {if $icon == 0}checked{/if} />&nbsp;" . $GLOBALS['config_vars']['Forums_no_posticon'];
	return $posticons;
}


// ====================================================================
// <<-- ICON- FUNCTIONEN -->>
// ====================================================================
function getStatusIcon($params) {
	global $set,$db;
	extract($params);
	
	if ($icon == "") return "";
	
	$imageName = $set["statusiconlist"][$icon] . ".png";
	return "<img src=\"theme/" . $GLOBALS['theme'] . "/images/forums/$imageName\" alt=\"$imageName\" />";
}

function getIcon($file, $alt)
{
	return "<img src=\"theme/" . $GLOBALS['theme'] . "/images/statusicons/$file\" alt=\"$alt\" title=\"$alt\"/>";
}

function getPostIcon($params) 
{
	extract($params);
	$sql = $GLOBALS['db']->Query("SELECT path FROM ".PREFIX."_posticons WHERE active='1' AND id='$icon'");
	$row = $sql->fetchrow();
	if($row != "0") return "<img src=\"theme/" . $GLOBALS['theme'] . "/images/posticons/$row->path\" alt=\"$icon\" />";
	
}



// ====================================================================
// <<-- Dropdownfunktionen Formatierung -->>
// ====================================================================
function fontdropdown()
{
	$set['fonts'] = array (
	"Arial" => "Arial",
	"Times" => "Times",
	"Courier" => "Courier",
	"Impact" => "Impact",
	"Geneva" => "Geneva",
	"Optima" => "Optima",
	"Trebuchet" => "Trebuchet"
	);
	$listfonts = array();
	foreach ( $set['fonts'] AS $font => $fontcode ){
		array_push($listfonts, array(
		'font' => $font,
		'fontname' => $fontcode
		)
	); }
	return $listfonts;
}


function sizedropdown()
{
	$set['size'] = array (
	"8" => '8',
	"10" => '10',
	"12" => '12',
	"14" => '14',
	"18" => '18',
	"24" => '24',
	"36" => '36'
	);
	$listfontsize = array();
	foreach ( $set['size'] AS $size => $fontsize ){
		array_push($listfontsize, array(
		'size' => $fontsize,
		'css_size' => $size
		)
	); }
	return $listfontsize;
}


function colordropdown()
{
	$set['color'] = array (
	"blue" => $GLOBALS['config_vars']['color_blue'],
	"red" => $GLOBALS['config_vars']['color_red'],
	"purple" => $GLOBALS['config_vars']['color_purple'],
	"orange" => $GLOBALS['config_vars']['color_orange'],
	"yellow" => $GLOBALS['config_vars']['color_yellow'],
	"gray" => $GLOBALS['config_vars']['color_grey'],
	"green" => $GLOBALS['config_vars']['color_green'],
	"indigo" => $GLOBALS['config_vars']['color_indigo'],
	"black" => $GLOBALS['config_vars']['color_black'],
	"white" => $GLOBALS['config_vars']['color_white']
	);
	$listfontcolor = array();
	foreach ( $set['color'] AS $color => $fontcolor ){
		array_push($listfontcolor, array(
		'color' => $color,
		'fontcolor' => $fontcolor
		)
	); }
	return $listfontcolor;
}

// ====================================================================
// <<-- "HTTP://" AUTOMATISCH HINZUFUEGEN -->>
// ====================================================================
function httptolink($code) {
	if((!ereg ("^http://", $code)) && ($code!="")){
		$link = "http://" . strip_tags(stripslashes($code));
	}else{
		$link = strip_tags(stripslashes($code));
	}
	return $link;
}

function formtext($text,$maxlength){
	$text = substr($text,0, $maxlength);
	return $text;
}
?>