Location: PHPKode > projects > top serious project > bco/change_colors.php
<?php

/***************************************************************************
 *   Copyright 2003 Ian Meyer, Ian Pitcher
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/* Please excuse how big and confusing this file is. I'm sorry. */

require("include.php");

//error_reporting(E_ALL);

if (!$logged_in) {
    //redirect("login.php?back=c.php");
    header("Location: $GLOBALS[base_url]" . "login.php?back=change_colors.php");
	exit;
}

if ($_GET['submit'] == "submit") {
    /* These if statements check all the input of the _GET array    */
    /* and checks for whether the color falls within hex ranges     */
    /* if an input item is not correct, it loads the default        */

    /* Check background color here. */
    if ($_GET['background'] == "") {
        $background = trim(BGCOLOR);
    } else {
        $background = trim($_GET['background']);
    }
    if (!ctype_xdigit($background)) {
        $background = trim(BGCOLOR);
    }

    /* Check tr1 color here. */
    if ($_GET['tr1color'] == "") {
        $tr1color = trim(TR1COLOR);
    } else {
        $tr1color = trim($_GET['tr1color']);
    }
    if (!ctype_xdigit($tr1color)) {
        $tr1color = trim(TR1COLOR);
    }

    /* Check tr2 color here. */
    if ($_GET['tr2color'] == "") {
        $tr2color = trim(TR2COLOR);
    } else {
        $tr2color = trim($_GET['tr2color']);
    }
    if (!ctype_xdigit($tr2color)) {
        $tr2color = trim(TR2COLOR);
    }

    /* Check form color here. */
    if ($_GET['formcolor'] == "") {
        $formcolor = FORMCOLOR;
    } else {
        $formcolor = trim($_GET['formcolor']);
    }
    if (!ctype_xdigit($formcolor)) {
        $formcolor = trim(FORMCOLOR);
    }

    /* Check mypost color here. */
    if ($_GET['mypostcolor'] == "") {
        $mypostcolor = trim(MYPOSTCOLOR);
    } else {
        $mypostcolor = trim($_GET['mypostcolor']);
    }
    if (!ctype_xdigit($mypostcolor)) {
        $mypostcolor = trim(MYPOSTCOLOR);
    }

    /* Check font face here */
    if ($_GET['fontface'] == "") {
        $fontface = trim(FONTFACE);
    } else {
        if (!preg_match("/^[A-Za-z0-9 -]+$/i", $_GET['fontface'])) {
            $fontface = trim(FONTFACE);
        } else {
            $fontface = trim($_GET['fontface']);
        }
    }

    /* check fontsize here after removing the "pt" from the string */
    $fontsize = eregi_replace("px", "", $_GET['fontsize']);
    if ($fontsize == "") {
        $fontsize = trim(FONTSIZE);
    } else {
        if (!ctype_digit($fontsize)) {
            $fontsize = "12";
        } else {
            $fontsize = trim($fontsize);
        }
    }
    
    $color_array = array("$background",
                        "$tr1color",
                        "$tr2color",
                        "$formcolor",
                        "$mypostcolor",
                        "$fontface",
                        "$fontsize");
    $color_string   = implode(",", $color_array);
    $stylesheet     = bco_generate_stylesheet($color_string);
    setUserColors($user_array['myuserid'],$color_string,$stylesheet);
    header("Location: $base_url" . "change_colors.php");
    exit;
} elseif ($_GET['submit'] == "reset") {
	// Get the default colors
	$colors_query = "select default_colors, default_stylesheet from bco_config_new limit 1";
	$row = pg_fetch_array(pg_query($colors_query), 0);	

	// Get the users current colors
//    $getColorQuery = "select colors from users where id=$user_array[myuserid]";
//    $myColors = pg_fetch_result(pg_query($getColorQuery), 0);

    $stylesheet = bco_generate_stylesheet($default_colors);

	$query = "update users set colors='$row[default_colors]', stylesheet='$row[default_stylesheet]' where id=$user_array[myuserid]";

    if (!pg_query($query)) {
        bco_error("Failed update: " . pg_last_error());
    }
    header("Location: $base_url" . "change_colors.php");
    exit;
} else {
    /* This is done if preview is select, or the page is loaded for the first time */
    if (isset($userinfo['colors'])) {
        $color_array = urldecode($userinfo['colors']);
        list($bgcolor,$tr1color,$tr2color,$formcolor,$mypostcolor,$fontface,$fontsize) = split(",",$color_array);
    } else {
        // We want to get what colors the user has in the db in the open.
        $getColorQuery = "select colors from users where id=$myuserid";
        $color_array = pg_fetch_result(pg_query($getColorQuery), 0);
        // Get the variables out of the array.
        list($bgcolor,$tr1color,$tr2color,$formcolor,$mypostcolor,$fontface,$fontsize) = split(",",$color_array);
    } 
    /* check to see if the $_GET form variables are set and if they are, replace the colors with them */
    if (isset($_GET['background'])) {
        $bgcolor = $_GET['background'];
    }

    if (isset($_GET['tr1color'])) {
        $tr1color = $_GET['tr1color'];
    }

    if (isset($_GET['tr2color'])) {
        $tr2color = $_GET['tr2color'];
    }

    if (isset($_GET['formcolor'])) {
        $formcolor = $_GET['formcolor'];
    }

    if (isset($_GET['mypostcolor'])) {
        $mypostcolor = $_GET['mypostcolor'];
    }
    
    if (isset($_GET['fontface'])) {
        $fontface = $_GET['fontface'];
    }
    
    if (isset($_GET[fontsize])) {
        $fontsize = $_GET[fontsize];
    }
    
	// We'll need this in case the user what's to change the default scheme.. but only if they're an admin.
	if ($is_admin) {
		$color_array = array("$bgcolor",
                        "$tr1color",
                        "$tr2color",
                        "$formcolor",
                        "$mypostcolor",
                        "$fontface",
                        "$fontsize");
	    $color_string = implode(",", $color_array);
	}

echo <<< END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Change your colors.</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
END;

// Stylesheet time.
echo "<style type=\"text/css\">";
echo bco_generate_stylesheet($color_string);
echo "</style>";

echo <<< END
</head>
<body>
END;

bco_index_menu("Top serious color scheme creator.");

echo <<< END
\n<br />
<form method="get" action="$PHP_SELF">
<table width="400" cellpadding="1" cellspacing="0" class="replytable">
    <tr>
        <td colspan="2" class="header">
        Change your colors, money.
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        background:
        </td>
        <td align="left" class="tr2">
        #<input type="text" name="background" maxlength="6" size="6" class="textfield" value="$bgcolor" />
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        table row 1:
        </td>
        <td align="left" class="tr2">
        #<input type="text" name="tr1color" maxlength="6" size="6" class="textfield" value="$tr1color" />
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        table row 2:
        </td>
        <td align="left" class="tr2">
        #<input type="text" name="tr2color" maxlength="6" size="6" class="textfield" value="$tr2color" />
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        form background:
        </td>
        <td align="left" class="tr2">
        #<input type="text" name="formcolor" maxlength="6" size="6" class="textfield" value="$formcolor" />
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        my posts:
        </td>
        <td align="left" class="tr2">
        #<input type="text" name="mypostcolor" maxlength="6" size="6" class="textfield" value="$mypostcolor"/>
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        font face:
        </td>
        <td align="left" class="tr2">
        <input type="text" name="fontface" maxlength="40" size="15" class="textfield" value="$fontface" />
        </td>
    </tr>
    <tr>
        <td align="right" class="tr2">font size: (in px)</td>
        <td align="left" class="tr2"><input type="text" name="fontsize" maxlength="2" size="5" class="textfield" value="$fontsize" />px</td>
    </tr>
    <tr>
        <td align="right" class="tr2">
        &nbsp;
        </td>
        <td align="left" class="tr2">
        <input type="submit" name="submit" value="preview" class="button" />
        <input type="submit" name="submit" value="reset" class="button" />
        <input type="submit" name="submit" value="submit" class="button" />
        </td>
    </tr>
</table>
</form>
<br />
END;

$query = "select count(id) as rank, substring(colors, 1, 34) as colors from users group by colors order by rank desc limit 5 offset 1";

if (!$result = pg_query($query)) {
	echo "<strong>Top 5 scheme query failed.<br />" . pg_last_error() . "</strong>";
}

// What rank are we? Start at 1
$i = 1;
while ($row = pg_fetch_array($result)) {
	list($bgcolor,$tr1color,$tr2color,$formcolor,$mypostcolor) = split(",", strtoupper($row['colors']));
    echo "\n&#149; $row[rank] users are using "
		. "<a href=\"" . $GLOBALS['base_url'] . "change_colors.php?background=$bgcolor&tr1color=$tr1color"
        . "&tr2color=$tr2color&formcolor=$formcolor&mypostcolor=$mypostcolor"
        . "&fontface=$fontface&fontsize=$fontsize&submit=preview\">"
        . strtoupper($row['colors']) . "</a> for their scheme. <br />";
	$i++;
}

echo <<< END
<br />
<strong>Here is what thread listings will look like:</strong>
<br />
<br />
<table width="100%" cellpadding="1" cellspacing="0" class="bigtable">
    <tr>
        <td align="left" class="tr1">
            <a href="#" class="tr1">ian</a>
        </td>

        <td align="left" width="60%" class="tr1">
            <a href="#" class="tr1">This is row number 1.</a>
        </td>
        <td align="right" class="tr1">
            01:27 AM 04-12-03 <span class="smallfont">by: ian</span>
        </td>
        <td align="center" class="tr1">
            &nbsp;&nbsp;&nbsp;08&nbsp;|&nbsp;11&nbsp;&nbsp;&nbsp;
        </td>
    </tr>
    <tr>
        <td align="left" class="tr2">
            <a href="#" class="tr2">ian</a>
        </td>
        <td align="left" width="60%" class="tr2">
            <a href="#" class="tr2">This is row number 2.</a>
        </td>

        <td align="right" class="tr2">
            05:37 PM 04-11-03 <span class="smallfont">by: ian</span>
        </td>
        <td align="center" class="tr2">
            &nbsp;&nbsp;&nbsp;23&nbsp;|&nbsp;11&nbsp;&nbsp;&nbsp;
        </td>
    </tr>
    <tr>
        <td align="left" class="mypost">
            <a href="#" class="mypost">ian</a>
        </td>
        <td align="left" width="60%" class="mypost">
            <a href="#" class="mypost">These are what my posts will look like.</span>
        </td>

        <td align="right" class="mypost">
            05:37 PM 04-11-03 <span class="smallfont">by: ian</span>
        </td>
        <td align="center" class="mypost">
            &nbsp;&nbsp;&nbsp;79&nbsp;|&nbsp;79&nbsp;&nbsp;&nbsp;
        </td>
    </tr>
</table>
<br />
<strong>And this is what threads will look like.</strong>
<br />
<br />
<table width="100%" cellpadding="1" cellspacing="0" class="threadtable">
    <tr>
        <td class="tr2thread">
            <a name="3"></a>
            <a href="#" class="tr2">ian</a> posted this on Mar 31st, 2003 at 11:28:52 pm: This is the row number 2 color.
        </td>
    </tr>
    <tr>
        <td class="tr1">
            The initial message body.
            <br />
            <br />
        </td>
    </tr>
    <tr>
        <td class="mypostthread">
            <a href="#" class="mypost">ian</a> posted this on Mar 31st, 2003 at 11:36:08 pm: This is using my post color.
        </td>
    </tr>
    <tr>
        <td class="tr1">
            The first reply.
            <br />
            <br />
        </td>
    </tr>
</table>
<br />
This is what forms will look like.
<table cellpadding="1" cellspacing="0" width="100%" class="replytbl">
    <tr>
        <td align="right" width="120" valign="top">
            username: 
        </td>
        <td align="left">
            <input type="text" name="username" size="20" maxlength="20" value="username" class="textfield" />
        </td>
    <tr>

        <td align="right" width="120" valign="top">
            password: 
        </td>
        <td align="left">
            <input type="password" name="password" size="20" maxlength="20" value="password" class="textfield" />
        </td>
    </tr>
    <tr>
        <td align="right" width="120" valign="top">
            say&nbsp;that&nbsp;shit: 
        </td>
        <td align="left">
            <textarea name="msgbody" rows="7" cols="60" class="textfield">This is the font inside the text box.</textarea>
        </td>
    </tr>
    <tr>
        <td align="right" width="120">
            &nbsp;
        </td>
        <td align="left">
            <input type="submit" name="submit" value="say it" class="button" />
        </td>
    </tr>
</table>
END;

bco_html_footer();

}

function setUserColors($myuserid,$color_string,$stylesheet)
{
	global $cookie_name_user, $cookie_name_color;
    // If the user gets here somehow without logging in, error out.
    if ((!isset($myuserid)) || ($myuserid == "")) {
        bco_error("Userid is not set. You need to log in.");
    } else {
        // This query will update the users color field to the new values.
        $updateColors = "update users set colors='$color_string', stylesheet='$stylesheet' where id=$myuserid";
        if (bco_user_exists($myuserid)) {
            pg_query($updateColors);
            setcookie("$cookie_name_color", "$color_string", time() - 3600, "$GLOBALS[cookie_dir]", "$GLOBALS[cookie_url]"); // unset it
            setcookie("$cookie_name_color", "$color_string", time() + 31536000, "$GLOBALS[cookie_dir]", "$GLOBALS[cookie_url]"); // set it';
        }
    }
} 

?>
Return current item: top serious project