<?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                   //
// ======================================================================== //
function getCategoriesAdmin($id, &$categories, $prefix) {
	// kategorien holen
	$q_cat = "
	SELECT 
	id, 
	title, 
	comment,
	position
	FROM 
	" . PREFIX . "_f_category 
	WHERE 
	parent_id = $id
	ORDER BY
	position ASC";
	
	$r_cat = new dbc($q_cat);
	// kategorien durchgehen
	while ($cat = $r_cat->fetchrow()) {
		$cat->forums = array();
		// foren zur kategorie holen
		$q_forum = "
		SELECT 
		f.id, 
		f.comment,
		f.category_id, 
		f.title,
		f.active,
		f.group_id,
		f.position,
		f.status
		FROM 
		" . PREFIX . "_f_forum AS f
		WHERE 
		category_id = '" . $cat->id . "'
		ORDER BY
		f.position";
		
		$result = new dbc($q_forum);
		
		// alle foren durchgehen
		while ($forum = $result->fetchrow()) {
			$group_ids = @explode(",", $forum->group_id);
			$forum->visible_title = $prefix . $forum->title;
			$q_sub_cat = "SELECT id, title, comment FROM " . PREFIX . "_f_category WHERE parent_id = " . $forum->id;
			$r_sub_cat = new dbc($q_sub_cat);
			
			$forum->categories = array();
			while ($sub_cat = $r_sub_cat->fetchrow()) {
				$forum->categories[] = $sub_cat;
			}
			
			// mods holen
			$q_mods = "
			SELECT 
			COUNT(m.forum_id) AS m_count 
			FROM 
			" . PREFIX . "_f_mods AS m
			WHERE
			m.forum_id = " . $forum->id;
			
			$r_mods = new dbc($q_mods);
			$mods = $r_mods->fetchrow();
			
			$forum->mods = $mods->m_count;
			
			$cat->forums[] = $forum;
		}
		
		$categories[] = $cat;
	}	
}


function sub($text,$maxlength){
	if(strlen($text)>$maxlength){
		$newtext = substr($text, 0, $maxlength)."...";
		$newtext = htmlspecialchars(stripslashes($newtext));
	} else {
		$newtext = htmlspecialchars(stripslashes($text));
	}
	return $newtext;
}


function adminperm($theid) {
	global $rights;
	if ($rights[$theid]==1) return true;
	if ($rights['alles']==1) return true;
	return false;
}

function siteperm($theid) {
	global $siterights;
	if ($siterights[$theid]==1) return true;
	if ($siterights['alles']==1) return true;
	return false;
}

function error($text){
	global $langadmin;
	$redir = $_SERVER['HTTP_REFERER'];
	$tmpl = new Koobi("templates/".ADMINTHEME."/");
	$tmpl->assign("lang", $langadmin);
	$tmpl->assign("area", $area);
	$tmpl->assign("redir", $redir);
	$tmpl->assign("theme", ADMINTHEME);
	$tmpl->assign("text", $text);
	$tmpl->assign("redir", $_SERVER['HTTP_REFERER']);
	$tmpl->display("error.tpl");
}


function messagenorm($text,$page){
	global $langadmin;
	$msg ='<script language="javascript">
	setTimeout(function() {
		location.href = "'.$page.'";
	}, 6000);
	</script>
	<table width="100%" align="center"  border="0" cellspacing="0" cellpadding="0">
	<tr><td class="boxstd"><table width="100%"  border="0" cellpadding="4" cellspacing="1" class="tableborder"><tr>
	<td colspan="2" class="header"><font size="+1"><b>'.$langadmin['messageheader'].'</b></font></td>
	</tr><tr><td colspan="2" class="secondrow"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
	<tr><td>'.$text.'<br><br>'.str_replace("__URL__", $page, $langadmin['redirect']).'</td>
	</tr></table></td></tr></table></td></tr></table><br><br>';
	return $msg;
}


function head(){
	echo '<table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr><td class="stdbox">';
}

function foot(){
	echo '</td></tr></table><br>';
}

function errorpre($text){
	global $langadmin;
	$preerror = "<li><span class=\"errorfont\">".$langadmin[$text]."</li></span>";
	return $preerror;
}


