Location: PHPKode > projects > BoltWire > barn/scripts/markups.php
<?php if (!defined('BOLTWIRE')) exit();

########################################
##  BOLTWIRE MARKUP TABLE             ##
##  Copyright 2010 Dan Vis            ##
##  See license.txt for details       ##
########################################

## MARKUPTABLE
## BELOW IS THE CURRENT MARKUP TABLE USED IN BOLTWIRE. YOU CAN OVERWRITE ANY VALUE IN A LOCAL CONFIG PAGE, OR DELETE OR ADD VALUES. YOU CAN EVEN OVERWRITE THE WHOLE MARKUPS SCRIPT BY INSERTING YOURS IN THE CONFIG DIRECTORY. BELOW ARE MOST FUNCTIONS USED BY THE MARKUP TABLE. MARKUPS ARE ORGANIZED INTO ORDERS > RULES

MarkUp('pre', 'escape', '/\`(.){1}/se', "BOLTescape('$1')");  // `escape char
MarkUp('pre', 'nomarkup', '/\/\=(.*?)\=\//se', "BOLTcharEncode(BOLTstripslashes('$1'))");  // /=no markup=/
MarkUp('pre', 'rules', '/&lt;rules (.+)&gt;(.*)&lt;\/rules&gt;/sieU', "BOLTMrules('$1', '$2')"); // <rules whatever...>
MarkUp('pre', 'boxes', '/&lt;(html|markup|code|box)( .*?)?&gt;(.*?)&lt;\/\1&gt;/sie', "BOLTMcode('$3', '$1', '$2')");  // <markup>
MarkUp('pre', 'comments', '/\/\*(.*?)\*\/(?=[^\/]|$)/se', '');  // / * comments * / 
MarkUp('pre', 'embed', '/&lt;embed ([-_\\w.]+)&gt;/ie', "BOLTMembed('$1')");  // <embed object>
MarkUp('pre', 'entities', '/&(amp;)?amp;([a-z0-9]+|#\\d+|#[x][a-f0-9]+);/i', '&$2;');
Markup('pre', 'snippets', '/(\$\$|\&\#36\;\&\#36\;)([_a-zA-Z0-9]+)(\:[-_a-zA-Z0-9\.]+)?/e', "BOLTMsnippets('$2', '$3', 'pre')"); // $$snippets
if (BOLTconfig('BOLTlanguage') != '') MarkUp('pre', 'translate', '/&gt;&gt;(.+?)&lt;&lt;/me', "BOLTtranslate('$1', '')"); // >>translated text<<
else MarkUp('pre', 'translate', '/&gt;&gt;(.+?)&lt;&lt;/m', '$1'); // >>translated text<<

//MarkUp('vars', 'request', '/\{\?([.a-zA-Z0-9]+)\}/e', "htmlspecialchars(\$_REQUEST['$1'], ENT_NOQUOTES, 'UTF-8')");  // {?var}
MarkUp('vars', 'request', '/\{\?([.a-zA-Z0-9]+)\}/e', "BOLTMrequest('$1')");  // {?var}
MarkUp('vars', 'var', '/\{([^~{}()+=*: ]+)\}/e', "BOLTvars('$1')");  // {var}
MarkUp('vars', 'var2', '/\{([^~{}()+= ]+)\}/e', "BOLTvars('$1')");  // {page:var}
MarkUp('vars', 'var3', '/\{([^~{}()+=* ]+)\}/e', "BOLTvars('$1')");  // {page:var}
MarkUp('vars', 'member',  '/\{\~([^{}+=* ]+)\}/e', "BOLTMmemberVar('$1')");  // {~var}

MarkUp('func', 'counter', '/\{ (\+|\-|\=)?([-+0-9\/.* =]+)? \}/e', "BOLTMfunc('counter $1 \"$2\" output=true')");  // { = }
MarkUp('func', 'vars', '/\{\((.*?)\)\}/e', "BOLTMfunc('$1')");  // {(func params)}
MarkUp('func', 'func', '/\[\((.*?)\)\]/e', "BOLTMfunc('$1')");  // [(func params)]

MarkUp('if', 'if', '/\[if (.*?)\](.*?)\[(if|else)\]/se', "BOLTMif('$1', '$2', '$3')");  // [if]
MarkUp('if', 'if2', '/\[if (.*?)\](.*?)\[if\]/se', "BOLTMif('$1', '$2', 'if')");
MarkUp('if', 'true', '/\$true (.*?)\$(.*?)\$true\$/se', "BOLTMtrue1('$1', '$2')");
MarkUp('if', 'if*', '/\[if\* (.*?)\](.*?)\[(if\*|else\*)\]/se', "BOLTMif('$1', '$2', '$3', '*')");  // [if*]
MarkUp('if', 'if2*', '/\[if\* (.*?)\](.*?)\[if\*\]/se', "BOLTMif('$1', '$2', 'if*', '*')");
MarkUp('if', 'true*', '/\$true\* (.*?)\$(.*?)\$true\*\$/se', "BOLTMtrue1('$1', '$2')");

MarkUp('form', 'func', '/&lt;\((.*?)\)&gt;/e', "BOLTMfunc('$1')");  // <(func params)>
MarkUp('form', 'inputs', '/(\n?)\[(fo\>rm|session|text|check|radio|password|hidden|image|select|option|box|file|button|submit|reset|link)(( {1,2}([-_a-z0-9]+=)?([-_a-z0-9]+\[\]|\"[^\"]*\"|\'[^\']*\'|[^\] ]+))*)? ?\]/ie', "BOLTMinputs('$2', '$3', '$1')");  // [form]

MarkUp('links', 'uploads', '/\[\^(.*?)\^\]/e', "BOLTMuploads('$1')"); // [^ ^] upload
MarkUp('links', 'image', '/(img\:([-_a-zA-Z0-9\/:.]+)\.(gif|jpg|png))/e', "BOLTMuploads('$1')");  // img:file.gif
MarkUp('links', 'links', '/\[\[(.*?)\]\]/e', "BOLTMlinks('$1')");  // [[ ]]
Markup('links', 'urls', '/(http:|ftp:)\/\//ie', "BOLTescape('$1//')");
Markup('links', 'citations', '/ ?\((cite|citations)( ?[^)]+)?\)/e', "BOLTMcitation('$1', '$2')");  // cite(url)

