html.php -
index
<?php
/*
"If in the course of a lecture I stated an unsolved problem, the
chances were [John von Neumann would] come to me at the end of the
lecture with the complete solution scribbled on a slip of paper."
-- Miodrag Petkovic, "Famous puzzles of great mathematicians"
*/
/* HTML.PHP - create the $html array for the web templates */
html(); // initializes on first use
function html($var = NULL, $val = NULL) {
static $html = array();
if ($html == array()) {
$hd = config('htmdir');
$td = config('themedir');
$html = configfile($hd.'html.ini');
$html['htmdir'] = $hd;
$html['themedir'] = $hd.$td;
include $hd.'templates.php';
if ($td)
include $hd.$td.'templates.php';
$html['index'] = 'Index';
$html['title'] = config('sitename');
$html['name'] = config('name');
$html['version'] = $GLOBALS['VERSION'];
$section = config('section');
if (isset($html[$section]))
foreach ($html[$section] as $k => $v)
$html[$k] = $v;
$op = getvar('op');
_configmenu($html);
_configlogin($html);
_confignavmenu($html);
if ($op == 'help')
$html['title'] = 'Help - '.$html['title'];
if (!config('allowcomments'))
$html['gotocomments'] = '';
}
if ($var === NULL)
return $html;
if ($val !== NULL)
return $html[$var] = $val;
if (isset($html[$var]))
return $html[$var];
return '';
}
/* htmltempl - this is just an alias, used by DISPLAY.PHP, just some clarity */
// might do more though...
function htmltempl($name) {
$html = html($name);
return $html;
}
/*
Some HTML is dynamic, depending on the "operation". These form that HTML -
menus basically.
*/
function _configmenu(&$html) {
if (!$html['menustr']) // skip menu entirely?
return;
$op = getvar('op'); // what we are doing
$arg = getvar('arg'); // where we are doing it
$id = getvar('id'); // who we are doing it to
$sections = config('sections');
if ($id)
$id = '#'.$id;
$op = getvar('op');
// UGH! special case for now:
if ($op == 'page') {
$html['menu'] = '';
return;
}
$h = @$html['menu'][$op];
if (!$h) {
$s = $html['menustr'];
$a = $html['menuafter'];
$h = html_elements($sections,$s,$arg,$a);
}
$html['menu'] = "<div id='menu'>$h</div>";
}
function _configlogin(&$html) {
$html['loginlink'] = '';
$html['loggedinstr'] = '';
$html['logoutlink'] = '';
if (!config('visitorcode'))
return;
$html['redirect'] = htmlspecialchars($_SERVER['QUERY_STRING']);
$from = cookie_get('from');
if ($from) {
$html['logoutstr'] = str_replace('$1',$from,$html['logoutstr']);
$html['loginout'] = $html['logoutform'];
}
else {
$html['loginout'] = $html['loginform'];
}
}
function _confignavmenu(&$html) {
if (isset($html['navstra']))
return;
if (!$html['navstr'] || !isset($html['navmenu']))
return;
$arg = getvar('arg');
$sections = config('sections');
$s = $html['navstr'];
$html['navstra'] = html_elements($sections,$s,$arg);
}
/*
This is the one "produce HTML based on array data" function. It is used to
eliminate having code in the templates (and therefore the need for anything
like a "template engine").
*/
/* html_elements - form HTML based on array data */
function html_elements($data, $html, $sel = '', $after = '') {
$htm = '';
if (!is_array(current($data))) { // single array
foreach ($data as $key => $value) {
$str = str_replace('$1',$key,$html); // note '$html'
$str = str_replace('$2',$value,$str); // note '$str'
$htm .= $str.PHP_EOL;
}
return $htm;
}
$i = 0;
$c = count($data);
foreach ($data as $key => $value) {
$names = array_keys($value);
if (++$i == $c) $after = '';
$str = str_replace('$key',$key,$html);
$selected = ($key == $sel) ? 'selected' : '';
$str = str_replace('$selected',$selected,$str);
foreach ($names as $n) {
$id = '$'.$n;
$str = str_replace($id,$value[$n],$str);
}
$htm .= $str.$after;
}
return $htm;
}