/**
* Entfernt unschönen HTML- Code
*/
function nicehtml($text,$io){
	$html = $text;
	$html = str_replace('<br _moz_editor_bogus_node=\"TRUE\"/>',"",$html);
	$html = str_replace('<br type=\"_moz\"/>', '<br />', $html);
	$html = str_replace(array("<br>","<BR>"),"<br />",$html);
	$html = str_replace(array("<br/>","<BR/>"),"<br />",$html);
	$html = str_replace("<TBODY>","",$html);
	$html = str_replace("</TBODY>","",$html);
	$html = @ereg_replace("$\<p\>", "", $html);
	if($io==1){
		$html = stripslashes(htmlspecialchars($html));
	}
	
	return $html;
}


function galdisp($kat_id, $i) {
	global $parent_id,$area;
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_gallery WHERE parent_id='$kat_id' AND area='$area' order by title asc");
	$i = 0;
	while($row = $sql->fetcharray()) {
		
		for($r=1;$r<$i;$r++) { $gdrop[test] .= '--'; }
		
		$sqls = new dbc("SELECT DISTINCT id FROM ".PREFIX."_gallery_items WHERE gal_id='".$row["id"]."'");
		$all = $sqls->numrows();
		
		$gdrop[test] .= ' '.stripslashes($row["title"]). ' [' . $all . ']' . '';
		$gdrop[test] .= galdisp($row["id"], $i+1);
		$i++;
	}
	return $gdrop;
}

/**
*
* Bilduploadfunktion
*
**/

function upload($_FILES){ 
	global $UID,$langadmin,$compression,$thumb_smallw,$thumb_smallh,$thumb_middlew,$thumb_middleh;
	$galpfad = "../uploads/galerie/";
	$status = array();
	
	for($i=0;$i<count($_FILES['files']['tmp_name']);$i++)
	
	{
		$size = $_FILES['files']['size'][$i];
		$name = $_FILES['files']['name'][$i];
		$temp = $_FILES['files']['tmp_name'][$i];
		$type = substr($_FILES['files']['type'][$i], 0, 5);
		
		if($size > 0  and $type=="image"){
			$fupload_name = strtolower($_FILES['files']['name'][$i]);
			$fupload_name = str_replace(" ","", $fupload_name);
			
			
			// umbenennen wenn existent
			if(file_exists($galpfad . $fupload_name)){
				$ndat = explode(".",$fupload_name);
				$fupload_name = $ndat[0]. date("s") .".".$ndat[1];
			}
			
			if($_FILES['files']['type'][$i]=="image/pjpeg" || $_FILES['files']['type'][$i]=="image/jpeg" || $_FILES['files']['type'][$i]=="image/x-png" || $_FILES['files']['type'][$i]=="image/png"){
				
				move_uploaded_file($_FILES['files']['tmp_name'][$i], $galpfad . $fupload_name);
				@chmod($galpfad . $fupload_name,0777);
				// =============================================================
				// <<-- Verkleinern -->>
				// =============================================================
				if ( (isset($_REQUEST['resize'])) && ($_REQUEST['w']!="") && ($_REQUEST['h']!="") ) {
					$error = 0;
					
					
					if (gdversion() >= 2) { 
						$sowhat = "imagecreatetruecolor";
					} else { 
						$sowhat = "imagecreate";
					}
					
					if (function_exists("imagecreate")) {
						$neues_bild = $sowhat($_REQUEST['w'], $_REQUEST['h']);
						if($_FILES['files']['type'][$i]=="image/pjpeg" || $_FILES['files']['type'][$i]=="image/jpeg"){
							$altes_bild = imagecreatefromjpeg($galpfad . $fupload_name);
						}
						if($_FILES['files']['type'][$i]=="image/png" || $_FILES['files']['type'][$i]=="image/x-png"){
							$altes_bild = imagecreatefrompng($galpfad . $fupload_name);
						}
						if($_FILES['files']['type'][$i]=="image/gif"){
							$error = 1;
						}
						if (isset($altes_bild)) {
							imagecopyresized($neues_bild, $altes_bild, 0, 0, 0, 0, imagesx($neues_bild), imagesy($neues_bild), imagesx($altes_bild), imagesy($altes_bild));
							if($_FILES['files']['type'][$i]=="image/pjpeg" || $_FILES['files']['type'][$i]=="image/jpeg"){
								@unlink($galpfad . $fupload_name);
								imagejpeg($neues_bild, $galpfad . $fupload_name);
							}
							if($_FILES['files']['type'][$i]=="image/png" || $_FILES['files']['type'][$i]=="image/x-png"){
								@unlink($galpfad . $fupload_name);
								imagepng($neues_bild, $galpfad . $fupload_name);
								
							}
						}
					}
				}
				
				$descr = $_REQUEST['descr'][$i];
				if($_REQUEST['autobr']) $descr = nl2br($descr);
				$sql = new dbc("INSERT INTO ".PREFIX."_gallery_items (ctime,id,gal_id,path,name,descr,author) VALUES ('".time()."','','{$_REQUEST[thegal]}','$fupload_name','{$_REQUEST[name][$i]}','$descr','$UID') ");
			}
			
			if($_FILES['files']['type'][$i]!="image/pjpeg" && $_FILES['files']['type'][$i]!="image/jpeg" && $_FILES['files']['type'][$i]!="image/x-png" && $_FILES['files']['type'][$i]!="image/png"){
				$status[$i]["filename"] =  $_FILES['files']['name'][$i];
				$status[$i]["status"] =  $langadmin['wrongfiletype'];
				$status[$i]["size"] =  ceil($_FILES['files']['size'][$i]/1024);
				$status[$i]["type"] =  $_FILES['files']['type'][$i];
			}
			else {
				$status[$i]["filename"] =   $_FILES['files']['name'][$i];
				$status[$i]["status"] =  $langadmin['saved'];
				$status[$i]["size"] =  ceil($_FILES['files']['size'][$i]/1024);
				$status[$i]["type"] =  $_FILES['files']['type'][$i];
			}
		}
		
	}
	return $status;
}


