Making this chunk of code easier to maintain
Posted: Sun May 14, 2006 9:33 pm
How would I, with the last amount of work, make this page easier to maintain? Try not to go to abstract, because I understand that ideally we'd have a Front-Controller and Commands and the whole shish-kabob. Baby steps please.
Code: Select all
<?php
header('Content-Type: text/html; charset=utf-8');
$LOCAL = file_exists('local.txt');
$PAGE = isset($_GET['page']) ? $_GET['page'] : 'Main_Page';
$WEBPATH = $LOCAL ? '/taijiclub' : '';
$LANG = isset($_GET['lang']) && ($_GET['lang'] == 'ch') ? 'ch' : 'en';
$DISPLAY_PAGE = true;
// bug 2 - automatic UI recompilation
$RECOMPILE_SKINS = false;
if (isset($_GET['recompile_skins'])) {
$RECOMPILE_SKINS = true;
$files_to_skin = array(
'phpbb' => 'forums/templates/subSilver/overall_header.tpl'
,'gallery2' => 'gallery2/themes/matrix/templates/local/theme.tpl'
);
function recompile_skins($text, $files) {
foreach ($files as $application => $file) {
$contents = file_get_contents($file);
// make sure dot is set to gobble newlines too with pattern modifier 's'
$contents = preg_replace('/<!-- taijiclub_links -->.*?(<!-- taijiclub_links -->)/s',
'<!-- taijiclub_links -->'.$text.'<!-- taijiclub_links -->',
$contents
);
$contents = str_replace(html_class_active(true), '', $contents);
$contents = str_replace('active-' . $application, 'active', $contents);
$handle = fopen($file, 'w');
fwrite($handle, $contents);
fclose($handle);
}
}
}
// config
require('classes/DB.php');
require('classes/KeyGenerator.php');
require('classes/DomainObject.php');
$wgLocaltimezone="America/New_York";
putenv("TZ=$wgLocaltimezone");
$wgLocalTZoffset = date("Z") / 3600;
$wgDBTablePrefix = '';
if ($LOCAL) {
$wgDB = new DB($credentials);
} else {
$wgDB = new DB($credentials);
}
$STRINGS = parse_ini_file("locales/$LANG.ini",true);
$REQUEST_TYPES = array(
'Main_Page',
'About',
'News',
'Events',
'Digest',
'Taiji_and_I',
'Downloads',
'Registration',
'Contact'
);
if (in_array($PAGE, $REQUEST_TYPES)) {
$PAGENAME = $STRINGS['Menu'][$PAGE];
} else {
header("Location: $WEBPATH/$LANG/Main_Page");
exit;
}
$CONTENTS = (string) @file_get_contents('data/' . $LANG . '/' . basename($PAGE) . '.html');
if ($PAGE == 'News' &&
!empty($_GET['month']) && !empty($_GET['year']) &&
is_numeric($_GET['month']) && is_numeric($_GET['year']) &&
strlen($_GET['month']) == 2 && strlen($_GET['year']) == 4 &&
((int) $_GET['month']) <= 12 && ((int) $_GET['month']) >= 1) {
$CONTENTS = (string) @file_get_contents('data/' . $LANG . '/' .
basename($PAGE) . '/' . basename($_GET['year']) . '/' .
basename($_GET['month']) . '.html');
if ($CONTENTS) $DISPLAY_PAGE = false;
}
if ($PAGE == 'Digest') {
$category= !empty($_GET['category'])&& ctype_alnum($_GET['category'])? $_GET['category']: null;
$topic = !empty($_GET['topic']) && ctype_alnum($_GET['topic']) ? $_GET['topic'] : null;
$article = !empty($_GET['article']) && ctype_alnum($_GET['article']) ? $_GET['article'] : null;
$DISPLAY_PAGE = false;
if ($category !== null && $topic !== null && $article !== null) {
$CONTENTS = (string) @file_get_contents("data/$LANG/Digest/$category/$topic/$article.html");
} elseif ($category !== null && $topic !== null) {
$CONTENTS = (string) @file_get_contents("data/$LANG/Digest/$category/$topic.html");
} elseif ($category !== null) {
$CONTENTS = (string) @file_get_contents("data/$LANG/Digest/$category.html");
} else {
$DISPLAY_PAGE = true;
}
}
$scripts = array('Registration');
if (in_array($PAGE, $scripts)) {
require('classes/scripts/' . $PAGE . '.php');
$class_name = 'Script_' . $PAGE;
$script = new $class_name();
$CONTENTS = $script->execute();
}
if (!$CONTENTS) {
$CONTENTS = (string) @file_get_contents('data/' . $LANG . '/Error.html');
}
//UTF-8 BOM Check
if (substr($CONTENTS,0,3) == chr(239) . chr(187) . chr(191)) {
$CONTENTS = substr($CONTENTS, 3, strlen($CONTENTS) - 3);
}
//reparse gallery2 to point to taijiclub.org
if ($LOCAL) {
$CONTENTS = str_replace('/gallery2/', 'http://www.taijiclub.org/gallery2/', $CONTENTS);
}
//expand macros
$CONTENTS = str_replace('{WEBPATH}', $WEBPATH, $CONTENTS);
function html_class_active($bool) {
if ($bool) return ' class="active"';
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php echo $PAGENAME ?> - Huaxia Taiji Club</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="screen, projection" href="<?php echo $WEBPATH; ?>/screen.css" />
<link rel="stylesheet" type="text/css" media="print" href="<?php echo $WEBPATH; ?>/print.css" />
<!--[if lt IE 7]><style type="text/css">@import "<?php echo $WEBPATH; ?>/IEFixes/common.css";</style><![endif]-->
<link rel="icon" href="<?php echo $WEBPATH; ?>/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="<?php echo $WEBPATH; ?>/favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="translation"><a href="<?php echo $WEBPATH; ?>/<?php
echo ($LANG == 'en') ? 'ch' : 'en' ?>/<?php echo $PAGE; ?>"><?php
echo ($LANG == 'en') ? '中文' : 'English' ?></a></div>
<div id="heading"><a href="<?php echo $WEBPATH; ?>/en/Main_Page" title="English Main Page">Huaxia Taiji Club</a>
<a class="heading_ch" href="<?php echo $WEBPATH; ?>/ch/Main_Page" title="中文主页">华夏太极俱乐部</a></div>
<ul id="menu"<?php if($LANG == 'ch') {?> class="ch"<?php } ?>>
<?php
$links = '';
foreach ($REQUEST_TYPES as $value) {
$active = html_class_active($value == $PAGE);
$links .= "<li><a href=\"$WEBPATH/$LANG/$value\"{$active}>{$STRINGS['Menu'][$value]}</a></li>";
}
$links .=
'<li><a href="http://www.taijiclub.org/gallery2/main.php" class="active-gallery2">Gallery</a></li>
<li><a href="http://www.taijiclub.org/forums/index.php" class="active-phpbb">Forums</a></li>';
echo $links;
if ($RECOMPILE_SKINS) recompile_skins($links, $files_to_skin);
?>
</ul>
<div id="content">
<?php if($DISPLAY_PAGE) { ?><h1 id="title"><?php echo $PAGENAME; ?></h1><?php } ?>
<?php echo $CONTENTS; ?>
</div>
</body>
</html>