MarkUp('fmt', 'headers', '/^(!{1,6})(.*)/me', "BOLTMheaders('$1', '$2')");
MarkUp('fmt', 'lists', '/(^|\n)((([*#=]+ )(.*?)(\n|$))+)/e', "BOLTMbullets('$2', '$1', '$6')"); // list block
//MarkUp('fmt', 'indent', '/(^|\n)\-(&gt;|>)\n(.+)\n(&lt;|<)\-(\n|$)/sUe', "BOLTMindents('$1', '$5', '$3')"); // -> <- indent (blockquote)
MarkUp('fmt', 'indent1', '/\-(&gt;|>)/', "<blockquote>"); // -> <- indent (blockquote)
MarkUp('fmt', 'indent2', '/(&lt;|<)\-/', "</blockquote>"); // -> <- indent (blockquote)
MarkUp('fmt', 'pre', '/&lt;pre&gt;(.*?)&lt;\/pre&gt;/s', '&lt;nolines&gt;<pre>$1</pre>&lt;/nolines&gt;');  // <pre>
MarkUp('fmt', 'table', '/\[([trch])([1-9]?)( .*?)?\]/ie', "BOLTMtables('$1', '$3', '$2')");  // [t] [r] [c]
//MarkUp('fmt', 'style', '/&lt;(box|div|p|span|a|ul|ol|li)( .*?)?&gt;(.*?)&lt;\/\1&gt;/sie', "BOLTMstyles('$3', '$1', '$2')");
MarkUp('fmt', 'style', '/&lt;(\/)?(box|div|p|span|a|ul|ol|li)( .*?)?&gt;/ie', "BOLTMstyles('$2', '$3', '$1')");

MarkUp('style', 'color', '/&lt;color[= ]?[\'"]?([a-zA-Z0-9\#]{0,9})[\'"]?&gt;(.*?)&lt;\/color&gt;/sie', "BOLTMcode('$2', 'color=$1')"); // <color>
MarkUp('style', 'align', '/&lt;(left|right|center|justify)&gt;(.*?)&lt;\/\1&gt;/s', '<div style=\'text-align:$1;\'>$2</div>'); // l, r, c
MarkUp('style', 'symbol', '/\((\w{1,2})\)/e', "BOLTMsymbol('$1')");
MarkUp('style', 'html', '/&lt;([\/]?)([a-z0-9]+)( ?\/)?&gt;/ie', "BOLTMhtml('$1', '$2', '$3')");  // allowed html
MarkUp('style', 'hr', '/^\-\-\-\-$/m', "\n<hr />\n"); // ---- horizontal line
MarkUp('style', 'fontstyles', '/((\*|\/|&gt;|&lt;|\_|\||\^|@|#)(\2+))(.+?)(\1|$)/me', "BOLTMfontstyles('$1', '$4')");  // Misc shortcuts...
MarkUp('style', 'spacing', '/([ ]{3,}|[\t]+|[\n]{3,})/e', "BOLTMspacing('$1')");  // spaces, tabs, and linebreaks
MarkUp('style', 'nolines', '/(&lt;|<)nolines(&gt;|>)(.+)\1\/nolines\2/Use', "BOLTescape(BOLTstripslashes('$3'))");  // <nolines>

//MarkUp('debug', 'debug', '/debug/e', 'print_r($content)');

## END MARKUPTABLE

## BELOW ARE THE FOLLOWING MARKUP FUNCTIONS LISTED IN ALPHABETICAL ORDER
## BULLETS, FORMS, HEADERS, INPUTS, LINES, LINKS, MEMBERVAR, SESSION, SNIPPETS, SPACING, STYLES, TABLES, UPLOADS, AND VARIOUS SYSTEM FUNCTIONS

function BOLTMbullets($list, $start='', $end='') {
## SCRIPT USED TO GENERATE A LIST OF BULLETS (ORDERED OR UNORDERED). CAN ALSO USE = TO ADD MULTIPLE PARAGRAPHS 
	$list = BOLTstripslashes($list);
	if (substr($list, -1) != "\n") $list = $list . "\n";
	$list = str_replace("\n==\n", "\n== \n", $list); // ??
	$lines = explode("\n", $list);
	foreach($lines as $line) {
		$bullet[] = substr($line, 0, strpos($line, " "));
		$text[] = substr($line, strpos($line, " ") + 1);
		}
	$index = strlen($bullet[0]);
	if (substr($bullet[0], 0, 1) == '*') $type[$index] = 'ul';
	else $type[$index] = 'ol';
	while ($i < $index) {
		$ii = $i + 1;
		$out = $out . "<$type[$index]>";
		$i = $i + 1;
		}
	$out .= "<li>" . $text[0];
	for ($i=1; $i < count($lines) - 1; $i++ ) {
		$x = strlen($bullet[$i]);
		if ($x == $index) {
			if (substr($bullet[$i],0 ,1) == '=') $out .= "\n$text[$i]";
			else $out .= "</li><li>$text[$i]";
			}
		if ($x > $index) {
			while ($index < strlen($bullet[$i])) {
				$index = $index + 1;
				if ($x == $index) {				
					if (substr($bullet[$i], 0, 1) == '*') $type[$index] = 'ul';
					else $type[$index] = 'ol'; 
					$out .= "<$type[$index]><li>$text[$i]";
					}
				else {
					$type[$index] = $type[$index - 1];
					$out .= "<$type[$index]>";
					}
				}
			}
		if ($x < $index) {
			while ($index > strlen($bullet[$i])) {
				$out .= "</li></$type[$index]>";
				$index = $index - 1;
				}
			if (substr($bullet[$i],0 ,1) == '=') $out .= "\n$text[$i]";
			else $out .= "</li><li>$text[$i]";
			}
		}
	while ($index > 0) {
		if ($type[$index] == '') $type[$index] = $type[$index + 1];
		$out .= "</li></$type[$index]>";
		$index = $index - 1;
		}
	$out = str_replace('</div></li></ul>', "</li></ul>\n</div>", $out);
	$out = str_replace("\n", "\n<br />", $out);
	return "$start$out$end";
	}