function toggnavi($param){
	$position = strpos($_COOKIE["navi"], "navid".$param["id"]);
	if ( !is_numeric($position) ) {$plusminus = "plus.gif";
	} else {
		$plusminus = "minus.gif";
	}
	$dnimage = "
	<img  border=\"0\" id=\"templates/".ADMINTHEME."/image_".$param["id"]."\" src=\"templates/".ADMINTHEME."/image/$plusminus\" 
	onmouseover=\"this.style.cursor = 'pointer'\"
	onclick=\"MWJ_changeDisplay('navid".$param["id"]."', MWJ_getStyle( 'navid".$param["id"]."', 'display' ) ? '' : 'none');
	koobi4_toggleImage('templates/".ADMINTHEME."/image_".$param["id"]."', this.src);
	koobi4_setCookie('navi', 'navid".$param["id"]."');\"
	alt=\"\" />";
	return $dnimage;
}

function toggnavi_td($param){
	$position = strpos($_COOKIE["navi"], "navid".$param["id"]);
	if ( !is_numeric($position) ) {$plusminus = "plus.gif";
	} else {
		$plusminus = "minus.gif";
	}
	$dyntd = "
	onmouseover=\"this.style.cursor = 'pointer'\"
	onclick=\"MWJ_changeDisplay('navid".$param["id"]."', MWJ_getStyle( 'navid".$param["id"]."', 'display' ) ? '' : 'none');
	koobi4_toggleImage('templates/".ADMINTHEME."/image_".$param["id"]."', this.src);
	koobi4_setCookie('navi', 'navid".$param["id"]."');\"";
	return $dyntd;
}

function toggimage($param){
	$position = strpos($_COOKIE["navi"], "navid".$param["id"]);
	if ( !is_numeric($position) ) {
		$disp= "none";	
	}
	return $disp;
}

// ========================================================
// <<-- F.A.Q- Löschfunktion -->>
// ========================================================
function faqdel($id) {
	$query = new dbc(" SELECT * FROM " . PREFIX . "_faq  WHERE parent_id=$id");
	
	while ($item = $query->fetchrow()) {
		$sql = new dbc("DELETE FROM " . PREFIX . "_faq WHERE id=$item->id");			
		
		faqdel($item->id);
	}
	
	$query = new dbc("DELETE FROM " . PREFIX . "_faq WHERE id='$id'");
}

// ========================================================
// <<-- Galerie- Löschfunktion -->>
// ========================================================
function galdel($id) {
	$query = new dbc(" SELECT id,parent_id FROM " . PREFIX . "_gallery  WHERE parent_id=$id");
	
	while ($item = $query->fetchrow()) {
		$sql = new dbc("SELECT path FROM " . PREFIX . "_gallery_items WHERE gal_id=$item->id");
		while($row_g = $sql->fetchrow()){
			@unlink("../uploads/galerie/" . $row_g->path);
		}
		
		$sql = new dbc("DELETE FROM " . PREFIX . "_gallery WHERE id=$item->id");			
		$sql = new dbc("DELETE FROM " . PREFIX . "_gallery_items WHERE gal_id=$item->id");
		
		// <<--Rekursion -->>
		galdel($item->id);
	}
	$sql = new dbc("SELECT path FROM " . PREFIX . "_gallery_items WHERE gal_id=$id");
	while($row_g = $sql->fetchrow()){
		@unlink("../uploads/galerie/" . $row_g->path);
	}
	
	$query = new dbc("DELETE FROM " . PREFIX . "_gallery WHERE id='$id'");
	$sql = new dbc("DELETE FROM " . PREFIX . "_gallery_items WHERE gal_id='$id'");
}


