<?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);
define('THIS_SCRIPT', 'vbhosting');

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array('vbhosting_create', 'register');

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array(
	'vbhosting_create'
);

// pre-cache templates used by specific actions
$actiontemplates = array(
    'rules' => array('vbhosting_create_rules'),
    'createforum' => array('vbhosting_create_createforum'),
    'docreateforum' => array('vbhosting_create_docreateforum')
);

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once('./includes/functions_vbhosting.php');
// For image verification:
require_once('./includes/functions_user.php');

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

if (!$vboptions['vbhosting_create_allowregister'] && !($permissions['adminpermissions'] & CANCONTROLPANEL))
{
    standard_error($vbphrase['vbhosting_create_disabled']);
}

// NOTE TO MYSELF: Should we create a special vBHosting permission?
if (!$bbuserinfo['userid'])
{
    print_no_permission();
}

if ($_REQUEST['do'] != 'createforum' && $_REQUEST['do'] != 'docreateforum')
{
    $_REQUEST['do'] = 'rules';
}

if ($_REQUEST['do'] == 'docreateforum')
{
    // Begin error checking
    $errors = array();

    // Make sure we have a value in all needed fields
    if (empty($_POST['forumtitle']))
    {
        $errors[] = construct_phrase($vbphrase['vbhosting_create_errors_missingfield'], $vbphrase['vbhosting_create_createforum_forumtitle']);
    }

    // Check the censor
    if ($_POST['forumtitle'] != fetch_censored_text($_POST['forumtitle']))
    {
        $errors[] = construct_phrase($vbphrase['vbhosting_create_errors_censored'], $vbphrase['vbhosting_create_createforum_forumtitle']);
    }
    if ($_POST['description'] != fetch_censored_text($_POST['description']))
    {
        $errors[] = construct_phrase($vbphrase['vbhosting_create_errors_censored'], $vbphrase['vbhosting_create_createforum_description']);
    }
    if ($_POST['websitetitle'] != fetch_censored_text($_POST['websitetitle']))
    {
        $errors[] = construct_phrase($vbphrase['vbhosting_create_errors_censored'], $vbphrase['vbhosting_create_createforum_websitetitle']);
    }

    // Check the regimage
    if ($vboptions['vbhosting_create_regimagecheck'] AND $vboptions['gdversion'])
    {
        $imagestamp = trim(str_replace(' ', '', $_POST['imagestamp']));
        $ih = $DB_site->query_first("SELECT imagestamp FROM " . TABLE_PREFIX . "regimage WHERE regimagehash = '" . addslashes($_POST[imagehash]) . "'");
        if (!$imagestamp OR strtoupper($imagestamp) != $ih['imagestamp'])
        {
            	eval('$errors[] = "' . fetch_phrase('register_imagecheck', PHRASETYPEID_ERROR) . '";');
            	$DB_site->query("DELETE FROM " . TABLE_PREFIX . "regimage WHERE regimagehash = '" . addslashes($_POST[imagehash]) . "'");
				unset($imagestamp);
            	unset($_POST['imagehash']);
				unset($_POST['imagestamp']);
        }
    }

    // Decide if we can create a forum now
    if (count($errors) > 0)
    {
        $_REQUEST['do'] = 'createforum';
        $_REQUEST['acceptrules'] = 1;
    }
    else
    {
        if ($vboptions['vbhosting_create_needmoderation'])
        {
            $DB_site->query("
                INSERT INTO " . TABLE_PREFIX . "vbhosting_newforumqueue
                    (userid, requesttime, forumtitle, forumdescription, websitelogo, websiteurl, websitetitle)
                VALUES
                    (
                        $bbuserinfo[userid],
                        " . TIMENOW . ",
                        '" . addslashes($_POST['forumtitle']) . "',
                        '" . addslashes($_POST['description']) . "',
                        '" . addslashes($_POST['logo']) . "',
                        '" . addslashes($_POST['websiteurl']) . "',
                        '" . addslashes($_POST['websitetitle']) . "'
                    )
            ");
            if (!empty($vboptions['vbhosting_create_notifymail']))
            {
                $queueid = $DB_site->insert_id();
				vbmail($vboptions['vbhosting_create_notifymail'], $vbphrase['vbhosting_create_mail_needmoderation_subject'], construct_phrase($vbphrase['vbhosting_create_mail_needmoderation_body'], unhtmlspecialchars($bbuserinfo['username']), $vboptions['bburl'], $queueid), true);
            }
        }
        else
        {
			$forumid = vbhosting_create_forum($bbuserinfo['userid'], $_POST['forumtitle'], $_POST['description'], $_POST['logo'], $_POST['websiteurl'], $_POST['websitetitle']);
            if (!empty($vboptions['vbhosting_create_notifymail']))
            {
				vbmail($vboptions['vbhosting_create_notifymail'], $vbphrase['vbhosting_create_mail_adminnotify_subject'], construct_phrase($vbphrase['vbhosting_create_mail_adminnotify_body'], unhtmlspecialchars($bbuserinfo['username']), $vboptions['bburl'], $forumid, $_POST['forumtitle']), true);
            }
        }
        $vbphrase[vbhosting_create_step] = construct_phrase($vbphrase[vbhosting_create_step], 3, $vbphrase["vbhosting_create_step_docreateforum"]);
        eval('$content = "' . fetch_template('vbhosting_create_docreateforum') . '";');
    }
}

if ($_REQUEST['do'] == 'createforum')
{
    // Check if the rules have been accepted
    if (!$_REQUEST['acceptrules'])
    {
        standard_error($vbphrase['vbhosting_create_rules_notaccepted']);
    }

	// image verification
	if ($vboptions['vbhosting_create_regimagecheck'] AND $vboptions['gdversion'])
	{
		// Transfer a successful image match over when other errors have occurred
		if (count($errors) AND $_POST['imagehash'])
		{
			$imagestamp = htmlspecialchars_uni($_POST['imagestamp']);
			$imagehash = htmlspecialchars_uni($_POST['imagehash']);
		}
		else
		{
			$string = fetch_registration_string(6);

			$imagehash = md5(uniqid(rand(), 1));

			// Gen hash and insert into database;
			$DB_site->query("INSERT INTO " . TABLE_PREFIX . "regimage (regimagehash, imagestamp, dateline) VALUES ('" . addslashes($imagehash) . "', '" . addslashes($string) . "', " . TIMENOW . ")");
		}

	}

    // Some output in case we had an error
    $oldinfo = array();
    $errortext = false;
    if (count($errors) > 0)
    {
        $oldinfo['forumtitle'] = htmlspecialchars_uni(trim($_POST['forumtitle']));
        $oldinfo['description'] = htmlspecialchars_uni(trim($_POST['description']));
        $oldinfo['logo'] = htmlspecialchars_uni(trim($_POST['logo']));
        $oldinfo['websiteurl'] = htmlspecialchars_uni(trim($_POST['websiteurl']));
        $oldinfo['websitetitle'] = htmlspecialchars_uni(trim($_POST['websitetitle']));
        $oldinfo['imagestamp'] = htmlspecialchars_uni(trim($_POST['imagestamp']));

        for ($i=0; $i<count($errors); $i++)
        {
            $errortext .= '<li>' . $errors[$i] . '</li>';
        }
    }

    $vbphrase[vbhosting_create_step] = construct_phrase($vbphrase[vbhosting_create_step], 2, $vbphrase["vbhosting_create_step_createforum"]);
    eval('$content = "' . fetch_template('vbhosting_create_createforum') . '";');
    $nextstep = 'docreateforum';
}

if ($_REQUEST['do'] == 'rules')
{
    // Well, there's only some output to do...
    $vbphrase[vbhosting_create_step] = construct_phrase($vbphrase[vbhosting_create_step], 1, $vbphrase["vbhosting_create_step_rules"]);
    eval('$content = "' . fetch_template('vbhosting_create_rules') . '";');
    $nextstep = 'createforum';
}

$navbits = array();
$navbits[lastelement] = $vbphrase['vbhosting_create_createforum'];
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('vbhosting_create') . '");');

?>