function BOLTMcitation($index, $reference) {
	global $BOLTcitations;
	if ($index == 'cite') {
		if (isset($BOLTcitations)) {
			foreach($BOLTcitations as $i => $ii) {
				if (BOLTescape($reference, false) == BOLTescape($ii, false)) $key = $i;
				}
			}
		if ($key == false) {
			$key = count($BOLTcitations) + 1;
			$BOLTcitations[$key] = $reference;
			}
		return "<sup class='cite'><a name='citeref-$key'></a><a href='#citenote-$key'>$key</a></sup>";
		}
	if (is_array($BOLTcitations)) {
		foreach ($BOLTcitations as $i => $ii) $out .= "<a name='citenote-$i'></a><a href='#citeref-$i' name='#citenote-$i'>$i.</a> $ii\n";
		return "<div id='citations'>\n$out\n</div>";
		}
	}

function BOLTMcode($text, $type, $attr='') {
## TAKES TEXT AND ESCAPES IT SO NO MARKUP IS PROCESSED. TRIGGERED BY <CODE>TEXT</CODE>
	global $Token, $pageLink;
	$attr = BOLTstripslashes($attr);
	$text = BOLTstripslashes($text);
	if (strpos($text, "\n") === false) $html = 'span';
	else $html = 'div';
	$box = "&lt;$html $attr class='box'&gt;$text&lt;/$html&gt;";
	if ($type == 'box') return $box;
	$attr = BOLTstyleAttrs($html, "$attr class=code", $debug=1);
	$text = BOLTtrimLines($text);
	if ($type == 'code' || $type == 'markup') {
		$code = BOLTescape($text, false);
		$code = str_replace(Array(' &#63; ', ' &#58; ', ' &#44; '), Array(' ? ', ' : ', ' , '), $code);
		$code = BOLTcharEncode($code, 'lines');
		$code = "<$html $attr>$code</$html>";
		if ($type == 'code') return $code;
		}
	if ($type == 'markup') {
		if ($html == 'span') return "$code $box";
		return "$code\n\n$box";
		}
	if ($type == 'html') {
		$out = BOLTdomarkup($text, $pageLink);
		$out = BOLTescape($out, false);
		$out = htmlspecialchars($out, ENT_NOQUOTES);
		$out = str_replace("\n", '<br />', $out);
		return BOLTescape("<$html $attr>$out</$html>");
		}
	if (substr($type, 0, 5) == 'color') return "<$html style='color: " . substr($type, 6) . "'>$text</$html>";
	}

function BOLTMembed($embed) {
## TAKES A SCRIPT (USUALLY JAVASCRIPT, ETC) FROM A CODE.EMBED PAGE, ACTIVATES IT, AND INSERTS INTO PAGE
	global $pageLink, $MarkUpTable;
	$code = BOLTconfig('BOLTcodePages', 'code');
	$out = BOLTloadpage("$code.embed.$embed");
	$out = str_replace(Array(' &#63; ', ' &#58; ', ' &#44; '), Array(' ? ', ' : ', ' , '), $out);
	$rules = $MarkUpTable;
	$out = BOLTescape(preg_replace($rules['pre']['snippets']['pattern'], $rules['pre']['snippets']['replace'], $out), false);
	$out = preg_replace($rules['vars']['request']['pattern'], $rules['vars']['request']['replace'], $out);
	$out = preg_replace($rules['vars']['var']['pattern'], $rules['vars']['var']['replace'], $out);
	$out = preg_replace($rules['vars']['var2']['pattern'], $rules['vars']['var2']['replace'], $out);
	$out = preg_replace($rules['vars']['var3']['pattern'], $rules['vars']['var3']['replace'], $out);
	$out = preg_replace($rules['vars']['member']['pattern'], $rules['vars']['member']['replace'], $out);
	return BOLTescape($out);
	}

function BOLTMfontstyles($type, $text) {
## SIMPLE SCRIPT USED TO GENERATE BOLD, ITALICS, SMALL&LARGE FONTS, UNDERLINES, HIGHLIGHTS, SUPERSCRIPTS, AND THE BOX SHORTCUT
	$text = BOLTstripslashes($text);
	while (preg_match('/((\*|\/|&gt;|&lt;|\_|\||\^|@|#)(\2))(.+?)(\1|$)/', $text) == 1) $text = preg_replace('/((\*|\/|&gt;|&lt;|\_|\||\^|@|#)(\2+))(.+?)(\1|$)/e', 'BOLTMfontstyles("$1", "$4")', $text);
	$type = str_replace(Array('&gt;', '&lt;'), Array('>', '<'), $type);
	$len = strlen($type) - 1;
	switch (substr($type, 0, 1)) {
		case '*' : return "<strong>$text</strong>";
		case '/' : return "<em>$text</em>";
		case '>' :
			$size = 100;
			for ($i=0; $i<$len; $i++) $size = $size * .9;
			return "<font style='font-size: $size%'>$text</font>";
		case '<' : 
			$size = 100 + (10 * $len);
			return "<font style='font-size: $size%'>$text</font>";
		case '_' : return "<u>$text</u>";
		case '|' : return "<span class='highlight'>$text</span>";
		case '^' : return "<sup>$text</sup>";
		case '@' : return "<font style='font-family: monospace'>$text</font>";
		case '#' : return BOLTMcode($text, 'box');
		}
	}

function BOLTMforms($attr, $initial='') {
## A SUBROUTINE USED TO GENERATE THE ACTUAL <FORM> TAGS IN A BOLTWIRE FORM AND SETS SECURITY KEY
	global $formmemory, $pageLink, $scriptURL, $cleanURL, $pageShow, $BOLTfieldKey;
	if (($attr == '') && ($formmemory != '')) {
		$formmemory = '';
		return BOLTescape('</form>');
		}
	$k = '';
	$args = BOLTargs($attr, 'form');
	if (strpos(BOLTloadpage($pageShow), "[file")) $u = "enctype='multipart/form-data' ";
	if (isset($args['action'])) $a = "action='$args[action]' method='post' ";
    else {
		if (isset($cleanURL)) $a = "action='$cleanURL$pageLink' method='post' ";
		else $a = "action='$scriptURL$pageLink' method='post' ";
		}
	$unique = false;
	while ($unique == false) {
		$key = rand(1000,9999);
		if (! isset($_SESSION[$BOLTfieldKey]['KEY'][$pageLink][$key])) $unique = true;
		}
	$key = trim(str_replace(".", "-", $pageLink)) . '-' . $key;
	$formmemory = $key;
	session_name('BOLTsession');
	session_start();
	$_SESSION[$BOLTfieldKey]['KEY'][$pageLink][$key] = 'set';
	$_SESSION[$BOLTfieldKey]['KEY'][$pageLink]['GET'] = $_GET;
	session_write_close();
	return $initial . BOLTescape("<form accept-charset='utf-8' $u$a$attr><input type='hidden' name='boltkey' value='$key' />");
	}