// ========================================================
// <<-- ARTIKELKATEGORIE- LOESCHFUNKTION -->>
// ========================================================
function artcatdel($id) {
	$query = new dbc("SELECT * FROM " . PREFIX . "_articlecat WHERE parent_id=$id");
	
	while ($item = $query->fetchrow()) {
		$sql = new dbc("DELETE FROM " . PREFIX . "_articlecat WHERE catid='" . $item->id . "'");			
		artcatdel($item->catid);
		@unlink("../uploads/articlecat_icons/" . $item->icon);
		$sql = new dbc("DELETE FROM " . PREFIX . "_articles WHERE  articlecat = $item->catid");
		$sql = new dbc("DELETE FROM " . PREFIX . "_articles WHERE  articlecat = $item->parent_id");
	}
	$query = new dbc("DELETE FROM " . PREFIX . "_articlecat WHERE catid='$id'");
}


function dlcatdel($id) {
	$query = new dbc("SELECT * FROM " . PREFIX . "_downloadcat WHERE parent_id=$id");
	
	while ($item = $query->fetchrow()) {
		$sql = new dbc("DELETE FROM " . PREFIX . "_downloadcat WHERE catid='" . $item->id . "'");			
		dlcatdel($item->catid);
		$sql = new dbc("DELETE FROM " . PREFIX . "_downloads WHERE  catid = $item->catid");
		$sql = new dbc("DELETE FROM " . PREFIX . "_downloads WHERE  catid = $item->parent_id");
	}
	$query = new dbc("DELETE FROM " . PREFIX . "_downloadcat WHERE catid='$id'");
}

function linkcatdel($id) {
	$query = new dbc("SELECT * FROM " . PREFIX . "_linkcat WHERE parent_id=$id");
	
	while ($item = $query->fetchrow()) {
		$sql = new dbc("DELETE FROM " . PREFIX . "_linkcat WHERE catid='" . $item->id . "'");			
		dlcatdel($item->catid);
		$sql = new dbc("DELETE FROM " . PREFIX . "_links WHERE  catid = $item->catid");
		$sql = new dbc("DELETE FROM " . PREFIX . "_links WHERE  catid = $item->parent_id");
	}
	$query = new dbc("DELETE FROM " . PREFIX . "_linkcat WHERE catid='$id'");
}


// ========================================================
// <<-- UEBERPRUEFT DOKUMENTRECHTE -->>
// ========================================================
function check_action($perm,$table,$rowid,$idtype){
	$sql = new dbc("SELECT UID FROM ".PREFIX."_$table WHERE uid='".UID."' AND $rowid='".$idtype."'");
	$num = $sql->numrows();
	if( (!permission($perm)) && ( $num < 1 ) ){
		return false;
	}else {
		return true;
	}
}
//================================================================
// <<-- ADMINLOG -->>
//================================================================
function alog($param_1,$param_2){
	/* up to 4
	global $langadmin;
	$sql = new dbc("INSERT INTO ".PREFIX."_adminlogs (id,uid,ctime,ip,action) VALUES ('','".UID."','".time()."','".$_SERVER['REMOTE_ADDR']."','".$langadmin[$param_1].$param_2."')");
	*/
}



function remoteFileSize($file)
{
	ereg("http:\/\/([^\/]*)\/(.*)", $file, $reg);
	
	$uri = '/' . $reg[2];
	
	if(strstr($reg[1], ':') !== false)
	{
		list($host, $port) = explode(':', $reg[1]);
	}
	else
	{
		$host = $reg[1];
		$port = 80;
	}
	
	$get = 'GET ' . $uri . ' HTTP/1.1' . "\r\n" . 'Host: ' . $host . "\r\n\r\n";
	
	$sock = fsockopen($host, $port);
	fwrite($sock, $get, strlen($get));
	$res = fread($sock, 1024);
	fclose($sock);
	
	if(eregi("Content-Length: ([0-9]*)", $res, $reg)) 
	{
		return $reg[1];
	}
	else
	{
		$i = 0;
		
		$fp = fopen($file, 'r');
		while(!feof($fp))
		{
			$i += strlen(fread($fp, 4096));
		}
		fclose($fp);
		
		return $i;
	}
	
}

?>