<?php
// ======================================================================== //
//   KOOBI - CMS                                                            //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   DREAM4 - AM MICHELSGRUND 7 - 69469 WEINHEIM - GERMANY                  //    
//   TEL. +49(0) 6201 - 980442 / FAX. +49(0) 6201 - 64809                   //
// ======================================================================== //

// ====================================================================
// <<-- HOEHE DER CODEBOXEN ERMITTELN -->>
// ====================================================================
function divheight($text){
	global $pref;
	static $maxlines;
	
	$sql = new dbc("SELECT maxlines FROM " . PREFIX . "_settings");
	$row = $sql->fetchrow();
	
	if (!isset($maxlines)){
		$maxlines = $row->maxlines;
	}
	
	$lines = max(substr_count($text, "\n"), substr_count($text, "<br />")) + 1;
	if ($lines > $maxlines AND $maxlines > 0){
		$lines = $maxlines;
	}
	else if ($lines < 1){
		$lines = 1;
	}
	return ($lines) * 15 + 18;
}


// ====================================================================
// <<-- 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($text){
	$string = htmlspecialchars($text);
	/*
	$string = str_replace ( '&', '&amp;', $text);
	$string = str_replace ( '\'', '&#039;',$string );
	$string = str_replace ( '\"', '&quot;', $string );
	$string = str_replace ( '<', '&lt;', $string );
	$string = str_replace ( '>', '&gt;', $string );
	*/
	return $string;
}
// ====================================================================
// <<-- BB- CODES VORBEREITEN -->>
// ====================================================================
function pre_kcodes($text) {
	global $set,$lang, $pref, $boxtype,$THEME;
	if(IMGCODE==1) { };
	
	$divheight = divheight($text);
	$sql = new dbc("SELECT boxwidthcomm,boxwidthforums,maxlengthword FROM " . PREFIX . "_settings");
	$row = $sql->fetchrow();
	$bwidth = ("pagecomments") ? $row->boxwidthcomm : $row->boxwidthforums;
	
	$head = '<div style="MARGIN: 5px 0px 0px">%%boxtitle%%</div><div class="divcode" style="margin:0px; padding:5px; border:1px inset; width:'.$bwidth.'; height:'.$divheight.'px; overflow:auto"><code style="white-space:nowrap">';
	$foot = '</code></div>';
	
	$head_quote = '<div style="MARGIN: 5px 0px 0px">%%boxtitle%%</div><div class="divcode" style="margin:0px; padding:5px; border:1px inset; width:95%;"><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 = kspecialchars($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 = 200;
				}
				if(strlen($tword)>$max_length_word) {
					$words[$i] = chunk_split($words[$i], $max_length_word, "<br />");
					$length = strlen($words[$i])-5;
					$words[$i] = substr($words[$i],0,$length);
				}
			}
			$lines[$n] = implode(" ", $words);
		} else {
			$lines[$n] = chunk_split($lines[$n], $max_length_word, "<br />");
		}
	}
	$text = implode("\n", $lines);
	$text = nl2br($text);
	$text = preg_replace("#\[color=(\#?[\da-fA-F]{6}|[a-z\ \-]{3,})\](.*?)\[/color\]+#i","<font color=\"\\1\">\\2</font>",$text);
	$text = preg_replace("#\[size=()?(.*?)\](.*?)\[/size\]#si", "<font size=\"\\2\">\\3</font>", $text);
	$text = preg_replace("#\[face=()?(.*?)\](.*?)\[/face\]#si", "<font face=\"\\2\">\\3</font>", $text);
	$text = preg_replace("#\[font=()?(.*?)\](.*?)\[/font\]#si", "<font face=\"\\2\">\\3</span>", $text);
	$text = preg_replace("!\[(?i)b\]!", "<b>", $text);
	$text = preg_replace("!\[/(?i)b\]!", "</b>", $text);
	$text = preg_replace("!\[(?i)u\]!", "<u>", $text);
	$text = preg_replace("!\[/(?i)u\]!", "</u>", $text);
	$text = preg_replace("!\[(?i)i\]!", "<i>", $text);
	$text = preg_replace("!\[/(?i)i\]!", "</i>", $text);
	$text = preg_replace("!\[(?i)url\](http://|ftp://)(.*?)\[/(?i)url\]+!", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", $text);
	$text = preg_replace("!\[(?i)url\](.*?)\[/(?i)url\]+!", "<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $text);
	$text = preg_replace("#\[url=(http://)?(.*?)\](.*?)\[/url\]#si", "<A HREF=\"http://\\2\" TARGET=\"_blank\">\\3</A>", $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 = preg_replace("!\[(?i)img\]([_-a-zA-Z0-9:/\?\[\]=.@-]+)\[(?i)/img\]!", "<img src=\"\\1\" border=\"0\" onload=\"if(this.width>screen.width*0.5) {this.resized=true; this.width=screen.width*0.5; this.alt='点击在新窗口查看全图\\nCTRL+鼠标滚轮放大或缩小';}\" onmouseover=\"if(this.resized) this.style.cursor='hand';\" onclick=\"if(!this.resized) {return true;} else {window.open('\\1');}\" onmousewheel=\"return imgzoom(this);\" alt=\"\" />", $text);
	$text = preg_replace("!\[(?i)IMG\]([_-a-zA-Z0-9:/\?\[\]=.@-]+)\[(?i)/IMG\]!", "<img src=\"\\1\" border=\"0\" onload=\"if(this.width>screen.width*0.5) {this.resized=true; this.width=screen.width*0.5; this.alt='点击在新窗口查看全图\\nCTRL+鼠标滚轮放大或缩小';}\" onmouseover=\"if(this.resized) this.style.cursor='hand';\" onclick=\"if(!this.resized) {return true;} else {window.open('\\1');}\" onmousewheel=\"return imgzoom(this);\" alt=\"\" />", $text);
	$text = preg_replace("/\[code\](.*?)\[\/code\]/si",str_replace("%%boxtitle%%", $lang['code'], $head).'\\1'.$foot, $text);
	$text = preg_replace("!\[(?i)quote\]!", str_replace("%%boxtitle%%", $lang['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);
	
	
	for($i=0;$i<count($erg[1]);$i++) {
		ob_start();
		@highlight_string(trim($erg[1][$i]));
		$highlight_string = ob_get_contents();
		ob_end_clean();
		$displaybox = str_replace("%%boxtitle%%", $lang['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);
	}
	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) {
	global $pref;
	if (COMMENTSBBCODE == 1){
		$text = pre_kcodes($text);
	}else {
		$text = nl2br(kspecialchars($text));
	}
	return $text;
}



function phpdecode($text)	{
	/* NICHT MEHR GENUTZT */
	return $text;
}

function phpencode($text)	{
	/* NICHT MEHR GENUTZT */
	return $text;
}

function kstripslashes($text){
	/* NICHT MEHR GENUTZT */
	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){
	global $area,$set,$THEME;
	if(SMILIES==1){
		$sql = new dbc("SELECT code,path FROM ".PREFIX."_smileys WHERE active='1' AND area='$area'");
		while($row_s = $sql->fetchrow()){
			$texts = @str_replace($row_s->code,'<img src="templates/'.$THEME.'/img/smilies/'.$row_s->path.'" border="0" alt="" />',$texts);
		}
	}
	return $texts;
}

// ====================================================================
// <<-- Smilies auflisten -->>
// ====================================================================
function listsmilies(){
	global $area,$pref,$set,$THEME;
	$sql = new dbc("SELECT smiliebr FROM " . PREFIX . "_settings");
	$row = $sql->fetchrow();
	$smilie_id = 0;
	$smiliesw .= '<table border="0"><tr>';
	
	$sql = new dbc("SELECT code,path FROM ".PREFIX."_smileys WHERE active='1' AND area='$area' order by posi asc");
	while($row_s = $sql->fetchrow()){
		$val = $row_s->code;
		$key = $row_s->path;
		$smiliesw .= '<td>';
		$smiliesw .= '<a href="javascript:smilie(\''.$val.'\')"><img hspace="2" vspace="2" src="templates/'.$THEME.'/img/smilies/'.$key.'" border="0" alt="" /></a>';
		$smilie_id++;
		$smiliesw .= '</td>';
		if($smilie_id==$row->smiliebr) { $smiliesw .= "</tr><tr>"; $smilie_id=0; }
		
	}
	$smiliesw .= '</tr></table>';
	return $smiliesw;
}

// ====================================================================
//  POST- ICONS
// ====================================================================
function getPosticons($icon = "0"){
	global $THEME,$lang;
	$i = 1;
	$sql = new dbc("SELECT id,path FROM ".PREFIX."_posticons WHERE active='1' order by posi asc");
	while($rows = $sql->fetchrow()){
		$br = "&nbsp;";
		if ( ($i % 6) == 0 ) $br = "<br />";
		
		if ($icon == $rows->id) $checked = "checked";
		else $checked = "";
		
		$posticons .= "<input class=\"noborder\" type=\"radio\" name=\"posticon\" value=\"$rows->id\" $checked />&nbsp;";
		$posticons .= "<img src=\"templates/$THEME/img/posticons/$rows->path\" alt=\"$row->path\" />$br";
		$i++;
		
	}
	$posticons .= "<input class=\"noborder\" type=\"radio\" name=\"posticon\" value=\"0\" {if $icon == 0}checked{/if} />&nbsp;$lang[no_posticon]";
	return $posticons;
}


// ====================================================================
// <<-- ICON- FUNCTIONEN -->>
// ====================================================================
function getStatusIcon($params) {
	global $set;
	extract($params);
	
	if ($icon == "") return "";
	
	$imageName = $set["statusiconlist"][$icon] . ".gif";
	return "<img src=\"templates/$theme/img/statusicons/$imageName\" alt=\"$imageName\" />";
}

function getIcon($file, $alt) {
	global $THEME;
	return "<img src=\"templates/$THEME/img/statusicons/$file\" alt=\"$alt\" title=\"$alt\"/>";
}

function getPostIcon($params) {
	global $set;
	extract($params);
	$sql = new dbc("SELECT path FROM ".PREFIX."_posticons WHERE active='1' and id='$icon'");
	$row = $sql->fetchrow();
	if($row != "0") return "<img src=\"templates/$theme/img/posticons/$row->path\" alt=\"$icon\" />";
	
}



// ====================================================================
// <<-- Dropdownfunktionen Formatierung -->>
// ====================================================================
function fontdropdown(){
	global $set;
	$listfonts = array();
	foreach ( $set['fonts'] AS $font => $fontcode ){
		array_push($listfonts, array(
		'font' => $font,
		'fontname' => $fontcode
		)
	); }
	return $listfonts;
}

function sizedropdown(){
	global $set;
	$listfontsize = array();
	foreach ( $set['size'] AS $size => $fontsize ){
		array_push($listfontsize, array(
		'size' => $fontsize
		)
	); }
	return $listfontsize;
}


function colordropdown(){
	global $set;
	$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;
}
?>