function BOLTMfunc($params) {
## THIS IS THE FUNCTION THAT HANDLES THE VARIOUS FUNCTION MARKUPS. IT ALSO HANDLES MANY OUTPUT OPTIONS INCLUDING: FALSE, SOURCE, NOLINES, CSV, & ESCAPE
	global $BOLTzone;
	$params = str_replace('&amp;&amp;', '&&', BOLTstripslashes($params));
	$function = $params;
	if (strpos($params, " ") !== false) {
		$function = substr($params, 0, strpos($params, " "));
		$params = substr($params, strlen($function));
		$args = BOLTargs($params, "f_$function");
		}
	else $args = Array();
	if (BOLTconfig('BOLTfuncMessages', 'true') == 'false') $args['msg'] = 'false';
	return BOLTfunc($function, $args, strtolower($BOLTzone));
	}

function BOLTMheaders($number, $text) {
## FUNCTION FOR HANDLING H1, H2, ETC HEADERS. TRIGGERED BY !, !!, ETC.
	$text = trim(BOLTstripslashes($text));
	$n = strlen($number);
	return "<h$n>$text</h$n>";
	}

function BOLTMhtml($slash='', $command, $end='') {
## FUNCTION THAT TAKES HTML SAVED ON A PAGE IN AN INACTIVE FORM AND REACTIVATES ALLOWED HTML RULES. SEE BOLTattrs FOR A LIST OF ALLOWED HTML RULES
	$BOLThtml = BOLTattrs('html');
	$command = strtolower($command);
	if (in_array($command, $BOLThtml)) return "<$slash$command>";
	return "&lt;$slash$command$end&gt;";
	}

function BOLTMif($condition, $content, $type, $nest='') {
## SYSTEM FUNCTION USED TO SETUP CONDITIONS FOR LATER PROCESSING. WORKS WITH BOLTMtrue1, BOLTMtrue2 & BOLTMtrue3. ALLOWS IF ELSE OPTION
	$condition = BOLTstripslashes($condition);
	$content = BOLTstripslashes($content);
	if ($type == "else$nest") return "\$true$nest $condition\$$content\$true$nest\$[if$nest ! ($condition)]";
	if ($type == "if$nest") return "\$true$nest $condition\$$content\$true$nest\$";
	}

function BOLTMindents($start, $end, $text) {
	$text = BOLTstripslashes(BOLTtrimLines($text));
	return "$start<blockquote>$text</blockquote>$end";
	}

