<?php
/*
##################     CWB  PRO   ########################
############################################################
CWB  PRO		      	Version 1.0
Writed by               GraFX (webmaster@grafxsoftware.com)
Created 03/01/03        Last Modified $Date: 2006/10/27 15:21:21 $
Scripts Home:           http://www.grafxsoftware.com
############################################################
File name               config.php
File purpose            Configuration Script
File created by         GraFX (webmaster@grafxsoftware.com)
############################################################
*/// $Id: config.php,v 1.4 2006/10/27 15:21:21 zelteto Exp $
	include_once("../config.inc.php");
	include_once(INDEX_PATH."cls_fast_template.php");
	include_once(INCLUDE_LANGUAGE_PATH.$LANG.".inc.php");
	include_once(INCLUDE_LANGUAGE_PATH.$LANG.".admin.inc.php");
	include_once(INCLUDE_ADMIN_PATH."authenticate.php");
	include_once(INCLUDE_ADMIN_PATH."menuleft.inc.php");
	include_once(INCLUDE_ADMIN_PATH."menuright.inc.php");
	include_once(INCLUDE_ADMIN_PATH."cls_string.php");
	include_once(INCLUDE_ADMIN_PATH."cls_miniFile.php");
	// begin authenticate part
	if(AUTH_TYPE == 1)
	{
		if (!$AUTHENTICATE)
		{
			header( "WWW-Authenticate: Basic realm=\"ADMIN ". CONF_SITE_NAME."\"");
			header( "HTTP/1.0 401 Unauthorized");
			$ft = new FastTemplate(ADMIN_TEMPLATE_CONTENT_PATH);
			$ft->define(array("main"=>"template_firstpage.html", "content"=>"authentication_failed.html"));
			$ft->assign("ADMIN_URL", ADMIN_URL);
			$ft->assign("VERSION", VERSION );
			$ft->assign("ADDON","<br /><br />");
			$ft->multiple_assign_define("LANG_");
			$ft->multiple_assign_define("CONF_");
			$ft->parse("BODY", array("content","main"));
			$ft->showDebugInfo(ERROR_DEBUG);
			$ft->FastPrint();
			exit;
		}
	}
	else if(AUTH_TYPE == 2)
	{
		include_once(INCLUDE_PATH.'cls_session.php');
		$sess = new MYSession();
		//session_regenerate_id();
		if (!$sess->get(SESSION_ID))
		{
			$sess->set('session_url_before',$_SERVER['REQUEST_URI']);
			header("Location: login.php");
			exit;
		}
	}// end authenticate part
	include_once(INCLUDE_PATH."cls_configwrapper.php");
	$st = new String("");
	$all_url_vars = array();
	$all_url_vars=$st->parse_all();
	if (empty($all_url_vars['action']))  $all_url_vars['action'] = "form";
	
	$mf=new MiniFile("config.php");
	
 if ($all_url_vars['action']=="form")
{
	$ft = new FastTemplate(ADMIN_TEMPLATE_CONTENT_PATH);
	$ft->define(array("main"=>"template_index.html", "content"=>"config.html"));
	
	$ft->assign("ADMIN_URL", ADMIN_URL);
	$ft->assign("MENULEFT",$menuleft);
	$ft->assign("MENURIGHT",$menuright);
	$ft->assign("VERSION", VERSION );
	
	if(!strstr(PHP_OS, 'WIN'))
	{
		if (!is_writable(INDEX_PATH."tmp/extraconfig.inc.php")) $ft->assign("ERROR", LANG_CONF_ERROR_NOMOD);
   			else   $ft->assign("ERROR", LANG_CONF_ERROR_SECURITY);
		if (!is_writable(REPOSITORY_PATH."config.php")) $ft->assign("ERROR2", LANG_CONF_ERROR_NOCONFIG);
   			else   $ft->assign("ERROR2", LANG_CONF_ERROR_SECURITY2);	
			
	}
	else {
			$ft->assign("ERROR", "");
			$ft->assign("ERROR2", "");
		}
	$i=0;
	foreach($mf->getArray() as $row){
		$id[$i] = $row["id"];
		$description[$i] = $row["description"];
		$name[$i] = $row["name"];
		$value[$i] = $row["value"];
		$comment[$i] = $row["comment"];
		$type[$i] = $row["type"];
		$i++;

	}
	$nr = $i;
	
	
	$ft->define_dynamic("cwb_conf_list","content");
	for ($i=0;$i<$nr;$i++)
	{
       switch ($type[$i])
	   {
			case 1:// textField
			   $value_str = "<input size=40 class=text type=text name='" . "CNF_".$name[$i] . "' value='" . $value[$i]  . "'>";
			   break;
			case 2:// textArea
			   $value_str = "<textarea cols=50 rows=5 class=text name='" . "CNF_".$name[$i] ."'>". $value[$i]  . "</textarea>";
			   break;
		   case 3:// select Menu
			   $value_str = "<select class=text name='" . "CNF_".$name[$i] . "'>";
			   $options = explode("|", $comment[$i]);
			   for ($j=0; $j<count($options); $j+=2){
			   		$desc = $options[$j+1];
			        $value_str.="<option value='" . $options[$j] . "' " . (($value[$i]==$options[$j])?"SELECTED":"") . ">" . constant($desc);
			   }
			   $value_str.="</select>";
			   break;
			case 4:// textField (for int values)
			   $value_str = "<input class=text size=5 maxlength=5 type=text name='" . "CNF_".$name[$i] . "' value='" . $value[$i]  . "'>";
			   break;
			case 5:// CheckBox
			   $options = explode("|", $comment[$i]);
			   $current_2power = 1;
			   $value_str="";
			   for ($j=0; $j < count($options); $j++){
			          $value_str .=$br."<input type=checkbox name='" . "CNF_CHBX_".$name[$i] . "[]' " . ((( $value[$i] & $current_2power) == $current_2power)?"CHECKED":"") . "> " . constant($options[$j]);
			   }
			   break;
		}//end for 

		$ft->assign("CONF_CWB_DESCRIPTION", constant($description[$i]));		
		$ft->assign("CONF_CWB_NAME", $name[$i]);
		$ft->assign("CONF_CWB_VALUE", $value_str);
		$ft->assign("NR_ROWS",$nr);
		$ft->parse("STAT_CONF_LIST",".cwb_conf_list");
	}
	$ft->assign("INDEX_PATH", INDEX_PATH);
	$ft->clear_dynamic("cwb_conf_list");
	$ft->multiple_assign_define("LANG_");
	$ft->multiple_assign_define("CONF_");
	$ft->parse("BODY", array("content","main"));
	$ft->showDebugInfo(ERROR_DEBUG);
	$ft->FastPrint();
}
else if ($all_url_vars['action'] == "submit")
{
	$nr_rows = $all_url_vars['nr_rows'];
	/*
	print_r($all_url_vars);
	print "<br>".$nr_rows;
	exit;
	*/
	$old_array=$mf->getArray();
	
	foreach($all_url_vars as $key =>$value)
	{
		  $ta=explode("CNF_",$key);
		  if(count($ta) && empty($ta[0]))
		  {
		  
		      $type=getConfigType($old_array,$ta[1]);
			  //print "sasasa".$type;
			   if ($type!=0)
				{
					//$type=$row['type'];
					if ($type!=5)
					{
						$old_array=update($old_array,$ta[1],$value);
						if($ta[1]=="EMAIL_NAME")
						print_r($old_array);
					}
					 else
					 {
						   $options = explode("|", $value);
						   $current_2power = 1;
						   $current_value = 0;
						   for ($j=0; $j<count($options); $j++)
						   {
								$current_value |=  (isset($all_url_vars[$key . $current_2power])?$current_2power:0);
								$current_2power *=2;
						   }
							$old_array=update($old_array,$ta[1],$current_value);
							
					 }// end else
					 
					 
					 
					 
				   }//end if
		}//end if
	}// end foreach<br />

	$mf->setArray($old_array);
	//print_r($old_array);
	$mf->save();

	$cw = new ConfigWrapper(0);
	// volt egy print print "sss".
	$cw->generateConfig(INDEX_PATH."tmp/extraconfig.inc.php",$old_array);
	header("Location: ".CONF_INDEX_URL."admintool/config.php");
}
function getConfigType($array,$name){

foreach($array as $row)
if($row["name"]==$name)
  return $row["type"];

return 0;

}

function update($array,$name,$value){

foreach($array as $key => $row)
{
	if($row["name"]==$name)
	  $array[$key]["value"]=$value;
}
return $array;

}


?>