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

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

## THE FIRST FOUR FUNCTIONS ARE USED BY THE MARKUP TABLE TO HANDLE CONDITIONALS
## THE PROCESSING IS BROKEN DOWN INTO TWO PARTS--THIS FIRST ONE HANDLES IF/ELSE/END
## THIS NEXT THREE EVALUATES THE CONDITIONS INTERPRETING NESTED BOOLEAN OPERATORS
## THE REMAINING FUNCTIONS ARE AVAILABLE CONDITIONS, IN ALPHABETICAL ORDER
## ADMIN, AUTH, BETWEEN, EDITOR, EQUAL, EXISTS, FALSE, INGROUP, INLIST, INPAGE
## INSOURCE, LESS, LOGIN, MORE, NEW, NUMBER, SET, STAMP, TIME, TRUE

function BOLTCadmin($args='') {
## CONDITION TO TEST IF CURRENT USER IS AN ADMIN. EQUIVALENT TO [IF INGROUP ADMIN]. CAN ONLY CHECK CURRENT USER.
	return BOLTingroup('admin', $args['id']);
	}

function BOLTCauth($args='') {
## ALLOWS YOU TO CHECK ANY KIND OF AUTHORIZATION POSSIBLE IN BOLTWIRE. PARAMETERS INCLUDE TYPE(OR POS1)=VIEW,WRITE,ETC. REQUIRED OR RETURNS FALSE. AND CHECK(OR POS2)=PAGENAME (USUALLY), DEFAULTS TO CURRENT PAGE. AND THIRD, FIND(OR POS3)=ID (USUALLY), DEFAULTS TO CURRENT USER. NOTE THIS CONDITION CAN BE USED TO CHECK ANY KIND OF AUTHORIZATION!
	if (! is_array($args)) return false;
	global $BOLTid, $pageLink, $BOLTadmin;
	$type = $args[1];
	if (isset($args['type'])) $type = $args['type'];
	if ($type == '') return false;
	if (isset($args['check'])) $check = $args['check'];
	elseif (isset($args[2])) $check = $args[2];
	else $check = $pageLink;
	if (isset($arg['find'])) $find = $args['find'];
	elseif (isset($arg[3])) $find = $args[3];
	else $find = $BOLTid;
	return BOLTauth($check, $find, $type);
	}	

function BOLTCbetween($args='') {
## REQUIRES THREE VALUES (POS1, POS2, & POS3) AND CHECKS IF 2 IS BETWEEN 1 AND 3. IF ALL THREE ARE NUMERIC, CALCULATES MATHEMATICALLY, ELSE CALCULATES ALPHABETICALLY.
	if (!isset($args[3])) return false;
	$test = Array($args[1], $args[2], $args[3]);
	$sort = $test;
	sort($sort);
	if ($sort == $test) return true;
	$rsort = $test;
	rsort($rsort);
	if ($rsort == $test) return true;
	return false;
	}

function BOLTCeditor($args='') {
## CONDITION TO TEST IF CURRENT USER IS AN EDITOR. EQUIVALENT TO [IF INGROUP EDITOR]. CAN ONLY CHECK CURRENT USER.
	return BOLTingroup('editor', $args['id']);
	}

function BOLTCequal($args='') {
## CHECKS TO SEE IF TWO VALUES ARE EQUAL. IF PARAMETER CASE=FALSE, RETURNS CASE INSENSITIVE RESULT. IF EITHER VALUES IS NULL, RETURNS FALSE
	if (!isset($args[2])) return false;
	$test0 = BOLTurl2utf(trim($args[1]));
	$test1 = BOLTurl2utf(trim($args[2]));
	if ($args['case'] == 'false') return (strtolower($test0) == strtolower($test1));
	return ($test0 == $test1);
	}

function BOLTCexists($args='') {
## CHECKS TO SEE IF A PAGE EXISTS. CAN TAKE PAGE SHORTCUTS. USE PARAMETER DIR TO SCAN OTHER DIRECTORIES RELATIVE TO PAGES FOLDER (I THINK). SPECIAL CASES DIR=PLUGINS AND DIR=FILES (OR FILES/SUBDIR)
	if (! is_array($args)) return false;
	global $pagesPath, $pluginPath;
	$page = $args[1];
	if (isset($args['dir'])) {
		$dir = $args['dir'] . '/';
		if ($dir == 'plugins/') return file_exists("$pluginPath/$page");
	 	if (substr($dir, 0, 6) == 'files/') return file_exists("$dir$page");
		}
	return BOLTexists(BOLTpageshortcuts($page, $dir));
	}