function BOLTMinputs($type, $attr, $initial='') {
## A COMPLEX AND POWERFUL FUNCTION FOR GENERATING HTML FORMS FROM BOLTWIRE MARKUP. MANY SHORTCUTS AND FEATURES BUILT INTO THE SCRIPT. MORE DOCUMENTATION NEEDED
	global $pagesPath, $pageLink, $scriptUrl, $BOLTformJs, $scriptURL, $cleanURL, $formmemory, $selectDefault, $labelcounter, $BOLTabortKey, $BOLTfieldKey;
	if ($type == 'fo>rm') $type = 'form';
	$attr = BOLTurl2utf(str_replace('=]', ']', $attr));
	$attr = BOLTstripslashes($attr);
	if ($type == 'box' || $type == 'text') {
		if (strpos($attr, '"') !== false || strpos($attr, "'") !== false) {
			if ((strpos($attr, '"') !== false) && ((strpos($attr, '"') < strpos($attr, "'")) || (strpos($attr, "'") === false))) {
				$r1 = strpos($attr, '"');
				$r2 = strrpos($attr, '"') + 1;
				$str1 = substr($attr, 0, $r1);
				$str2 = str_replace('"', '&#34;', substr($attr, $r1 + 1, $r2 - $r1 - 2));
				$str3 = substr($attr, $r2);
				$attr = "$str1\"$str2\"$str3";
				}
			else {
				$r1 = strpos($attr, "'");
				$r2 = strrpos($attr, "'") + 1;
				$str1 = substr($attr, 0, $r1);
				$str2 = str_replace("'", '&#39;', substr($attr, $r1 + 1, $r2 - $r1 - 2));
				$str3 = substr($attr, $r2);
				$attr = "$str1'$str2'$str3";
				}
			}
		}
	if ($type == 'session') return BOLTMsession($attr);
	if ($type == 'select') {
		if ($attr == '') return BOLTescape('</select>');
		if (strpos($attr, 'jump') !== false) $attr = str_replace('jump', 'js=submit name=nextpage', $attr);
		}
	if (($type == 'box') && ($attr == '')) return BOLTescape('</textarea>') . ' ';
//	if (($type == 'check') || ($type == 'radio')) $attr = str_replace('checked', 'checked=true', $attr);
	if ($type == 'check') $type = 'checkbox';

// create arg array and optimize for various options
	$args = BOLTargs($attr, $type);
	$attr = '';
	if ($type == 'select') $selectDefault = $args['default'];
	if (isset($args['path'])) $path = $args['path'] . '/';
	if ($type == 'option') {
		if (! isset($args['value'])) $args['value'] = $args[1];
		if ($selectDefault == $args['value']) $attr = "selected ";
		if (isset($args['label'])) $label = $args['label'];
		elseif (isset($args[2])) $label = $args[2];
		else $label = $args[1];
		}
	if (($type == 'submit') || ($type == 'reset') || ($type == 'button')) {
		if (! isset($args['name'])) $args['name'] = $type;
		if (! isset($args['value'])) $args['value'] = $args[1];
		if (! isset($args['value'])) $args['value'] = strtoupper($type);
		$args['value'] = BOLTtranslate($args['value']);
        }
	else {
		if (! isset($args['name']) && isset($args[1])) {
			$args['name'] = $args[1];
			unset($args[1]);
			};
		if ((! isset($args['value'])) && (isset($args[2]))) {
			$args['value'] = $args[2];
			unset($args[2]);
			}
		}
	if (! isset($args['value'])) $args['value'] = '';
	if (isset($args['label'])) {
		if ($type == 'checkbox' || $type == 'radio') {
			if (isset($args['id'])) $id = $args['id'];
			else {
				$labelcounter = $labelcounter + 1;
				$id = "label$labelcounter";
				$args['id'] = $id;
				}
			$label = " <label for='$id'>" . $args['label'] . '</label> ';
			}
		}
	if ($args['checked'] == '') unset($args['checked']);
	if ($type == 'radio' || $type == 'checkbox') {
		if ($args['checked'] != '') {
			if (substr($args['name'], -2) == "[]") {
				if (strpos(',' . $args['checked'] . ',', $args['value']) !== false || $args['checked'] == 'true') $args['checked'] = 'true';
				else unset($args['checked']);
				}
			elseif ($args['checked'] == $args['value'] || $args['checked'] == 'true') $args['checked'] = 'true';
			else unset($args['checked']);
			}
		}
	if ($type == 'box') {
//		if (isset($args['gui']) && function_exists(BOLTgui)) {
//			$gui = BOLTgui($args['gui'], $args['name']);
//			$args['id'] = 'BOLTgui';

		if (isset($args['gui'])) {
			if (BOLTexists("code.embed.gui.$args[gui]")) $guipage = "gui.$page";
			else $guipage = "gui";
			$gui = BOLTMembed($guipage) . BOLTMembed('guiscript');
			$args['id'] = 'BOLTgui';
			}
		if (isset($args['value'])) {
			$value = BOLTvars($args['value']);
			}
		}
	if (isset($args['source'])) $args['value'] = BOLTFsource(BOLTargs("$args[source] escape=false"));
	if ($BOLTabortKey) {
		if (strpos('session,password,button,reset,submit', $type) === false && isset($_POST[$args['name']])) $args['value'] = $_POST[$args['name']];
		}
	$args['value'] = str_replace('&nbsp;', '&amp;nbsp;', $args['value']);
	if (isset($args['js'])) {
		if ($type == 'select' || $type == 'text' || $type == 'box') $BOLTformJs['submit'] = "onChange='this.form.submit();'";
		$attr = " " . $BOLTformJs[$args['js']] . " $attr" ;
		}
	foreach($args as $f => $v) {
		if (is_array($v)) continue;
		if (! in_array($f, BOLTattrs($type))) continue;
		if (strpos($v, "'") !== false) $attr .= " $f=\"$v\" ";
		else $attr .= " $f='$v' ";
		}
	if ($type == 'form') {
		return BOLTMforms($attr, $initial);
		}
	$key = ''; // page-link-key
	$k = ''; // key
	if (strpos('option,checkbox,radio', $type) === false) {
		if ((isset($args['name'])) && ($args['name'] != '')) {
			$session = "'$args[name]' '$args[value]'";
			BOLTMsession($session);
			}
		}

// prepare final html output and return to page
	switch($type) {
		case 'select' :
			return $initial . BOLTescape("<select $attr>");
		case 'option' :
			if (isset($label)) return BOLTescape("<option $attr>$label</option>");
			else return BOLTescape("<option $attr />");
		case 'image': 
			if (substr($args['src'], 0, 4) == 'http') return $initial . BOLTescape("<input type='$type' $attr />") . ' ';
			if ($path == '') $path = BOLTconfig('BOLTfilesDir', 'files');
			$attr = str_replace("src='", "src='$path/", $attr);
			return $initial . BOLTescape("<input type='$type' $attr />") . ' ';
		case 'box' :
			return $initial . BOLTescape("$gui<textarea $attr>") . BOLTescape($value);
		case 'link' :
			if (isset($args['linkpage'])) $args['name'] = $args['linkpage'];
			if (isset($args['label'])) $args['value'] = $args['label'];
			return $initial . BOLTescape("<a href='$scriptUrl?p=$args[name]' onclick='document.$args[form].submit()'>" . BOLTtranslate($args[value]) . "</a>") . ' ';
		case 'checkbox' :
			return $initial . BOLTescape("<input type='$type' $attr />$label") . ' ';
		case 'radio' : 
			return $initial . BOLTescape("<input type='$type' $attr />$label") . ' ';
		default : 
			return $initial . BOLTescape("<input type='$type' $attr />") . ' ';
		}
	return;
	}

