<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

$stylescript = "skinmng.pl";
$thisdir = dirname( __FILE__ );


function create_skin_from_skin ($res_login, $orig_skin_id, $new_skinname, &$alerts) { 
	$skinname = str_replace("'", "\\'", $new_skinname);
	$skin     = $orig_skin_id;

	global $stylescript;
	global $thisdir;
	global $bindir;
	global $sbindir;
	
	$id = db_query("select count(*) as c from custom_skins where show_name = '$skinname' and owner_type='reseller' and anbieter='$PHP_AUTH_USER'");
	$wer = db_fetch_array($id);
	if ($wer["c"]) 	{
		$alerts[] = ltext("sk_skin_exists");
		include $thisdir . "/skins_inc/editskins.php";
	} else 	{
		settype($skin, "integer");
		//echo "SELECT name, owner_type FROM custom_skins WHERE id = $skin and ( owner_type = 'preinstalled' or ( owner_type = 'reseller' and anbieter='$res_login' ) )";
		$id = db_query("SELECT name, owner_type, version FROM custom_skins WHERE (id = $skin and ( owner_type = 'preinstalled' or ( owner_type = 'reseller' and anbieter='$PHP_AUTH_USER' )) )");
		if(db_num_rows($id))
		{
			$row = db_fetch_array($id);
			if($id1 = db_query("insert into custom_skins (show_name, owner_type, anbieter, version) values ('$skinname', 'reseller', '$res_login', '{$row['version']}')"))
			{
			  global $db_type;
			  if ($db_type=='mysql'){
			    $skin_id = db_insert_id($id1);
			  }else{
			    $skin_id = db_insert_id( "custom_skins","id");
			  }
				if($row["owner_type"] == "preinstalled")
				{
					$useskin = $row["name"];
				}
				else
				{
					$useskin = "skin_" . $skin;
				}
//				echo("<p>$sbindir/$stylescript --skin skin_$skin_id --new_skin --use_dir $useskin</p>");
				exec("$sbindir/$stylescript --skin skin_$skin_id --new_skin --use_dir $useskin", $out, $ret);
				if($ret != 0)
				{
					echo '<pre>';
					echo implode ("\n", $out);
					echo '</pre>';
					$alerts[] = ltext('sk_system_error');
					db_query("delete from custom_skins where id=$skin_id");
					return false;
				}
				else 
				{
					db_query("update custom_skins set path = '/skins/skin_$skin_id' where id='$skin_id'");
					return true;
				}
			}
			else
			{
				$alerts[] = ltext('sk_system_error');
				return false;
			}
		}
		else
		{
			$alerts[] = ltext('sk_invalid_skin');
			return false;;
		}
		
	}
	return true;
}

function remove_skin($res_login, $skin_id, &$alerts) {
  $skin = $skin_id;

	global $stylescript;
	global $thisdir;
	global $bindir;
	global $sbindir;
	
  $id = db_query("select count(*) as c from custom_skins where id = $skin and anbieter='$res_login' and owner_type='reseller'");
  $w = db_fetch_array($id);
  if(!$w['c']) {
	$alerts[] = ltext("sk_invalid_skin");	
	return false;
  }

  exec("$sbindir/$stylescript --skin skin_$skin --del_skin", $out, $ret);
  if($ret != 0) {
	  echo '<pre>';
	  print_r($out);
	  echo '</pre>';
	  return false;
  }

  db_query("delete from custom_skins where id = $skin and anbieter='$PHP_AUTH_USER' and owner_type='reseller'");	
  return true;
  
}

function create_default_res_skin($reseller_login) {
	// if no skins exists -> create default
	global $stylescript;
	global $thisdir;
	
	$res_anbieter_skins = db_query("SELECT * FROM custom_skins WHERE owner_type='reseller' AND anbieter = '$reseller_login'");
	if (db_num_rows($res_anbieter_skins) == 0) {
		$res_current_skin = db_query("SELECT skin_id FROM skins WHERE cuser='$reseller_login'");
		if (db_num_rows($res_current_skin) > 0) {
			$current_skin = db_fetch_array($res_current_skin);
			$skin_id = $current_skin['skin_id'];
		} else {
			$res_default_skin = db_query("SELECT * FROM custom_skins WHERE name='skin_1'");
			$default_skin = db_fetch_array($res_default_skin);
			$skin_id = $default_skin['id'];
		}
		$alerts = array();
		create_skin_from_skin($reseller_login, $skin_id, ltext('sk_default_skin'), $alerts);
	}
}



?>