function BOLTCfalse($args='') {
## RETURNS FALSE ALWAYS, GENERALLY USED FOR TESTING
	return false;
	}

function BOLTCingroup($args='') {
## CHECKS IF CURRENT USER IS IN A GROUP. PARAMETER GROUP(POS1) IS GROUP NAME. CAN ONLY CHECK CURRENT USER. I SUPPOSE WE SHOULD BE ABLE TO SPECIFY A USER... CAN DO [IF INLIST ID GROUP.NAME] EASILY ENOUGH HOWEVER
//	if (isset($args[2])) return BOLTCinlist(Array($args[2], 'group.'.$args[1])); // Test for specified user...
	if (! is_array($args)) return false;
	if (isset($args['group'])) $group = $args['group'];
	else $group = $args[1];
	return BOLTingroup($group, $args['id']);
	}

function BOLTCinlist($args='') {
## CHECKS TO SEE IF AN ITEM (ITEM OR POS1) IS IN CSV LIST (LIST OR POS2). IF LIST IS A PAGENAME, THE PAGE IS LOADED AND CONVERTED TO A CSV LIST. A BIT QUIRKY AS ITEM DEFAULTS TO CURRENT ID IF NOT SPECIFIED, BUT LIST IS SPECIFIED. SO CAN DO [IF INLIST LIST=SOME.PAGE] BUT CAN'T DO [IF INLIST SOME.PAGE]. PROBABLY NEED TO REWRITE AND CHANGE THE ORDER SO LIST IS BEFORE ITEM BUT WHAT WILL IT BREAK? DOESN'T MATCH OTHER IN? CONDITIONS...
	if (! is_array($args)) return false;
	global $BOLTid;
	if (isset($args['item'])) $item = $args['item'];
	elseif (isset($args[1])) $item = trim($args[1]);
	else $item = $BOLTid;
	if (isset($args['list'])) $list = $args['list'];
	else $list = $args[2];
	if (BOLTexists($list)) $list = str_replace("\n", ',', BOLTloadpage($list));
	$list = BOLTcsv($list);
	if ($args['case'] == 'false') {
		$list = strtolower($list);
		$item = strtolower($item);
		}
	if (strpos(",$list,", ','.trim($item).',') === false) return false;
	else return true;
	}

function BOLTCinpage($args='') {
## CHECKS TO SEE IF AN ITEM (FIND OR POS2) IS IN PAGE (PAGE OR POS1). PAGE DEFAULTS TO CURRENT PAGE AND FIND DEFAULTS TO CURRENT ID. CASE INSENSITIVE. SHOULD POSSIBLY HAVE AN OPTION TO DO CASE SENSITIVE CHECK
	global $pagesPath, $pageLink, $BOLTid;
	if (isset($args['page'])) $page = $args['page'];
	elseif (isset($args[1])) $page = $args[1];
	else $page = $pageLink;
	if (isset($args['find'])) $find = $args['find'];
	elseif (isset($args[2])) $find = strtolower($args[2]);
	else $find = $BOLTid;
	$content = strtolower(BOLTloadpage($page));
	if (strpos($content, BOLTurl2utf($find)) !== false) return true;
	else return false;
	}

function BOLTCinsource($args='') {
## CHECKS TO SEE IF AN ITEM (FIND OR POS2) IS IN SOURCE (POS2). TAPS INTO BOLTFsource FOR LATTER. FIND REQUIRED (SHOULD IT DEFAULT TO ID?). SOURCE DEFAULTS TO CURRENT PAGE. ALL SOURCE PARAMETERS AVAILABLE
	$args['escape'] = 'false';
	$data = BOLTFsource($args);
	if (isset($args['find'])) $find = $args['find'];
	else $find = $args[2];
	if ($find == '') return false;
	$find = str_replace('\n', "\n", $find);
	if (strpos($data, $find) !== false) return true;
	return false;
	}	

function BOLTCless($args='') {
## REQUIRES TWO VALUES (POS1 & POS2) AND CHECKS IF 1 IS LESS THAN 2. IF BOTH ARE NUMERIC, CALCULATES MATHEMATICALLY, ELSE CALCULATES ALPHABETICALLY.
	if (! isset($args[2])) return false;
	$test0 = trim($args[1]);
	$test1 = trim($args[2]);
	if (is_numeric($test0) && is_numeric($test1)) return $test0 < $test1;
	else if (strcasecmp($test0, $test1) < 0) return true;
	return false;
	}