function BOLTMlinks($x) {
## FAIRLY COMPLEX MARKUP RULE HANDLING LINKS. MORE DOCUMENTATION NEEDED.
	global $scriptURL, $cleanURL, $pageLink, $BOLTmissingMark, $BOLTlinkJs;
	$x = BOLTstripslashes($x);
	if ($BOLTmissingMark == '') $BOLTmissingMark = "<span class='marks'><sup>?</sup></span>";
	if (BOLTconfig('BOLTmissingMark', 'true') == 'true') $missing = $BOLTmissingMark;
	if (substr($x, 0, 1) == '?') {
		$x = substr($x, 1);
		$missing = '';
		}
	if (substr($x, 0, 1) == '#') {
		list($link,$label) = explode('|',$x,2);
		$link = substr($link, 1);
		if ($label == '+') {
			if ($link == BOLTutf2url($link)) $label = ucfirst($link);
			else $label = $link;
			}
//		else $label = BOLTdomarkupTable($label);
		$link = BOLTutf2url($link);
		return BOLTescape("<a name='$link'>$label</a>");
		}
	list($link,$label,$attrs) = explode('|',$x,3);
	$link = trim($link);
	$label = trim($label);
	if (substr($link, 0, 1) == '~') {
		$login = BOLTconfig('BOLTloginPages', 'login');
		$loginLabel = BOLTconfig('BOLTloginLabel', 'member');
		$loginLink = BOLTconfig('BOLTloginLink', $login);
		$link = substr($link, 1);
		if ($label == '') $label = BOLTvars("$loginLink.$link:$loginLabel");
		$link = trim("$loginLink.$link");
		}
	if (substr($link, 0, 4) == 'tag.') {
		if (BOLTconfig('BOLTtags') == 'true') return; 
		}
	if ($label == '') {
		if (BOLTconfig('BOLTlinkTitles') == 'true') $label = '+';
		else $label = $link;
		}
	$attrs = trim($attrs);
	$args = BOLTargs($attrs, 'links');
	if (is_array($args)) {
		foreach($args as $f => $v) {
			if (! in_array($f, BOLTattrs('a'))) continue;
			if (ereg('/[\'#0-9a-zA-Z]+/', $v)) continue;
			$attr .= " $f=\"" . $v . "\" "; 
			}
		}
	$attr = str_replace('xmllang', 'xml:lang', $attr);
	if (isset($args['js'])) $attr = " " . $BOLTlinkJs[$args['js']] . " $attr" ;
	if (strtolower(substr($link, 0, 3)) == "www") {
		if ($link != strtolower($x) && $label == $x) $label = $link;
		$link = "http://$link";
		}
	if (strpos($link, '://') == false) {
		if (strpos($link, ":")) {
			$interwiki = substr($link, 0, strpos($link, ":"));
			if ("$interwiki:" == $label) $label = substr($label, 0, -1);
			$interwiki = BOLTinfoVar(BOLTconfig('BOLTinterwiki', 'site.interwiki'), $interwiki);
			if ($interwiki == '') return BOLTtranslate(BOLTinfoVar('site.messages', 'interwiki_missing', 'Interwiki entry not found.'));
			$link = $interwiki . substr($link, strpos($link, ":") + 1);
			}
		elseif (strpos($link, '@')) $link = "mailto:$link";
		else {
			$link = BOLTpageshortcuts($link);
			if (!preg_match('/^[-_a-zA-Z0-9~\.%]+(\#|\&|$)/', $link)) return BOLTtranslate(BOLTinfoVar('site.messages', 'invalid_link', 'Invalid link.'));
			if (! BOLTexists($link) && strpos($link, '&') === false) $missingPage = $missing;
			if ($label == '+') {
				if (strpos($link, '&') !== false) $label = BOLTvars(substr($link, 0, strpos($link, '&')) . ':title');
				elseif (strpos($link, '#') !== false) $label = ucfirst(substr($link, strpos($link, '#') + 1));
				else $label = BOLTvars("$link:title");
				}
			if (isset($cleanURL)) $link = $cleanURL . str_replace('.', '/', $link);
			else $link = $scriptURL . $link;
			}
			if ($attr == '') $defaultAttr =  BOLTconfig('BOLTlinkIntAttr', '');
		}
	else {
		if ($attr == '') $defaultAttr =  BOLTconfig('BOLTlinkExtAttr', " rel='nofollow'");
		if ($label == '+' || $label == $link) $label = BOLTescape($link);
		}
	if ($label == '=') $label = $link;
	$label = BOLTurl2utf($label);
//	$label = BOLTdomarkupTable($label);
	$link = str_replace('&', '&amp;', $link);
	if (function_exists('myBOLTMlinks')) return myBOLTMlinks($link, $defaultAttr, $attr, $label, $missingPage);
	return BOLTescape('<a href="'.$link.'" '.$defaultAttr.$attr.'>') . BOLTtranslate($label) . BOLTescape("$missingPage</a>");
	}

function BOLTMmemberVar($field) {
## A SHORTCUT FUNCTION THAT HANDLES MARKUP LIKE {~FIELD} OR {~MEMBER:FIELD} AND RETRIEVES DATA VALUES
	global $BOLTid;
	$BOLTloginPages = BOLTconfig('BOLTloginPages', 'login');
	if (strpos($field, ":") === false) return BOLTvars("$BOLTloginPages.$BOLTid:$field");
	else return BOLTvars("$BOLTloginPages.$field");
	}

function BOLTMrequest($field) {
	global $BOLTsession;
	$out = $_REQUEST[$field];
	if ($out == '') $out = $BOLTsession['POST'][$field];
	if (is_array($out)) $out = implode(",", $out);
	return htmlspecialchars($out, ENT_NOQUOTES, 'UTF-8');
	}

function BOLTMrules($rules, $content) {
	global $pageLink, $BOLTrules, $BOLTaltMarkUpTables, $MarkUpTable, $BOLTzone;
	$BOLTrules = '';
	if (is_array($BOLTaltMarkUpTables[$rules]) == true) $BOLTrules = $BOLTaltMarkUpTables[$rules];
	else {
		$rules = ",$rules,";
		$myMarkUpTable = $MarkUpTable;
		foreach($myMarkUpTable as $i => $order) {
			foreach ($order as $ii => $iii) {
				if (strpos($rules, ",$i,") !== false || strpos($rules, ",$i:$ii,") !== false) {
					$BOLTrules[$i][$ii] = $MarkUpTable[$i][$ii];
					}
				}
			}
		}
	$out = BOLTdomarkup($content, $pageLink, strtolower($BOLTzone), '', false);
	$BOLTrules = '';
	return BOLTescape($out);
	}

function BOLTMsession($attr) {
## AN IMPORTANT PART OF THE FORM GENERATION SECURITY SYSTEM. ALL SESSION VALUES ARE STORED TO A SESSION VARIABLE AND COMPARED WITH FORM SUBMISSION TO PROPERLY HIDE FORM INFORMATION FROM BROWSERS AND AUTHENTICATE SAFELY
	global $pageLink, $formmemory, $BOLTinput;
	$args = BOLTargs($attr);
	if (isset($args['name'])) $f = $args['name'];
	else $f = $args[1];
	if (isset($args['value'])) $v = $args['value'];
	else $v = $args[2]; // = trim(substr($attr, strlen($f) + 2)); 
	if (preg_match('/\~\~([0-9]+)\~\~/', $v) == 1) $v = BOLTescape($v, false);
	if (isset($args['source'])) $v = BOLTFsource(BOLTargs("$args[source] escape=false"));
	if (isset($args['template'])) {
		$templatePages = BOLTconfig('BOLTtemplatePages', 'template');
		$v = BOLTloadpage("$templatePages.$args[template]");
		}
//	$f = BOLTstripslashes($f);
//	$v = BOLTstripslashes($v);
	$v = str_replace(Array('&#34;', '&#39;'), Array('"', "'"), $v);
	$key = $formmemory;
	session_start();
	$BOLTinput[$key][$f] = $v;
	session_write_close();
	return;
	}

