Location: PHPKode > projects > WB News > admin/configuration.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 21st April 2007                         #||
||#     Filename: configuration.php                      #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: configuration.php,v 1.1.2.3.2.1 2008/07/14 11:04:04 pmcilwaine Exp $
 */

require_once( "global.php" );
$auth->login_if( !$auth->has_perm( "canconfig" ) );

$myform = "configuration";
if ( !isset($_SESSION["return_page"]) )
{
	$_SESSION["return_page"] = MAIN_SERVER . "/admin/configuration.php";
}

$page = new PageTemplateEngine( BuildPath("admin/main-page.ihtml") );
$tmpl->SetFilename( BuildPath("admin/configuration.ihtml") );

$params = array(
	"entry" => "\t<li>%s</li>\n\t",
	"separator" => FALSE,
	"class_open" => "open",
	"links" => toolbarmenu()
	);
$toolbar = include( INCDIR . "/page-menu.php" );

$page->AddParam( "toolbar", $toolbar );
$page->AddParam( "userinfo", $userinfo );

$formdata =& $tmpl->AddParam( "formdata", array() );
$options =& $tmpl->AddParam( "options", array() );

$sections =& $tmpl->AddParam( "sections", array() );
$configuration =& $tmpl->AddParam( "configuration", array() );

$DB->query( "SELECT * FROM " . TBL_ADMINSECTIONS . " ORDER BY displayorder ASC" );
while ( $DB->next_record() )
{
	$sections[$DB->field("sectionid")] = array(
		"section" => htmlspecialchars( $DB->field("section") )
		);
}

$n_configs = $DB->ListBy( TBL_NEWSCONFIG, NULL, array("*"), "displayorder ASC" );
foreach ( $n_configs as $n_config )
{
	$configuration[$n_config["sectionid"]][] = array(
		"title" => htmlspecialchars( $n_config["title"] ),
		"description" => $n_config["description"],
		"var" => htmlspecialchars( $n_config["var"] ),
		"formtype" => $n_config["fieldtype"]
		);

	$formdata[$n_config["var"]] = htmlspecialchars( $n_config["value"] );

	$optionlist = $n_config["optionlist"];
	if ( "" != $optionlist )
	{
		if ( preg_match( "/[a-zA-Z]:[0-9]+/", $optionlist ) )
		{
			$optionlist = unserialize( $optionlist );
		}

		if ( is_array( $optionlist ) )
		{
			$options[ $n_config["var"] ] = $optionlist;
		}
		else
		{
			list( $type ) = explode( "::", $optionlist );
			switch ( $type )
			{
			case "FUNCTION":
				list( $_, $function ) = explode( "::", $optionlist );
				$options[ $n_config["var"] ] = function_exists($function) ? $function() : array();
				break;
			}
		}
	}
}

if ( $_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["form"]) && $_POST["form"] == $myform )
{

	$err_msg = array();

	switch ( Submit() )
	{
	case "Update_Configuration" :
		
		$err_msg = array();
		
		foreach ( $n_configs as $n_config )
		{
			$value = sanitize_post( $n_config["var"] );
			
			$options = array();
			if ( "" != $n_config["validate"] )
			{
				$validation = explode( ".", $n_config["validate"] );
				if ( count($validation) == 1 )
				{
					switch ( $validation[0] )
					{
					case "{optionlist}":
						
						$optionlist = $n_config["optionlist"];
						if ( "" != $optionlist )
						{
							if ( preg_match( "/[a-zA-Z]:[0-9]+/", $optionlist ) )
							{
								$optionlist = unserialize( $optionlist );
							}

							if ( is_array( $optionlist ) )
							{
								$options[ $n_config["var"] ] = $optionlist;
							}
							else
							{
								list( $type ) = explode( "::", $optionlist );
								switch ( $type )
								{
								case "FUNCTION":
									list( $_, $function ) = explode( "::", $optionlist );
									$options[ $n_config["var"] ] = $function();
									break;
								}
							}
						}

						if ( !array_key_exists( $value, $options[$n_config["var"]] ) )
						{
							$err_msg[$n_config["var"]] = "Invalid Option";
						}
						
						break;
					case "is_numeric":
						if ( !is_numeric( $value ) )
						{
							$err_msg[$n_config["var"]] = "Is not an integer";
						}
						break;
					}
				}
			}
		}

		if ( count($err_msg) > 0 )
		{
			$_SESSION["formdata"] = $_POST;
			$_SESSION["err_msg"][$myform] = $err_msg;
			redirect( make_url() );
			exit;
		}

		$commit = TRUE;
		$DB->BeginTransaction();
		foreach ( $n_configs as $n_config )
		{
			$var = $n_config["var"];
			$value = addslashes(trim( sanitize_post( $n_config["var"] )));

			if ( $n_config["value"] != $value )
			{
				$sql = "UPDATE " . TBL_NEWSCONFIG . " SET \"value\"='$value' WHERE \"var\"='$var'";
				if ( !$DB->query( $sql ) )
				{
					$commit = FALSE;
					break;
				}
			}
		}

		$DB->EndTransaction( $commit );
		
		Message( $commit ? "Updated news configuration" : "Error occurred, didn't update news configuration" );
		redirect( make_url() );
		exit;
	case "Cancel" :
		redirect( $_SESSION["return_page"] );
		exit;
	}
	
}

$formdata["hidden"] = array(
	"form" => $myform
	);
	
$tmpl->AddParam( "buttons", "Update Configuration" );

$pagemenu =& $tmpl->AddParam( "pagemenu", array() );
$page->BindParam( "pagemenu", $tmpl );

$params = array(
	"entry" => "\t<li>%s</li>\n\t",
	"separator" => "\t<li>|</li>\n\t",
	"links" => buildmenu( $theme_info["pagemenu_build"] )
	);
$params = array_merge( $params, $theme_info["pagemenu"] );
$pagemenu = include( INCDIR . "/page-menu.php" );

$page->ParseContent( $tmpl->GetHTML() );
$page->ShowPage();

?>
Return current item: WB News