function BOLTClogin($args='') {
## SIMPLE CHECK IF CURRENT USER IS LOGGED IN. CANNOT SPECIFY OTHER USER
	global $BOLTid;
	if ($BOLTid != '') $login = true;
	else $login = false;
	return $login;
	}

function BOLTCmore($args='') {
## REQUIRES TWO VALUES (POS1 & POS2) AND CHECKS IF 1 IS MORE THAN 2. IF BOTH ARE NUMERIC, CALCULATES MATHEMATICALLY, ELSE CALCULATES ALPHABETICALLY.
	if (! isset($args[2])) return false;
	$test0 = trim($args[1]);
	$test1 = trim($args[2]);
	if (is_numeric($test0) && is_numeric($test1)) return $test0 > $test1;
	else if (strcasecmp($test0, $test1) > 0) return true;
	return false;
	}

function BOLTCnew($args='') {
## A CLEVER CONDITION USEFUL IN TEMPLATES TO SEE IF A VALUE HAS CHANGED. FOR MORE COMPLICATED TEMPLATES, CAN SPECIFY A NAME PARAMETER TO KEEP TRACK OF MULTIPLE VALUES. EXAMPLE: SUPPOSE YOU HAVE A SEARCH OUTPUT SORTED BY STATE. [IF NEW {+:STATE}] COULD BE USED TO INSERT STATE NAME TO SHOW HEADER FOR RESULTS UNDER EACH STATE.
	if (! is_array($args)) return false;
	global $BOLTnewCond;
	$c = $args['name'];
	if ($c == '') $type = count($BOLTnewCond);
	$i = $args[1];
	if (isset($BOLTnewCond[$c][$i])) return false;
	else $BOLTnewCond[$c][$i] = 'true';
	return true;
	}

function BOLTCnumber($args='') {
## SIMPLE CHECK TO TELL IF THE VALUE IN POS1 IS A NUMERIC. CAN TEST A PAGE NAME TO SEE IF PAGE IS NUMERIC. THIS MEANS ABC.123 PASSES (PAGE IS 123)
	if (! is_array($args)) return false;
	return is_numeric(BOLTvars($args[1] . ":page"));
	}

function BOLTCset($args='') {
## CHECKS TO SEE IF SOME VARIABLE HAS A VALUE, AS IN [IF SET {VAR}]
	if (! is_array($args) || implode('', $args) == '') return false;
	return true;
	}

function BOLTCstamp($args='') {
## TAKES A PAGE NAME AND TESTS TO SEE IF ONE OR MORE STAMPS EXIST FOR THAT PAGE
	global $pageLink;
	$BOLTstampsDir = BOLTconfig('BOLTstampsDir', 'stamps');
	$pat = "/^" . str_replace(".", "\.", $pageLink) . "\.([0-9]+)$/";
	if (! file_exists($BOLTstampsDir)) BOLTfixdir($BOLTstampsDir);
	$list = BOLTlistpages($pat, $BOLTstampsDir);
	return count($list) > 0;
	}

function BOLTCtime($args='') {
## CHECKS TO SEE IF A TIMESTAMP (POS1, OR DEFAULT NOW) IS BETWEEN PARAMETERS TO & FROM. TO AND FROM DEFAULT TO 1 SEC AHEAD/PAST, SO ALWAYS PASS IF NOT SPECIFIED
	global $BOLTtime;
	if (isset($args['to'])) {
		$to = $args['to'];
		if (preg_match('/^[0-9]{10}$/', $to) == 0) $to = strtotime($to);
		}
	else $to = $BOLTtime + 1;
	if (isset($args['from'])) {
		$from = $args['from'];
		if (preg_match('/^[0-9]{10}$/', $from) == 0) $from = strtotime($from);
		}
	else $from = $BOLTtime - 1;
	if (isset($args[1])) $check = $args[1];
	else $check = $BOLTtime;
	if ($from < $check && $check < $to) return true;
	return false;
	}

function BOLTCtrue($args='') {
## RETURNS TRUE ALWAYS, GENERALLY USED FOR TESTING
	return true;
	}
Return current item: BoltWire