function BOLTMsnippets($var, $page='', $rules='') {
## TAKES SNIPPETS AND PREPARES FOR DISPLAY IN PAGE OR SKIN
	$out = BOLTdomarkup(BOLTsnippets($var, $page), '', 'SNIPPETS', $rules);
	if ('<p>' . substr($out, 3, -4) . '</p>' == $out) $out = substr($out, 3, -4);		
	return $out;
	}

function BOLTMspacing($space) {
	$key = substr($space, 0, 1);
	if ($key == ' ') return str_replace(' ', '&nbsp;', $space);
	if ($key == "\t") return str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $space);
	$space = substr($space, 2);
	return "\n\n" . str_replace("\n", "<br />\n", $space) . "\n";
	}

function BOLTMstyles($type, $attr='', $end='') {
## A FUNCTION USED TO HANDLE DIV'S, P'S, AND SPAN'S (PLUS A UL OL & LI), ESSENTIALLY MARKUP WITH POSSIBLE PARAMETERS, SUCH AS STYLING.
	if ($end == '/') return "</$type>";
	if ($attr != '') $attr = BOLTescape(BOLTstyleAttrs($type, $attr));
	return "<$type$attr>";
	}

function BOLTMsymbol($symbol) {
## A SIMPLE BUT CONFIGURABLE SYMBOL GENERATOR WHICH CONVERTS (c) TO COPYRIGHT SYMBOL FOR EXAMPLE. ALSO RECOGNIZES (r), (t) or (tm). CAN DEFINE ADDITIONAL SYMBOLS IN A CONFIG FILE.
	global $BOLTsymbolIndex;
	BOLTdefault($BOLTsymbolIndex['c'], '&copy;');
	BOLTdefault($BOLTsymbolIndex['r'], '&reg;');
	BOLTdefault($BOLTsymbolIndex['t'], '&trade;');
	BOLTdefault($BOLTsymbolIndex['tm'], '&trade;');
	if (isset($BOLTsymbolIndex[$symbol])) return $BOLTsymbolIndex[$symbol];
	return "($symbol)";
	}

function BOLTMtables($type, $attr, $level='0') {
## HERE'S THE MARKUP PROCESSOR FOR BOLTWIRE'S TABLES. FAIRLY COMPLEX CODE ALLOWS POWERFUL BUT SIMPLE MARKUP
	global $tablememory, $stripememory, $BOLTtableInputs, $BOLTtableOutputs;
	if ($attr != '') {
		$attr = BOLTescape($attr, false);
		if ($type == 't' && strpos($attr, 'class=stripe') !== false) $stripememory[$level] = 1;
		if (! isset($BOLTtableInputs)) $BOLTtableInputs = array(' center', ' left', ' right', ' top', ' middle', ' bottom', ' color=', ' padding=', ' spacing=', ' rows=', ' cols=', ' image=');
		if (! isset($BOLTtableOutputs)) $BOLTtableOutputs  = array(' align=center', ' align=left', ' align=right', ' valign=top', ' valign=middle', ' valign=bottom', ' bgcolor=', ' cellpadding=', ' cellspacing=', ' rowspan=', ' colspan=', ' background=');
		$attr = str_replace($BOLTtableInputs, $BOLTtableOutputs, $attr);
		$args = BOLTargs($attr, $type);
		if (isset($args['height'])) $args['style'] .= " height: $args[height];";
		$attr = '';
		foreach($args as $f => $v) {
			if (is_array($v)) continue;
			if (! in_array($f, BOLTattrs($type))) continue;
//			if (ereg('/[\'#0-9a-zA-Z]+/', $v)) continue;
			$attr .= " $f='$v' ";
			}
		$attr = BOLTescape($attr);
		}
	switch($type) {
		case 't' :
			if ($tablememory["$level-t"] != 'on') {
				$tablememory["$level-t"] = 'on';
				return "\n<table $attr>";
				}
			else {
				$tablememory["$level-t"] = 'off';
				$tablememory["$level-r"] = 'off';
				$stripememory[$level] = 0;
				if ($tablememory["$level-c"] == 'on') {		
					$tablememory["$level-c"] = 'off';
					return "</td></tr>\n</table>\n";
					}
				if ($tablememory["$level-h"] == 'on') {		
					$tablememory["$level-h"] = 'off';
					return "</th></tr>\n</table>\n";
					}
				}
		case 'r' :
			if ($stripememory[$level] > 0) {
				$stripememory[$level] = $stripememory[$level] + 1;
				if (is_int($stripememory[$level] / 2)) $stripe = " class='striperow' ";
				}
			if ($tablememory["$level-r"] != 'on') {
				$tablememory["$level-r"] = 'on';
				return "<tr $attr$stripe>";
				}
			else {
				if ($tablememory["$level-c"] == 'on') {
					$tablememory["$level-c"] = 'off';
					return "</td></tr>\n<tr $attr$stripe>";
					}
				if ($tablememory["$level-h"] == 'on') {
					$tablememory["$level-h"] = 'off';
					return "</th></tr>\n<tr $attr$stripe>";
					}
				}
		case 'c' :
			if ($tablememory["$level-c"] != 'on') {
				if ($tablememory["$level-h"] == 'on') {
					$tablememory["$level-h"] = 'off';
					$tablememory["$level-c"] = 'on';
					return "</th><td $attr>";
					}
				$tablememory["$level-c"] = 'on';
				return "<td $attr>";
				}
			else return "</td><td $attr>";
		case 'h' :
			if ($tablememory["$level-h"] != 'on') {
				if ($tablememory["$level-c"] == 'on') {
					$tablememory["$level-c"] = 'off';
					$tablememory["$level-h"] = 'on';
					return "</td><th $attr>";
					}
				$tablememory["$level-h"] = 'on';
				return "<th $attr>";
				}
			else return "</th><th $attr>";
		}
	}

function BOLTMtrue1($condition, $content) {
## SYSTEM FUNCTION USED TO SETUP CONDITIONS FOR LATER PROCESSING. WORKS WITH BOLTMtrue1, BOLTMtrue2 & BOLTMtrue3. ALLOWS IF ELSE OPTION
	$content = BOLTstripslashes($content);
	$condition = str_replace('&amp;&amp;', '&&', $condition);
	$condition = BOLTstripslashes("($condition)");
	while (strpos($condition, "(") !== false) {
		$condition = preg_replace('/\(([^()]*)?\)/e', 'BOLTMtrue2("$1")', $condition);
		}
	if ($condition === 'true') return $content;
	return;
	}

