<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBHosting Version 2                                              # ||
|| # ---------------------------------------------------------------- # ||
|| # All PHP code in this file is ©2004 GameCrash.org.                # ||
|| # http://www.gamecrash.org (soon) | http://www.letzplay.de         # ||
|| # ---------------------------------------------------------------- # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);

// #################### PRE-CACHE TEMPLATES AND DATA ######################
$phrasegroups = array();
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

// ############################# LOG ACTION ###############################
vbhosting_log();

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

if (!vbhosting_haspermission('managestyles'))
{
	print_cp_no_permission();
}

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

print_cp_header($vbphrase['vbhosting_edit_images']);

$dostyleid = $DB_site->query_first("SELECT styleid FROM " . TABLE_PREFIX . "vbhosting_hosted WHERE hostedid={$hostedid}");
$dostyleid = $dostyleid['styleid'];

cache_styles();
$parentstyle = $stylecache["$dostyleid"]['parentid'];
$parentstylevars = unserialize($stylecache["$parentstyle"]['stylevars']);
$parentimagedir = $parentstylevars['imgdir_button'];

$randomstring = rand(10, 10000);

if ($_REQUEST['do'] == 'modify')
{
	$template = $DB_site->query_first("SELECT template FROM " . TABLE_PREFIX . "template WHERE title='imgdir_button' AND templatetype='stylevar' AND styleid={$dostyleid}");
	$imagedir = $template['template'];
	
	print_form_header('image', 'switchbuttonset');
	construct_hidden_code('hostedid', $hostedid);
	print_table_header($vbphrase['vbhosting_edit_images']);
	
	print_description_row('<label for="ub_0"><input type="radio" name="usecustombuttons" value="0" id="ub_0"' . iif(empty($imagedir), ' checked="checked"') . ' />' . $vbphrase['vbhosting_use_default_buttons'] . '</label><br /><label for="ub_1"><input type="radio" name="usecustombuttons" value="1" id="ub_1"' . iif(!empty($imagedir), ' checked="checked"') . ' />' . $vbphrase['vbhosting_use_custom_buttons'] . '</label>', 0, 2, 'tfoot');
	print_description_row($vbphrase['vbhosting_image_editor_description']);
	
	print_submit_row();
	print_table_footer();
	
	if (!empty($imagedir))
	{
		//construct_hidden_code('MAX_FILE_SIZE', 1000000);
		
		vbhosting_print_image_group($vbphrase['vbhosting_new_posting_images'], array('newthread.gif', 'reply.gif', 'threadclosed.gif'));
		vbhosting_print_image_group($vbphrase['vbhosting_postbit_images'], array('edit.gif', 'reply_small.gif'));
	}
}

if ($_REQUEST['do'] == 'updatebuttons')
{
	$currentdir = dir("./images/buttons/vbhosting/$hostedid");
	while (false !== ($entry = $currentdir->read()))
	{
		$currentfile = str_replace('.', '_____', $entry);
		if (is_file("./images/buttons/vbhosting/$hostedid/$entry") && isset($_FILES[$currentfile]['tmp_name']))
		{
			$upload = trim($_FILES[$currentfile]['tmp_name']);
			$upload_name = trim($_FILES[$currentfile]['name']);
	
			if (is_uploaded_file($upload))
			{
				if ($vboptions['safeupload'])
				{
					$path = $vboptions['tmppath'] . "/$tmp_name";
					if (move_uploaded_file($upload, $path) AND file_exists($path))
					{
						$filename = $path;
					}
				}
				else if (file_exists($upload))
				{
					$filename = $upload;
				}
			}

			if (!file_exists($filename))
			{
				continue;
			}
			
			if (!$imginfo = @getimagesize($filename) || ($imginfo[2] != 1 && $imginfo[2] != 2 && $imginfo[2] != 3))
			{
				@unlink($filename);
				continue;
			}
			
			copy($filename, "./images/buttons/vbhosting/$hostedid/$entry");
			@unlink($filename);
		}
	}
	$currentdir->close();

	print_cp_message($vbphrase['vbhosting_images_updated_succesfully'], "image.php?hostedid={$hostedid}&do=modify");
}

if ($_REQUEST['do'] == 'revert')
{
	globalize($_REQUEST, array('image'));
	
	if (empty($image) || strpos($image, '/') !== false || strpos($image, '\\') !== false || !file_exists("$parentimagedir/$image"))
	{
		print_cp_message($vbphrase['vbhosting_please_select_an_image_to_revert']);
	}
	
	copy("$parentimagedir/$image", "./images/buttons/vbhosting/$hostedid/$image");
	
	print_cp_message($vbphrase['vbhosting_images_updated_succesfully'], "image.php?hostedid={$hostedid}&do=modify");
}

if ($_REQUEST['do'] == 'switchbuttonset')
{
	globalize($_POST, array('usecustombuttons'));
	
	if ($usecustombuttons)
	{
		$DB_site->query("REPLACE INTO " . TABLE_PREFIX . "template (styleid, title, template, templatetype, dateline, username) VALUES ($dostyleid, 'imgdir_button', 'images/buttons/vbhosting/$hostedid', 'stylevar', " . TIMENOW . ", '" . addslashes($bbuserinfo['username']) . "')");
		
		if (!is_dir("./images/buttons/vbhosting/$hostedid"))
		{
			mkdir("./images/buttons/vbhosting/$hostedid", 0755);
			$parentdir = dir($parentimagedir);
			while (false !== ($entry = $parentdir->read()))
			{
				if (is_file("$parentimagedir/$entry"))
				{
	   				copy("$parentimagedir/$entry", "./images/buttons/vbhosting/$hostedid/$entry");
				}
			}
			$parentdir->close();
		}
	}
	else
	{
		$template = $DB_site->query_first("DELETE FROM " . TABLE_PREFIX . "template WHERE title='imgdir_button' AND templatetype='stylevar' AND styleid={$dostyleid}");
	}

	vbhosting_quiet_build_style($dostyleid, '', array('docss' => 0, 'dostylevars' => 1, 'doreplacements' => 0, 'doposteditor' => 0));

	print_cp_message($vbphrase['vbhosting_buttontype_changed_successfully'], "image.php?hostedid={$hostedid}&do=modify");
}

print_cp_footer();

?>