function BOLTMtrue2($condition) {
## SYSTEM FUNCTION USED TO SETUP CONDITIONS FOR LATER PROCESSING. WORKS WITH BOLTMtrue1, BOLTMtrue2 & BOLTMtrue3. ALLOWS IF ELSE OPTION
	if (strpos($condition, '&&')) {
		$x = explode('&&', $condition);
		foreach ($x as $i => $ii) $x[$i] = BOLTMtrue3($ii); 
		if (in_array('false', $x)) return 'false';
		else return 'true';
		}
	elseif (strpos($condition, '||')) {
		$x = explode('||', $condition);
		foreach ($x as $i => $ii) $x[$i] = BOLTMtrue3($ii);
		if (in_array('true', $x)) return 'true';
		else return 'false';
		}
	return BOLTMtrue3($condition);
	}

function BOLTMtrue3($condition) {
## SYSTEM FUNCTION USED TO SETUP CONDITIONS FOR LATER PROCESSING. WORKS WITH BOLTMtrue1, BOLTMtrue2 & BOLTMtrue3. ALLOWS IF ELSE OPTION
	global $BOLTtoolmap;
	$r = Array('(', ')', '! !');
	$condition = trim(str_replace($r, '', $condition));
	$condition = trim($condition);
	$tf = true;
	if (substr($condition, 0, 1) == "!") {
		$tf = false;
		$condition = trim(substr($condition, 1));
		}
	if (strpos($condition, " ") !== false) {
		$params = substr($condition, strpos($condition, " ") + 1);
		$condition = substr($condition, 0, strpos($condition, " "));
		$args = BOLTargs($params, "c_$condition");
		}
	else $args = '';
	if ($BOLTtoolmap['c'][$condition] != '') $condition = $BOLTtoolmap['c'][$condition];
	else $condition = BOLTtranslate($condition, '', 1);
	if (function_exists("BOLTC$condition")) {
		$BOLTcondition = "BOLTC$condition";
		$value = $BOLTcondition($args);
		if ($value == $tf) return 'true';
		}
	return 'false';
	}

function BOLTMuploads($file) {
## HERE IS THE MARKUP PROCESSOR FOR FILE UPLOADS. CREATE MARKUP FOR AN IMAGE AND IF DOESN'T EXIST PUTS A LINK TO AN UPLOAD FORM. IF EXISTS, DISPLAYS IMAGE OR SHOWS DOWNLOAD LINK
	global $scriptURL, $fieldURL, $pageLink, $BOLTid, $BOLTmissingMark, $BOLTreloadMark, $imgURL;
	$args = BOLTargs($file, 'uploads');
	if (isset($args['file'])) $file = $args['file'];
	else $file = $args[1];
	if (substr($file, 0, 4) == 'img:') $file = substr($file, 4);
	if (strpos('jpg,gif,png', substr(strrchr($file, "."), 1)) !== false) $type = 'img';
	else $type = 'a';
	foreach($args as $f => $v) {
		if (! in_array($f, BOLTattrs($type))) continue;
		$attr .= "$f='$v' ";
		}
	if (strpos($file, '://') !== false) {
		if ($type == 'img')	return BOLTescape("<img src='$file' $attr />");
		return;
		}
	if (! isset($BOLTmissingMark)) $BOLTmissingMark = "<span class='marks'><sup>?</sup></span>";
	if (BOLTconfig('BOLTmissingMark', 'true') == 'true') $missing = $BOLTmissingMark;
	if (substr($file, 0, 1) == "?") {
		$file = substr($file, 1);
		unset($BOLTmissingMark);
		}
	$BOLTfilesDir = BOLTconfig('BOLTfilesDir', 'files');
	if (BOLTconfig('BOLTuploadCase') != 'true') $file = strtolower($file);	
	if (BOLTfilter($file, '/^[-_a-zA-Z0-9.]+$/') == '') return BOLTtranslate(BOLTinfoVar('site.messages', 'invalid_file', 'Invalid file name.'));
	$dir = '';
	if (isset($args['dir'])) {
		$dir = $args['dir'];
		if (BOLTfilter($dir, '/^[-_a-zA-Z0-9\/]+$/') == '') return BOLTtranslate(BOLTinfoVar('site.messages', 'invalid_dir', 'Unauthorized upload directory.'));
		$location = "$BOLTfilesDir/$dir/$file";
		$dirLink = "&dir=$dir";
		}
	else $location = "$BOLTfilesDir/$file";
	if (isset($args['label'])) $label = $args['label'];
	else $label = $file;
	if (! file_exists($location)) $location = strtolower($location);
	if (! file_exists($location)) $location = "$imgURL/$file";
//	if (strpos($location, '://') !== false) $urlexists = @fopen("$imgPath/$file", 'r');
	if (@fopen($location, 'rb')) {
//if (true) {
		if (substr($location, 0, strlen($imgURL)) != $imgURL) $location = $fieldURL . $location;
		if (! BOLTauth($file, $BOLTid, 'files')) return 'File access blocked';
		if ($args['reload'] == 'true' || $_GET['reload'] == 'true') {
				if (! isset($BOLTreloadMark)) $BOLTreloadMark = "<span class='marks'><sup>^</sup></span>";
				if (BOLTconfig('BOLTreloadMark', 'true') == 'true') $reload = BOLTescape("<a href='$scriptURL$pageLink$fileLink&action=upload&file=$file$dirLink' rel='nofollow'>$BOLTreloadMark</a>");
				}
		if (strpos('jpg,gif,png', substr(strrchr($file, "."), 1)) !== false) {
			if (!isset($args['alt'])) $attr .= "alt='' ";
			if (file_exists($location)) {
				if (strpos($attr, 'height=') === false && strpos($attr, 'width=') === false) {
					$size = getimagesize($location);
					$attr .= $size[3];
					}
				}
			return BOLTescape("<img src='$location' $attr />$reload");
			}
		else return BOLTescape("<a href='$location' $attr>") . "$label</a>$reload";
		}
	else return BOLTescape("<a href='$scriptURL$pageLink$fileLink&action=upload&file=$file$dirLink'>") . "$label$missing</a>";
	}
Return current item: BoltWire